/
kickstart.py
executable file
·124 lines (97 loc) · 4.53 KB
/
kickstart.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
#! /usr/bin/env python -B
import os, sys, shutil, re, yaml
from optparse import OptionParser
import kickstarter
parser = OptionParser()
parser.add_option("-t", "--template", dest="template", help="default project template", default="default")
parser.add_option("-a", "--add", dest="add", help="install new template")
parser.add_option("-r", "--remove", dest="remove", help="remove a template")
parser.add_option("-d", "--default", dest="default_template", help="set default template")
parser.add_option("-l", "--list", action="store_true", dest="list_templates", help="list all installed templates")
(options, args) = parser.parse_args()
template_dir = os.path.join(kickstarter.__path__[0], 'templates', options.template)
def list_templates():
templates_dir = os.path.join(kickstarter.__path__[0], 'templates')
for template in os.listdir(templates_dir):
if template != 'default':
if os.path.exists('%s/%s/config.yaml' % (templates_dir, template)):
c = open('%s/%s/config.yaml' % (templates_dir, template), 'r')
conf = yaml.load(c.read())
c.close()
try:
description = ': %s' % conf['description']
except KeyError:
description = ''
else:
description = ''
print '%s%s' % (template, description)
def set_default():
templates_dir = os.path.join(kickstarter.__path__[0], 'templates')
shutil.rmtree('%s/default' % templates_dir)
shutil.copytree('%s/%s' % (templates_dir, options.default_template), '%s/%s' % (templates_dir, 'default'))
shutil.copytree('%s/default/%s' % (templates_dir, options.default_template), '%s/default/default' % templates_dir)
shutil.rmtree('%s/default/%s' % (templates_dir, options.default_template))
def create_template():
templates_dir = os.path.join(kickstarter.__path__[0], 'templates')
shutil.copytree(options.add, '%s/%s' % (templates_dir, options.add))
def remove_template():
templates_dir = os.path.join(kickstarter.__path__[0], 'templates')
shutil.rmtree('%s/%s' % (templates_dir, options.remove))
def create_project(name, template):
config = {}
config['project'] = re.sub('\-', '_', args[0])
config['project_module'] = re.sub('\-', '_', args[0])
config['project_name'] = args[0]
config['project_root'] = '%s/%s' % (os.getcwd(), args[0])
def replace_variable(match):
key = match.groups(1)[0].lower()
return config[key]
def config_prompt(template):
if os.path.exists('%s/%s' % (template, 'config.yaml')):
c = open('%s/%s' % (template, 'config.yaml'), 'r')
parsed_conf = re.sub('{{\s*(\w+)\s*}}', args[0], c.read())
c.close()
conf = yaml.load(parsed_conf)
for option in conf['config']:
default = conf['config'][option]
value = str(raw_input('%s (leave blank for %s): ' % (option, default)))
config[option.lower()] = value if value != '' else default
def copy_template():
config_prompt(template)
shutil.copytree(template, name)
if os.path.exists('%s/%s' % (name, 'config.yaml')):
os.remove('%s/%s' % (name, 'config.yaml'))
for dirname, dirnames, files in os.walk(name):
for d in dirnames:
if d == options.template:
shutil.copytree('%s/%s' % (dirname, d), '%s/%s' % (dirname, name))
shutil.rmtree('%s/%s' % (dirname, d))
for dirname, dirnames, files in os.walk(name):
for filename in files:
f = open('%s/%s' % (dirname, filename), 'r')
lines = f.readlines()
f.close()
first_pass = [re.sub('{{\s*(\w+)\s*}}', replace_variable, line) for line in lines]
new_lines = [re.sub('__config_(\w+)__', replace_variable, line) for line in first_pass]
f = open('%s/%s' % (dirname, filename), 'w')
f.write(''.join(new_lines))
f.close()
if os.path.exists('./%s' % name):
override = raw_input('%s already exists. Override? [y/n]: ' % name)
if override == 'y':
shutil.rmtree('./%s' % name)
copy_template()
else:
print 'Aborting'
else:
copy_template()
if options.list_templates:
list_templates()
elif options.add:
create_template()
elif options.remove:
remove_template()
elif options.default_template:
set_default()
else:
create_project(args[0], template_dir)