]> git.ipfire.org Git - pakfire.git/commitdiff
base: Add PakfireContext
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Dec 2016 21:36:37 +0000 (22:36 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Dec 2016 21:41:49 +0000 (22:41 +0100)
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 <michael.tremer@ipfire.org>
src/pakfire/base.py

index aa9bdafd423e0c48bd514382ee1dd2ca3bb1341b..07f63d2e3250615268c5f9a964465cce513842a7 100644 (file)
@@ -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"