From dcbbdb63f24ffe513992df97f6c393fe8f6493f9 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Tue, 14 Dec 2021 22:03:03 -0600 Subject: [PATCH] Cleaned up code, added sha256sum checking, and added -o --- .gitignore | 1 + mcUp.py | 39 +++++++++++--------- parsers/__pycache__/paperMC.cpython-310.pyc | Bin 2291 -> 0 bytes parsers/paperMC.py | 15 +++++--- 4 files changed, 32 insertions(+), 23 deletions(-) delete mode 100644 parsers/__pycache__/paperMC.cpython-310.pyc diff --git a/.gitignore b/.gitignore index d392f0e..f5797a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.jar +__pycache__ diff --git a/mcUp.py b/mcUp.py index ad7a376..8b78663 100755 --- a/mcUp.py +++ b/mcUp.py @@ -3,14 +3,17 @@ import parsers.paperMC import argparse import shutil +import hashlib +from os import error parser = argparse.ArgumentParser(description='A command-line tool to update a Minecraft Server.') parser.add_argument('api', metavar='api', help='which API to use') parser.add_argument('project', metavar='project', help='which project to query for') parser.add_argument('action', metavar='action', help='what action to execute') parser.add_argument('subAction', metavar='subAction', help='what sub action to execute') +parser.add_argument('-o', nargs="?", help='Optional: Specify output filepath') args = parser.parse_args() print("mcUp.py, written by Caleb Fontenot") - +print(args.o) # PaperMC command functions def paperMC(project, action, subAction): if action == "get": @@ -24,26 +27,26 @@ def paperMC(project, action, subAction): if subAction == "latest": latestVersion = parsers.paperMC.getLatest(project) output = parsers.paperMC.getJarInfo(project, latestVersion) - print(output["name"]) + if args.o != None: # Check if user set an output filepath + output_file = args.o + else: + output_file = output["name"] with parsers.paperMC.downloadVersion(project, latestVersion) as raw: - with open(output["name"], 'wb') as file_object: + with open(output_file, 'wb') as file_object: shutil.copyfileobj(raw.raw, file_object) - print("Downloaded "+latestVersion+" to "+output["name"]) + print("Downloaded "+latestVersion+" to "+output_file) + #Calculate hash, compare with API given hash + h_sha256 = hashlib.sha256() + with open(output_file, 'rb') as file_object: + chunk = 0 + while chunk != b'': + chunk = file_object.read(1024) + h_sha256.update(chunk) + if h_sha256.hexdigest() == output["sha256"]: + print("sha256sum of downloaded file matches the sum that the API gave, jar is safe to use") + else: + raise error - - #latestVersion = parsers.paperMC.getLatest(project) - #jarInfo = parsers.paperMC.getJarInfo(project, latestVersion) - #print(jarInfo) - #print(jarInfo[0]) - #print(type(jarInfo)) - - - - - -#print(type(getVersionVelocity)) - -#print("Latest build of velocity is", parsers.paperMC.getLatest#("velocity")) # Determine which API to use: if args.api == "paperMC": diff --git a/parsers/__pycache__/paperMC.cpython-310.pyc b/parsers/__pycache__/paperMC.cpython-310.pyc deleted file mode 100644 index e1981eeb602d2fadedcd695c1159636491ef4ecf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2291 zcmZ`)%aYqf6jkf($2>9#nG6ZR1r&vg!YY!iQWOQ5@JNbylLGicNuM;_Y}-R()? z@~px(dzP7HK7n7+mQ`4@nhl)Wvc{e~fG{Y`__ z$HCwm{MDZ@+~RCx#TK_?CKz`PZP_zEJGRlfBWvu$4zq;)!acD}>z!CHd|?lR6IPA6 zcf#UOgi9}XM7ZDJe%$0i+~Of`{9(my-sCN`YrM_Z(5~}!zJYdwZ}JOhH~AL7i1q?! z53TN{SBTE)f4|pdrIQrdY)6XUrb21eE0Xju83@&z%F!r0xcQ1<Y3*3H?A;KVw%=Qa;^EFcK`vn#s^BAg651EGHwQf2u>eb(*udb8%Y zX)2VWxzlD;$b2YLt)iKO$(O7QbT$_GRG)4>)QO&|&Th^{CsVgOWq7nWs2i4EA+w1t z+Xsmf`vAKSM#``zs&w$$vb8vB4=0w6(z0wRbLM`EGfQ`<@=4bzgGCIqT@{E^lx;e( z9=){WC9rjRD|(WTMU*BZaS#nM9X-ewL-u; zG2ey>*%fw~ZLttPc^Qs#7iGgie{)e+&^fy(HV5IyY|glCh<5t3%PU0xgdmhQG{5=} zPuG?WY&UN}{{r=q7Zn}@O1_6-*+7idUW!HeFccYLqqnCNOZ||5&P4Ph2Fuunwsy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