]> git.ipfire.org Git - pakfire.git/commitdiff
base: Turn Pakfire initialization into a context
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 3 Dec 2016 16:13:25 +0000 (17:13 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 3 Dec 2016 16:13:25 +0000 (17:13 +0100)
This makes code smaller and easier to read and users
of the Pakfire class do not have to take care of any
initialization and cleanup.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/base.py
src/pakfire/builder.py

index 40a6e6b7bdf29dfe20d32cff69f27375b23a6dbd..b6faa0516d5ddc99eda3f16b658c286eef251550 100644 (file)
@@ -38,17 +38,13 @@ from . import util
 import logging
 log = logging.getLogger("pakfire")
 
-from .config import Config
 from .constants import *
 from .i18n import _
 
 class Pakfire(object):
        mode = None
 
-       def __init__(self, path="/", config=None, configs=None, arch=None, **kwargs):
-               # Indicates if this instance has already been initialized.
-               self.initialized = False
-
+       def __init__(self, path="/", arch=None):
                # Check if we are operating as the root user.
                self.check_root_user()
 
@@ -59,18 +55,8 @@ class Pakfire(object):
                if not self.mode and self.path == "/":
                        self.check_is_ipfire()
 
-               # Get the configuration.
-               if config:
-                       self.config = config
-               else:
-                       self.config = self._load_config(configs)
-
-               # Update configuration with additional arguments.
-               for section, settings in list(kwargs.items()):
-                       self.config.update(section, settings)
-
-               # Dump the configuration.
-               self.config.dump()
+               # Load configuration
+               self.config = config.Config("general.conf")
 
                # Initialize the keyring.
                self.keyring = keyring.Keyring(self)
@@ -84,32 +70,23 @@ class Pakfire(object):
                self.pool = satsolver.Pool(self.distro.arch)
                self.repos = repository.Repositories(self)
 
-       def initialize(self):
+       def __enter__(self):
                """
-                       Initialize pakfire instance.
+                       Called to initialize this Pakfire instance when
+                       the context is entered.
                """
-               if self.initialized:
-                       return
+               # Dump the configuration when we enter the context
+               self.config.dump()
 
-               # Initialize repositories.
+               # Initialize repositories
                self.repos.initialize()
 
-               self.initialized = True
+               return self
 
-       def _load_config(self, files=None):
-               """
-                       This method loads all needed configuration files.
-               """
-               if files is None:
-                       files = []
-
-               return config.Config(*files)
-
-       def destroy(self):
+       def __exit__(self, type, value, traceback):
+               # Close repositories
                self.repos.shutdown()
 
-               self.initialized = False
-
        @property
        def supported_arches(self):
                return system.supported_arches
@@ -151,9 +128,6 @@ class Pakfire(object):
                return self.mode == "builder"
 
        def install(self, requires, interactive=True, logger=None, signatures_mode=None, **kwargs):
-               # Initialize this pakfire instance.
-               self.initialize()
-
                if not logger:
                        logger = logging.getLogger("pakfire")
 
@@ -236,9 +210,6 @@ class Pakfire(object):
                        If strict is True, only a package with excatly the same UUID
                        will replace the currently installed one.
                """
-               # Initialize this pakfire instance.
-               self.initialize()
-
                if logger is None:
                        logger = logging.getLogger("pakfire")
 
@@ -347,9 +318,6 @@ class Pakfire(object):
                        check indicates, if the method should return after calculation
                        of the transaction.
                """
-               # Initialize this pakfire instance.
-               self.initialize()
-
                if logger is None:
                        logger = logging.getLogger("pakfire")
 
@@ -409,9 +377,6 @@ class Pakfire(object):
                if logger is None:
                        logger = logging.getLogger("pakfire")
 
-               # Initialize this pakfire instance.
-               self.initialize()
-
                # Create a new request.
                request = self.pool.create_request()
 
@@ -455,9 +420,6 @@ class Pakfire(object):
                if logger is None:
                        logger = logging.getLogger("pakfire")
 
-               # Initialize this pakfire instance.
-               self.initialize()
-
                # Create a new request.
                request = self.pool.create_request(remove=pkgs)
 
@@ -481,9 +443,6 @@ class Pakfire(object):
                t.run()
 
        def info(self, patterns):
-               # Initialize this pakfire instance.
-               self.initialize()
-
                pkgs = []
 
                # For all patterns we run a single search which returns us a bunch
@@ -502,14 +461,11 @@ class Pakfire(object):
                                        if pkg in pkgs:
                                                continue
 
-                                       pkgs.append(pkg)
+                               pkgs.append(pkg)
 
                return sorted(pkgs)
 
        def search(self, pattern):
-               # Initialize this pakfire instance.
-               self.initialize()
-
                # Do the search.
                pkgs = {}
                for solv in self.pool.search(pattern, satsolver.SEARCH_STRING|satsolver.SEARCH_FILES):
@@ -530,15 +486,9 @@ class Pakfire(object):
                self.install("@%s" % group, **kwargs)
 
        def grouplist(self, group):
-               # Initialize this pakfire instance.
-               self.initialize()
-
                return self.pool.grouplist(group)
 
        def provides(self, patterns):
-               # Initialize this pakfire instance.
-               self.initialize()
-
                pkgs = []
                for pattern in patterns:
                        for pkg in self.pool.whatprovides(self, pattern):
@@ -553,21 +503,12 @@ class Pakfire(object):
                return pkgs
 
        def resolvdep(self, pkg):
-               # Initialize this pakfire instance.
-               self.initialize()
-
                return self.pool.resolvdep(self, pkg)
 
        def repo_list(self):
-               # Initialize this pakfire instance.
-               self.initialize()
-
                return [r for r in self.repos]
 
        def clean_all(self):
-               # Initialize this pakfire instance.
-               self.initialize()
-
                log.debug("Cleaning up everything...")
 
                # Clean up repository caches.
@@ -577,9 +518,6 @@ class Pakfire(object):
                """
                        Try to fix any errors in the system.
                """
-               # Initialize this pakfire instance.
-               self.initialize()
-
                # Detect any errors in the dependency tree.
                # For that we create an empty request and solver and try to solve
                # something.
index 133e955f665f1f2862fc9b24d9ed96ba218f945d..ffd9c9bf8f5a8f0d337b412da494fe7ad03d4069 100644 (file)
@@ -157,8 +157,6 @@ class BuildEnviron(object):
                pass
 
        def start(self):
-               assert not self.pakfire.initialized, "Pakfire has already been initialized"
-
                # Unshare namepsace.
                # If this fails because the kernel has no support for CLONE_NEWIPC or CLONE_NEWUTS,
                # we try to fall back to just set CLONE_NEWNS.
@@ -180,9 +178,6 @@ class BuildEnviron(object):
                # Lock the build environment.
                self.lock()
 
-               # Initialize pakfire instance.
-               self.pakfire.initialize()
-
                # Optionally enable private networking.
                if self.settings.get("private_network", None):
                        _pakfire.unshare(_pakfire.SCHED_CLONE_NEWNET)