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 zcmd1j<>g{vU|{&-wKMS*2Lr=n5C<7EGcYhXFfcF_&tPC+NMT4}%wdRPNMVX%Ol3@A z&Somo1B)?5F{Lo1u;ei0GDk5pGNdvsU|GljrdbyrEs?}MDeEZr0}*dMDeBYrSP{fMDeExqzJY! zL-iUO{4Vc4B&Jv0iCW zPEKZ$@k>Sq28JrhjFOUqVk>?9g2aNF()T8FI^!$wInRHs5mn}uUJD< zPfxFk%P~1QwYV50_lrxvpeR2pHMyi%zlvE;Pj4mTE%uVk+|>Nil3xPBC5a`a#R|^( zDX9vX#a0TpILe>~X>#0REhx&&E4js!lvtb^pIDF?Us{xNivt!=x0usYOKx#PjS9)k zO})ijTvBw4yBKU{d~$wD>MfS6;`}^K=3DGgH-NMk7v$#^r{3ZLnOKwx;jLsS5@BFq z_+_G>k)NBYpPZPJnxvneS)%WonU|Vel$chc8(NT(Sdv<#UyxW-oLW>2i3ne3y@JYH z+(-ey2MT!}1_lNWMhQkSMj=KHMh?azF$M+(l!#(tU|;}Ya70NUM-*cXLo;J7V+~^p z6F8um{i?)}0@^3BB(=B%9Lz8O|NsAAlc|W4fq?;RgCKm^pvmG_C5{v(UWr8@>%k!s#hH>{o|luK zm{J_YomfzilbM`Yl9`_e%ReBuXtE-F8^sCnV_sT*6nA-kQFdltx=UtqNfZwf#|Vg5t9Xp&+s0eKseq*%a7 zh7D_?Vi9LZVH0P7l-1%4DI6v2DV)8`wUC6voKzX5w@5 z)1x>Git-B*(?R7w$ODiv2ULz0feKHEZ$SyD2<&-~E07Wf!V?@ex%nxjIjMG_D!W(( lR0K1yfGY?dMh->+aOPlRWMO1s