-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-static-content.py
executable file
·198 lines (159 loc) · 6.43 KB
/
update-static-content.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
import argparse
import json
import os
import shutil
import subprocess
import sys
DOCUMENT_ROOT = os.path.dirname(os.path.abspath(sys.argv[0]))
IMAGE_DIR = os.path.join(DOCUMENT_ROOT, 'images')
PYTHON_SOURCE_DIR = os.path.join(DOCUMENT_ROOT, 'functions')
CONFIG_FILE = os.path.join(DOCUMENT_ROOT, 'config.json')
CONVERT = '/usr/bin/convert'
WIDTHS = {
'': 1280,
'small': 200,
}
with open(CONFIG_FILE) as f:
CONFIG = json.loads(f.read())
LANGUAGES = [CONFIG['default_l10n']] + CONFIG['additional_l10ns']
def run_command(command):
subprocess.run(command, check=True, timeout=10)
def generate_resized_image(source, variant='', conversion='jpg', levels=[]):
target = source.rsplit('.', 1)[0]
if variant:
target += '-' + variant
target += '.' + conversion
width = WIDTHS[variant]
command = [CONVERT, source]
if levels:
command += ['-level', ','.join(level.strip() for level in levels)]
command += [
'-resize', f'{width}x{int(width*1.33333)}>',
'-background', 'white',
'-flatten',
target,
]
run_command(command)
return target
def generate_small_image(source, levels=[]):
return generate_resized_image(source, variant='small', levels=levels)
def copy_file(source, target_dir=DOCUMENT_ROOT, target_name=None):
if target_name is None:
target_name = os.path.basename(source)
target = os.path.join(target_dir, target_name)
os.makedirs(target_dir, exist_ok=True)
shutil.copy2(source, target)
return (target, target_dir, target_name)
def copy_image(source, target_dir=IMAGE_DIR, target_name=None,
small=False, small_levels=[], conversion=None):
(target, _, _) = copy_file(
source, target_dir=target_dir, target_name=target_name)
if conversion is not None:
tmp_source = target + '-tmp'
os.rename(target, tmp_source)
target = generate_resized_image(tmp_source)
os.remove(tmp_source)
if small:
generate_small_image(target, levels=small_levels)
def process_scanarium_dir(dir):
scenes_dir = os.path.join(dir, 'scenes')
for scene in os.listdir(scenes_dir):
scene_dir = os.path.join(scenes_dir, scene)
if os.path.isdir(scene_dir):
scene_file = os.path.join(scene_dir, 'scene-bait.png')
target_dir = os.path.join(IMAGE_DIR, 'scenes', scene)
copy_image(scene_file, target_dir=target_dir, small=True)
copy_image(os.path.join(dir, 'docs', 'images', 'bait.gif'))
groups = []
for (name, group) in [
('LocalizerFactory.py', 'translate'),
('Localizer.py', 'translate'),
('MessageFormatter.py', 'translate'),
('Util.py', 'util'),
]:
copy_file(os.path.join(dir, 'scanarium', name),
target_dir=os.path.join(PYTHON_SOURCE_DIR, group))
if group not in groups:
init_file = os.path.join(PYTHON_SOURCE_DIR, group, '__init__.py')
open(init_file, 'w').close()
groups.append(group)
for language in LANGUAGES:
copy_file(os.path.join(dir, 'localization', f'{language}.json'),
target_dir=os.path.join(PYTHON_SOURCE_DIR, 'translate',
'localizations'))
def process_scanarium_support_dir(dir):
name = os.path.join(
dir, 'avatar',
'avatar-ohne-hintergrund-ohne-schwarzem-rand-mit-schatten.png')
copy_image(name, target_name='logo-big.png')
dir_handbook = os.path.join(dir, 'handbook', 'images')
for file in os.listdir(dir_handbook):
copy_image(os.path.join(dir_handbook, file), small=True)
dir_handbook = os.path.join(dir, 'handbook', 'images-big')
for file in os.listdir(dir_handbook):
copy_image(os.path.join(dir_handbook, file), small=True, conversion='jpg')
def process_scanarium_homepage_dir(dir):
icon_target_dir = os.path.join(IMAGE_DIR, 'icons')
for image in [
'facebook',
'github',
'instagram',
'language-selector',
'scanarium',
'twitter',
'youtube',
]:
copy_image(os.path.join(dir, f'dynamisch/images/icons/{image}.png'),
target_dir=icon_target_dir)
source = os.path.join(dir, 'published-html', 'pdfs', 'fairies',
'FlowerFairy', 'de', 'Blumenfee.pdf')
copy_image(source, target_name='sample-coloring-page-de.pdf',
conversion='jpg', small=True, small_levels=['90%', '100%'])
copy_image(source, target_name='flower-fairy-normal.pdf',
conversion='jpg', small=True, small_levels=['90%', '100%'])
source = os.path.join(dir, 'published-html', 'pdfs', 'fairies',
'FlowerFairy', 'de', 'Blumenfee-mit-Details.pdf')
copy_image(source, target_name='flower-fairy-detailed.pdf',
conversion='jpg', small=True, small_levels=['90%', '100%'])
def process_base_images():
pass
def parse_arguments():
parser = argparse.ArgumentParser(
description='Updates static content sourced from other repos',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--scanarium-dir', default=None,
help='The directory where the `scanarium` repo can be found')
parser.add_argument(
'--scanarium-homepage-dir', default=None,
help='The directory where the `Homepages/scanarium.com` repo can '
'be found')
parser.add_argument(
'--scanarium-support-dir', default=None,
help='The directory where the `scanarium-support` repo can be found')
return parser.parse_args()
if __name__ == '__main__':
args = parse_arguments()
exit_code = 0
for repo_conf in [
{'name': 'scanarium',
'directory': args.scanarium_dir,
'processor': process_scanarium_dir},
{'name': 'scanarium-support',
'directory': args.scanarium_support_dir,
'processor': process_scanarium_support_dir},
{'name': 'scanarium-homepage',
'directory': args.scanarium_homepage_dir,
'processor': process_scanarium_homepage_dir},
]:
dir = repo_conf['directory']
if dir:
repo_conf['processor'](dir)
print(f'`{repo_conf["name"]}` repo done.')
else:
exit_code = 1
print(f'No {repo_conf["name"]} dir given. Skipping corresponding '
'updates')
process_base_images()
sys.exit(exit_code)