-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap.py
More file actions
executable file
·224 lines (184 loc) · 6.13 KB
/
bootstrap.py
File metadata and controls
executable file
·224 lines (184 loc) · 6.13 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Script prepares python enviroment for deploying into OpenShift cloud.
Heavily inspired by virtualenv code (actually using some parts of it).
For more see README
"""
import subprocess
import os
import sys
import stat
import optparse
try:
import virtualenv
except ImportError:
print "You don't have virtualenv installed!"
print "Try pip install virtualenv or easy_install virtualenv"
exit()
def after_install(home_dir, options):
"""
After install steps - copying files and creating application
script
@param home_dir: virtualenv location
@param options: passed commandline options
"""
if options.app_name == None:
app_name = "application"
else:
app_name = options.app_name
# you need Python2.6 package folder (for server side)
if sys.version_info[1] == 7:
python27 = os.path.join('.', 'python2.7')
python26 = os.path.join(home_dir, 'lib', 'python2.6')
os.symlink(python27, python26)
pip = os.path.join(home_dir, 'bin', 'pip')
if options.framework.lower() == "flask":
install_flask(home_dir, app_name, pip)
elif options.framework.lower() == "django":
install_django(home_dir, app_name, pip)
gitignore = os.path.join(home_dir, '..', '.gitignore')
file(gitignore, 'w').write("""\
*.pyc
*.org
*.bak
*.old
*.sw[po]
""")
def install_flask(home_dir, app_name, pip):
"""
Installs basic Flask stack for OpenShift
@param home_dir: virtualenv location
@param app_name: Inner name of application
@param pip: Location of pip
"""
subprocess.call([pip, 'install', 'flask'])
app_dir = os.path.join(home_dir, '..', 'libs', app_name)
os.makedirs(app_dir)
# basic Flask application
init = os.path.join(app_dir, '__init__.py')
file(init, 'w').write("""\
from flask import Flask, render_template
app = Flask(__name__)
DEBUG = True
@app.route("/")
def index():
return render_template("index.html")
""")
# Some templates, just to prove
templates = os.path.join(app_dir, 'templates')
os.makedirs(templates)
base = os.path.join(templates, 'base.html')
file(base, 'w').write("""\
<!DOCTYPE html>
<html>
<head>
<title>It works!</title>
</head>
<body>
<h1>This is generated testing page</h1>
<div id = 'content'>{% block content %}{% endblock %}</div>
<footer>Generated by <a href = 'https://github.com/sputnikus/openshift-python-bootstrap'>OpenShift bootstrap for Python</a>, which is unofficial tool created by Martin Putniorz</footer>
</body>
</html>
""")
index = os.path.join(templates, 'index.html')
file(index, 'w').write("""\
{% extends "base.html" %}
{% block content %}
Hi, some instructions for you:
<ul>
<li>Your app goes to <code><project home>/libs/application</code></li>
<li>I don't recommend to change <code><project home>/wsgi/application</code></li>
{% endblock %}
""")
# and, of course, wsgi script
application = os.path.join(home_dir, '..', 'wsgi', 'application')
os.remove(application)
file(application, 'w').write("""\
#!/usr/bin/env python
import os
import sys
here = os.path.dirname(os.path.abspath(__file__))
flaskapp = os.path.join(here, "../libs")
activate = os.path.join(here, "../env/bin/activate_this.py")
pythoneggs = os.path.join(here, "../data/python-eggs")
sys.path.append(flaskapp)
execfile(activate, dict(__file__=activate))
os.environ["PYTHON_EGG_CACHE"] = pythoneggs
from """+app_name+""" import app as application
sys.stdout = sys.stderr
# For local testing
if __name__ == '__main__':
application.run()
""")
os.chmod(application, stat.S_IRWXU|stat.S_IRGRP|stat.S_IROTH)
def install_django(home_dir, app_name, pip):
"""
Installs basic Django stack for OpenShift
@param home_dir: virtualenv location
@param app_name: Inner name of application
@param pip: Location of pip
"""
subprocess.call([pip, 'install', 'django'])
libs = os.path.join(home_dir, '..', 'libs')
root = os.getcwd()
# django-admin.py needs to be executed in destinated directory
os.chdir(libs)
admin = os.path.join('..', home_dir, 'bin', 'django-admin.py')
subprocess.call([admin, 'startproject', app_name])
os.chdir(root)
application = os.path.join(home_dir, '..', 'wsgi', 'application')
os.remove(application)
file(application, 'w').write("""\
#!/usr/bin/env python
import os
import sys
here = os.path.dirname(os.path.abspath(__file__))
djangoapp = os.path.join(here, '..', 'libs')
activate = os.path.join(here, '..', 'env', 'bin', 'activate_this.py')
pythoneggs = os.path.join(here, '..', 'data', 'python-eggs')
sys.path.append(djangoapp)
execfile(activate, dict(__file__=activate))
os.environ["PYTHON_EGG_CACHE"] = pythoneggs
os.environ['DJANGO_SETTINGS_MODULE'] = '"""+app_name+""".settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
""")
os.chmod(application, stat.S_IRWXU|stat.S_IRGRP|stat.S_IROTH)
def main():
parser = optparse.OptionParser(
usage = "%prog [OPTIONS] DEST_DIR")
parser.add_option(
'-f', '--framework',
action = 'store',
dest = 'framework',
help = "Specifies used framework (flask | django)")
parser.add_option(
'-n', '--name',
action = 'store',
dest = 'app_name',
help = "Name of your application folder ('application' is default)")
options, args = parser.parse_args()
if options.framework == None:
print "No framework specified!"
parser.print_help()
sys.exit(2)
elif options.framework.lower() not in ("flask", "django"):
print "Invalid framework specified!"
parser.print_help()
sys.exit(2)
if not args:
print "No DEST_DIR specified!"
parser.print_help()
sys.exit(2)
if len(args) > 1:
print "Invalid DEST_DIR given!"
parser.print_help()
sys.exit(2)
home_dir = args[0]
virtualenv.create_environment(home_dir, site_packages=False,
use_distribute=True)
after_install(home_dir, options)
if __name__ == '__main__':
main()