]> git.ipfire.org Git - pbs.git/commitdiff
config: Make Pakfire an async context manager
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Aug 2023 17:18:19 +0000 (17:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Aug 2023 17:18:19 +0000 (17:18 +0000)
This should help us to block less when initializing a Pakfire instance.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/__init__.py
src/buildservice/config.py
src/buildservice/jobs.py
src/buildservice/keys.py
src/buildservice/releasemonitoring.py
src/buildservice/repository.py
src/buildservice/sources.py
tests/setup.py

index 8260860d18794308f7a51f351425d87a7f91d99a..b69493fcf420b5ad9084b02de716da8eb7906f86 100644 (file)
@@ -463,14 +463,11 @@ class Backend(object):
                """
                        Opens a package and returns the archive
                """
-               return await asyncio.to_thread(self._open, path)
-
-       def _open(self, path):
                log.debug("Opening %s..." % path)
 
                # Open the archive
-               with self.pakfire() as p:
-                       return p.open(path)
+               async with self.pakfire() as p:
+                       return await asyncio.to_thread(p.open, path)
 
        @property
        def ssl_context(self):
index cdb3b75f9feb828f77014406da032ea9705befb5..65c237b0467629f06c35a36ccb37af837cccd5e4 100644 (file)
@@ -1,10 +1,10 @@
 #!/usr/bin/python3
 
+import asyncio
 import configparser
 import io
 import logging
 import pakfire
-import tempfile
 
 from . import base
 
@@ -75,22 +75,24 @@ class PakfireConfig(base.Object):
 
        # Context
 
-       def __enter__(self):
+       async def __aenter__(self):
                # Make configuration
                config = self._make_config(local=True)
 
                log.debug("Launching Pakfire with configuration:\n%s", self._to_string(config))
 
-               with tempfile.NamedTemporaryFile(mode="w+") as t:
+               async with self.backend.tempfile(mode="w+") as t:
                        # Write the configuration to disk
                        config.write(t)
                        t.flush()
 
-                       # Launch a new Pakfire instance
-                       return pakfire.Pakfire(arch=self.arch, conf=t.name,
+                       # Launch a new Pakfire instance (in a separate thread)
+                       p = await asyncio.to_thread(pakfire.Pakfire, arch=self.arch, conf=t.name,
                                logger=self._log, offline=False)
 
-       def __exit__(self, type, value, traceback):
+               return p
+
+       async def __aexit__(self, type, value, traceback):
                pass
 
        def _make_config(self, local=False):
index 9f806fa816dfa8ad5d26b0159bb2038003b277ac..5dbe8538afe91ff00770aeb885c82b22b1b50e7e 100644 (file)
@@ -1030,7 +1030,7 @@ class Job(base.DataObject):
 
                packages = []
 
-               with self.pakfire(include_source=True) as p:
+               async with self.pakfire(include_source=True) as p:
                        for package in self.packages:
                                for pkg in p.whatprovides("%s = %s" % (package.name, package.evr)):
                                        # XXX we should search straight away for the UUID here,
index c780b2958ddda8b502a60669be2e8f3a90193c17..a31159f1aa6e0aa38ea694d370016e419136fba9 100644 (file)
@@ -39,7 +39,7 @@ class Keys(base.Object):
                        Creates a new key
                """
                # Launch a new Pakfire instance
-               with self.backend.pakfire() as p:
+               async with self.backend.pakfire() as p:
                        # Generate the new key
                        key = p.generate_key(DEFAULT_ALGORITHM, comment)
 
index ce8dafdeff61688b0e1b006f3e8c9cdf6ae3fb77..01d7dd79399a77b8ad43fef9bff67f46b0802f8f 100644 (file)
@@ -773,7 +773,7 @@ class Release(base.DataObject):
                        makefile = os.path.join(tmp, "%s.nm" % package.name)
 
                        # Create a Pakfire instance from this distribution
-                       with self.monitoring.distro.pakfire() as p:
+                       async with self.monitoring.distro.pakfire() as p:
                                # Open the archive
                                archive = await asyncio.to_thread(p.open, package.path)
 
index 2ea4bfa54feaa34904fbbfbb989edcd26f37bf71..f27a4bc142fdc59b411aa049e65d6b8c95f02c3d 100644 (file)
@@ -898,7 +898,7 @@ class Repository(base.DataObject):
                # Pakfire instances at a time which leads to Pakfire only being initialized once
                # per architecture. We are then able to run all dependency checks one after the
                # other.
-               with contextlib.ExitStack() as stack:
+               async with contextlib.AsyncExitStack() as stack:
                        ps = {}
 
                        success = False
@@ -908,7 +908,7 @@ class Repository(base.DataObject):
                                try:
                                        p = ps[job.arch]
                                except KeyError:
-                                       p = ps[job.arch] = stack.enter_context(
+                                       p = ps[job.arch] = await stack.enter_async_context(
                                                self.pakfire(arch=job.arch)
                                        )
 
@@ -930,7 +930,7 @@ class Repository(base.DataObject):
 
                # Create a new pakfire instance
                async with self.backend.tempdir(prefix="pakfire-repo-") as t:
-                       with self.pakfire() as p:
+                       async with self.pakfire() as p:
                                # Fetch the key
                                if self.key:
                                        key = self.key._make_key(p)
index 7d1e3331e6f69ed3358f6ac8c941cc056a7562e8..4269a4991f5942d489e0886daab530ec218f71d6 100644 (file)
@@ -814,7 +814,7 @@ class Job(base.DataObject):
                                makefile = os.path.join(path, "%s/%s.nm" % (self.name, self.name))
 
                                # Launch a Pakfire instance with the repository configuration
-                               with self.source.repo.pakfire() as p:
+                               async with self.source.repo.pakfire() as p:
                                        log.debug("Running dist for %s..." % makefile)
 
                                        # Run dist() in a separate thread
index 2829f1c5678ac74a894fe2624b986e3b17a1ff70..6f0701d527df40273571b2228947ff23a9a6bb8d 100755 (executable)
@@ -6,11 +6,11 @@ import unittest
 import test
 
 class SetupTestCase(test.TestCase):
-       def test_pakfire(self):
+       async def test_pakfire(self):
                """
                        This test checks whether we can launch into a local Pakfire instance
                """
-               with self.backend.pakfire() as p:
+               async with self.backend.pakfire() as p:
                        pass
 
        async def test_unlink(self):