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

Constant Summary collapse

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

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

Constructor

Parameters:

  • repo_alias (String)

    Repository alias (unique identifier)

  • repo_id (Fixnum)

    Repository ID

  • name (String)

    Name

  • url (URI::Generic)

    Repository URL (expanded)

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

    Optional raw repository URL (unexpanded)

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

    Product directory

  • enabled (Boolean)

    Is the repository enabled?

  • autorefresh (Boolean)

    Is auto-refresh enabled for this repository?


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

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
  @url = url
  @raw_url = raw_url || url
  @product_dir = product_dir
end

Instance Attribute Details

#nameString (readonly)

Returns Repository name.

Returns:

  • (String)

    Repository name


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

def name
  @name
end

#product_dirString (readonly)

Returns Product directory.

Returns:

  • (String)

    Product directory


53
54
55
# File 'library/packages/src/lib/y2packager/repository.rb', line 53

def product_dir
  @product_dir
end

#raw_urlURI::Generic (readonly)

Returns Raw repository URL (unexpanded).

Returns:

  • (URI::Generic)

    Raw repository URL (unexpanded)


51
52
53
# File 'library/packages/src/lib/y2packager/repository.rb', line 51

def raw_url
  @raw_url
end

#repo_aliasString (readonly)

Returns Repository alias.

Returns:

  • (String)

    Repository alias


55
56
57
# File 'library/packages/src/lib/y2packager/repository.rb', line 55

def repo_alias
  @repo_alias
end

#repo_idFixnum (readonly)

Returns Repository ID.

Returns:

  • (Fixnum)

    Repository ID


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

def repo_id
  @repo_id
end

#urlURI::Generic

Returns Repository URL (expanded).

Returns:

  • (URI::Generic)

    Repository URL (expanded)


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

def url
  @url
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

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

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)

    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:


115
116
117
118
119
120
121
122
# File 'library/packages/src/lib/y2packager/repository.rb', line 115

def create(name:, url:, product_dir: "", enabled: true, autorefresh: true)
  repo_id = Yast::Pkg.RepositoryAdd(
    "name" => name, "base_urls" => [url], "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


87
88
89
# File 'library/packages/src/lib/y2packager/repository.rb', line 87

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

.enabledArray<Repository>

Return only enabled repositories

Returns:

  • (Array<Repository>)

    Array containing enabled repositories


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

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


97
98
99
100
101
102
103
104
105
# File 'library/packages/src/lib/y2packager/repository.rb', line 97

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: URI(repo_data["url"]), raw_url: URI(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:


199
200
201
# File 'library/packages/src/lib/y2packager/repository.rb', line 199

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


190
191
192
# File 'library/packages/src/lib/y2packager/repository.rb', line 190

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

244
245
246
247
248
249
# File 'library/packages/src/lib/y2packager/repository.rb', line 244

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

228
229
230
231
232
233
# File 'library/packages/src/lib/y2packager/repository.rb', line 228

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

212
213
214
215
216
217
# File 'library/packages/src/lib/y2packager/repository.rb', line 212

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


183
184
185
# File 'library/packages/src/lib/y2packager/repository.rb', line 183

def enabled?
  @enabled
end

#local?Boolean

Determine if the repository is local

Returns:

  • (Boolean)

    true if the repository is considered local; false otherwise


176
177
178
# File 'library/packages/src/lib/y2packager/repository.rb', line 176

def local?
  LOCAL_SCHEMES.include?(scheme)
end

#productsArray<Y2Packager::Product>

Return products contained in the repository

Returns:

See Also:


160
161
162
163
164
165
166
167
168
169
170
171
# File 'library/packages/src/lib/y2packager/repository.rb', line 160

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


151
152
153
# File 'library/packages/src/lib/y2packager/repository.rb', line 151

def scheme
  url.scheme ? url.scheme.to_sym : nil
end