From 802af0c909bbf843df01e423b89dd4b8e2c18169 Mon Sep 17 00:00:00 2001 From: Alexander Hayden Date: Wed, 30 Jan 2019 10:21:29 -0500 Subject: Make code nicer --- update.py | 62 ++++++++++++++++++++++---------------------------------------- 1 file changed, 22 insertions(+), 40 deletions(-) (limited to 'update.py') diff --git a/update.py b/update.py index 70f3d51..68e598a 100755 --- a/update.py +++ b/update.py @@ -14,19 +14,9 @@ with open("pack-location.txt", "r") as f: # Apply updates to the actual mod pack def install(): print("Updating pack...") - # (fname, url) - mods = [] - # Set of jar file names - names = set() - - with open("downloads.txt", "r") as f: - for line in f: - mod = line.strip().split(" ") - if len(line) >= 1 and line[0] != '#': - # run strip on each element - mod = tuple(map(lambda x: x.strip(), mod)) - mods.append(mod) - names.add(mod[0]) + # (fname, checksum, url) + mods = read_file("downloads.txt") + names = [mod[0] for mod in mods] for mod in mods: if mod[0] in os.listdir(INSTALL_DIR) and hashlib.sha1(open(os.path.join(INSTALL_DIR, mod[0]), 'rb').read()).hexdigest() == mod[1]: print("Skipping " + mod[0] + ", already up to date") @@ -49,14 +39,7 @@ def install(): # Using the latest urls, update downloads.txt to match the urls and have the correct sha1 def apply_updates(): print("Reading update file...") - mods = set() - with open('latest-urls.txt') as f: - for line in f: - mod = line.strip().split() - if len(line) >= 1 and line[0] != '#': - # run strip on each element - mod = tuple(map(lambda x: x.strip(), mod)) - mods.add(mod) + mods = read_file("latest-urls.txt") print("Downloading new versions of all mods...") with open('downloads.txt', 'w') as f: f.write('# Format: \n') @@ -70,28 +53,27 @@ def apply_updates(): # Find if any updates are available def check_updates(): print("Reading update files...") - latest_urls = {} - with open('latest-urls.txt') as f: - for line in f: - mod = line.strip().split() - if len(line) >= 1 and line[0] != '#': - # run strip on each element - mod = tuple(map(lambda x: x.strip(), mod)) - latest_urls[mod[0]] = mod[1] - old_urls = {} - with open('downloads.txt') as f: + latest = read_file("latest-urls.txt") + old = read_file("downloads.txt") + old_urls = [mod[2] for mod in old] + + print("Checking updates...\nThe following mods have updates available:\n") + for mod in latest: + resp = requests.get(mod[1]) + if not resp.url in old_urls: + print(f" -> Found update for {mod[0]}: {resp.url.split('/')[-1]}") + print("\nFinished checking for updates!") + +def read_file(fil): + strings = set() + with open(fil) as f: for line in f: - mod = line.strip().split() + string = line.strip().split() if len(line) >= 1 and line[0] != '#': # run strip on each element - mod = tuple(map(lambda x: x.strip(), mod)) - old_urls[mod[0]] = mod[2] - print("Checking updates...\nThe following mods have updates available:\n") - for mod in latest_urls: - resp = requests.get(latest_urls[mod]) - if resp.url != old_urls[mod]: - print(f" -> Found update for {mod}!") - print("Finished checking for updates!") + string = tuple(map(lambda x: x.strip(), string)) + strings.add(string) + return strings if len(sys.argv) < 2: -- cgit v1.2.3