Skip to content

Commit

Permalink
Merge pull request #102 from victormlg/ENT-12526-improving-package-do…
Browse files Browse the repository at this point in the history
…wnload

Improved handling of already downloaded packages
  • Loading branch information
olehermanse authored Dec 19, 2024
2 parents a5c1588 + ebe4bb7 commit 56c92c4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 8 additions & 0 deletions cf_remote/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import os
import sys
import re
Expand Down Expand Up @@ -202,3 +203,10 @@ def whoami():
def print_progress_dot(*args):
print(".", end="")
sys.stdout.flush() # STDOUT is line-buffered


def is_different_checksum(checksum, content) :
assert type(content) == bytes

digest = hashlib.sha256(content).digest().hex()
return checksum != digest
19 changes: 11 additions & 8 deletions cf_remote/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import urllib.request
import json
from collections import OrderedDict
from cf_remote.utils import user_error, write_json, mkdir, parse_json
from cf_remote.utils import is_different_checksum, user_error, write_json, mkdir, parse_json
from cf_remote import log
from cf_remote.paths import cf_remote_dir, cf_remote_packages_dir

Expand Down Expand Up @@ -39,22 +39,25 @@ def download_package(url, path=None, checksum=None):

# Use "ab" to prevent truncation of the file in case it is already being
# downloaded by a different thread.
with open(path, "ab") as f:
with open(path, "ab+") as f:
# Get an exclusive lock. If the file size is != 0 then it's already
# downloaded, otherwise we download.
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
st = os.stat(path)
if st.st_size != 0:
log.debug("Package '{}' already downloaded".format(path))
print("Package '{}' already downloaded".format(path))

f.seek(0)
content = f.read()
if checksum and is_different_checksum(checksum, content):
user_error("Downloaded file '{}' does not match expected checksum '{}'. Please delete the file.".format(filename, checksum))

else:
print("Downloading package: '{}'".format(path))

answer = urllib.request.urlopen(url).read()

if checksum:
digest = hashlib.sha256(answer).digest().hex()
if checksum != digest:
user_error("Downloaded file '{}' does not match expected checksum '{}'".format(filename, checksum))
if checksum and is_different_checksum(checksum, answer):
user_error("Downloaded file '{}' does not match expected checksum '{}'".format(filename, checksum))

f.write(answer)
f.flush()
Expand Down

0 comments on commit 56c92c4

Please sign in to comment.