forked from pycountry/pycountry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
71 lines (52 loc) · 1.99 KB
/
generate.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
# vim:fileencoding=utf-8
# Copyright (c) 2008 gocept gmbh & co. kg
# Copyright (c) 2014+ Christian Theune, [email protected]
# See also LICENSE.txt
# $Id$
"""Generate the necessary data files and directory structures from the Debian
project's data."""
import glob
import os.path
import shutil
import subprocess
REVISION = 'iso-codes-4.5.0'
data_dir = 'parts/data'
base_dir = os.path.join('src', 'pycountry')
if not os.path.exists(data_dir):
subprocess.check_call(
['git', 'clone', 'https://salsa.debian.org/iso-codes-team/iso-codes.git',
data_dir])
subprocess.check_call(
['git', '-C', data_dir, 'fetch'])
subprocess.check_call(
['git', '-C', data_dir, 'checkout', REVISION])
assert os.path.exists(base_dir), 'pycountry src directory not found'
assert os.path.exists(data_dir), 'pkg-isocodes data directory not found'
database_dir = os.path.join(base_dir, 'databases')
locales_dir = os.path.join(base_dir, 'locales')
STANDARDS = ['639-3', '639-5', '3166-1', '3166-2', '3166-3', '4217', '15924']
# Put the database files in place
if not os.path.exists(database_dir):
os.mkdir(database_dir)
for standard in STANDARDS:
src = os.path.join(data_dir, 'data', 'iso_%s.json' % standard)
print(src)
dst = os.path.join(database_dir, 'iso%s.json' % standard)
shutil.copyfile(src, dst)
# Put the PO files in place and compile them
for standard in STANDARDS:
for src in glob.glob(
os.path.join(data_dir, 'iso_{}'.format(standard), '*.po')):
print(src)
dir, locale = os.path.split(src)
locale = locale.replace('.po', '')
dst_dir = os.path.join(locales_dir, locale, 'LC_MESSAGES')
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
dst = os.path.join(dst_dir, 'iso%s.po' % standard)
dst_mo = dst.replace('.po', '.mo')
shutil.copyfile(src, dst)
print(src + " -> " + dst)
subprocess.check_call(['msgfmt', dst, '-o', dst_mo])
os.unlink(dst)
# Generate the MO files.