commit: 5346ae96d8d4b141454fe802a74e855e3e246593 Author: Zac Medico <zmedico <AT> gentoo <DOT> org> AuthorDate: Mon Jan 18 14:37:17 2021 +0000 Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> CommitDate: Mon Jan 18 15:03:28 2021 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=5346ae96
RepoStorageInterface: Use async and await syntax Signed-off-by: Zac Medico <zmedico <AT> gentoo.org> .../repository/storage/hardlink_quarantine.py | 45 +++++++---------- lib/portage/repository/storage/hardlink_rcu.py | 57 +++++++++------------- lib/portage/repository/storage/inplace.py | 27 +++------- lib/portage/repository/storage/interface.py | 17 +++---- lib/portage/util/futures/_sync_decorator.py | 6 +-- 5 files changed, 56 insertions(+), 96 deletions(-) diff --git a/lib/portage/repository/storage/hardlink_quarantine.py b/lib/portage/repository/storage/hardlink_quarantine.py index 165ab8324..bab34997b 100644 --- a/lib/portage/repository/storage/hardlink_quarantine.py +++ b/lib/portage/repository/storage/hardlink_quarantine.py @@ -1,4 +1,4 @@ -# Copyright 2018 Gentoo Foundation +# Copyright 2018-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 from portage import os @@ -7,10 +7,6 @@ from portage.repository.storage.interface import ( RepoStorageInterface, ) from portage.util.futures import asyncio -from portage.util.futures.compat_coroutine import ( - coroutine, - coroutine_return, -) from _emerge.SpawnProcess import SpawnProcess @@ -38,60 +34,55 @@ class HardlinkQuarantineRepoStorage(RepoStorageInterface): self._spawn_kwargs = spawn_kwargs self._current_update = None - @coroutine - def _check_call(self, cmd, loop=None): + async def _check_call(self, cmd): """ Run cmd and raise RepoStorageException on failure. @param cmd: command to executre @type cmd: list """ - p = SpawnProcess(args=cmd, scheduler=asyncio._wrap_loop(loop), **self._spawn_kwargs) + p = SpawnProcess(args=cmd, scheduler=asyncio.get_event_loop(), **self._spawn_kwargs) p.start() - if (yield p.async_wait()) != os.EX_OK: + if await p.async_wait() != os.EX_OK: raise RepoStorageException('command exited with status {}: {}'.\ format(p.returncode, ' '.join(cmd))) - @coroutine - def init_update(self, loop=None): + async def init_update(self): update_location = os.path.join(self._user_location, '.tmp-unverified-download-quarantine') - yield self._check_call(['rm', '-rf', update_location], loop=loop) + await self._check_call(['rm', '-rf', update_location]) # Use rsync --link-dest to hardlink a files into self._update_location, # since cp -l is not portable. - yield self._check_call(['rsync', '-a', '--link-dest', self._user_location, + await self._check_call(['rsync', '-a', '--link-dest', self._user_location, '--exclude=/distfiles', '--exclude=/local', '--exclude=/lost+found', '--exclude=/packages', '--exclude', '/{}'.format(os.path.basename(update_location)), - self._user_location + '/', update_location + '/'], loop=loop) + self._user_location + '/', update_location + '/']) self._update_location = update_location - coroutine_return(self._update_location) + return self._update_location @property - def current_update(self, loop=None): + def current_update(self): if self._update_location is None: raise RepoStorageException('current update does not exist') return self._update_location - @coroutine - def commit_update(self, loop=None): + async def commit_update(self): update_location = self.current_update self._update_location = None - yield self._check_call(['rsync', '-a', '--delete', + await self._check_call(['rsync', '-a', '--delete', '--exclude=/distfiles', '--exclude=/local', '--exclude=/lost+found', '--exclude=/packages', '--exclude', '/{}'.format(os.path.basename(update_location)), - update_location + '/', self._user_location + '/'], loop=loop) + update_location + '/', self._user_location + '/']) - yield self._check_call(['rm', '-rf', update_location], loop=loop) + await self._check_call(['rm', '-rf', update_location]) - @coroutine - def abort_update(self, loop=None): + async def abort_update(self): if self._update_location is not None: update_location = self._update_location self._update_location = None - yield self._check_call(['rm', '-rf', update_location], loop=loop) + await self._check_call(['rm', '-rf', update_location]) - @coroutine - def garbage_collection(self, loop=None): - yield self.abort_update(loop=loop) + async def garbage_collection(self): + await self.abort_update() diff --git a/lib/portage/repository/storage/hardlink_rcu.py b/lib/portage/repository/storage/hardlink_rcu.py index 68081494c..0f7c9cf70 100644 --- a/lib/portage/repository/storage/hardlink_rcu.py +++ b/lib/portage/repository/storage/hardlink_rcu.py @@ -1,4 +1,4 @@ -# Copyright 2018-2020 Gentoo Authors +# Copyright 2018-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 import datetime @@ -10,10 +10,6 @@ from portage.repository.storage.interface import ( RepoStorageInterface, ) from portage.util.futures import asyncio -from portage.util.futures.compat_coroutine import ( - coroutine, - coroutine_return, -) from _emerge.SpawnProcess import SpawnProcess @@ -104,8 +100,7 @@ class HardlinkRcuRepoStorage(RepoStorageInterface): self._latest_canonical = None self._snapshots_dir = os.path.join(self._storage_location, 'snapshots') - @coroutine - def _check_call(self, cmd, privileged=False, loop=None): + async def _check_call(self, cmd, privileged=False): """ Run cmd and raise RepoStorageException on failure. @@ -118,16 +113,15 @@ class HardlinkRcuRepoStorage(RepoStorageInterface): kwargs = dict(fd_pipes=self._spawn_kwargs.get('fd_pipes')) else: kwargs = self._spawn_kwargs - p = SpawnProcess(args=cmd, scheduler=asyncio._wrap_loop(loop), **kwargs) + p = SpawnProcess(args=cmd, scheduler=asyncio.get_event_loop(), **kwargs) p.start() - if (yield p.async_wait()) != os.EX_OK: + if await p.async_wait() != os.EX_OK: raise RepoStorageException('command exited with status {}: {}'.\ format(p.returncode, ' '.join(cmd))) - @coroutine - def init_update(self, loop=None): + async def init_update(self): update_location = os.path.join(self._storage_location, 'update') - yield self._check_call(['rm', '-rf', update_location], loop=loop) + await self._check_call(['rm', '-rf', update_location]) # This assumes normal umask permissions if it doesn't exist yet. portage.util.ensure_dirs(self._storage_location) @@ -138,19 +132,18 @@ class HardlinkRcuRepoStorage(RepoStorageInterface): os.stat(self._user_location)) # Use rsync --link-dest to hardlink a files into update_location, # since cp -l is not portable. - yield self._check_call(['rsync', '-a', '--link-dest', self._latest_canonical, - self._latest_canonical + '/', update_location + '/'], loop=loop) + await self._check_call(['rsync', '-a', '--link-dest', self._latest_canonical, + self._latest_canonical + '/', update_location + '/']) elif not os.path.islink(self._user_location): - yield self._migrate(update_location, loop=loop) - update_location = (yield self.init_update(loop=loop)) + await self._migrate(update_location) + update_location = await self.init_update() self._update_location = update_location - coroutine_return(self._update_location) + return self._update_location - @coroutine - def _migrate(self, update_location, loop=None): + async def _migrate(self, update_location): """ When repo.user_location is a normal directory, migrate it to storage so that it can be replaced with a symlink. After migration, @@ -163,27 +156,26 @@ class HardlinkRcuRepoStorage(RepoStorageInterface): portage.util.apply_stat_permissions(update_location, os.stat(self._user_location)) # It's probably on a different device, so copy it. - yield self._check_call(['rsync', '-a', - self._user_location + '/', update_location + '/'], loop=loop) + await self._check_call(['rsync', '-a', + self._user_location + '/', update_location + '/']) # Remove the old copy so that symlink can be created. Run with # maximum privileges, since removal requires write access to # the parent directory. - yield self._check_call(['rm', '-rf', user_location], privileged=True, loop=loop) + await self._check_call(['rm', '-rf', self._user_location], privileged=True) self._update_location = update_location # Make this copy the latest snapshot - yield self.commit_update(loop=loop) + await self.commit_update() @property - def current_update(self, loop=None): + def current_update(self): if self._update_location is None: raise RepoStorageException('current update does not exist') return self._update_location - @coroutine - def commit_update(self, loop=None): + async def commit_update(self): update_location = self.current_update self._update_location = None try: @@ -231,18 +223,13 @@ class HardlinkRcuRepoStorage(RepoStorageInterface): os.symlink(self._latest_symlink, new_symlink) os.rename(new_symlink, self._user_location) - coroutine_return() - yield None - - @coroutine - def abort_update(self, loop=None): + async def abort_update(self): if self._update_location is not None: update_location = self._update_location self._update_location = None - yield self._check_call(['rm', '-rf', update_location], loop=loop) + await self._check_call(['rm', '-rf', update_location]) - @coroutine - def garbage_collection(self, loop=None): + async def garbage_collection(self): snap_ttl = datetime.timedelta(days=self._ttl_days) snapshots = sorted(int(name) for name in os.listdir(self._snapshots_dir)) # always preserve the latest snapshot @@ -259,4 +246,4 @@ class HardlinkRcuRepoStorage(RepoStorageInterface): snap_timestamp = datetime.datetime.utcfromtimestamp(st.st_mtime) if (datetime.datetime.utcnow() - snap_timestamp) < snap_ttl: continue - yield self._check_call(['rm', '-rf', snap_path], loop=loop) + await self._check_call(['rm', '-rf', snap_path]) diff --git a/lib/portage/repository/storage/inplace.py b/lib/portage/repository/storage/inplace.py index 3dbcbd7ad..3ff073554 100644 --- a/lib/portage/repository/storage/inplace.py +++ b/lib/portage/repository/storage/inplace.py @@ -1,11 +1,10 @@ -# Copyright 2018 Gentoo Foundation +# Copyright 2018-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 from portage.repository.storage.interface import ( RepoStorageException, RepoStorageInterface, ) -from portage.util.futures.compat_coroutine import coroutine, coroutine_return class InplaceRepoStorage(RepoStorageInterface): @@ -18,32 +17,22 @@ class InplaceRepoStorage(RepoStorageInterface): self._user_location = repo.location self._update_location = None - @coroutine - def init_update(self, loop=None): + async def init_update(self): self._update_location = self._user_location - coroutine_return(self._update_location) - yield None + return self._update_location @property - def current_update(self, loop=None): + def current_update(self): if self._update_location is None: raise RepoStorageException('current update does not exist') return self._update_location - @coroutine - def commit_update(self, loop=None): + async def commit_update(self): self.current_update self._update_location = None - coroutine_return() - yield None - @coroutine - def abort_update(self, loop=None): + async def abort_update(self): self._update_location = None - coroutine_return() - yield None - @coroutine - def garbage_collection(self, loop=None): - coroutine_return() - yield None + async def garbage_collection(self): + pass diff --git a/lib/portage/repository/storage/interface.py b/lib/portage/repository/storage/interface.py index 4f5be6dbc..924d47f0b 100644 --- a/lib/portage/repository/storage/interface.py +++ b/lib/portage/repository/storage/interface.py @@ -1,8 +1,7 @@ -# Copyright 2018 Gentoo Foundation +# Copyright 2018-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 from portage.exception import PortageException -from portage.util.futures.compat_coroutine import coroutine class RepoStorageException(PortageException): @@ -32,8 +31,7 @@ class RepoStorageInterface: """ raise NotImplementedError - @coroutine - def init_update(self, loop=None): + async def init_update(self): """ Create an update directory as a destination to sync updates to. The directory will be populated with files from the previous @@ -50,7 +48,7 @@ class RepoStorageInterface: raise NotImplementedError @property - def current_update(self, loop=None): + def current_update(self): """ Get the current update directory which would have been returned from the most recent call to the init_update method. This raises @@ -62,16 +60,14 @@ class RepoStorageInterface: """ raise NotImplementedError - @coroutine - def commit_update(self, loop=None): + async def commit_update(self): """ Commit the current update directory, so that is becomes the latest immutable snapshot. """ raise NotImplementedError - @coroutine - def abort_update(self, loop=None): + async def abort_update(self): """ Delete the current update directory. If there was not an update in progress, or it has already been committed, then this has @@ -79,8 +75,7 @@ class RepoStorageInterface: """ raise NotImplementedError - @coroutine - def garbage_collection(self, loop=None): + async def garbage_collection(self): """ Remove expired snapshots. """ diff --git a/lib/portage/util/futures/_sync_decorator.py b/lib/portage/util/futures/_sync_decorator.py index 3da065789..648e56574 100644 --- a/lib/portage/util/futures/_sync_decorator.py +++ b/lib/portage/util/futures/_sync_decorator.py @@ -1,4 +1,4 @@ -# Copyright 2018 Gentoo Foundation +# Copyright 2018-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 import functools @@ -17,9 +17,7 @@ def _sync_decorator(func, loop=None): """ @functools.wraps(func) def wrapper(*args, **kwargs): - nonlocal loop - loop = kwargs['loop'] = asyncio._wrap_loop(kwargs.get('loop') or loop) - return loop.run_until_complete(func(*args, **kwargs)) + return (loop or asyncio.get_event_loop()).run_until_complete(func(*args, **kwargs)) return wrapper