|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from index import win_path |
| 3 | +from tqdm import tqdm |
| 4 | +from urllib.parse import quote |
| 5 | +from concurrent.futures import as_completed |
| 6 | +import concurrent.futures |
| 7 | +import urllib3 |
| 8 | +import datetime |
| 9 | +import requests |
| 10 | +import backoff |
| 11 | +import shutil |
| 12 | +import stat |
| 13 | +import json |
| 14 | +import lzma |
| 15 | +import time |
| 16 | +import sys |
| 17 | +import os |
| 18 | + |
| 19 | +@backoff.on_exception(backoff.expo, (requests.exceptions.RequestException, urllib3.exceptions.ProtocolError)) |
| 20 | +def download_file(session, url, dest): |
| 21 | + with session.get(url, stream=True, timeout=10) as r: |
| 22 | + with open(dest, 'wb') as f: |
| 23 | + shutil.copyfileobj(r.raw, f) |
| 24 | + |
| 25 | +# Fix for "read-only" files on Windows |
| 26 | +def chown_file(path): |
| 27 | + os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) |
| 28 | + |
| 29 | +def fetch_index(version, endpoint): |
| 30 | + r = requests.get('%s/%s.json.xz' % (endpoint, version)) |
| 31 | + return json.loads(lzma.decompress(r.content)) |
| 32 | + |
| 33 | +if __name__ == '__main__': |
| 34 | + |
| 35 | + with open('config.json', 'r') as f: |
| 36 | + config = json.load(f) |
| 37 | + |
| 38 | + if len(sys.argv) != 4: |
| 39 | + print('Usage: update.py <flashpoint-path> <current-version> <target-version>') |
| 40 | + sys.exit(0) |
| 41 | + |
| 42 | + flashpoint = win_path(sys.argv[1]) |
| 43 | + if not os.path.isdir(flashpoint): |
| 44 | + print('Error: Flashpoint path not found.') |
| 45 | + sys.exit(0) |
| 46 | + |
| 47 | + endpoint = config['index_endpoint'] |
| 48 | + try: |
| 49 | + current, target = fetch_index(sys.argv[2], endpoint), fetch_index(sys.argv[3], endpoint) |
| 50 | + except requests.exceptions.RequestException: |
| 51 | + print('Could not retrieve indexes for the versions specified.') |
| 52 | + sys.exit(0) |
| 53 | + |
| 54 | + start = time.time() |
| 55 | + tmp = os.path.join(flashpoint, '.tmp') |
| 56 | + os.mkdir(tmp) |
| 57 | + to_download = list() |
| 58 | + print('Preparing contents...') |
| 59 | + for hash in tqdm(target['files'], unit=' files', ascii=True): |
| 60 | + if hash in current['files']: |
| 61 | + path = os.path.normpath(current['files'][hash][0]) |
| 62 | + os.rename(os.path.join(flashpoint, path), os.path.join(tmp, hash)) |
| 63 | + else: |
| 64 | + to_download.append(hash) |
| 65 | + |
| 66 | + print('Downloading new data...') |
| 67 | + session = requests.Session() |
| 68 | + with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor: |
| 69 | + tasks = list() |
| 70 | + for hash in to_download: |
| 71 | + url = '%s/%s' % (config['file_endpoint'], quote(target['files'][hash][0])) |
| 72 | + tasks.append(executor.submit(download_file, session, url, os.path.join(tmp, hash))) |
| 73 | + for future in tqdm(as_completed(tasks), total=len(tasks), unit=' files', ascii=True): |
| 74 | + future.result() |
| 75 | + |
| 76 | + print('Removing obsolete files...') |
| 77 | + for r, d, f in os.walk(flashpoint, topdown=False): |
| 78 | + if r == tmp: |
| 79 | + continue |
| 80 | + for x in f: |
| 81 | + path = os.path.join(r, x) |
| 82 | + chown_file(path) |
| 83 | + os.remove(path) |
| 84 | + for x in d: |
| 85 | + path = os.path.join(r, x) |
| 86 | + if path != tmp: |
| 87 | + chown_file(path) |
| 88 | + os.rmdir(path) |
| 89 | + |
| 90 | + print('Creating file structure...') |
| 91 | + for hash in tqdm(target['files'], unit=' files', ascii=True): |
| 92 | + paths = target['files'][hash] |
| 93 | + while paths: |
| 94 | + path = os.path.normpath(paths.pop(0)) |
| 95 | + parent = os.path.dirname(path) |
| 96 | + if parent: |
| 97 | + os.makedirs(os.path.join(flashpoint, parent), exist_ok=True) |
| 98 | + tmpfile = os.path.join(tmp, hash) |
| 99 | + dest = os.path.join(flashpoint, path) |
| 100 | + if paths: |
| 101 | + shutil.copy(tmpfile, dest) |
| 102 | + else: # No more paths, we can move instead |
| 103 | + os.rename(tmpfile, dest) |
| 104 | + |
| 105 | + for path in target['empty']: |
| 106 | + os.makedirs(os.path.join(flashpoint, os.path.normpath(path))) |
| 107 | + |
| 108 | + os.rmdir(tmp) |
| 109 | + print('Update completed in %s' % str(datetime.timedelta(seconds=time.time() - start))) |
0 commit comments