Class: Y2Packager::Package

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

Overview

This class represents a libzypp package and it offers an API to common operations.

The idea is extending this class with new methods when needed.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, repo_id, version) ⇒ Package

Constructor


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

def initialize(name, repo_id, version)
  @name = name
  @repo_id = repo_id
  @version = version
end

Instance Attribute Details

#nameString (readonly)


29
30
31
# File 'library/packages/src/lib/y2packager/package.rb', line 29

def name
  @name
end

#repo_idInteger (readonly)


31
32
33
# File 'library/packages/src/lib/y2packager/package.rb', line 31

def repo_id
  @repo_id
end

#versionString (readonly)


33
34
35
# File 'library/packages/src/lib/y2packager/package.rb', line 33

def version
  @version
end

Class Method Details

.find(name) ⇒ Array<Package>?

Find packages by name


41
42
43
44
45
46
47
48
# File 'library/packages/src/lib/y2packager/package.rb', line 41

def find(name)
  resolvables = Yast::Pkg.Resolvables({ kind: :package, name: name },
    [:name, :source, :version])

  return nil if resolvables.nil?

  resolvables.map { |i| new(i["name"], i["source"], i["version"]) }
end

.last_version(name, statuses: [:available, :selected]) ⇒ Y2Packager::Package?

Find the highest version of requested package with given statuses


56
57
58
59
60
61
62
63
64
# File 'library/packages/src/lib/y2packager/package.rb', line 56

def last_version(name, statuses: [:available, :selected])
  packages = find(name)

  return nil unless packages

  packages
    .select { |i| statuses.include?(i.status) }
    .max { |a, b| Yast::Pkg.CompareVersions(a.version, b.version) }
end

Instance Method Details

#download_to(path) ⇒ Object

Download a package to the given path


104
105
106
107
# File 'library/packages/src/lib/y2packager/package.rb', line 104

def download_to(path)
  downloader = Packages::PackageDownloader.new(repo_id, name)
  downloader.download(path.to_s)
end

#extract_to(directory) ⇒ Object

Download and extract the package to the given directory


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

def extract_to(directory)
  tmpfile = Tempfile.new("downloaded-package-#{name}-")
  download_to(tmpfile.path)
  extractor = Packages::PackageExtractor.new(tmpfile.path)
  extractor.extract(directory.to_s)
ensure
  tmpfile.close
  tmpfile.unlink
end

#statusSymbol

Return package status

Ask libzypp about package status.

See Also:

  • Yast::Pkg.Resolvables

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'library/packages/src/lib/y2packager/package.rb', line 84

def status
  resolvables = Yast::Pkg.Resolvables({ kind: :package, name: name,
    version: version, source: repo_id }, [:status])

  log.warn "Found multiple resolvables: #{resolvables}" if resolvables.size > 1

  resolvable = resolvables.first

  if !resolvable
    log.warn "Resolvable not found: #{name}-#{version} from repo #{repo_id}"
    return nil
  end

  resolvable["status"]
end