From: Michael Tremer Date: Wed, 7 Dec 2016 21:36:37 +0000 (+0100) Subject: base: Add PakfireContext X-Git-Tag: 0.9.28~1285^2~1384 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ecdbcd1a9c5a67d1a06d63e5149677a03434069;p=pakfire.git base: Add PakfireContext To make initializing Pakfire easier, this patch introduces PakfireContext. So writing code like with Pakfire() as p: p.some_action() will automatically take care of initializing Pakfire and cleaning up afterwards. Actions that do not require initialization will remain in Pakfire so that they can be accessed as p = Pakfire() p.some_other_action() Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/base.py b/src/pakfire/base.py index aa9bdafd4..07f63d2e3 100644 --- a/src/pakfire/base.py +++ b/src/pakfire/base.py @@ -83,7 +83,7 @@ class Pakfire(object): # Initialize repositories self.repos.initialize() - return self + return PakfireContext(self) def __exit__(self, type, value, traceback): # Close repositories @@ -426,29 +426,6 @@ class Pakfire(object): # Process the transaction. t.run() - def info(self, patterns): - pkgs = [] - - # For all patterns we run a single search which returns us a bunch - # of solvables which are transformed into Package objects. - for pattern in patterns: - if os.path.exists(pattern) and not os.path.isdir(pattern): - pkg = packages.open(self, self.repos.dummy, pattern) - if pkg: - pkgs.append(pkg) - - else: - solvs = self.pool.search(pattern, satsolver.SEARCH_GLOB, "solvable:name") - - for solv in solvs: - pkg = packages.SolvPackage(self, solv) - if pkg in pkgs: - continue - - pkgs.append(pkg) - - return sorted(pkgs) - def search(self, pattern): # Do the search. pkgs = {} @@ -548,6 +525,41 @@ class Pakfire(object): return pkg.dist(resultdir=resultdir) +class PakfireContext(object): + """ + This context has functions that require + pakfire to be initialized. + + That means that repository data has to be downloaded + and imported to be searchable, etc. + """ + def __init__(self, pakfire): + self.pakfire = pakfire + + def info(self, patterns): + pkgs = [] + + # For all patterns we run a single search which returns us a bunch + # of solvables which are transformed into Package objects. + for pattern in patterns: + if os.path.exists(pattern) and not os.path.isdir(pattern): + pkg = packages.open(self.pakfire, self.pakfire.repos.dummy, pattern) + if pkg: + pkgs.append(pkg) + + else: + solvs = self.pakfire.pool.search(pattern, satsolver.SEARCH_GLOB, "solvable:name") + + for solv in solvs: + pkg = packages.SolvPackage(self.pakfire, solv) + if pkg in pkgs: + continue + + pkgs.append(pkg) + + return sorted(pkgs) + + class PakfireBuilder(Pakfire): mode = "builder"