Class: Y2Packager::Repository

Inherits:
Object
  • Object
show all
Defined in:
library/packages/src/lib/y2packager/repository.rb

Overview

This class represents a libzypp repository

It offers a simple API to list them, query basic attributes, find out the products they offer and enabling/disabling them.

Examples:

Get all repositories

all_repos = Y2Packager::Repository.all     #=> [#<Y2Packager::Repository>, ...]
enabled = Y2Packager::Repository.enabled   #=> [#<Y2Packager::Repository>]

Get a repository using a repo ID

repo = Y2Packager::Repository.find(1) #=> #<Y2Packager::Repository>
repo.autorefresh?                   #=> true
repo.url                            #=> "http://download.opensuse.org..."

Disabling a repository

repo = Y2Packager::Repository.find(1) #=> #<Y2Packager::Repository>
repo.enabled?                       #=> true
repo.disabled!
repo.enabled?                       #=> false

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_id:, repo_alias:, name:, url:, raw_url: nil, product_dir: "", enabled:, autorefresh:) ⇒ Repository

Note:

This class calculates the expanded URL (#url) out of the unexpanded version

Constructor

(#raw_url), so there is no need to provide both versions in the constructor. Still, both :url and :raw_url are accepted for backwards compatibility. If :raw_url is provided, :url will be ignored (it can be calculated at any point).

Parameters:

  • repo_alias (String)

    Repository alias (unique identifier)

  • repo_id (Fixnum)

    Repository ID

  • name (String)

    Name

  • url (URI::Generic, ZyppUrl)

    Repository URL

  • raw_url (URI::Generic, ZyppUrl) (defaults to: nil)

    Optional raw repository URL

  • product_dir (String) (defaults to: "")

    Product directory

  • enabled (Boolean)

    Is the repository enabled?

  • autorefresh (Boolean)

    Is auto-refresh enabled for this repository?


133
134
135
136
137
138
139
140
141
# File 'library/packages/src/lib/y2packager/repository.rb', line 133

def initialize(repo_id:, repo_alias:, name:, url:, raw_url: nil, product_dir: "", enabled:, autorefresh:)
  @repo_id = repo_id
  @repo_alias = repo_alias
  @name    = name
  @enabled = enabled
  @autorefresh = autorefresh
  @raw_url = ZyppUrl.new(raw_url || url)
  @product_dir = product_dir
end

Instance Attribute Details

#nameString (readonly)

Returns Repository name.

Returns:

  • (String)

    Repository name


43
44
45
# File 'library/packages/src/lib/y2packager/repository.rb', line 43

def name
  @name
end

#product_dirString (readonly)

Returns Product directory.

Returns:

  • (String)

    Product directory


47
48
49
# File 'library/packages/src/lib/y2packager/repository.rb', line 47

def product_dir
  @product_dir
end

#raw_urlZyppUrl

Returns Repository URL.

Returns:


45
46
47
# File 'library/packages/src/lib/y2packager/repository.rb', line 45

def raw_url
  @raw_url
end

#repo_aliasString (readonly)

Returns Repository alias.

Returns:

  • (String)

    Repository alias


49
50
51
# File 'library/packages/src/lib/y2packager/repository.rb', line 49

def repo_alias
  @repo_alias
end

#repo_idFixnum (readonly)

Returns Repository ID.

Returns:

  • (Fixnum)

    Repository ID


41
42
43
# File 'library/packages/src/lib/y2packager/repository.rb', line 41

def repo_id
  @repo_id
end

Class Method Details

.all(enabled_only: false) ⇒ Array<Repository>

Return all registered repositories

Parameters:

  • enabled_only (Boolean) (defaults to: false)

    Returns only enabled repositories

Returns:

  • (Array<Repository>)

    Array containing all repositories

See Also:

  • Yast::Pkg.SourceGetCurrent
  • find

65
66
67
68
69
# File 'library/packages/src/lib/y2packager/repository.rb', line 65

def all(enabled_only: false)
  Yast::Pkg.SourceGetCurrent(enabled_only).map do |repo_id|
    find(repo_id)
  end
end

.create(name:, url:, product_dir: "", enabled: true, autorefresh: true) ⇒ Y2Packager::Repository?

Add a repository

Parameters:

  • name (String)

    Name

  • url (URI::Generic, ZyppUrl)

    Repository URL

  • product_dir (String) (defaults to: "")

    Product directory

  • enabled (Boolean) (defaults to: true)

    Is the repository enabled?

  • autorefresh (Boolean) (defaults to: true)

    Is auto-refresh enabled for this repository?

Returns:


108
109
110
111
112
113
114
115
# File 'library/packages/src/lib/y2packager/repository.rb', line 108

def create(name:, url:, product_dir: "", enabled: true, autorefresh: true)
  repo_id = Yast::Pkg.RepositoryAdd(
    "name" => name, "base_urls" => [url.to_s], "enabled" => enabled,
    "autorefresh" => autorefresh, "prod_dir" => product_dir
  )

  repo_id ? find(repo_id) : nil
end

.disabledArray<Repository>

Return only disabled repositories

Returns:

  • (Array<Repository>)

    Array containing disabled repositories


81
82
83
# File 'library/packages/src/lib/y2packager/repository.rb', line 81

def disabled
  all.reject(&:enabled?)
end

.enabledArray<Repository>

Return only enabled repositories

Returns:

  • (Array<Repository>)

    Array containing enabled repositories


74
75
76
# File 'library/packages/src/lib/y2packager/repository.rb', line 74

def enabled
  all.select(&:enabled?)
end

.find(repo_id) ⇒ Y2Packager::Repository

Return a repository with the given repo_id

Parameters:

  • repo_id (Fixnum)

    Repository ID

Returns:

Raises:

  • NotFound


91
92
93
94
95
96
97
98
# File 'library/packages/src/lib/y2packager/repository.rb', line 91

def find(repo_id)
  repo_data = Yast::Pkg.SourceGeneralData(repo_id)
  raise NotFound if repo_data.nil?

  new(repo_id: repo_id, repo_alias: repo_data["alias"], enabled: repo_data["enabled"],
    name: repo_data["name"], autorefresh: repo_data["autorefresh"],
    url: repo_data["raw_url"], product_dir: repo_data["product_dir"])
end

Instance Method Details

#addonsArray<Y2Packager::Product>

Return addons in the repository

Returns:

See Also:


204
205
206
# File 'library/packages/src/lib/y2packager/repository.rb', line 204

def addons
  products.select { |p| p.category == :addon }
end

#autorefresh?Boolean

Determine if auto-refresh is enabled for the repository

Returns:

  • (Boolean)

    true if auto-refresh is enabled; false otherwise


195
196
197
# File 'library/packages/src/lib/y2packager/repository.rb', line 195

def autorefresh?
  @autorefresh
end

#delete!Boolean

Remove the repository, the repo_id is set to nil after removal.

The repository will be removed only in memory. Calling to Yast::Pkg.SourceSaveAll will make the removal persistent.

Returns:

  • (Boolean)

    true on success, false otherwise

See Also:

  • Yast::Pkg.SourceDelete
  • Yast::Pkg.SourceSaveAll

249
250
251
252
253
254
# File 'library/packages/src/lib/y2packager/repository.rb', line 249

def delete!
  return false unless Yast::Pkg.SourceDelete(repo_id)

  @repo_id = nil
  true
end

#disable!Boolean

Disable the repository

The repository status will be stored only in memory. Calling to Yast::Pkg.SourceSaveAll will make it persistent.

Returns:

  • (Boolean)

    true on success, false otherwise

See Also:

  • Yast::Pkg.SourceSetEnabled
  • Yast::Pkg.SourceSaveAll

233
234
235
236
237
238
# File 'library/packages/src/lib/y2packager/repository.rb', line 233

def disable!
  return false unless Yast::Pkg.SourceSetEnabled(repo_id, false)

  self.enabled = false
  true
end

#enable!Boolean

Enable the repository

The repository status will be stored only in memory. Calling to Yast::Pkg.SourceSaveAll will make it persistent.

Returns:

  • (Boolean)

    true on success, false otherwise

See Also:

  • Yast::Pkg.SourceSetEnabled
  • Yast::Pkg.SourceSaveAll

217
218
219
220
221
222
# File 'library/packages/src/lib/y2packager/repository.rb', line 217

def enable!
  return false unless Yast::Pkg.SourceSetEnabled(repo_id, true)

  self.enabled = true
  true
end

#enabled?Boolean

Determine if the repository is enabled

Returns:

  • (Boolean)

    true if repository is enabled; false otherwise


188
189
190
# File 'library/packages/src/lib/y2packager/repository.rb', line 188

def enabled?
  @enabled
end

#local?Boolean

Determine if the repository is local

Returns:

  • (Boolean)

    true if the repository is considered local; false otherwise


173
174
175
# File 'library/packages/src/lib/y2packager/repository.rb', line 173

def local?
  raw_url.local?
end

#productsArray<Y2Packager::Product>

Return products contained in the repository

Returns:

See Also:


157
158
159
160
161
162
163
164
165
166
167
168
# File 'library/packages/src/lib/y2packager/repository.rb', line 157

def products
  return @products if @products

  # Filter products from this repository
  candidates = Y2Packager::Resolvable.find(kind: :product, source: repo_id)

  # Build an array of Y2Packager::Product objects
  @products = candidates.map do |data|
    Y2Packager::Product.new(name: data.name, version: data.version,
      arch: data.arch, category: data.category, vendor: data.vendor)
  end
end

#schemeSymbol?

Return repository scheme

The scheme is determined using the URL

Returns:

  • (Symbol, nil)

    URL scheme, nil if the URL is not defined


148
149
150
# File 'library/packages/src/lib/y2packager/repository.rb', line 148

def scheme
  raw_url&.scheme&.to_sym
end

#urlZyppUrl

Repository URL, expanded version (ie. with the repository variables already replaced by their values)

Returns:


181
182
183
# File 'library/packages/src/lib/y2packager/repository.rb', line 181

def url
  raw_url.expanded
end