Class: Y2Packager::Repository

Inherits:
Object
  • Object
show all
Defined in:
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:, name:, enabled:, autorefresh:, url:) ⇒ Repository

Constructor

Parameters:

  • repo_id (Fixnum)

    Repository ID

  • name (String)

    Name

  • enabled (Boolean)

    Is the repository enabled?

  • autorefresh (Boolean)

    Is auto-refresh enabled for this repository?

  • url (URI::Generic)

    Repository URL


120
121
122
123
124
125
126
# File 'src/lib/y2packager/repository.rb', line 120

def initialize(repo_id:, name:, enabled:, autorefresh:, url:)
  @repo_id = repo_id
  @name    = name
  @enabled = enabled
  @autorefresh = autorefresh
  @url = url
end

Instance Attribute Details

#nameString (readonly)

Returns Repository name

Returns:

  • (String)

    Repository name


46
47
48
# File 'src/lib/y2packager/repository.rb', line 46

def name
  @name
end

#repo_idFixnum (readonly)

Returns Repository ID

Returns:

  • (Fixnum)

    Repository ID


44
45
46
# File 'src/lib/y2packager/repository.rb', line 44

def repo_id
  @repo_id
end

#urlURI::Generic (readonly)

Returns Repository URL

Returns:

  • (URI::Generic)

    Repository URL


48
49
50
# File 'src/lib/y2packager/repository.rb', line 48

def url
  @url
end

Class Method Details

.allArray<Repository>

Return all registered repositories

Returns:

  • (Array<Repository>)

    Array containing all repositories

See Also:

  • Yast::Pkg.SourceGetCurrent
  • find

63
64
65
66
67
# File 'src/lib/y2packager/repository.rb', line 63

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

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

Add a repository

Parameters:

  • name (String)

    Name

  • enabled (Boolean)

    Is the repository enabled?

  • autorefresh (Boolean)

    Is auto-refresh enabled for this repository?

  • url (URI::Generic)

    Repository URL

Returns:


104
105
106
107
108
109
110
# File 'src/lib/y2packager/repository.rb', line 104

def create(name:, url:, enabled: true, autorefresh: true)
  repo_id = Yast::Pkg.RepositoryAdd(
    "name" => name, "base_urls" => [url], "enabled" => enabled, "autorefresh" => autorefresh
  )
  return nil unless repo_id
  new(repo_id: repo_id, name: name, url: URI(url), enabled: enabled, autorefresh: autorefresh)
end

.disabledArray<Repository>

Return only disabled repositories

Returns:

  • (Array<Repository>)

    Array containing disabled repositories


79
80
81
# File 'src/lib/y2packager/repository.rb', line 79

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

.enabledArray<Repository>

Return only enabled repositories

Returns:

  • (Array<Repository>)

    Array containing enabled repositories


72
73
74
# File 'src/lib/y2packager/repository.rb', line 72

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


89
90
91
92
93
94
95
# File 'src/lib/y2packager/repository.rb', line 89

def find(repo_id)
  repo_data = Yast::Pkg.SourceGeneralData(repo_id)
  raise NotFound if repo_data.nil?
  new(repo_id: repo_id, enabled: repo_data["enabled"],
    name: repo_data["name"], autorefresh: repo_data["autorefresh"],
    url: URI(repo_data["url"]))
end

Instance Method Details

#addonsArray<Y2Packager::Product>

Return addons in the repository

Returns:

See Also:


185
186
187
# File 'src/lib/y2packager/repository.rb', line 185

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


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

def autorefresh?
  @autorefresh
end

#disable!Object

Disable the repository

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

See Also:

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

208
209
210
211
# File 'src/lib/y2packager/repository.rb', line 208

def disable!
  success = Yast::Pkg.SourceSetEnabled(repo_id, false)
  success && self.enabled = false
end

#enable!Object

Enable the repository

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

See Also:

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

196
197
198
199
# File 'src/lib/y2packager/repository.rb', line 196

def enable!
  success = Yast::Pkg.SourceSetEnabled(repo_id, true)
  success && self.enabled = true
end

#enabled?Boolean

Determine if the repository is enabled

Returns:

  • (Boolean)

    true if repository is enabled; false otherwise


169
170
171
# File 'src/lib/y2packager/repository.rb', line 169

def enabled?
  @enabled
end

#local?Boolean

Determine if the repository is local

Returns:

  • (Boolean)

    true if the repository is considered local; false otherwise


162
163
164
# File 'src/lib/y2packager/repository.rb', line 162

def local?
  LOCAL_SCHEMES.include?(scheme)
end

#productsArray<Y2Packager::Product>

Return products contained in the repository

Returns:

See Also:

  • Yast::Pkg.ResolvableProperties
  • Product

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'src/lib/y2packager/repository.rb', line 143

def products
  return @products unless @products.nil?

  # Filter products from this repository
  candidates = Yast::Pkg.ResolvableProperties("", :product, "").select do |pro|
    pro["source"] == repo_id
  end

  # 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"], status: data["status"],
      vendor: data["vendor"])
  end
end

#schemeSymbol?

Return repository scheme

The scheme is determined using the URL

Returns:

  • (Symbol, nil)

    URL scheme


133
134
135
# File 'src/lib/y2packager/repository.rb', line 133

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