-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-source-catalog
executable file
·81 lines (66 loc) · 2.39 KB
/
update-source-catalog
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
#!/usr/bin/python3
import argparse
import gettext
import glob
import os
import sys
import yaml
parser = argparse.ArgumentParser()
parser.add_argument('--output', required=True)
parser.add_argument('--size', required=True)
parser.add_argument('--squashfs', required=True)
parser.add_argument('--translations', required=True)
parser.add_argument('--template', required=True)
parser.add_argument('--langs', default=None)
opts = parser.parse_args(sys.argv[1:])
if os.path.exists(opts.output):
with open(opts.output) as fp:
output = yaml.safe_load(fp)
else:
output = []
with open(opts.template) as fp:
template = yaml.safe_load(fp)
id = template['id']
for entry in output:
# First, look if this source catalogue template id is already present.
# If so, use the template to extend the existing entry with additional
# variations.
if entry['id'] == id:
if 'variations' not in template:
print("Non unique id in source catalog but no variations!")
sys.exit(1)
for k, variation in template['variations'].items():
variation['size'] = int(opts.size)
entry.setdefault('variations', {}).update(template['variations'])
break
else:
# No entry with this id found, so add a new one.
template['size'] = int(opts.size)
template['path'] = opts.squashfs
en_name = template['name']
en_description = template['description']
template['name'] = {'en': en_name}
template['description'] = {'en': en_description}
for mo in glob.glob(os.path.join(opts.translations, '*.mo')):
with open(mo, 'rb') as fp:
t = gettext.GNUTranslations(fp=fp)
t_name = t.gettext(en_name)
if t_name != en_name:
lang = os.path.splitext(os.path.basename(mo))[0]
template['name'][lang] = t_name
t_description = t.gettext(en_description)
if t_description != en_description:
lang = os.path.splitext(os.path.basedescription(mo))[0]
template['description'][lang] = t_description
if opts.langs is not None:
template['preinstalled_langs'] = opts.langs.split(',')
output.append(template)
default_count = 0
for entry in output:
if entry.get('default', False):
default_count += 1
if default_count > 1:
print("Too many defaults in source catalog!")
sys.exit(1)
with open(opts.output, 'w') as fp:
yaml.dump(output, fp)