forked from benreynwar/pyvivado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
31 lines (28 loc) · 854 Bytes
/
utils.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
import jinja2
import hashlib
import json
def format_file(template_filename, output_filename, parameters):
'''
Create a file from a template and parameters.
'''
with open(template_filename, 'r') as f:
template_text = f.read()
template = jinja2.Template(template_text)
formatted_text = template.render(**parameters)
with open(output_filename, 'w') as g:
g.write(formatted_text)
def files_hash(fns):
'''
Generate a hash from the contents of several files.
'''
h = hashlib.sha1()
for fn in fns:
with open(fn, 'rb') as f:
finished = False
while not finished:
buf = f.read(4096)
if buf:
h.update(hashlib.sha1(buf).digest())
else:
finished = True
return h.digest()