Class: Y2Packager::ZyppUrl

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/packages/src/lib/y2packager/zypp_url.rb

Overview

This class represents a libzypp URL

Libzypp URLs do not conform to rfc3986 because they can include the so-called Repository Variables. Those vars can have several formats like $variable, $variable, $variable-word or $variable+word. According to libzypp's documentation, the variables can appear in any component of the URL (host, path, port...) except in scheme, user or password. But, at the time of writing, zypper throws an "invalid port component" error when trying to use variables as part of the port, even with the most recent available version of libzypp.

See https://doc.opensuse.org/projects/libzypp/HEAD/zypp-repovars.html

The current implementation relies on SimpleDelegator to expose all the methods of an underlying URI object, so objects of this class can be used as a direct replacement in places that used to use URI, like Repository#raw_url

Constant Summary collapse

LOCAL_SCHEMES =
[:cd, :dvd, :dir, :hd, :iso, :file].freeze

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ ZyppUrl

Constructor

If the argument is a string with an invalid URL, an empty URL is created

Parameters:

  • url (String, ZyppUrl, URI::Generic)

47
48
49
50
51
52
53
54
55
56
57
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 47

def initialize(url)
  uri =
    begin
      URI(repovars_escape(url.to_s))
    rescue URI::InvalidURIError => e
      log.error "Failed to parse URL, considered as empty: #{e.inspect}"
      URI("")
    end

  __setobj__(uri)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Compares two URLs

NOTE: this considers an URI::Generic object to be equal if it represents the same URL. That should increase a bit the robustness when a ZyppUrl object is introduced to replace an existing URI one.


145
146
147
148
149
150
151
152
153
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 145

def ==(other)
  if other.is_a?(URI::Generic)
    uri == other
  elsif other.class == self.class
    uri == other.uri
  else
    false
  end
end

#expandedZyppUrl

Expanded version of the URL in which the repository vars has been replaced by their value

Returns:

  • (ZyppUrl)

    an URL that is expected to conform to rfc3986


128
129
130
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 128

def expanded
  ZyppUrl.new(Yast::Pkg.ExpandedUrl(to_s))
end

#hostnameString

See URI::Generic#hostname

Returns:

  • (String)

76
77
78
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 76

def hostname
  repovars_unescape(uri.hostname)
end

#inspectString

String representation of the state of the object

Returns:

  • (String)

135
136
137
138
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 135

def inspect
  # Prevent SimpleDelegator from forwarding this to the wrapped URI object
  "#<#{self.class}:#{object_id}} @uri=#{uri.inspect}>"
end

#local?Boolean

Whether the URL is local

Returns:

  • (Boolean)

    true if the URL is considered local; false otherwise


120
121
122
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 120

def local?
  LOCAL_SCHEMES.include?(scheme&.to_sym)
end

#pathString

See URI::Generic#path

Returns:

  • (String)

103
104
105
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 103

def path
  repovars_unescape(uri.path)
end

#portString

See URI::Generic#hostname

FIXME: escaping does not work here because the port wouldn't accept the escaped characters either. If we really want to support the usage of repository variables in the port, we would likely have to implement one of these solutions:

1) Modify the regexp used by URI to parse/validate the port, so it accepts the vars (escaped or not). That's not as easy as the documentation of the URI class suggests and may affect the reliability of the parsing. 2) Extract the port section before parsing the URL if it contains repovars, storing its value in a separate instance variable and restoring it on demand.

Anyways, using variables in the port of an URL doesn't seem to really work in libzypp, although it's documented to be valid.

Returns:

  • (String)

96
97
98
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 96

def port
  repovars_unescape(uri.port)
end

#queryString

See URI::Generic#path

Offered for completeness, even if the query component makes very little sense in a zypper URL.

Returns:

  • (String)

113
114
115
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 113

def query
  repovars_unescape(uri.query)
end

#to_sString

Constructs String from the URL

Returns:

  • (String)

69
70
71
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 69

def to_s
  repovars_unescape(uri.to_s)
end

#uriURI::Generic Also known as: to_uri

Returns:

  • (URI::Generic)

60
61
62
# File 'library/packages/src/lib/y2packager/zypp_url.rb', line 60

def uri
  __getobj__
end