import json, os, re, subprocess, sys from datetime import datetime from pathlib import Path from zipfile import ZipFile DEAL_CONVERT_PATH = os.environ.get('LIGA_BOARDS_DEAL_CONVERT_PATH') PBN_EVENT_TAG = re.compile('\[Event ".*"\]') def deal_convert(source_file, target_files, params=''): command = 'python2 %s %s %s %s' % (DEAL_CONVERT_PATH, params, source_file, ' '.join(target_files)) try: subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True, shell=True) except subprocess.CalledProcessError as ex: print('ERROR: %s' % (ex.output), file=sys.stderr) def ensure_pbn(source_id, config_name, event_name): target_path = Path('output') / config_name / 'files' / (source_id + '.pbn') if target_path.exists(): return target_path print('Target path %s does not exist, generating' % (target_path), file=sys.stderr) source_path = Path('pbns') / config_name / (source_id + '.pbn') target_path.parent.mkdir(parents=True, exist_ok=True) deal_convert(str(source_path), [str(target_path)], '--jfr') with open(target_path) as target_file: pbn_content = target_file.read() pbn_content = re.sub(PBN_EVENT_TAG, '[Event "%s"]' % (event_name), pbn_content) with open(target_path, 'w') as target_file: target_file.write(pbn_content) return target_path def ensure_pdf(source_id, config_name): target_path = Path('output') / config_name / 'files' / (source_id + '.pdf') if target_path.exists(): return target_path print('Target path %s does not exist, generating' % (target_path), file=sys.stderr) source_path = Path('output') / config_name / 'files' / (source_id + '.pbn') target_path.parent.mkdir(parents=True, exist_ok=True) deal_convert(str(source_path), [str(target_path)]) return target_path def ensure_zip(source_id, config_name, source_files): target_path = Path('output') / config_name / 'files' / (source_id + '.zip') if target_path.exists(): return target_path print('Target path %s does not exist, generating' % (target_path), file=sys.stderr) target_path.parent.mkdir(parents=True, exist_ok=True) with ZipFile(target_path, 'w') as zip_file: for source_file in source_files: zip_file.write(source_file, arcname=source_file.name) return target_path output_files = [] config_dir = Path('config') for config_path in config_dir.glob('*.json'): config_name = config_path.stem print('Found config "%s" in %s' % (config_name, config_path), file=sys.stderr) with open(config_path) as config_file: config = json.load(config_file) output_dir = Path('output') / config_name output_dir.mkdir(parents=True, exist_ok=True) content = '' for section in config['sections']: content += '