-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpopulate.py
executable file
·259 lines (180 loc) · 7 KB
/
populate.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
import fnmatch
import os
import re
import sys
import textwrap
from datetime import date
from string import capwords
from subprocess import check_call, check_output
from os.path import abspath, dirname, join
try:
from ConfigParser import SafeConfigParser as ConfigParser
except ImportError:
try:
from configparser import SafeConfigParser as ConfigParser
except ImportError:
from configparser import ConfigParser
assert ConfigParser # silence pyflakes
this_dir = dirname(abspath(__file__))
template_dir = join(this_dir, 'template')
populate_ini = join(this_dir, 'populate.ini')
if sys.version_info < (3, 0):
def items(x):
return x.iteritems()
else:
def items(x):
return x.items()
def find_templated_files():
for root, _, filenames in os.walk(template_dir): # pylint: disable=W0612
for f in fnmatch.filter(filenames, '*.template'):
yield join(root, f)
def find_templated_directories():
for root, dirnames, _ in os.walk(template_dir): # pylint: disable=W0612
for d in fnmatch.filter(dirnames, '{{ * }}'):
yield join(root, d)
def git(*args):
return check_output(('git',) + tuple(args)).strip()
def read_requirements(requirements_file_basename):
with open(join(template_dir, requirements_file_basename)) as f:
content = f.read()
for line in content.splitlines():
line = re.sub('#.*', '', line).strip()
if not line:
continue
# Ignore included requirements files.
if line.startswith('-r'):
continue
yield line
def get_author_email():
return git('config', 'user.email')
def get_author_name():
return git('config', 'user.name')
def get_copyright_years():
return str(date.today().year)
def get_github_info():
result = re.search(
'github.com[:/](?P<user>[^/]+)/(?P<repo>.+)[.]git',
git('config', '--get', 'remote.origin.url'))
if not result:
raise RuntimeError(
'Failed to find a GitHub user and/or repository name in the '
'output of "git config --get remote.origin.url".')
return result.group('user'), result.group('repo')
def get_install_requires():
return tuple(read_requirements('requirements.txt'))
def get_tests_require():
return tuple(read_requirements('requirements_test.txt'))
def get_populate_ini_settings():
config = ConfigParser()
config.readfp(open(populate_ini))
values = dict(
package_name=config.get('global', 'package_name'),
package_version=config.get('global', 'package_version'),
short_description=config.get('global', 'short_description'))
empty_values = [k for k, v in items(values) if not v]
if empty_values:
raise RuntimeError(
'Please specify values in "populate.ini" for the following: '
'{empty}'.format(empty=empty_values))
return values
def get_template_values():
user, repo = get_github_info()
values = dict(
author_email=get_author_email(),
author_name=get_author_name(),
copyright_years=get_copyright_years(),
github_user=user,
repo_name=repo,
install_requires=get_install_requires(),
tests_require=get_tests_require())
values.update(get_populate_ini_settings())
# The actual package directory should not have dashes in it, but dashes are
# pretty common for package names.
values['package_dir_name'] = values['package_name'].replace('-', '_')
print('Using the following template values:\n {values}'.format(
values='\n '.join(
'{k}: {v!r}'.format(k=k, v=v)
for k, v in items(values))))
return values
def replace_multiline(text, key, value, filter_name, replacement_fn):
token = '{{{{ {k}|{f} }}}}'.format(k=key, f=filter_name)
regex = re.compile('(?P<indent>[ \t]*){token}'.format(
token=re.escape(token)))
while True:
match = regex.search(text)
if not match:
break
indent = match.group('indent')
replacement = replacement_fn(indent=indent, value=value)
text = regex.sub(replacement, text, count=1)
return text
def replace_pystrings(text, key, value):
return replace_multiline(
text=text, key=key, value=value,
filter_name='pystring',
replacement_fn=lambda indent, value: indent + ('\n' + indent).join(
repr(line) for line in textwrap.wrap(
value,
drop_whitespace=False,
width=70 - len(indent))))
def replace_pytuples(text, key, value):
text = replace_multiline(
text=text, key=key, value=value,
filter_name='pytuple',
replacement_fn=lambda indent, value: '\n'.join(
indent + repr(v) + ',' for v in value))
# squash empty tuples
text = re.sub('[(]\s+[)]', '()', text)
return text
def replace_raw(text, key, value):
return text.replace('{{{{ {k} }}}}'.format(k=key), value)
def replace_capitalize(text, key, value):
return text.replace(
'{{{{ {k}|capitalize }}}}'.format(k=key), capwords(value))
def do_replacements(text, key, value, fns):
for fn in fns:
text = fn(text=text, key=key, value=value)
return text
def populate_files(template_values):
for template_path in find_templated_files():
with open(template_path) as f:
content = f.read()
for k, v in items(template_values):
if isinstance(v, basestring):
fns = replace_raw, replace_capitalize, replace_pystrings
else:
fns = replace_pytuples,
content = do_replacements(text=content, key=k, value=v, fns=fns)
with open(template_path, 'wb') as f:
f.write(content)
populated_path = re.sub('[.]template$', '', template_path)
git('mv', '-f', template_path, populated_path)
def populate_directories(template_values):
for template_dir in find_templated_directories():
for k, v in items(template_values):
if not isinstance(v, basestring):
continue
renamed_dir = template_dir.replace('{{{{ {k} }}}}'.format(k=k), v)
if renamed_dir != template_dir:
git('mv', template_dir, renamed_dir)
def main():
try:
template_values = get_template_values()
populate_files(template_values)
populate_directories(template_values)
# No longer need the template setup files!
git('rm', '-f', populate_ini)
git('rm', abspath(__file__))
# Move everything in template/ to the root of the project.
for filename in os.listdir(template_dir):
git('mv', '-f', join(template_dir, filename), this_dir)
# The template dir is unneeded now and should be empty.
check_call(('rmdir', template_dir))
# Stage the rest of the updated files.
git('add', '-u')
except RuntimeError as e:
print('[ERROR] {e}'.format(e=e))
sys.exit(1)
if __name__ == '__main__':
main()