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

Update pyusbtool.py #119

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
96 changes: 56 additions & 40 deletions pyusbtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__":
Expand Down Expand Up @@ -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():
Expand Down