-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathflask_skeleton.py
More file actions
171 lines (147 loc) · 5.25 KB
/
flask_skeleton.py
File metadata and controls
171 lines (147 loc) · 5.25 KB
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
# -*- coding: utf-8 -*-
import sys
import os
import argparse
import jinja2
import codecs
import subprocess
import shutil
if sys.version_info < (3, 0):
from shutilwhich import which
else:
from shutil import which
import platform
# Globals #
cwd = os.getcwd()
script_dir = os.path.dirname(os.path.realpath(__file__))
# Jinja2 environment
template_loader = jinja2.FileSystemLoader(
searchpath=os.path.join(script_dir, "templates"))
template_env = jinja2.Environment(loader=template_loader)
def get_arguments(argv):
parser = argparse.ArgumentParser(description='Scaffold a Flask Skeleton.')
parser.add_argument('appname', help='The application name')
parser.add_argument('-s', '--skeleton', help='The skeleton folder to use.')
parser.add_argument('-b', '--bower', help='Install dependencies via bower')
parser.add_argument('-v', '--virtualenv', action='store_true')
parser.add_argument('-g', '--git', action='store_true')
args = parser.parse_args()
return args
def generate_brief(args):
template_var = {
'pyversion': platform.python_version(),
'appname': args.appname,
'bower': args.bower,
'virtualenv': args.virtualenv,
'skeleton': args.skeleton,
'path': os.path.join(cwd, args.appname),
'git': args.git
}
template = template_env.get_template('brief.jinja2')
return template.render(template_var)
def main(args):
print("\nScaffolding...")
# Variables #
appname = args.appname
fullpath = os.path.join(cwd, appname)
skeleton_dir = args.skeleton
# Tasks #
# Copy files and folders
print("Copying files and folders...")
shutil.copytree(os.path.join(script_dir, skeleton_dir), fullpath)
# Create config.py
print("Creating the config...")
secret_key = codecs.encode(os.urandom(32), 'hex').decode('utf-8')
template = template_env.get_template('config.jinja2')
template_var = {
'secret_key': secret_key,
}
with open(os.path.join(fullpath, 'project', 'config.py'), 'w') as fd:
fd.write(template.render(template_var))
# Add bower dependencies
if args.bower:
print("Adding bower dependencies...")
bower = args.bower.split(',')
bower_exe = which('bower')
if bower_exe:
os.chdir(os.path.join(fullpath, 'project', 'client', 'static'))
for dependency in bower:
output, error = subprocess.Popen(
[bower_exe, 'install', dependency],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
if error:
print("An error occurred with Bower")
print(error)
else:
print("Could not find bower. Ignoring.")
# Add a virtualenv
virtualenv = args.virtualenv
if virtualenv:
print("Adding a virtualenv...")
virtualenv_exe = which('pyvenv')
if virtualenv_exe:
output, error = subprocess.Popen(
[virtualenv_exe, os.path.join(fullpath, 'env')],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
if error:
with open('virtualenv_error.log', 'w') as fd:
fd.write(error.decode('utf-8'))
print("An error occurred with virtualenv")
sys.exit(2)
venv_bin = os.path.join(fullpath, 'env/bin')
output, error = subprocess.Popen(
[
os.path.join(venv_bin, 'pip'),
'install',
'-r',
os.path.join(fullpath, 'requirements.txt')
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
if error:
with open('pip_error.log', 'w') as fd:
fd.write(error.decode('utf-8'))
sys.exit(2)
else:
print("Could not find virtualenv executable. Ignoring")
# Git init
if args.git:
print("Initializing Git...")
output, error = subprocess.Popen(
['git', 'init', fullpath],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
if error:
with open('git_error.log', 'w') as fd:
fd.write(error.decode('utf-8'))
print("Error with git init")
sys.exit(2)
shutil.copyfile(
os.path.join(script_dir, 'templates', '.gitignore'),
os.path.join(fullpath, '.gitignore')
)
if __name__ == '__main__':
arguments = get_arguments(sys.argv)
print(generate_brief(arguments))
if sys.version_info < (3, 0):
input = raw_input
proceed = input("\nProceed (yes/no)? ")
valid = ["yes", "y", "no", "n"]
while True:
if proceed.lower() in valid:
if proceed.lower() == "yes" or proceed.lower() == "y":
main(arguments)
print("Done!")
break
else:
print("Goodbye!")
break
else:
print("Please respond with 'yes' or 'no' (or 'y' or 'n').")
proceed = input("\nProceed (yes/no)? ")