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

Parameters:

  • name (String)

    Package name

  • repo_id (Integer)

    Repository ID

  • version (String)

    Package version


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)

Returns Package name.

Returns:

  • (String)

    Package name


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

def name
  @name
end

#repo_idInteger (readonly)

Returns Id of the repository where the package lives.

Returns:

  • (Integer)

    Id of the repository where the package lives


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

def repo_id
  @repo_id
end

#versionString (readonly)

Returns Package version.

Returns:

  • (String)

    Package version


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

Parameters:

  • name (String)

    Package name

Returns:

  • (Array<Package>, nil)

    Packages named like name. It returns nil if some problem occurs interacting with libzypp.


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

Parameters:

  • name (String)

    name of searched package

  • statuses (Array<Symbol>) (defaults to: [:available, :selected])

    allowed package statuses

Returns:


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

Parameters:

  • path (String, Pathname)

    Path to download the package to

See Also:


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

Parameters:

  • directory (String, Pathname)

    Path to extract the package to

See Also:


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.

Returns:

  • (Symbol)

    Package status (:installed, :available, etc.)

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