diff --git a/asab/library/providers/libsreg.py b/asab/library/providers/libsreg.py index a4de95c4a..691498bc6 100644 --- a/asab/library/providers/libsreg.py +++ b/asab/library/providers/libsreg.py @@ -56,9 +56,13 @@ def __init__(self, library, path, layer): assert len(self.URLs) > 0 tempdir = tempfile.gettempdir() - self.RepoPath = os.path.join( + self.RootPath = os.path.join( tempdir, "asab.library.libsreg", + ) + + self.RepoPath = os.path.join( + self.RootPath, hashlib.sha256(self.URLs[0].encode('utf-8')).hexdigest() ) @@ -104,25 +108,32 @@ async def _periodic_pull(self, event_name): etag_incoming = response.headers.get('ETag') - # Create a separate temporary directory for extraction - with tempfile.TemporaryDirectory() as temp_extract_dir: - fname = os.path.join(temp_extract_dir, "new.tar.xz") - with open(fname, 'wb') as ftmp: - while True: - chunk = await response.content.read(16 * 1024) - if not chunk: - break - ftmp.write(chunk) - - # Extract the contents to the temporary directory - with tarfile.open(fname, mode='r:xz') as tar: - tar.extractall(temp_extract_dir) - - # Synchronize the directories - synchronize_dirs(self.RepoPath, temp_extract_dir) - if etag_incoming is not None: - with open(etag_fname, 'w') as f: - f.write(etag_incoming) + # Download new version + newtarfname = os.path.join(self.RootPath, "new.tar.xz") + with open(fname, 'wb') as ftmp: + while True: + chunk = await response.content.read(16 * 1024) + if not chunk: + break + ftmp.write(chunk) + + # Extract the contents to the temporary directory + temp_extract_dir = os.path.join( + self.RootPath, + "new" + ) + # TODO: Remove temp_extract_dir if exists (from last, failed run) + with tarfile.open(newtarfname, mode='r:xz') as tar: + tar.extractall(temp_extract_dir) + + # Synchronize the directories + synchronize_dirs(self.RepoPath, temp_extract_dir) + if etag_incoming is not None: + with open(etag_fname, 'w') as f: + f.write(etag_incoming) + + # TODO: Remove temp_extract_dir + # TODO: Remove newtarfname elif response.status == 304: # The repository has not changed ...