forked from envtpl/envtpl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
envtpl.py
306 lines (247 loc) · 10.8 KB
/
envtpl.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# -*- coding: utf-8 -*-
'''
envtpl - jinja2 template rendering with shell environment variables
Copyright (C) 2014 Andreas Jansson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import re
import sys
import argparse
import jinja2
import json
import io
import subprocess
import hashlib
import uuid
import fnmatch
try:
from jinja2 import pass_eval_context as eval_context
except ImportError:
from jinja2 import evalcontextfilter as eval_context
try:
from jinja2 import contextfunction as cfunction
except ImportError:
from jinja2 import pass_context as cfunction
EXTENSION = '.tpl'
IS_PYTHON_2 = sys.version_info < (3, 0)
def main():
parser = argparse.ArgumentParser(
description='jinja2 template rendering with shell environment '
'variables'
)
parser.add_argument('input_file',
nargs='?', help='Input filename. Defaults to stdin.')
parser.add_argument('-o', '--output-file',
help='Output filename. If none is given, and the '
'input file ends with "%s", the output filename '
'is the same as the input filename, without the '
'%s extension. Otherwise, defaults to stdout.'
% (EXTENSION, EXTENSION))
parser.add_argument('-i', '--search-paths',
help='coma separated additional search paths for '
'template inheritance')
parser.add_argument('--allow-missing', action='store_true',
help='Allow missing variables. By default, envtpl'
' will die with exit code 1 if an environment '
'variable is missing')
parser.add_argument('--keep-template', action='store_true',
help='Keep original template file. By default, '
'envtpl will delete the template file')
parser.add_argument('--reduce-multi-blank-lines', action='store_true',
help='Reduce multi empty or blank lines to a '
'single empty line. By default envtpl will keep '
'multi blank lines')
parser.add_argument('--jinja2-extension', action='append',
help='Load the given extension (can be used '
'multiple times')
args = parser.parse_args()
jinja2_extensions = []
if args.jinja2_extension is not None:
jinja2_extensions = args.jinja2_extension
variables = dict([(k, _unicodify(v)) for k, v in os.environ.items()])
try:
if args.search_paths is None:
extra_search_paths = []
else:
extra_search_paths = [x.strip()
for x in args.search_paths.split(',')]
process_file(args.input_file, args.output_file, variables,
not args.allow_missing, not args.keep_template,
extra_search_paths,
not args.reduce_multi_blank_lines,
jinja2_extensions)
except (Fatal, IOError) as e:
sys.stderr.write('Error: %s\n' % str(e))
sys.exit(1)
sys.exit(0)
def process_file(input_filename, output_filename, variables,
die_on_missing_variable, remove_template,
extra_search_paths=[], keep_multi_blank_lines=True,
jinja2_extensions=[]):
if not input_filename and not remove_template:
raise Fatal('--keep-template only makes sense if you specify '
'an input file')
if die_on_missing_variable:
undefined = jinja2.StrictUndefined
else:
undefined = jinja2.Undefined
if input_filename and not output_filename:
if not input_filename.endswith(EXTENSION):
raise Fatal('If no output filename is given, '
'input filename must end in %s' % EXTENSION)
output_filename = input_filename[:-len(EXTENSION)]
if not output_filename:
raise Fatal('Output filename is empty')
if input_filename:
output = _render_file(input_filename, variables, undefined,
extra_search_paths=extra_search_paths,
keep_multi_blank_lines=keep_multi_blank_lines,
jinja2_extensions=jinja2_extensions)
else:
output = _render_string(stdin_read(), variables, undefined,
extra_search_paths=extra_search_paths,
keep_multi_blank_lines=keep_multi_blank_lines,
jinja2_extensions=jinja2_extensions)
if output_filename and output_filename != '-':
with open(output_filename, 'wb') as f:
f.write(output.encode('utf-8'))
else:
stdout_write(output)
if input_filename and remove_template:
os.unlink(input_filename)
def render_string(string, extra_variables={}, die_on_missing_variable=True,
extra_search_paths=[], keep_multi_blank_lines=True,
jinja2_extensions=[]):
"""
Renders a templated string with envtpl.
Args:
string: templated string to render.
extra_variables: dict (string: string) of variables to add to env
for template rendering (these variables are not really added
to environnement).
die_on_missing_variable (boolean): if True (default), an exception
is raised when there are some missing variables.
extra_search_path (list): list of paths (string) for templates
searching (inheritance, includes...).
keep_multi_blank_lines (boolean): if True (default), multi blank
lines are kept (otherwise they are reduced to a single one).
jinja2_extensions (list of string): list of jinja2 extensions to load
(list of strings, for example: ['jinja2.ext.i18n']).
Returns:
string: rendered template.
"""
if die_on_missing_variable:
undefined = jinja2.StrictUndefined
else:
undefined = jinja2.Undefined
variables = dict([(k, _unicodify(v)) for k, v in os.environ.items()])
for (key, value) in extra_variables.items():
variables[key] = value
return _render_string(string, variables, undefined,
extra_search_paths=extra_search_paths,
keep_multi_blank_lines=keep_multi_blank_lines,
jinja2_extensions=jinja2_extensions)
def _render_string(string, variables, undefined,
extra_search_paths=[], keep_multi_blank_lines=True,
jinja2_extensions=[]):
template_name = 'template_name'
loader1 = jinja2.DictLoader({template_name: _unicodify(string)})
loader2 = jinja2.FileSystemLoader([os.getcwd()] + extra_search_paths,
followlinks=True)
loader = jinja2.ChoiceLoader([loader1, loader2])
return _render(template_name, loader, variables,
undefined, keep_multi_blank_lines=keep_multi_blank_lines,
jinja2_extensions=jinja2_extensions)
def _unicodify(s):
if isinstance(s, str) and IS_PYTHON_2:
s = unicode(s, 'utf-8') # NOQA
return s
def stdin_read():
if IS_PYTHON_2:
return _unicodify(sys.stdin.read())
else:
return io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8').read()
def stdout_write(output):
if IS_PYTHON_2:
sys.stdout.write(_unicodify(output))
else:
io.TextIOWrapper(sys.stdout.buffer,
encoding='utf-8').write(output)
def _render_file(filename, variables, undefined,
extra_search_paths=[], keep_multi_blank_lines=True,
jinja2_extensions=[]):
dirname = os.path.dirname(filename)
loader = jinja2.FileSystemLoader([dirname] + extra_search_paths,
followlinks=True)
relpath = os.path.relpath(filename, dirname)
return _render(relpath, loader, variables, undefined,
keep_multi_blank_lines=keep_multi_blank_lines,
jinja2_extensions=jinja2_extensions)
def _render(template_name, loader, variables, undefined,
keep_multi_blank_lines=True, jinja2_extensions=[]):
env = jinja2.Environment(loader=loader, undefined=undefined,
extensions=jinja2_extensions)
env.filters['from_json'] = from_json
env.filters['shell'] = shell
env.filters['getenv'] = getenv
env.filters['uuid'] = getuuid
env.filters['fnmatch'] = _fnmatch
template = env.get_template(template_name)
template.globals['environment'] = get_environment
try:
output = template.render(**variables)
except jinja2.UndefinedError as e:
raise Fatal(e)
# jinja2 cuts the last newline
source, _, _ = loader.get_source(env, template_name)
if source.split('\n')[-1] == '' and output.split('\n')[-1] != '':
output += '\n'
# reduce multi empty or blank lines to single empty line
if not keep_multi_blank_lines:
output = re.sub(r'\n\s*\n', '\n\n', output)
output = re.sub(r'^\ns*\n', '\n', output)
return output
@cfunction
def get_environment(context, prefix=''):
for key, value in sorted(context.items()):
if not callable(value) and key.startswith(prefix):
yield key[len(prefix):], value
@eval_context
def from_json(eval_ctx, value):
return json.loads(value)
@eval_context
def shell(eval_ctx, value, die_on_error=False, encoding="utf8"):
if die_on_error:
cmd = value
else:
cmd = "%s ; exit 0" % value
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT,
shell=True)
return output.decode(encoding)
@eval_context
def getenv(eval_ctx, value, default=None):
result = os.environ.get(value, default)
if result is None:
raise Exception("can't find %s environnement variable" % value)
return result
@eval_context
def getuuid(eval_ctx, value, default=None):
v = str(uuid.uuid4()).replace('-', '') + value
return hashlib.md5(v.encode()).hexdigest()
@eval_context
def _fnmatch(eval_ctx, value, pattern):
return fnmatch.fnmatch(value, pattern)
class Fatal(Exception):
pass
if __name__ == '__main__':
main()