Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new Resolvable class, use it in Packages.rb (bsc#1136051)
- Loading branch information
Showing
11 changed files
with
316 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# ------------------------------------------------------------------------------ | ||
# Copyright (c) 2019 SUSE LINUX GmbH, Nuremberg, Germany. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it under | ||
# the terms of version 2 of the GNU General Public License as published by the | ||
# Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
# ------------------------------------------------------------------------------ | ||
|
||
require "yast" | ||
|
||
Yast.import "Pkg" | ||
|
||
module Y2Packager | ||
# | ||
# This class represents a libzypp resolvable object (package, pattern, patch, | ||
# product, source package) | ||
# | ||
# @note The returned Resolvables might not be valid anymore after changing | ||
# the package manager status (installing/removing packages, changing | ||
# repositories, etc.). After such a change you need to load the resolvables | ||
# again, avoid storing them for later if possible. | ||
# | ||
# @example All installed packages | ||
# Y2Packager::Resolvable.find(kind: :package, status: :installed) | ||
# | ||
# @example Available (not installed) "yast2" packages | ||
# Y2Packager::Resolvable.find(kind: :package, status: :available, name: "yast2") | ||
# | ||
# @example Lazy loading | ||
# res = Y2Packager::Resolvable.find(kind: :package, status: :installed) | ||
# # the `summary` attribute is loaded from libzypp when needed | ||
# res.each {|r| puts "#{r.name} - {r.summary}"} | ||
# | ||
# @example Preloading the attributes | ||
# # the `summary` attribute is loaded from libzypp already at the initial state | ||
# res = Y2Packager::Resolvable.find(kind: :package, status: :installed, [:summary]) | ||
# # this returns the cached `summary` attribute, this is much more efficient | ||
# res.each {|r| puts "#{r.name} - {r.summary}"} | ||
# | ||
# @since 4.2.6 | ||
class Resolvable | ||
include Yast::Logger | ||
|
||
# | ||
# Find the resolvables which match the input parameters. See Yast::Pkg.Resolvables | ||
# | ||
# @param params [Hash<Symbol,Object>] The search filter, only the matching resolvables | ||
# are returned. | ||
# @param preload [Array<Symbol>] The list of attributes which should be preloaded. | ||
# The missing attributes are lazy loaded, however for performance reasons | ||
# you might ask to preload the attributes right at the beginning and avoid | ||
# querying libzypp again later. | ||
# @return [Array<Y2Packager::Resolvable>] Found resolvables or empty array if nothing found | ||
# @see https://yast-pkg-bindings.surge.sh/ Yast::Pkg.Resolvables | ||
def self.find(params, preload = []) | ||
attrs = (preload + UNIQUE_ATTRIBUTES).uniq | ||
Yast::Pkg.Resolvables(params, attrs).map { |r| new(r) } | ||
end | ||
|
||
# | ||
# Is there any resolvable matching the requested parameters? This is similar to | ||
# the .find method, just instead of a resolvable list it returns a simple Boolean. | ||
# | ||
# @param params [Hash<Symbol,Object>] The requested attributes | ||
# @return [Boolean] `true` if any matching resolvable is found, `false` otherwise. | ||
# @see .find | ||
def self.any?(params) | ||
Yast::Pkg.AnyResolvable(params) | ||
end | ||
|
||
# | ||
# Constructor, initialize the object from a pkg-bindings resolvable hash. | ||
# | ||
# @param hash [Hash<Symbol,Object>] A pkg-bindings resolvable hash. | ||
def initialize(hash) | ||
from_hash(hash) | ||
end | ||
|
||
# | ||
# Dynamically load the missing attributes from libzypp. | ||
# | ||
# @param method [Symbol] the method called | ||
# @param *_args no used so far | ||
# | ||
# @return [<Type>] <description> | ||
# | ||
def method_missing(method, *_args) | ||
return instance_variable_get("@#{method}") if instance_variable_defined?("@#{method}") | ||
|
||
# load a missing attribute | ||
if UNIQUE_ATTRIBUTES.all? { |a| instance_variable_defined?("@#{a}") } | ||
load_attribute(method) | ||
else | ||
raise "Missing attributes for identifying the resolvable." | ||
end | ||
end | ||
|
||
private | ||
|
||
# attributes required for identifying a resolvable | ||
UNIQUE_ATTRIBUTES = [:kind, :name, :version, :arch, :source].freeze | ||
|
||
# Load the attributes from a Hash | ||
# | ||
# @param hash [Hash] The resolvable Hash obtained from pkg-bindings. | ||
def from_hash(hash) | ||
hash.each do |k, v| | ||
instance_variable_set("@#{k}", v) | ||
end | ||
end | ||
|
||
# | ||
# Lazy load a missing attribute. | ||
# | ||
# @param attr [Symbol] The required attribute to load. | ||
# @return [Object] The read value. | ||
def load_attribute(attr) | ||
attrs = Hash[(UNIQUE_ATTRIBUTES.map { |a| [a, instance_variable_get("@#{a}")] })] | ||
resolvables = Yast::Pkg.Resolvables(attrs, [attr]) | ||
|
||
# Finding more than one result is suspicious, log a warning | ||
log.warn("Found several resolvables: #{resolvables.inspect}") if resolvables.size > 1 | ||
|
||
resolvable = resolvables.first | ||
raise NoMethodError unless resolvable && resolvable.key?(attr.to_s) | ||
instance_variable_set("@#{attr}", resolvable[attr.to_s]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
The testing data was downloaded from the [YaST:Head OBS]( | ||
https://build.opensuse.org/project/show/YaST:Head) repository. | ||
|
||
- repodata: https://download.opensuse.org/repositories/YaST:/Head/openSUSE_Leap_15.0/repodata/ |
Binary file added
BIN
+86.6 KB
.../repodata/4d295860e625859a714dcc91c6a2e6f7634aab288eb13cf01c09ae2a65a7eeab-primary.xml.gz
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<repomd xmlns="http://linux.duke.edu/metadata/repo" xmlns:rpm="http://linux.duke.edu/metadata/rpm"> | ||
<revision>1558080877</revision> | ||
<tags> | ||
<repo>obsrepository://build.opensuse.org/YaST:Head/openSUSE_Leap_15.0</repo> | ||
<repo>obsbuildid:1533048310</repo> | ||
</tags> | ||
<data type="primary"> | ||
<checksum type="sha256">4d295860e625859a714dcc91c6a2e6f7634aab288eb13cf01c09ae2a65a7eeab</checksum> | ||
<open-checksum type="sha256">6c154c0ff76aca4f1b376bbe91f5bf00d56eff19bf5cdf08b680472e42fb4ee1</open-checksum> | ||
<location href="repodata/4d295860e625859a714dcc91c6a2e6f7634aab288eb13cf01c09ae2a65a7eeab-primary.xml.gz"/> | ||
<timestamp>1558080876</timestamp> | ||
<size>88670</size> | ||
<open-size>762992</open-size> | ||
</data> | ||
<data type="filelists"> | ||
<checksum type="sha256">1d3843cc83a1bd651295caf1790d9487739a0df387e7cdf1251edbb284f8c387</checksum> | ||
<open-checksum type="sha256">4921d6efe5170264f781275d9ca0408c37d96f262191c143ba6e06562d601820</open-checksum> | ||
<location href="repodata/1d3843cc83a1bd651295caf1790d9487739a0df387e7cdf1251edbb284f8c387-filelists.xml.gz"/> | ||
<timestamp>1558080876</timestamp> | ||
<size>239386</size> | ||
<open-size>3058860</open-size> | ||
</data> | ||
<data type="other"> | ||
<checksum type="sha256">06ae21ca0bb3653b69dc367b078d56d7c73004d1c6dbc0c730a2eff9dd84a9ac</checksum> | ||
<open-checksum type="sha256">f770a3ae116812d8eeba60b2912d1205175026e1efcefaf899c2d3e03f44888f</open-checksum> | ||
<location href="repodata/06ae21ca0bb3653b69dc367b078d56d7c73004d1c6dbc0c730a2eff9dd84a9ac-other.xml.gz"/> | ||
<timestamp>1558080876</timestamp> | ||
<size>230366</size> | ||
<open-size>1439396</open-size> | ||
</data> | ||
</repomd> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-----BEGIN PGP SIGNATURE----- | ||
Version: GnuPG v1.0.7 (GNU/Linux) | ||
|
||
iD8DBQBc3m1tWyVH2GHn0GwRAgZfAJ49sk+alvmPOAGhdAaH7Y4TwpTM6ACZAXnG | ||
Oqgdb6W/cPGRP7c7vIhT/Y8= | ||
=kSvT | ||
-----END PGP SIGNATURE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-----BEGIN PGP PUBLIC KEY BLOCK----- | ||
Version: GnuPG v1.4.5 (GNU/Linux) | ||
|
||
mQGiBEfZbkMRBADjWPMrWcXDcSDlPYsnn5tgT1LMJqqJwBisS6fNYWAxEMof4lWJ | ||
MIiRyDE86uAgUvwCsDxmTHeM5uV8KOmsKedxd2bU8lNPSwcSXMLrVhL0mUpPz+EP | ||
eq3VJkLCR+HSszWGkOrh8FddeaQBR5gYiwYu3Spoc8lHiUncbw/N2CeWDwCggMO/ | ||
GsLl+mXbmn4AYFLhNGvRUPUD/2tSlTOAIiGF3Y3svpYdIidMZlNo/+nss0qOMWaM | ||
gZ+3pgXpZ+5pdXbukBupXipEvk+6JnmZjE/sZ/iY2j3WDb9Bh45ix7CA1zXMYtTk | ||
0oAzUmxSWbJVqegcMQkR+9HT1Ys5x+7TQKuQQWXBPtp79zAQlOOa3Vy9Blgnom4R | ||
nue8BACps5N0eQovsbBhGHr9uDcHrYRt74sgLqA83MP6voATCj+NLMFhHgIxJYrt | ||
bItoeEeoPBaJ77AkcMJpitUBW2Ud6bDeChqf48nlnGjVBlIAwx63d0kkYPnsn5kN | ||
BnhkkH9fUDgc934krzsf7dPrpFIkG0LI/TlSshI3HDI71m0r07QqWWFTVCBPQlMg | ||
UHJvamVjdCA8WWFTVEBidWlsZC5vcGVuc3VzZS5vcmc+iGYEExECACYFAlrseIYC | ||
GwMFCRcxukMGCwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRBbJUfYYefQbHQDAJ9Q | ||
4JMWDMaX74S6exNQQiMTdO3whACcDz1jkagJ0BBQY8usbzaJsjCmOm2IRgQTEQIA | ||
BgUCR9luQwAKCRA7MBG3a51lI3yCAJ4jfMqPAn8uPoosOSeA80t7MQdYOACgq9yA | ||
o67MnrLsqTdAM0Ud3ld3NhA= | ||
=JmLZ | ||
-----END PGP PUBLIC KEY BLOCK----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.