From cc802f7727d101523fec0c9e30f6c03ed4d94052 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Wed, 29 Mar 2023 21:26:33 +0000 Subject: [PATCH] Make all `*_temporary_metadata` methods take/return unique names rather than filenames --- codearchiver/storage.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/codearchiver/storage.py b/codearchiver/storage.py index 6bd2519..4dc755f 100644 --- a/codearchiver/storage.py +++ b/codearchiver/storage.py @@ -83,7 +83,7 @@ class Storage(abc.ABC): @abc.abstractmethod def search_temporary_metadata(self, criteria: list[tuple[str, typing.Union[str, tuple[str]]]]) -> collections.abc.Iterator[str]: - '''Same as `search_metadata`, but for the temporary metadata written by `add_temporary_metadata`.''' + '''Same as `search_metadata`, but for the temporary metadata written by `add_temporary_metadata`, returning the unique names instead.''' @abc.abstractmethod def open_temporary_metadata(self, name: str) -> typing.TextIO: @@ -190,13 +190,14 @@ class DirectoryStorage(Storage): # Build a filename based on the current time in nanoseconds and a (truncated) hash of the metadata; this should guaranteed uniqueness to a sufficient degree. serialised = metadata.serialise().encode('utf-8') metadataHash = hashlib.sha512(serialised).hexdigest()[:128] - filename = f'tmp_{time.time_ns()}_{metadataHash}_codearchiver_temporary_metadata.txt' + name = f'tmp_{time.time_ns()}_{metadataHash}' + filename = f'{name}_codearchiver_temporary_metadata.txt' self._ensure_directory() _logger.info(f'Writing temporary metadata to {filename}') with open(os.path.join(self._directory, filename), 'xb') as fp: fp.write(serialised) _logger.info('Done writing temporary metadata file') - return filename + return name def search_temporary_metadata(self, criteria): yield from self.search_metadata(criteria, _suffix = '_codearchiver_temporary_metadata.txt') @@ -207,14 +208,15 @@ class DirectoryStorage(Storage): yield fp def replace_temporary_metadata(self, name, filename, metadata): + _logger.info(f'Replacing temporary metadata file {name}') self.put(filename, metadata) self.remove_temporary_metadata(name) def remove_temporary_metadata(self, name): - if not name.endswith('_codearchiver_temporary_metadata.txt'): + if name.endswith('_codearchiver_temporary_metadata.txt'): raise RuntimeError('invalid temporary metadata name provided') _logger.info(f'Removing temporary metadata file {name}') - os.remove(os.path.join(self._directory, name)) + os.remove(os.path.join(self._directory, f'{name}_codearchiver_temporary_metadata.txt')) def wait_temporary_metadata(self, names, sleepTime = 5): _logger.info(f'Waiting for temporary metadata: {names!r}') @@ -223,6 +225,7 @@ class DirectoryStorage(Storage): with self.lock(blocking = False) as locked: if locked: remaining = {filename for filename in remaining if os.path.exists(filename)} + _logger.debug(f'Remaining: {remaining!r}') if not remaining: break time.sleep(sleepTime)