Mailinglist Archive: yast-devel (29 mails)

< Previous Next >
Re: [yast-devel] Improving the pkg-bindings API - announcing the new API
Dne 14. 05. 19 v 15:04 Ladislav Slezak napsal(a):

The Proposal

To not break the backward compatibility we cannot change the
ResolvableProperties() call so let's create a new one:

# <filter_hash> = required matching values # <attributes_list> = returned
attributes, empty list => return all Yast::Pkg.Resolvables(<filter_hash>,
<attributes_list>)

[...]

Yast::Pkg.AnyResolvable(name: "foo", kind: :package, status: :installed) =>
Boolean

That would be basically the same as Pkg.Resolvables, just returning a simple
Boolean as the result instead of the details. (And it could be faster, it can
return after finding the first matching item, it does not need to iterate
over all
objects.)

This proposal has been fully implemented in yast2-pkg-bindings-4.2.0
(https://github.com/yast/yast-pkg-bindings/pull/114).


Ruby Wrapper

Unfortunately the C++ Pkg-bindings API does not allow specifying optional
parameters (that's a limitation of the YaST component system) and cannot
provide
an object-oriented API so probably having a small Ruby wrapper would be nice:

In the end I used the Y2Packager namespace as we already have some similar
classes there.

The new Y2Packager::Resolvable instance methods maps to the old
Pkg.ResolvableProperties() keys, e.g. Pkg.ResolvableProperties() returns
"vendor"
in the has so use the #vendor method to read it.

Note: there is no direct link between the Ruby part and libzypp, if you change
the
libzypp state (e.g. add/remove repositories, install packages etc.) then the
previously returned objects might be invalid or obsolete, you should always
(re)load
the latest status and do not store them for later.

Implemented in yast2-packager-4.2.8
(https://github.com/yast/yast-packager/pull/442).

Some examples:

---------------------------------------------------------------------------------------

require "y2packager/resolvable"

# returns Array<Y2Packager::Resolvable> (or empty list if nothing found)
res = Y2Packager::Resolvable.find(kind: :package, name: "yast2")
r = res.first

# => "yast2"
r.name

# => "4.0.77-lp150.2.3.1"
r.version

# the Y2Packager::Resolvable object loads only the basic values from libzypp
# to save the memory, the other attributes are lazy loaded when needed (via
# method_missing)

# this will query libzypp to find the resolvable and fetch the description
# => "This package contains scripts and data needed for SUSE Linux\n" \
# "installation with YaST2"
r.description

# the result is cached so the next call does not need libzypp and is much faster
# => "This package ...."
r.description


# but if you need to process a large collection then the lazy loading might be
slow,
# in that case you can preload additional attributes in the initial call
pkgs = Y2Packager::Resolvable.find({kind: :package}, [:description])

# this will NOT call libzypp for each object to get the description
pkgs.each {|p| puts "#{p.name}: #{p.description}"}

---------------------------------------------------------------------------------------

Enjoy!


BTW there are more than one hundred Pkg.ResolvableProperties() calls in YaST, we
should gradually replace them by the new API...



--
Ladislav Slezák
YaST Developer

SUSE LINUX, s.r.o.
Corso IIa
Křižíkova 148/34
18600 Praha 8
--
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: yast-devel+owner@xxxxxxxxxxxx

< Previous Next >
References