/
from_cookiecutter.py
64 lines (57 loc) · 2.33 KB
/
from_cookiecutter.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
import os
import json
import stat
from collections import OrderedDict
try:
from binaryornot.check import is_binary
except ImportError:
# We don't have binaryornot - assume all files are text
def is_binary(filename):
return False
def spec_from_cookiecutter(dirname):
template = os.path.join(dirname, '{{cookiecutter.repo_name}}')
jsonfile = os.path.join(dirname, 'cookiecutter.json')
if not (os.path.exists(template) and os.path.exists(jsonfile)):
raise ValueError("{} is not a valid Cookiecutter".format(dirname))
variables = []
with open(jsonfile) as j:
vardata = json.load(j, object_pairs_hook=OrderedDict)
for name, default in vardata.items():
variables.append({'name': name, 'default': default})
specs = []
for dirpath, dirnames, filenames in os.walk(template):
basepath = dirpath[len(dirname):]
for filename in filenames:
fullpath = os.path.join(dirpath, filename)
spec = {}
name = os.path.join(basepath, filename)
spec['name'] = name
spec['name_renderer'] = 'jinja2'
binary_file = is_binary(fullpath)
if not binary_file:
try:
# Cookiecutter uses the default Jinja2 loader,
# which expects template files to be un utf-8
with open(fullpath, encoding='utf-8') as f:
# Read the data first, so we get an error for binary files
# before we set encoding or renderer
spec['content'] = f.read()
spec['encoding'] = f.encoding
spec['renderer'] = 'jinja2'
except UnicodeDecodeError:
# Looks like it wasn't UTF-8 after all - assume binary
binary_file = True
if binary_file:
with open(fullpath, 'rb') as f:
spec['content'] = f.read()
spec['binary'] = True
if (os.stat(fullpath).st_mode & stat.S_IXUSR) != 0:
spec['executable'] = True
specs.append(spec)
return specs, variables
if __name__ == '__main__':
import sys
from pprint import pprint
specs, variables = spec_from_cookiecutter(sys.argv[1])
pprint(specs)
pprint(variables)