-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaleidoscope.py
171 lines (133 loc) · 5.03 KB
/
kaleidoscope.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
#!/usr/bin/python
import os, sys, getopt
from subprocess import call
from utilities import tabify,writeFile
import settingsConfig, appsConfig, pagesConfig, devConfig
import consts, globs
globs.DELETED = []
globs.URLS = []
consts.PATH = os.getcwd()
consts.KSCOPE = os.path.abspath(os.path.dirname(__file__))
consts.RESOURCES = os.path.join(consts.KSCOPE, 'resources')
sys.path.append(consts.PATH) #this is so we can include the compiled ks dictionary
def writeURLS(properties):
"""Writes the urls file, last thing done in the setup process to make sure there are no stray urls to be added"""
urlfile = """from django.conf.urls import include, patterns, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
"""
for url in globs.URLS:
urlfile += tabify(url+",",1)
urlfile += ")\n"
writeFile(os.path.join(consts.PROJECT,properties['website']['name'],'urls.py'),urlfile)
def handleMakefile(properties):
"""Makefile generation for easy launching of the test site"""
with open(os.path.join(consts.RESOURCES, 'makefile'), 'r') as f:
makefile = f.read()
with open(os.path.join(consts.PATH, 'makefile'), 'w') as f:
f.write(makefile % (properties['website']['name'],\
os.path.join(consts.ENV,'bin','activate'),\
os.path.join(consts.ENV,properties['website']['name']),\
consts.ENV))
def setup(properties):
"""
Creates basic django project
TODO: Error handling and resuming after failure
"""
try:
if not os.path.exists(consts.ENV):
call(["virtualenv", consts.ENV])
except:
print "Error creating virtual environment. Make sure viurtualenv is installed."
sys.exit(1)
try:
call(["cp", os.path.join(consts.RESOURCES, 'req.pip'), "./"])
call([os.path.join(consts.ENV, 'bin', 'pip'), "install", "-r", os.path.join(consts.PATH, 'req.pip')])
except:
print "Error installing django or south"
sys.exit(2)
try:
print "Building project"
os.chdir(consts.ENV)
call([os.path.join(consts.ENV, 'bin', 'django-admin.py'), "startproject", properties["website"]["name"]])
except OSError as e:
print "Error creating django project", e
sys.exit(3)
try:
call(["git", "init", consts.PATH])
except:
print "Failed to initialize git"
sys.exit(4)
def parse(filename):
properties = {}
try:
os.system("/usr/local/lib/kaleidoscope/parser < %s" % (filename))
from dictionary import d
properties = d
except:
exc_type,exc_value,exc_traceback = sys.exc_info()
print(exc_type)
print(exc_value)
print(exc_traceback)
sys.exit(1)
return properties
def main():
opts, args = getopt.getopt(sys.argv[1:], "ut:s:")
filename = "infile"
flags = [o for o,a in opts]
arguments = {key: value for (key, value) in opts}
properties = parse(os.path.join(consts.PATH, args[0]))
try:
theme = properties['website']['theme']
except KeyError:
theme = "default"
#setup v environment vars
try:
globs.ENVNAME = properties["environment"]
except KeyError:
globs.ENVNAME = "venv"
consts.ENV = os.path.join(consts.PATH, globs.ENVNAME)
#get the hostname (localhost if none provided)
try:
consts.HOSTNAME = properties["website"]["host"]
except KeyError:
consts.HOSTNAME = "localhost"
#try and get the root directory on the server (/webapps otherwise)
try:
consts.WEBAPPS = properties["website"]["root"]
except KeyError:
consts.WEBAPPS = '/webapps'
#marked as update update
if "-u" in flags:
consts.UPDATE = True #need to modify this to build missing portions
else:
setup(properties) #this fn is where the project is built
consts.UPDATE = False
#configure apps
appsConfig.createApps(properties, theme)
#configure the pages
pagesConfig.createPages(properties)
#write the urls page
writeURLS(properties)
#after the project is built time to start changing properties in the settings
settingsConfig.handleSettings(os.path.join(consts.PROJECT,\
properties["website"]["name"],\
'settings.py'),\
properties)
#prepare the configuration files for the dev environment
devConfig.createDevFiles(properties)
#copy the makefile over
handleMakefile(properties)
#if there are static files copy them
if "-s" in flags:
call(["cp", "-a", os.path.join(consts.PATH, arguments["-s"], "."), os.path.join(consts.MAIN, 'static')])
#intiialize the test database
try:
call([consts.PYTHON, consts.MANAGE, "syncdb"])
except:
print "failed to initialize the database"
#entry point
if __name__ == "__main__":
main()