aboutsummaryrefslogtreecommitdiff
path: root/update.py
blob: 70f3d51804ef1775d9b149e7e0132d3635851200 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
import hashlib
import requests
import os
import shutil
import sys

# Initalize from config
INSTALL_DIR = ""
with open("pack-location.txt", "r") as f:
    INSTALL_DIR = f.read().strip()


# 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])
    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")
        else:
            print(f'Installing {mod[0]} from {mod[2]}...')
            download_obj = requests.get(mod[2], stream=True)
            with open(os.path.join(INSTALL_DIR, mod[0]), "wb") as write_file:
                shutil.copyfileobj(download_obj.raw, write_file)
            print("Done!")

    print("\nRemoving old versions...")
    for jar in os.listdir(INSTALL_DIR):
        if jar not in names and os.path.splitext(jar)[1] == ".jar":
            os.remove(os.path.join(INSTALL_DIR, jar))
            print(f"Removing '{jar}'")

    print("\nFinished updating pack!")


# 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)
    print("Downloading new versions of all mods...")
    with open('downloads.txt', 'w') as f:
        f.write('# Format: <jarname> <hex digested sha1> <direct download url>\n')
        for mod in mods:
            print(f"Downloading {mod[0]}...")
            resp = requests.get(mod[1])
            hsh = hashlib.sha1(resp.content)
            f.write(f'{mod[0]} {hsh.hexdigest()} {resp.url}\n')
    print("\nDone downloading 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:
        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))
                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!")


if len(sys.argv) < 2:
    print(f"Usage: {sys.argv[0]} <apply_updates|find_updates>")
    sys.exit(-1)
elif sys.argv[1] == 'install':
    install()
elif sys.argv[1] == 'apply_updates':
    apply_updates()
elif sys.argv[1] == 'check_updates':
    check_updates()
else:
    print(f"Usage: {sys.argv[0]} <install|apply_updates|check_updates>")
    sys.exit(-1)