Skip to content

Commit

Permalink
Reviewing
Browse files Browse the repository at this point in the history
  • Loading branch information
ateska authored Oct 18, 2023
1 parent 778ea02 commit 9bebc08
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions asab/library/providers/libsreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)

Expand Down Expand Up @@ -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 ...
Expand Down

0 comments on commit 9bebc08

Please sign in to comment.