forked from goswamid/ozp-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.py
148 lines (119 loc) · 4.71 KB
/
release.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
"""
Running this script will generate Wheels for the ozp-backend django app and its
dependencies
setuptools, wheel, and django libraries must be installed in the current python
environment before running this script
NOTE: It's not yet clear whether we want to install a Wheel for the ozp_backend
application itself, or if it will be better to install wheels for all of the
dependencies, then use the source of ozp_backend for running the app.
Installing ozp_backend as a wheel will place it in <python_env>/lib/Python3.4.3/site-packages/,
which might not be as convenient as running from source.
After running this script, the application can be deployed "from source"
by doing:
tar -xzf <release tarball>
pip install --no-index --find-links=release/wheelhouse -r requirements.txt
gunicorn ozp.wsgi ... (from the release directory referencing the local ozp package)
Alternatively, the wheel for the application can be installed in the python
env and the source is no longer needed:
tar -xzf <release tarball>
pip install --no-index --find-links=release/wheelhouse ozp_backend==1.0
gunicorn ozp.wsgi ... (ozp.wsgi is installed in site-packages, so it can be run
from anywhere)
One problem with the latter method is that it will only include the python
packages (no static files, test data, etc). Theoretically we should be able
to modify setup.py and specify those things in data_files, but that has not
yet worked out
"""
import argparse
import datetime
import glob
import os
import re
import shutil
from subprocess import call
VERSION_FILE = "_version.py"
PACKAGE = 'ozp_backend'
def get_version():
"""
Get the version number from VERSION_FILE
"""
verstrline = open(VERSION_FILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
if mo:
verstr = mo.group(1)
return verstr
else:
raise RuntimeError(
"Unable to find version string in {0!s}.".format(VERSION_FILE))
def get_date_time():
"""
Get current date/time string
"""
return datetime.datetime.now().strftime('%m_%d_%Y-%H-%M')
def cleanup():
"""
Remove build directories
"""
shutil.rmtree("wheel", ignore_errors=True)
shutil.rmtree("wheelhouse", ignore_errors=True)
shutil.rmtree("dist", ignore_errors=True)
shutil.rmtree("build", ignore_errors=True)
shutil.rmtree("release", ignore_errors=True)
shutil.rmtree("{0!s}.egg-info".format(PACKAGE), ignore_errors=True)
def create_release_dir():
"""
Creates a directory for the release and moves files there
"""
os.mkdir("release")
shutil.copytree("wheelhouse", "release/wheelhouse")
shutil.copytree("ozp", "release/ozp")
shutil.copytree("ozpcenter", "release/ozpcenter")
shutil.copytree("ozpiwc", "release/ozpiwc")
shutil.copytree("static", "release/static")
shutil.copytree("plugins", "release/plugins")
shutil.copytree("plugins_util", "release/plugins_util")
shutil.copy("_version.py", "release")
shutil.copy("manage.py", "release")
shutil.copy("README.md", "release")
shutil.copy("requirements.txt", "release")
shutil.copy("Makefile", "release")
def run():
parser = argparse.ArgumentParser()
parser.add_argument('--version', dest='version', action='store_true',
help='Use the version number in the file output')
parser.add_argument('--no-version', dest='version', action='store_false',
help='Use the current date/time in the file output')
parser.set_defaults(version=False)
args = parser.parse_args()
# clean up previous builds
cleanup()
# collect static files
call("mkdir -p static", shell=True)
call("python manage.py collectstatic --noinput", shell=True)
# make directory for wheelhouse
call("mkdir -p wheelhouse", shell=True)
# build wheel for ozp_backend - creates wheel in dist/
call("python setup.py bdist_wheel", shell=True)
# build/collect wheels for dependencies (this will put wheels in
# wheelhouse/)
call("pip wheel -r requirements.txt --wheel-dir wheelhouse", shell=True)
# add our wheel to the wheelhouse
for file in glob.glob(r'dist/*.whl'):
shutil.copy(file, "wheelhouse")
# create release directory including the wheelhouse (dependencies) and the
# relevant source for ozp_backend
create_release_dir()
# tar everything up
if args.version:
version = get_version()
call("tar -czf {0!s}-{1!s}.tar.gz release".format('backend', version),
shell=True)
else:
date = get_date_time()
call("tar -czf {0!s}-{1!s}.tar.gz release".format('backend', date),
shell=True)
# cleanup build dirs
cleanup()
if __name__ == "__main__":
run()