From da5256cca15ad7d796d1e231ea162888951b7c2d Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 10 Feb 2025 15:25:03 +0000 Subject: [PATCH] backend: Use the @run_in_thread decorator This allows us to write shorter code but achieve the same. Signed-off-by: Michael Tremer --- src/buildservice/__init__.py | 45 +++++++++++++++++++----------------- src/buildservice/jobs.py | 10 ++++---- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/buildservice/__init__.py b/src/buildservice/__init__.py index 074f7b08..69d6f8dd 100644 --- a/src/buildservice/__init__.py +++ b/src/buildservice/__init__.py @@ -44,6 +44,7 @@ log = logging.getLogger("pbs") from .__version__ import VERSION as __version__ from .constants import * +from .decorators import * class Backend(object): version = __version__ @@ -339,17 +340,27 @@ class Backend(object): "kinit", "-k", "-t", keytab, principal, **kwargs, ) - async def exists(self, *args, **kwargs): + @run_in_thread + def exists(self, *args, **kwargs): """ Checks whether a file exists """ - return await asyncio.to_thread(os.path.exists, *args, **kwargs) + return os.path.exists(*args, **kwargs) - async def makedirs(self, path, **kwargs): + @run_in_thread + def makedirs(self, path, **kwargs): """ Creates a new directory """ - return await asyncio.to_thread(os.makedirs, path, **kwargs) + return os.makedirs(path, **kwargs) + + async def make_parent_directory(self, path): + """ + Creates the parent directory of path + """ + path = os.path.dirname(path) + + return await self.makedirs(path, exist_ok=True) async def copy(self, src, dst, mode=None): """ @@ -365,7 +376,7 @@ class Backend(object): # Set mode if mode: - await asyncio.to_thread(os.chmod, dst, mode) + await self.chmod(dst, mode) async def move(self, src, dst, **kwargs): """ @@ -379,15 +390,8 @@ class Backend(object): # Move! await asyncio.to_thread(shutil.move, src, dst, **kwargs) - async def make_parent_directory(self, path): - """ - Creates the parent directory of path - """ - path = os.path.dirname(path) - - return await self.makedirs(path, exist_ok=True) - - async def unlink(self, path): + @run_in_thread + def unlink(self, path): """ Unlinks path """ @@ -400,9 +404,6 @@ class Backend(object): log.debug("Unlinking %s" % path) - await asyncio.to_thread(self._unlink, path) - - def _unlink(self, path): # Unlink the file we were asked to unlink try: os.unlink(path) @@ -426,7 +427,8 @@ class Backend(object): log.debug(" Cleaned up %s..." % path) - async def rmtree(self, path): + @run_in_thread + def rmtree(self, path): """ Removes a directory recursively """ @@ -440,14 +442,15 @@ class Backend(object): log.debug("Removing %s..." % path) try: - await asyncio.to_thread(shutil.rmtree, path) + shutil.rmtree(path) # Ignore if path didn't exist in the first place except FileNotFoundError: pass - async def chmod(self, *args, **kwargs): - return await asyncio.to_thread(os.chmod, *args, **kwargs) + @run_in_thread + def chmod(self, *args, **kwargs): + return os.chmod(*args, **kwargs) def tempfile(self, mode="w+b", sync=False, **kwargs): """ diff --git a/src/buildservice/jobs.py b/src/buildservice/jobs.py index 3c4b86ac..6a007ec2 100644 --- a/src/buildservice/jobs.py +++ b/src/buildservice/jobs.py @@ -971,7 +971,8 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin): # Open Log - async def open_log(self): + @run_in_thread + def open_log(self): """ Opens the log file, and returns an open file handle """ @@ -979,15 +980,12 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin): if not self.has_log(): raise FileNotFoundError - return await asyncio.to_thread(self._open_log, self.log_path) - - def _open_log(self, path): # Open gzip-compressed files - if path.endswith(".gz"): + if self.log_path.endswith(".gz"): return gzip.open(path) # Open uncompressed files - return open(path, "rb") + return open(self.log_path, "rb") # Tail Log -- 2.47.2