-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoiler
392 lines (303 loc) · 14.7 KB
/
foiler
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/python
import os
import shutil
import re
import uuid
import json
import subprocess
import sys
import zipfile
import urllib2
import StringIO
import flask_boilerplate
from flask_script import Manager, Command, Option
github_suffix = ''
module_folder = os.path.dirname(os.path.abspath(flask_boilerplate.__file__))
template_folder = os.path.join(module_folder, './boilerplate/')
def get_package_json():
path = traverse_for_file('./app.json')
if path:
with open(path, 'r') as f:
d = json.loads(f.read())
return d
def traverse_for_folder_containing_file(fname):
path = os.getcwd()
while not os.path.isfile(os.path.join(path, fname)):
path = os.path.realpath(os.path.join(path, '../'))
if path == '/':
return None
return path
def traverse_for_file(fname):
path = os.getcwd()
while not (os.path.isfile(os.path.join(path, fname)) or os.path.exists(os.path.join(path, fname))):
path = os.path.realpath(os.path.join(path, '../'))
if path == '/':
return None
path = os.path.realpath(os.path.join(path, fname))
return path
def within_valid_project():
if traverse_for_file('app.json'):
return True
return False
def extract(f,dist='dist', destination='./'):
# Destination is relative to app root.
with zipfile.ZipFile(f, mode='r') as zf:
zip_prefix = zf.namelist()[0]
dist_folder = os.path.join(zip_prefix, dist + "/" if dist != '' else dist)
extractable = filter(lambda f: f.startswith(dist_folder) and not f.endswith('/'), zf.namelist())
prefix = os.path.realpath(os.path.join(traverse_for_folder_containing_file('app.json'), destination))
for member in extractable:
with zf.open(member, 'r') as m:
output_name = os.path.join(prefix, member.replace(dist_folder, ''))
if output_name.strip() != '':
output_directory = os.path.dirname(output_name)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
with open(output_name, 'w+') as o:
o.write(m.read())
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks=symlinks, ignore=ignore)
else:
# hacky fix to stop pyc files
if not d.endswith('.pyc'):
shutil.copy2(s, d)
# Do some hacking to flask_script to allow the use without an app.
class HackCommand(Command):
def __call__(self, app=None, *args, **kwargs):
if app:
return super(Command, self).__call__(app, *args, **kwargs)
else:
return self.run(*args, **kwargs)
class MyManager(Manager):
def __call__(self, app=None, **kwargs):
pass
class Server(HackCommand):
"Spawn a flask server for the app in the current working directory."
option_list = (
Option('--hostname', '-h', dest='hostname', default='0.0.0.0', type=str),
Option('--port', '-p', dest='port', default=8000, type=int),
)
def run(self, port, hostname):
if not within_valid_project():
print "Not within a Flask Project"
exit(1)
# Compile and run the run script
root_folder = traverse_for_folder_containing_file('app.json')
subprocess.call(['make debug ARGS="--port %d --hostname %s"' % (port, hostname)],
cwd=root_folder, shell = True)
class Init(HackCommand):
"Initialise a new Flask Boilerplate in the current working directory."
def run(self):
app_folder = os.path.join(os.getcwd())
if os.path.isfile(os.path.join(app_folder, './app.json')):
print "Cannot re-init in an already initialised application"
exit(1)
safe_name_re = re.compile('^[A-Za-z0-9_\-]+$')
print "App Name: ",
app_name = raw_input()
while not safe_name_re.match(app_name):
print "Please enter a valid app name ([A-Za-z0-9_\-]+)"
print "App Name: ",
app_name = raw_input()
print "Initialising..."
copytree(template_folder,app_folder, ignore=shutil.ignore_patterns('*.pyc'))
data = dict(app_name=app_name)
with open(os.path.join(app_folder, 'app.json'), 'w') as f:
f.write(json.dumps(data))
vars_file = os.path.join(app_folder, 'Application/config/variables.py')
if os.path.isfile(vars_file):
os.remove(vars_file)
print "Initialised %s successfully." % (app_name)
class Configure(HackCommand):
"Configure/Reconfigure the app in the current working directory."
def run(self):
# Configure the app.
section = re.compile(r'^#(.*)')
prop = re.compile(r'^([A-Za-z_][0-9A-Za-z_]*) *= *(.*)(#.*)?$')
root_folder = traverse_for_folder_containing_file('app.json')
variables_file = os.path.join(root_folder, 'Application/config/variables.py')
variables_file_example = os.path.join(root_folder, 'Application/config/variables.py.example')
if os.path.isfile(variables_file):
new_app = False
fname = variables_file
else:
new_app = True
fname = variables_file_example
output_lines = []
with open(fname) as f:
for line in f:
line = line.strip()
section_match = section.match(line)
if section_match:
print "\033[32m" + section_match.group(1).strip() + "\033[0m"
output_lines.append("# %s" % (section_match.group(1).strip()))
prop_match = prop.match(line)
if prop_match:
if new_app and (prop_match.group(1).strip() == 'SECRET_KEY' or prop_match.group(1).strip() == 'SECURITY_PASSWORD_SALT'):
# Instead generate these
value = "'%s%s%s'" % (uuid.uuid4().hex, uuid.uuid4().hex, uuid.uuid4().hex)
elif new_app and prop_match.group(1).strip() == 'APP_NAME':
value = '"%s"' % get_package_json()['app_name']
else:
print "%s (%s):" % (prop_match.group(1).strip(), prop_match.group(2).strip()),
value = raw_input()
was_empty = False
if value == '':
was_empty = True
value = prop_match.group(2)
if not (was_empty or value == 'True' or value == 'False' or re.match(r'^[0-9]+(\.[0-9]*)?$', value)):
value = "'%s'" % (value)
value = value.strip()
output_lines.append("%s = %s" % (prop_match.group(1).strip(), value))
# print output_lines
with open(variables_file, 'w') as f:
for line in output_lines:
f.write(line + "\n")
class Install_Framework(HackCommand):
"Install a CSS/JS package listed on bower OR A package from a github repository"
option_list = (
Option('packages', action='store', metavar='PACKAGE',nargs='+', help="A package listed on bower OR A github repository. (user/repo)."),
Option('--version', '-v', dest='version', type=str, help="Supply a version to use (Github tag or release)"),
Option('--with-link', '-l', dest='with_link', default=False, action='store_true', help="Automatically include this package in the WebApp's header (Experimental - Only includes minifies CSS/JS ending in .min.css)"),
)
def run(self, packages, version, with_link):
# Reuse my legacy code here
if not within_valid_project():
print "Not within a Flask Project"
exit(1)
root_folder = traverse_for_folder_containing_file('app.json')
git_package = re.compile(r'^([A-Za-z0-9\-_\.]+/[A-Za-z0-9_\-\.]+)$')
bower_package = re.compile(r'^([A-Za-z0-9\._\-]+)$')
for package in packages:
github_resolved = None
version = None
if git_package.match(package):
m = git_package.match(package)
github_resolved = m.group(1)
elif bower_package.match(package):
m = bower_package.match(package)
# Resolve this into a github package
try:
data = urllib2.urlopen("http://bower.herokuapp.com/packages/%s" % (package)).read()
data = json.loads(data)
# Get the github url
github_resolved = re.match(r'^git://github\.com/([A-Za-z0-9\._\-]+/[A-Za-z0-9\._\-]+)\.git$', data['url'])
if not github_resolved:
raise Exception('Unable to resolve github url: %s' % (data['url']))
github_resolved = github_resolved.group(1)
except Exception, e:
print "Error loading package:"
print e
exit(1)
else:
print "Not a valid package name/formation"
print "Please provide a package in the form of (<bower package>|<<github user>/<github repo>>)[#<tag name>]"
exit(1)
print "Found Package %s" % (github_resolved)
package_name = github_resolved.replace('/', '-')
try:
data = urllib2.urlopen("https://api.github.com/repos/%s/releases%s" % (github_resolved, github_suffix)).read()
data = json.loads(data)
if len(data) > 0:
found = False
active_release = None
if version:
for release in data:
if re.search(version, release['tag_name']):
active_release = release
break
if not found:
print "Could not find a release with that tag."
exit(1)
else:
# Use the latest release
active_release = data[0]
response = urllib2.urlopen(active_release['assets'][0]['browser_download_url'] + github_suffix)
zipcontent= response.read()
f = StringIO.StringIO()
f.write(zipcontent)
f.seek(0)
# Extract all because this is dist
extract(f, dist='', destination='./Application/static/vendor/%s/' % (package_name))
else:
# We couldn't find a release. repo probably doesn't use github's release tags.
# Fallback to standard tags, use the standard zipball and find the dist folder
data = urllib2.urlopen("https://api.github.com/repos/%s/tags%s" % (github_resolved, github_suffix)).read()
data = json.loads(data)
active_tag = None
if version:
for tag in data:
if version == tag['name']:
active_tag = tag
break
else:
active_tag = data[0]
if not active_tag:
print "Could not find a tag with the name: %s" % (version)
exit(1)
response = urllib2.urlopen(active_tag['zipball_url'] + github_suffix)
zipcontent= response.read()
f = StringIO.StringIO()
f.write(zipcontent)
f.seek(0)
# Use the dist folder because thats where things /should/ be.
extract(f, dist='dist', destination='./Application/static/vendor/%s/' % (package_name))
print "Installed: %s" % (package_name)
if with_link:
print "Linking: %s" % (package_name)
# We need to link it up
scripts = []
stylesheets = []
def cleanname(name):
g = re.search(r'(/static/vendor/.*)', name).group(1)
return g
for dirname, dirnames, filenames in os.walk(traverse_for_file('./Application/static/vendor/%s/' % (package_name))):
for filename in filenames:
fullpath = os.path.join(dirname, filename)
if "i18n" in fullpath:
continue
if re.search('\.min\.css$', filename):
stylesheets.append(cleanname(fullpath))
elif re.search("\.min\.js$", filename):
scripts.append(cleanname(fullpath))
f = traverse_for_file('./Application/static/vendor/')
f = os.path.join(f, 'autoinclude.json')
data = {'scripts':[], 'stylesheets':[]}
if os.path.isfile(f):
with open(f, 'r') as fh:
data = json.loads(fh.read())
for script in scripts:
if not script in data['scripts']:
data['scripts'].append(script)
print " - Linked: %s" % (script)
for stylesheet in stylesheets:
if not stylesheet in data['stylesheets']:
data['stylesheets'].append(stylesheet)
print " - Linked: %s" % (stylesheet)
with open(f, 'w+') as fh:
fh.write(json.dumps(data))
except Exception, e:
print e
class Test(HackCommand):
"Test the application using the supplied project tests"
def run(self):
if not within_valid_project():
print "Not within a Flask Project"
exit(1)
# Compile and run the run script
root_folder = traverse_for_folder_containing_file('app.json')
subprocess.call(['make test'], cwd=root_folder, shell = True)
manager = MyManager(with_default_commands=False,
description="Foiler: The Flask Boilerplate starter. "
"Easily configure and create WebApps using foiler.")
manager.add_command('init', Init())
manager.add_command('server', Server())
manager.add_command('configure', Configure())
manager.add_command('test', Test())
manager.add_command('install-framework', Install_Framework())
manager.run()
exit()