|
| 1 | +#!env python3 |
| 2 | +from pathlib import Path |
| 3 | +from datetime import datetime |
| 4 | +import subprocess |
| 5 | +import json |
| 6 | +import os |
| 7 | +import fcntl |
| 8 | + |
| 9 | +from utils import get_logger, parse_config, update_status_json, parse_state |
| 10 | + |
| 11 | +general, mirrors = parse_config() |
| 12 | +os.chdir(general['chdir']) |
| 13 | +log = get_logger(general['logdir'], 'sync') |
| 14 | + |
| 15 | + |
| 16 | +def do_sync(mirror, lastsuccess=None): |
| 17 | + status['state'] = f'Y{int(datetime.now().timestamp())}' |
| 18 | + if lastsuccess: |
| 19 | + status['state'] += f'O{int(lastsuccess.timestamp())}' |
| 20 | + log.info(f'syncing {mirror}') |
| 21 | + with open(path, 'w') as f: |
| 22 | + f.write(json.dumps(status)) |
| 23 | + update_status_json(mirrors) |
| 24 | + |
| 25 | + proc_output_path = Path('logs.d', 'output', f'{mirror}-{status["state"]}') |
| 26 | + proc_output = open(proc_output_path, 'w') |
| 27 | + proc = subprocess.Popen( |
| 28 | + executable='/bin/sh', |
| 29 | + args=["sh", "-c", status['command']], |
| 30 | + stderr=subprocess.STDOUT, |
| 31 | + stdout=proc_output, |
| 32 | + ) |
| 33 | + proc_output.close() |
| 34 | + |
| 35 | + try: |
| 36 | + code = proc.wait() |
| 37 | + except KeyboardInterrupt: |
| 38 | + code = -1 |
| 39 | + |
| 40 | + if code == 0: |
| 41 | + log.info(f'successfully synced {mirror}') |
| 42 | + message = f'Successfully Synced {mirror}.' |
| 43 | + proc_output_path.unlink() |
| 44 | + status['state'] = f'S{int(datetime.now().timestamp())}' |
| 45 | + elif code == -1: |
| 46 | + message = f'Paused Syncing {mirror}.' |
| 47 | + status['state'] = f'P{int(datetime.now().timestamp())}' |
| 48 | + if lastsuccess: |
| 49 | + message += f' Last Successful Sync: {lastsuccess.strftime(general["timeformat"])}.' |
| 50 | + status['state'] += f'O{int(lastsuccess.timestamp())}' |
| 51 | + else: |
| 52 | + message = f'Error Occured Syncing {mirror}.' |
| 53 | + log.error(f'error syncing {mirror}') |
| 54 | + status['state'] = f'F{int(datetime.now().timestamp())}' |
| 55 | + if lastsuccess: |
| 56 | + message += f' Last Successful Sync: {lastsuccess.strftime(general["timeformat"])}.' |
| 57 | + status['state'] += f'O{int(lastsuccess.timestamp())}' |
| 58 | + |
| 59 | + with open(path, 'w') as f: |
| 60 | + f.write(json.dumps(status)) |
| 61 | + update_status_json(mirrors) |
| 62 | + |
| 63 | + |
| 64 | +if os.path.exists('sync.lock'): |
| 65 | + print('sync.py already running...') |
| 66 | + exit() |
| 67 | + |
| 68 | +with open('sync.lock', 'w+') as f: |
| 69 | + try: |
| 70 | + fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 71 | + except IOError: |
| 72 | + print('sync.py already running...') |
| 73 | + exit() |
| 74 | + |
| 75 | + for mirror in mirrors.sections(): |
| 76 | + path = Path('status.d', mirror) |
| 77 | + path.touch() |
| 78 | + with open(path, 'r') as f: |
| 79 | + status = f.read() |
| 80 | + try: |
| 81 | + status = json.loads(status) |
| 82 | + except json.JSONDecodeError: |
| 83 | + status = {} |
| 84 | + if not status: |
| 85 | + log.warning(f'{mirror} never synced, syncing for the first time...') |
| 86 | + status['name'] = mirror |
| 87 | + status['command'] = mirrors[mirror]['command'].format(**general['vars']) |
| 88 | + lastsuccess = None |
| 89 | + for state, time in parse_state(status.get('state') or ''): # follows mirrorz rules |
| 90 | + if state == 'S': |
| 91 | + if (datetime.now() - time).total_seconds() < 28800: |
| 92 | + log.info(f'skipping {mirror}, less than 8 hours since last sync') |
| 93 | + break |
| 94 | + lastsuccess = time |
| 95 | + elif state == 'Y': |
| 96 | + break |
| 97 | + elif state == 'O' and lastsuccess is None: |
| 98 | + lastsuccess = time |
| 99 | + else: |
| 100 | + do_sync(mirror, lastsuccess) |
| 101 | + |
| 102 | + # execute this unconditionally on exit |
| 103 | + # to update status.json when mirrorz.meta.json changes |
| 104 | + update_status_json(mirrors) |
| 105 | + try: |
| 106 | + fcntl.flock(f, fcntl.LOCK_UN) |
| 107 | + finally: |
| 108 | + os.remove('sync.lock') |
0 commit comments