forked from darthryking/djangotemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.py
150 lines (107 loc) · 3.84 KB
/
configure.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
#!/usr/bin/env python
"""
configure.py
Sets up the Django template as a new, distinct project.
"""
import os
import sys
if len(sys.argv) <= 1:
print "You must supply a project name."
sys.exit(1)
PROJECT_NAME = sys.argv[1]
HERE = os.path.dirname(os.path.abspath(__file__))
class NoDjTemplateDir(Exception):
def __str__(self):
return (
"Directory 'djangotemplate' does not exist. "
"Are you sure this is a valid djangotemplate fork?"
)
class NoDjSettings(Exception):
def __str__(self):
return (
"Could not open {}/settings.py. "
"Are you sure this is a valid djangotemplate fork?"
.format(PROJECT_NAME)
)
def rename_directory():
""" Renames the djangotemplate directory to PROJECT_NAME. """
djtemplatePath = os.path.join(HERE, 'djangotemplate')
if not os.path.exists(djtemplatePath):
raise NoDjTemplateDir()
os.rename(djtemplatePath, PROJECT_NAME)
def replace_references(dir=HERE):
""" Recursively walks through the project and replaces references to
'djangotemplate' with PROJECT_NAME.
"""
for item in os.listdir(dir):
itemPath = os.path.join(dir, item)
if os.path.isdir(itemPath) and item != '.git':
# Recursively walk directories and replace references
replace_references(itemPath)
# Replace references in all *.py files (except configure.py),
# and also the Procfile.
elif ((item.endswith('.py') and item != 'configure.py')
or item == 'Procfile'):
# Read the file data
with open(itemPath, 'r') as f:
data = f.read()
# Replace all references
newData = data.replace('djangotemplate', PROJECT_NAME)
# Re-save the file
with open(itemPath, 'w') as f:
f.write(newData)
def generate_key():
""" Generate a new Django secret key for use in settings.py. """
settingsPath = os.path.join(HERE, PROJECT_NAME, 'settings.py')
try:
with open(settingsPath, 'r') as f:
data = f.read()
except IOError:
raise NoDjSettings()
# Generate a new secret key (consisting of 50 OS-produced random bytes)
newKey = (
''.join(
(
'\\x{}'.format(
'0' + hex(n)[2:] if n < 0x10 else hex(n)[2:]
)
for n in (ord(b) for b in os.urandom(10))
)
)
for i in xrange(5)
)
# Insert the new key into settings.py
newData = data.replace(
'\'[[ INSERT_SECRET_KEY_HERE ]]\'',
'(\n{})'.format(
''.join(
(
'{}\'{}\'\n'.format(' ' * 4, keyPart)
for keyPart in newKey
)
)
)
)
# Save the new settings.py
with open(settingsPath, 'w') as f:
f.write(newData)
def main():
print "Renaming directory 'djangotemplate' to '{}'... ".format(
PROJECT_NAME
),
rename_directory()
print "Done!"
print "Replacing 'djangotemplate' references with '{}'... ".format(
PROJECT_NAME
),
replace_references()
print "Done!"
print "Generating new Django secret key... ",
generate_key()
print "Done!"
print ""
print "Configuration finished!"
print "You may now remove this file (configure.py)."
return 0
if __name__ == '__main__':
sys.exit(main())