forked from seanfisk/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·117 lines (93 loc) · 3.95 KB
/
generate.py
File metadata and controls
executable file
·117 lines (93 loc) · 3.95 KB
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
#!/usr/bin/env python
# This script replaces all template files with a real file with contents
# properly substituted.
from __future__ import print_function
import os
import os.path
import shutil
import stat
from string import Template
import sys
import subprocess
## Python 2.6 subprocess.check_output compatibility. Thanks Greg Hewgill!
if 'check_output' not in dir(subprocess):
def check_output(cmd_args, *args, **kwargs):
proc = subprocess.Popen(
cmd_args, *args,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
out, err = proc.communicate()
if proc.returncode != 0:
raise subprocess.CalledProcessError(args)
return out
subprocess.check_output = check_output
def main():
# If not in the project root directory, go to it.
project_root = os.path.dirname(os.path.dirname(
os.path.realpath(__file__)))
os.chdir(project_root)
sys.path.insert(0, os.path.realpath('my_module'))
# Can't use `from my_module import metadata' since `__init__.py' is
# templated.
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('.'):
# 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: my_module ->', metadata.package)
os.rename('my_module', metadata.package)
print('Making main script executable...')
main_py_path = os.path.join(metadata.package, 'main.py')
# This is a no-op on Windows.
os.chmod(main_py_path, os.stat(main_py_path).st_mode | stat.S_IXUSR)
print('Removing internal Travis-CI test file...')
os.remove('.travis.yml')
print("Revising `LICENSE' file...")
# Open file for reading and writing.
with open('LICENSE', 'r+') as license_file:
# Strip off the first two lines.
new_contents = ''.join(license_file.readlines()[2:])
license_file.seek(0)
print(new_contents, file=license_file)
print('Removing internal directory...')
shutil.rmtree('internal')
git_revision = subprocess.check_output(
['git', 'rev-parse', 'HEAD']).rstrip()
print('Dropping PPT version cookie (revision {0})...'.format(git_revision))
# Append the commit hash of the current revision to the file.
with open('.ppt-version', 'a') as ppt_version_cookie_file:
print(git_revision, file=ppt_version_cookie_file)
print('''
To finish project setup:
1. Change the `classifiers' keyword in `setup.py' as necessary.
2. Change the license in `setup.py' and replace the generated `LICENSE' file
with the one of your choice. If you would like to use the MIT license, no
change is necessary.
3. Install `argparse' package when developing on Python 2.6.
4. Change `README.rst' to your own text.
''')
if __name__ == '__main__':
main()