This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
generate.py
executable file
·124 lines (95 loc) · 4.24 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
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
#!/usr/bin/env python
import glob
import os
import shutil
import sys
import codecs
import urllib2
from optparse import OptionParser
import settings
# Import vendor lib.
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
'vendor'))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
'vendor', 'jinja2'))
import jinja2
import helpers
from dotlang.translate import translate
ENV = jinja2.Environment(
loader=jinja2.FileSystemLoader([
os.path.join(settings.ROOT, 'templates'),
]), extensions=[])
# Hook up template filters.
helpers.load_filters(ENV)
optparser = OptionParser(usage='%prog --output-dir=/tmp/path/example')
optparser.add_option("--output-dir", action="store", dest="output_path",
help="Specify the output directory")
optparser.add_option('-f', '--force', action='store_true', dest='force',
default=False, help='Delete output dir if it exists.')
optparser.add_option('--nowarn', action='store_false', dest='warn',
default=True, help=("Don't warn if unknown L10n strings "
"are encountered"))
optparser.add_option('-v', '--version', action='store', dest='version',
default=settings.BUILD_VERSION,
help="Version to generate. Accepts 'passive' or 'urgent'")
(options, args) = optparser.parse_args()
OUTPUT_PATH = (options.output_path if options.output_path else
os.path.join(settings.ROOT, 'html'))
def copy_file(output_dir, fileName):
"""Helper function that copies a file to a new folder."""
resource_path = os.path.split(settings.ROOT)[0]
shutil.copyfile(os.path.join(resource_path, fileName),
os.path.join(output_dir, fileName))
def write_output(output_dir, filename, text):
"""Helper function that writes a string out to a file."""
f = codecs.open(os.path.join(output_dir, filename), 'w', 'utf-8')
f.write(text)
f.close()
def main():
"""Function run when script is run from the command line."""
template = ENV.get_template('index.html')
# allow parameter to override settings build version
if options.version not in ('passive', 'urgent'):
options.version = settings.BUILD_VERSION
sys.stdout.write("Writing %s template to %s\n" % (options.version, OUTPUT_PATH))
if os.path.exists(OUTPUT_PATH):
if not options.force:
sys.stderr.write('Output path "%s" exists, please remove it or '
'run with --force to overwrite automatically.\n' % (
OUTPUT_PATH))
sys.exit(1)
else:
shutil.rmtree(OUTPUT_PATH)
os.makedirs(OUTPUT_PATH)
# Copy "root" files into output dir's root.
for f in (glob.glob(os.path.join(settings.ROOT, 'root', '*')) +
glob.glob(os.path.join(settings.ROOT, 'root', '.*'))):
shutil.copy(f, OUTPUT_PATH)
# Place static files into output dir.
STATIC_PATH = os.path.join(OUTPUT_PATH, 'static')
for folder in settings.STATIC_FOLDERS:
folder_path = os.path.join(STATIC_PATH, folder)
shutil.copytree(os.path.join(settings.ROOT, folder), folder_path)
for lang in settings.LANGS:
# Make language dir, or symlink to fallback language
LANG_PATH = os.path.join(OUTPUT_PATH, lang)
if lang in settings.LANG_FALLBACK:
os.symlink(settings.LANG_FALLBACK[lang], LANG_PATH)
continue
else:
os.makedirs(LANG_PATH)
# symlink static folders into language dir
for folder in settings.STATIC_FOLDERS:
os.symlink(os.path.join('..', 'static', folder),
os.path.join(LANG_PATH, folder))
# Data to be passed to template
data = {
'LANG': lang,
'DIR': 'rtl' if lang in settings.RTL_LANGS else 'ltr',
'VERSION': options.version,
}
# Load _() translation shortcut for jinja templates and point it to dotlang.
ENV.globals['_'] = lambda txt: translate(lang, txt, warn=options.warn)
write_output(LANG_PATH, 'index.html', template.render(data))
if __name__ == '__main__':
main()