From: Michael Tremer Date: Tue, 1 Aug 2023 17:18:19 +0000 (+0000) Subject: config: Make Pakfire an async context manager X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22a89cd70a6f534a1d4d7300571c950bcb4d5ce3;p=pbs.git config: Make Pakfire an async context manager This should help us to block less when initializing a Pakfire instance. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/__init__.py b/src/buildservice/__init__.py index 8260860d..b69493fc 100644 --- a/src/buildservice/__init__.py +++ b/src/buildservice/__init__.py @@ -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): diff --git a/src/buildservice/config.py b/src/buildservice/config.py index cdb3b75f..65c237b0 100644 --- a/src/buildservice/config.py +++ b/src/buildservice/config.py @@ -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): diff --git a/src/buildservice/jobs.py b/src/buildservice/jobs.py index 9f806fa8..5dbe8538 100644 --- a/src/buildservice/jobs.py +++ b/src/buildservice/jobs.py @@ -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, diff --git a/src/buildservice/keys.py b/src/buildservice/keys.py index c780b295..a31159f1 100644 --- a/src/buildservice/keys.py +++ b/src/buildservice/keys.py @@ -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) diff --git a/src/buildservice/releasemonitoring.py b/src/buildservice/releasemonitoring.py index ce8dafde..01d7dd79 100644 --- a/src/buildservice/releasemonitoring.py +++ b/src/buildservice/releasemonitoring.py @@ -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) diff --git a/src/buildservice/repository.py b/src/buildservice/repository.py index 2ea4bfa5..f27a4bc1 100644 --- a/src/buildservice/repository.py +++ b/src/buildservice/repository.py @@ -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) diff --git a/src/buildservice/sources.py b/src/buildservice/sources.py index 7d1e3331..4269a499 100644 --- a/src/buildservice/sources.py +++ b/src/buildservice/sources.py @@ -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 diff --git a/tests/setup.py b/tests/setup.py index 2829f1c5..6f0701d5 100755 --- a/tests/setup.py +++ b/tests/setup.py @@ -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):