|
1 | 1 | import json |
| 2 | +import os |
2 | 3 | from pathlib import Path |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import time |
| 7 | +import traceback |
| 8 | +from typing import List, Optional |
| 9 | + |
| 10 | +class SevenZip: |
| 11 | + def __init__(self, possibleExecutablePaths: List[str], flagsForSearch: List[str]): |
| 12 | + maybePath = SevenZip.findWorkingExecutablePath(possibleExecutablePaths, flagsForSearch) |
| 13 | + if maybePath is None: |
| 14 | + raise Exception(f"Couldn't find working 7zip executable, tried {possibleExecutablePaths}") |
| 15 | + |
| 16 | + self.executablePath = maybePath #type: str |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def findWorkingExecutablePath(executable_paths, flags): |
| 20 | + #type: (List[str], List[str]) -> Optional[str] |
| 21 | + """ |
| 22 | + Try to execute each path in executable_paths to see which one can be called and returns exit code 0 |
| 23 | + The 'flags' argument is any extra flags required to make the executable return 0 exit code |
| 24 | + :param executable_paths: a list [] of possible executable paths (eg. "./7za", "7z") |
| 25 | + :param flags: a list [] of any extra flags like "-h" required to make the executable have a 0 exit code |
| 26 | + :return: the path of the valid executable, or None if no valid executables found |
| 27 | + """ |
| 28 | + with open(os.devnull, 'w') as os_devnull: |
| 29 | + for path in executable_paths: |
| 30 | + try: |
| 31 | + if subprocess.call([path] + flags, stdout=os_devnull, stderr=os_devnull) == 0: |
| 32 | + return path |
| 33 | + except: |
| 34 | + pass |
| 35 | + |
| 36 | + return None |
| 37 | + |
| 38 | + def sevenZipMakeArchive(self, input_path, output_filename): |
| 39 | + tryRemoveTree(output_filename) |
| 40 | + subprocess.call([self.executablePath, "a", output_filename, input_path]) |
| 41 | + |
| 42 | +def tryRemoveTree(path): |
| 43 | + attempts = 5 |
| 44 | + for i in range(attempts): |
| 45 | + try: |
| 46 | + if os.path.isdir(path): |
| 47 | + shutil.rmtree(path) |
| 48 | + else: |
| 49 | + os.remove(path) |
| 50 | + return |
| 51 | + |
| 52 | + except FileNotFoundError: |
| 53 | + return |
| 54 | + except Exception: |
| 55 | + print(f'Warning: Failed to remove "{path}" attempt {i}/{attempts}') |
| 56 | + traceback.print_exc() |
| 57 | + |
| 58 | + time.sleep(1) |
| 59 | + |
| 60 | +def download(url): |
| 61 | + print(f"Starting download of URL: {url}") |
| 62 | + subprocess.call(['curl', '-OJLf', url]) |
| 63 | + |
| 64 | +def clearOldFiles(clearOutput): |
| 65 | + filesToClear = [ |
| 66 | + 'versionData.json', |
| 67 | + 'installData.json', |
| 68 | + 'cachedDownloadSizes.json', |
| 69 | + ] |
| 70 | + |
| 71 | + # Note: need to keep 'updates.json' for compatability with old installers |
| 72 | + # which expect the updates.json in the release |
| 73 | + # Can be removed once it is unlikely people are using the old installer |
| 74 | + if clearOutput: |
| 75 | + filesToClear.extend(['installerMetaData.zip', 'updates.json']) |
| 76 | + |
| 77 | + for file in filesToClear: |
| 78 | + tryRemoveTree(file) |
| 79 | + |
| 80 | +# Remove old files |
| 81 | +clearOldFiles(clearOutput = True) |
| 82 | + |
| 83 | +# Look for 7zip executable |
| 84 | +sevenZip = SevenZip(["7za", "7z"], ['-h']) |
| 85 | + |
| 86 | +# Build updates.json file |
| 87 | +# This contains some html which is displayed in the installer, so we can update the installer html without building a new installer release. |
| 88 | +# For example, for writing news about updates etc. |
3 | 89 |
|
4 | 90 | out_json_path = 'updates.json' |
5 | 91 |
|
|
19 | 105 |
|
20 | 106 | with open(out_json_path, 'w', encoding="utf-8") as file: |
21 | 107 | json.dump(combined, file) |
| 108 | + |
| 109 | +## Download installData.json etc. |
| 110 | +download("https://github.com/07th-mod/python-patcher/raw/refs/heads/master/versionData.json") |
| 111 | +download("https://github.com/07th-mod/python-patcher/raw/refs/heads/master/installData.json") |
| 112 | +download("https://github.com/07th-mod/python-patcher/raw/refs/heads/master/cachedDownloadSizes.json") |
| 113 | + |
| 114 | +## Zip the json files into an archive |
| 115 | +sevenZip.sevenZipMakeArchive('*.json', "installerMetaData.zip") |
| 116 | + |
| 117 | +# Cleanup after script finished - may want to disable this when debugging |
| 118 | +clearOldFiles(clearOutput = False) |
0 commit comments