-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
72 lines (56 loc) · 2.29 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This script replaces all template files with a real file with contents
# properly substituted.
import os
import os.path
import shutil
import stat
from string import Template
import sys
import subprocess
def main():
# If not in the project root directory, go to it.
project_root = os.path.dirname(os.path.realpath(__file__))
os.chdir(project_root)
# Can't use `from my_module import metadata' since `__init__.py.tpl' is
# templated.
sys.path.insert(0, os.path.realpath('package'))
import metadata
# The title of the main documentation should match the project name in the
# metadata. Under it should be a matching underline of equal signs. Produce
# it for the template.
project_underline = len(metadata.project) * '='
# Substitute values into template files and produce their real
# counterparts.
for dirpath, dirnames, filenames in os.walk(project_root):
# Don't recurse into git directory.
if '.git' in dirnames:
dirnames.remove('.git')
for filename in filenames:
# Ignore all non-template files.
# Using splitext cuts off the last extension.
root, extension = os.path.splitext(filename)
if extension != '.tpl':
continue
# Substitute values and write the new file.
tpl_path = os.path.join(dirpath, filename)
real_path = os.path.join(dirpath, root)
with open(tpl_path) as tpl_file:
template = Template(tpl_file.read())
print('Substituting', tpl_path, '->', real_path)
with open(real_path, 'w') as real_file:
real_file.write(template.safe_substitute(
project_underline=project_underline,
**metadata.__dict__))
# Remove the template file.
os.remove(tpl_path)
print('Renaming the package module: package ->', metadata.package)
os.rename('package', '%s' % metadata.package)
os.mkdir(project_root + '/' + metadata.package + '/ui/static/' +
metadata.package)
open(project_root + '/' + metadata.package + '/ui/static/' +
metadata.package + '/empty', 'a').close()
os.remove('generate.py')
if __name__ == '__main__':
main()