-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.py
278 lines (231 loc) · 8.11 KB
/
deploy.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
import os
import shutil
import argparse
import run_tests as tests
import sys
from twine.commands import upload
import ftplib
import glob
from pathlib import Path
from datetime import datetime
from natural_selection import __version__ as version_name
_project_name = 'natural_selection'
_git_files_for_add = [
"./docs/source/*.rst",
f"./{_project_name}/__init__.py",
f"./{_project_name}/__version__.py",
]
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def upload_to_pypi(release_name):
print(f'{bcolors.UNDERLINE}{bcolors.BOLD}{bcolors.WARNING}-- Using version {version_name}!{bcolors.ENDC}')
if release_name and release_name.lower() in ['master', 'test', 'development']:
raise ValueError('May not rename wheel to {}. To deploy a wheel by that name, checkout that branch')
if not release_name:
release_name = version_name
release_name = release_name.replace("-", "_")
local_file = f"dist/{_project_name}-{version_name}.tar.gz"
remote_file = f"{_project_name}-{release_name}.tar.gz"
os.mkdir('deploy')
shutil.copy(local_file, f"deploy/{remote_file}")
print(f"{bcolors.UNDERLINE}{bcolors.BOLD}{bcolors.OKBLUE}-- Deploying [{local_file}] -> [{remote_file}]{bcolors.ENDC}")
upload.main(['deploy/*', '-u', os.getenv('TWINE_USERNAME'), '-p', os.getenv('TWINE_PASSWORD')])
print(f"{bcolors.OKGREEN}{bcolors.BOLD}-- Deployed!{bcolors.ENDC}")
def build_documentation():
print(f'{bcolors.UNDERLINE}{bcolors.BOLD}{bcolors.HEADER}-- Change dir and make HTML docs{bcolors.ENDC}')
os.chdir('./docs')
if sys.platform == "win32":
os.system('make.bat html')
else:
os.system('make html')
os.chdir('..')
print(f'{bcolors.UNDERLINE}-- Made docs, change dir{bcolors.ENDC}')
def lock_and_gen_pipreq():
print(f'{bcolors.UNDERLINE}{bcolors.BOLD}{bcolors.HEADER}-- Locking Pipfile.lock and generating requirements.txt{bcolors.ENDC}')
os.system("pipenv lock -r > requirements.txt")
def upload_docs_via_ftp():
print(f'{bcolors.UNDERLINE}{bcolors.BOLD}{bcolors.HEADER}-- Upload documentation to FTP server! {bcolors.ENDC}')
try:
with ftplib.FTP(os.getenv('FTP_HOST'), os.getenv('FTP_USERNAME'), os.getenv('FTP_PASSWORD')) as ftp:
ftp.cwd(os.getenv('FTP_DIRECTORY'))
for f in glob.glob('./docs/build/html/*'):
if os.path.isfile(f):
with open(f, 'rb') as _f:
file_path = Path(f)
try:
ftp.storlines(f'STOR {file_path.name}', _f)
print(f"{bcolors.OKBLUE}Uploaded: {file_path.name}{bcolors.ENDC}")
except Exception as exc:
print(f"{bcolors.FAIL}{bcolors.BOLD}-- ERROR: {f} {str(exc)}!{bcolors.ENDC}")
except Exception as exc:
print(f"{bcolors.FAIL}{bcolors.BOLD}-- ERROR: {f} {str(exc)}!{bcolors.ENDC}")
return
print(f'{bcolors.UNDERLINE}-- Docs uploaded! {bcolors.ENDC}')
def build_wheel():
print(f'{bcolors.UNDERLINE}{bcolors.BOLD}{bcolors.HEADER}-- Deleting artefacts and building wheel{bcolors.ENDC}')
delete_build()
delete_dist()
os.system("python setup.py sdist bdist_wheel")
def delete_build():
if sys.platform == "win32":
os.system("RMDIR build /s /q")
os.system(f"RMDIR {_project_name}.egg-info /s /q")
else:
os.system("rm -rf build")
os.system(f"rm -rf {_project_name}.egg-info" )
def delete_dist():
if sys.platform == "win32":
os.system("RMDIR -f dist /s /q")
os.system("RMDIR deploy /s /q")
else:
os.system("rm -rf dist")
os.system("rm -rf deploy" )
def do_unit_tests(args):
if args.unit:
print(f'{bcolors.UNDERLINE}{bcolors.BOLD}{bcolors.HEADER}-- Running all unit tests!{bcolors.ENDC}')
return tests.all_unit_tests(args.coverage)
return True
def do_integration_tests(args):
if args.integration:
print(f'{bcolors.UNDERLINE}{bcolors.BOLD}{bcolors.HEADER}-- Running all integration tests!{bcolors.ENDC}')
return tests.all_integration_tests(args.coverage)
return True
def add_files_for_commit():
for f in _git_files_for_add:
os.system(f"git add {f}")
def broadcast_update_announce():
from broadcast import broadcast_update_linkedin
broadcast_update_linkedin()
def main(args):
if not do_unit_tests(args):
print(f'{bcolors.FAIL}{bcolors.BOLD}Unit testing failed, not building!{bcolors.ENDC}')
return
if not do_integration_tests(args):
print(f'{bcolors.FAIL}{bcolors.BOLD}Integration testing failed, not building!{bcolors.ENDC}')
return
if args.pipreq:
lock_and_gen_pipreq()
if args.build or args.deploy:
with open(f"{_project_name}/__version__.py", "r") as f:
lines = f.readlines()
lines[1] = f'__date__ = "{datetime.today().strftime("%Y-%m-%d")}"\n'
if lines:
with open(f"{_project_name}/__version__.py", "w") as f:
f.writelines(lines)
build_wheel()
if args.deploy:
upload_to_pypi(args.remotename)
if args.sphinx:
build_documentation()
if args.ftp:
upload_docs_via_ftp()
if args.remove:
delete_build()
delete_dist()
if args.deploy:
# All went well!
with open(f"{_project_name}/__version__.py", "r") as f:
lines = f.readlines()
v = version_name.split('.')
major = int(v[0])
minor = int(v[1])
patch = int(v[2]) + 1
lines[0] = f"__version__ = '{major}.{minor}.{patch}'\n"
if lines:
with open(f"{_project_name}/__version__.py", "w") as f:
f.writelines(lines)
print(f"{bcolors.OKGREEN}{bcolors.BOLD}-- Version number bumped to {major}.{minor}.{patch}!{bcolors.ENDC}")
if args.git:
add_files_for_commit()
if args.linkedin:
broadcast_update_announce()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=f"""
Builds {_project_name} libs package, runs tests, builds docs, and deploys to PyPi.
""",
)
parser.add_argument(
"--build",
"-b",
help="Build wheel, not required if -d is used",
action="store_true",
default=False
)
parser.add_argument(
"--remove",
"-r",
help="Removes build artifacts",
action="store_true",
default=False
)
parser.add_argument(
"--unit",
"-u",
help="Pre-run all unit tests",
action="store_true",
)
parser.add_argument(
"--integration",
"-i",
help="Pre-run all integration tests",
action="store_true",
)
parser.add_argument(
"--coverage",
"-c",
help="Run coverage over tests",
action="store_true",
)
parser.add_argument(
"--deploy",
"-d",
help="Deploys the built package to PyPi",
action="store_true",
)
parser.add_argument(
"--remotename",
type=str,
default=None,
help="Define release name for remote object")
parser.add_argument(
"--pipreq",
"-p",
help="Lock pipenv dependencies and generate requirements.txt",
action="store_true",
)
parser.add_argument(
"--sphinx",
"-s",
help="Builds the sphinx documentation",
action="store_true",
)
parser.add_argument(
"--ftp",
"-f",
help="Uploads the generated documentation via FTP",
action="store_true",
)
parser.add_argument(
"--git",
"-g",
help="Add usual files for commit",
action="store_true",
default=True,
)
parser.add_argument(
"--linkedin",
"-l",
help="Post update to LinkedIn",
action="store_true",
default=False
)
main(parser.parse_args())