#!/usr/bin/python3 import json, os, time from typing import Tuple meta_file_name = 'meta-v1.json' latest_file_name = 'latest-v1.json' latest_file_path = os.path.join('latest', latest_file_name) def confirm(prompt: str) -> bool: confirmed = input(prompt + ' (Y/N) ') return confirmed.lower() == 'y' def load_latest_data() -> dict: if os.path.isfile(latest_file_path): return json.loads(open(latest_file_path).read()) return {'id': 0} def match_name(filename: str, extension: str=None, exact_name: str=None): if exact_name is not None: return filename == exact_name elif extension is not None: return filename.lower().endswith(extension.lower()) return True def scan_for_file(ask: bool=False, extension: str=None, exact_name: str=None) -> Tuple[str]: for file in os.scandir(): if file.is_dir(): continue if not match_name(file.name, extension, exact_name): continue if ask: if not confirm(f"Found {file.name} in the current directory, do you want to proceed with it?"): return (None, None) return (file.path, file.name) return (None, None) def wait_for_file(waiting_dir: str, extension: str=None, exact_name: str=None) -> Tuple[str]: print(f"Please put a {extension} file in {waiting_dir}") while True: files = [i for i in os.scandir(waiting_dir)] if len(files) == 0: time.sleep(0.5) continue file = files[0] filepath = file.path filename = file.name if match_name(filename, extension, exact_name): break else: os.remove(filepath) print(f"Not a {extension} file: {filename}") return (filepath, filename) def write_metadata(metadata: dict): dir = metadata['label'] metadata = json.dumps(metadata) for filepath in [os.path.join('releases', dir, meta_file_name), latest_file_path]: file = open(filepath, 'w') file.write(metadata) file.close() def main(): latest_data = load_latest_data() if latest_data['id'] > 0: print(f"Current release: {latest_data['label']}") print(f"Released {time.strftime('%d.%m.%Y %H:%M', time.localtime(latest_data['timestamp']))}") print('') version = input('New version? ') if os.path.isdir(version): raise FileExistsError() filepath, filename = scan_for_file(ask=True, extension='jar') if filepath is None: os.makedirs('pending', exist_ok=True) try: filepath, filename = wait_for_file('pending', 'jar') except KeyboardInterrupt: os.rmdir('pending') return changelog = confirm('Do you want to include a changelog?') if changelog: cfilepath, cfilename = scan_for_file(ask=True, exact_name='changelog.txt') if cfilepath is None: try: os.makedirs('pending', exist_ok=True) cfilepath, cfilename = wait_for_file('pending', 'txt') except KeyboardInterrupt: os.rmdir('pending') return version_dir = os.path.join('releases', version) os.mkdir(version_dir) os.rename(filepath, os.path.join(version_dir, filename)) if changelog: os.rename(cfilepath, os.path.join(version_dir, 'changelog.txt')) try: os.rmdir('pending') except FileNotFoundError: pass metadata = { 'label': version, 'id': latest_data['id'] + 1, 'timestamp': int(time.time()), 'file': filename } write_metadata(metadata) print("Done, don't forget to commit and push") if __name__ == "__main__": main()