-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgenerate.py
298 lines (257 loc) · 10.2 KB
/
generate.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import binascii
import argparse
from settings import *
#---------------------------------------------------------------------
# Helper functions
#---------------------------------------------------------------------
def enum(**enums):
return type('Enum', (), enums)
AppTypes = enum(BASIC=1, LARGE=2, ANGULAR=3)
def get_filemap(app_type, app_name):
if app_type == AppTypes.BASIC:
filemap = {
'dirs': [
'static/css',
'static/img',
'static/js',
'templates',
],
'remote_files': [
(bootstrap_css_url, 'static/css/bootstrap.css'),
(bootstrap_responsive_css_url, 'static/css/bootstrap-responsive.css'),
(bootstrap_js_url, 'static/js/bootstrap.js'),
(bootstrap_js_min_url, 'static/js/bootstrap.min.js'),
(jquery_url, 'static/js/jquery.js'),
(jquery_min_url, 'static/js/jquery.min.js'),
(favicon_url, 'static/img/favicon.ico'),
],
'local_files': [
('README.md', 'README.md'),
('.gitignore', '.gitignore'),
('Procfile', 'Procfile'),
('basic_app/app.py', 'app.py'),
('templates/base.html', 'templates/base.html'),
('templates/index.html', 'templates/index.html'),
('templates/404.html', 'templates/404.html'),
('templates/about.html', 'templates/about.html'),
('static/css/main.css', 'static/css/main.css'),
],
}
return filemap
elif app_type == AppTypes.LARGE:
filemap = {
'dirs': [
app_name,
app_name + '/static/css',
app_name + '/static/img',
app_name + '/static/js',
app_name + '/templates',
],
'remote_files': [
(bootstrap_css_url, app_name + '/static/css/bootstrap.css'),
(bootstrap_responsive_css_url, app_name + '/static/css/bootstrap-responsive.css'),
(bootstrap_js_url, app_name + '/static/js/bootstrap.js'),
(bootstrap_js_min_url, app_name + '/static/js/bootstrap.min.js'),
(jquery_url, app_name + '/static/js/jquery.js'),
(jquery_min_url, app_name + '/static/js/jquery.min.js'),
(favicon_url, app_name + '/static/img/favicon.ico'),
],
'local_files': [
('README.md', 'README.md'),
('.gitignore', '.gitignore'),
('Procfile', 'Procfile'),
('large_app/runserver.py', 'runserver.py'),
('large_app/__init__.py', app_name + '/__init__.py'),
('large_app/core.py', app_name + '/core.py'),
('large_app/models.py', app_name + '/models.py'),
('large_app/settings.py', app_name + '/settings.py'),
('large_app/controllers.py', app_name + '/controllers.py'),
('templates/base.html', app_name + '/templates/base.html'),
('templates/index.html', app_name + '/templates/index.html'),
('templates/about.html', app_name + '/templates/about.html'),
('templates/404.html', app_name + '/templates/404.html'),
('static/css/main.css', app_name + '/static/css/main.css'),
],
}
return filemap
elif app_type == AppTypes.ANGULAR:
filemap = {
'dirs': [
app_name,
app_name + '/static/css',
app_name + '/static/img',
app_name + '/static/js',
app_name + '/static/lib',
app_name + '/static/lib/angular',
app_name + '/static/lib/jquery',
app_name + '/static/lib/bootstrap',
app_name + '/static/partials',
app_name + '/templates',
],
'remote_files': [
(bootstrap_css_url, app_name + '/static/css/bootstrap.css'),
(bootstrap_responsive_css_url, app_name + '/static/css/bootstrap-responsive.css'),
(favicon_url, app_name + '/static/img/favicon.ico'),
(bootstrap_js_url, app_name + '/static/lib/bootstrap/bootstrap.js'),
(bootstrap_js_min_url, app_name + '/static/lib/bootstrap/bootstrap.min.js'),
(jquery_url, app_name + '/static/lib/jquery/jquery.js'),
(jquery_min_url, app_name + '/static/lib/jquery/jquery.min.js'),
(angularjs_url, app_name + '/static/lib/angular/angular.js'),
(angularjs_resource_url, app_name + '/static/lib/angular/angular-resource.js'),
(angularjs_min_url, app_name + '/static/lib/angular/angular.min.js'),
(angularjs_resource_min_url, app_name + '/static/lib/angular/angular-resource.min.js'),
],
'local_files': [
('README.md', 'README.md'),
('.gitignore', '.gitignore'),
('Procfile', 'Procfile'),
('angular_app/runserver.py', 'runserver.py'),
('angular_app/manage.py', 'manage.py'),
('angular_app/__init__.py', app_name + '/__init__.py'),
('angular_app/core.py', app_name + '/core.py'),
('angular_app/models.py', app_name + '/models.py'),
('angular_app/settings.py', app_name + '/settings.py'),
('angular_app/controllers.py', app_name + '/controllers.py'),
('angular_app/index.html', app_name + '/templates/index.html'),
('angular_app/404.html', app_name + '/templates/404.html'),
('static/css/main.css', app_name + '/static/css/main.css'),
('angular_app/app.js', app_name + '/static/js/app.js'),
('angular_app/controllers.js', app_name + '/static/js/controllers.js'),
('angular_app/services.js', app_name + '/static/js/services.js'),
('angular_app/about.html', app_name + '/static/partials/about.html'),
('angular_app/landing.html', app_name + '/static/partials/landing.html'),
],
}
return filemap
else:
raise Exception('That app type is not supported!')
def random_binascii(n):
return binascii.b2a_hex(os.urandom(n))
def make_replacements(filedata, replacements):
for replacement in replacements:
filedata = filedata.replace(replacement[0], replacement[1])
return filedata
def git_add_and_commit(message):
os.system('git add .')
os.system('git commit -m "' + message + '"')
def configure_github(github_user, github_repo):
os.system('git remote add origin https://github.com/' + github_user + '/' + github_repo + '.git')
def configure_heroku(heroku_app):
os.system('heroku create --stack cedar')
os.system('heroku apps:rename ' + heroku_app)
def configure_virtualenvwrapper(app_name, virtualenvwrapper_path):
os.system('source ' + virtualenvwrapper_path + '; mkvirtualenv ' + app_name + '; workon ' + app_name + '; sudo pip install ' + packages_to_pip_install + '; pip freeze > requirements.txt')
def configure_virtualenv(app_name):
os.system('virtualenv venv --distribute; source venv/bin/activate; sudo pip install ' + packages_to_pip_install + '; pip freeze > requirements.txt')
def make_dirs(dirs):
for d in dirs:
os.makedirs(d)
def curl_files(files):
for f in files:
src_url = f[0]
dest_name = f[1]
os.system('curl ' + src_url + ' > ' + dest_name)
#---------------------------------------------------------------------
# App creator
#---------------------------------------------------------------------
class AppCreator(object):
def __init__(self, app_name, app_type):
self.app_name = app_name
self.app_type = app_type
if app_type == AppTypes.LARGE:
self.runserver_filename = 'runserver'
elif app_type == AppTypes.ANGULAR:
self.runserver_filename = 'runserver'
else:
self.runserver_filename = 'app'
self.resource_path = '../resources/'
self.destination_path = './'
def init_app(self):
os.mkdir(self.app_name)
os.chdir(self.app_name)
os.system('git init')
def build_app(self):
#filenames = ['README.md', '.gitignore', 'Procfile']
#self.create_files(filenames, '')
filemap = get_filemap(self.app_type, self.app_name)
for d in filemap['dirs']:
os.makedirs(d)
for fd in filemap['local_files']:
with open(self.destination_path + fd[1], 'w') as f:
filedata = self.render_filedata(fd[0], '')
f.write(filedata)
for fd in filemap['remote_files']:
os.system('curl ' + fd[0] + ' > ' + fd[1])
def load_resource(self, filename, resource_dir):
filepath = self.resource_path + resource_dir + filename
try:
with open(filepath, 'r') as f:
filedata = f.read()
except:
print filepath
raise Exception('No template found with that name.')
else:
return filedata
def render_filedata(self, filename, resource_dir):
replacements = [
('[[SECRET_KEY]]', random_binascii(24)),
('[[APP_NAME]]', self.app_name),
('[[RUNSERVER_FILENAME]]', self.runserver_filename)
]
filedata = self.load_resource(filename, resource_dir)
filedata = make_replacements(filedata, replacements)
return filedata
#---------------------------------------------------------------------
# Main
#---------------------------------------------------------------------
def main():
# parse arguments
parser = argparse.ArgumentParser(description='Autogenerate a Flask app')
parser.add_argument('appname', help='the name of the app to be created')
parser.add_argument('--githubrepo', dest='github_repo', help='the name of the remote github repo that will be hosting the app')
parser.add_argument('--herokuapp', dest='heroku_app', help='the name that the heroku app will be renamed to if it has not yet been taken')
parser.add_argument('--venvname', dest='virtualenv_name', help='the name of the virtualenv for the app')
parser.add_argument('--push', dest='push', help='automatically push to github and heroku when app has been created', action='store_true')
parser.add_argument('--large', dest='app_type', action='store_const', const=AppTypes.LARGE)
parser.add_argument('--angular', dest='app_type', action='store_const', const=AppTypes.ANGULAR)
args = parser.parse_args()
# take in additional arguments from settings
args.github_user = github_user
args.virtualenvwrapper_path = virtualenvwrapper_path
# create app files
if args.app_type:
app_creator = AppCreator(args.appname, args.app_type)
else:
app_creator = AppCreator(args.appname, AppTypes.BASIC)
print ''
app_creator.init_app()
print ''
app_creator.build_app()
print '\n'
if args.app_type == AppTypes.ANGULAR:
os.system('python manage.py create_db')
print 'Creating db...\n'
# configure github
if args.github_user and args.github_repo:
configure_github(args.github_user, args.github_repo)
# configure heroku
if args.heroku_app:
configure_heroku(args.heroku_app)
# configure virtualenv
if args.virtualenv_name:
if args.virtualenvwrapper_path:
configure_virtualenvwrapper(args.virtualenv_name, args.virtualenvwrapper_path)
else:
configure_virtualenv(args.virtualenv_name)
# commit all files
git_add_and_commit('first commit')
# push to github and heroku
if args.push:
if args.github_user and args.github_repo:
os.system('git push origin master')
if args.heroku_app:
os.system('git push heroku master; heroku open')
print "\nYour app has been created!\n"
if __name__ == "__main__":
main()