diff --git a/pyusbtool.py b/pyusbtool.py index 25a9bfb..77c348b 100755 --- a/pyusbtool.py +++ b/pyusbtool.py @@ -11,6 +11,7 @@ import pathlib import urllib.request import sys +from pathlib import Path USER_AGENT = "python-frc-csa-tool/1.0" CHUNK_SIZE = 2**20 @@ -20,53 +21,62 @@ def download(url: str, dst_fname: pathlib.Path): """ Downloads a file to a specified directory """ + try: - def _reporthook(count, blocksize, totalsize): - percent = int(count * blocksize * 100 / totalsize) - if percent < 0 or percent > 100: - sys.stdout.write("\r--%") - else: - sys.stdout.write("\r%02d%%" % percent) + def _reporthook(count, blocksize, totalsize): + percent = int(count * blocksize * 100 / totalsize) + if percent < 0 or percent > 100: + sys.stdout.write("\r--%") + else: + sys.stdout.write("\r%02d%%" % percent) + sys.stdout.flush() + + + print("Downloading", url) + + request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}) + + with contextlib.closing(urllib.request.urlopen(request)) as fp: + headers = fp.info() + + with open(dst_fname, "wb") as tfp: + # copied from urlretrieve source code, Python license + bs = 1024 * 8 + size = -1 + blocknum = 0 + read = 0 + if "content-length" in headers: + size = int(headers["Content-Length"]) + + while True: + block = fp.read(bs) + if not block: + break + read += len(block) + tfp.write(block) + blocknum += 1 + _reporthook(blocknum, bs, size) + + sys.stdout.write("\n") sys.stdout.flush() - - print("Downloading", url) - - request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}) - - with contextlib.closing(urllib.request.urlopen(request)) as fp: - headers = fp.info() - - with open(dst_fname, "wb") as tfp: - # copied from urlretrieve source code, Python license - bs = 1024 * 8 - size = -1 - blocknum = 0 - read = 0 - if "content-length" in headers: - size = int(headers["Content-Length"]) - - while True: - block = fp.read(bs) - if not block: - break - read += len(block) - tfp.write(block) - blocknum += 1 - _reporthook(blocknum, bs, size) - - sys.stdout.write("\n") - sys.stdout.flush() + except Exception as e: + print("Could not download file {}".format(url)) + print(e) def md5_file(fname: pathlib.Path) -> str: - with open(fname, "rb") as fp: - h = hashlib.md5() - chunk = fp.read(CHUNK_SIZE) - while chunk: - h.update(chunk) + try: + with open(fname, "rb") as fp: + h = hashlib.md5() chunk = fp.read(CHUNK_SIZE) + while chunk: + h.update(chunk) + chunk = fp.read(CHUNK_SIZE) - return h.hexdigest() + return h.hexdigest() + except Exception as e: + print("Could not find file {}".format(fname)) + print(e) if __name__ == "__main__": @@ -103,6 +113,12 @@ def md5_file(fname: pathlib.Path) -> str: md5 = md5.lower() valid_checksum = md5 != "0" * len(md5) + # Check whether the specified path exists or not + #print(args.dst) + + #creating a new directory if needed + Path(args.dst).mkdir(parents=True, exist_ok=True) + fname = args.dst / fname is_invalid = False if fname.exists():