1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
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 check_path(target_path, source_paths):
source_mtime = max([path.stat().st_mtime for path in source_paths])
if target_path.exists():
if source_mtime <= target_path.stat().st_mtime:
return True
print('Target path %s older than source, regenerating' % (target_path), file=sys.stderr)
target_path.unlink()
else:
print('Target path %s does not exist, generating' % (target_path), file=sys.stderr)
target_path.parent.mkdir(parents=True, exist_ok=True)
return False
def ensure_pbn(source_id, config_name, event_name):
target_path = Path('output') / config_name / 'files' / (source_id + '.pbn')
source_path = Path('pbns') / config_name / (source_id + '.pbn')
if check_path(target_path, [source_path]):
return target_path
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')
source_path = Path('output') / config_name / 'files' / (source_id + '.pbn')
if check_path(target_path, [source_path]):
return target_path
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')
source_paths = [Path(path) for path in source_files]
if check_path(target_path, source_paths):
# TODO: check if file list haven't changed
return target_path
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 += '<tr><td class="bdnt12" colspan="3">%s</td></tr>' % (section['title'])
for dealset in section['sets']:
set_enabled = False
set_finished = True
set_content = '<tr><td class="bdcc1" colspan="3">%s</td></tr>' % (dealset['title'])
set_files = []
for pbn in dealset['files']:
if pbn.get('enabled', 0):
set_files.append(
ensure_pbn(pbn['path'], config_name, pbn['title']))
set_files.append(
ensure_pdf(pbn['path'], config_name))
set_enabled = True
set_content += '<tr>'
set_content += '<td class="bd1">%s</td>' % (pbn['name'])
for filetype in ('pbn', 'pdf'):
set_content += '<td class="bdc"><a class="zb" href="files/%s.%s">%s</a></td>' % (pbn['path'], filetype, filetype)
set_content += '</tr>'
else:
set_finished = False
output_files += set_files
if set_finished and 'zip' in dealset:
output_files.append(
ensure_zip(dealset['zip']['id'], config_name, set_files))
set_content += '<tr>'
set_content += '<td class="bd1" colspan="2">%s</td>' % (dealset['zip'].get('title', 'cały mecz'))
set_content += '<td class="bdc"><a class="zb" href="files/%s.zip">zip</a></td>' % (dealset['zip']['id'])
set_content += '</tr>'
if set_enabled:
content += set_content
content += '<tr><td class="e" colspan="3"> </td></tr>'
with open('template/%s.html' % (config_name)) as template_file:
template = template_file.read()
with open('template/%s.logo.html' % (config_name)) as logoh_file:
template = template.replace('<!---[LOGOH]--->', logoh_file.read())
template = template.replace('<!---[CONTENT]--->', content)
template = template.replace('<!---[GENDATE]--->', datetime.now().strftime('%Y-%m-%d %X'))
output_path = output_dir / 'index.html'
with open(output_path, 'w') as output_file:
output_file.write(template)
print('Written output file %s' % (output_path), file=sys.stderr)
output_files.append(output_path)
existing_files = Path('output').glob('**/*.*')
for existing_file in existing_files:
if not existing_file.name.startswith('.') and existing_file not in output_files:
print('Extraneous file: %s, deleting' % (existing_file))
existing_file.unlink()
|