Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved handling of already downloaded packages #102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading