-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.py
214 lines (161 loc) · 4.93 KB
/
build.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
# SPDX-License-Identifier: Apache-2.0
# Licensed to the Ed-Fi Alliance under one or more agreements.
# The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
# See the LICENSE and NOTICES files in the project root for more information.
"""Build automation scripts"""
import os
from subprocess import run
import shutil
import sys
from typing import List
import logging
logger = logging.getLogger(__name__)
def _display_help():
message = """Please specify a command followed by a module. Valid commands:
* install
* test
* coverage
* coverage:html
* coverage:xml
* lint
* typecheck
* typecheck:xml
* build
* publish
* ci:test
* ci:publish
For example, to run unit tests with code coverage, on utility `edfi-paging-test`:
> ./build.py coverage:xml edfi_paging_test
"""
logger.info(message)
exit(-1)
def _run_command(command: List[str], exit_immediately: bool = True):
logger.info("\033[95m" + " ".join(command) + "\033[0m")
# On Windows, must run inside of cmd.exe in order to get the user's $PATH.
if os.name == "nt":
# All versions of Windows are "nt"
command = ["cmd", "/c", *command]
script_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
package_name = sys.argv[2]
package_dir = os.path.join(script_dir, "..", "src", package_name)
if not os.path.exists(package_dir):
raise RuntimeError(f"Cannot find package {package_name}")
logger.info(package_dir)
result = run(command, cwd=package_dir)
return_code = result.returncode
if exit_immediately:
# Exits the script regardless of the prior return code
exit(return_code)
if return_code != 0:
# Only exits the script for non-zero return code
exit(return_code)
def _run_install(exit_immediately: bool = True):
_run_command(["poetry", "install"], exit_immediately)
def _run_tests(exit_immediately: bool = True):
_run_command(
[
"poetry",
"run",
"pytest",
"tests",
],
exit_immediately,
)
def _run_coverage_without_report():
_run_command(
[
"poetry",
"run",
"coverage",
"run",
"-m",
"pytest",
"tests",
"--junitxml=pytest-junit.xml"
],
exit_immediately=False,
)
def _run_coverage(exit_immediately: bool = True):
_run_coverage_without_report()
_run_command(
[
"poetry",
"run",
"coverage",
"report",
],
exit_immediately=exit_immediately,
)
def _run_coverage_html(exit_immediately: bool = True):
_run_coverage(exit_immediately)
_run_command(
[
"poetry",
"run",
"coverage",
"html",
],
exit_immediately,
)
def _run_coverage_xml():
_run_coverage_without_report(
[
"poetry",
"run",
"coverage",
"run",
"-m",
"pytest",
"tests",
],
exit_immediately=False,
)
_run_command(["poetry", "run", "coverage", "xml"], exit_immediately=False)
def _run_lint(exit_immediately: bool = True):
_run_command(["poetry", "run", "flake8"], exit_immediately)
def _run_typecheck():
_run_command(["poetry", "run", "mypy"], exit_immediately=True)
def _run_typecheck_xml(exit_immediately: bool = True):
_run_command(["poetry", "run", "mypy", "--junit-xml", "mypy-junit.xml"], exit_immediately)
def _run_build(exit_immediately: bool = True):
_run_command(["poetry", "build"], exit_immediately)
def _run_publish(exit_immediately: bool = True):
shutil.rmtree("dist", ignore_errors=True)
_run_build(False)
_run_command(["poetry", "run", "twine", "upload", "dist/*"], exit_immediately)
def _run_ci_test():
"""
Calls the commands required for a continuous unit testing, type-checking, and linting job.
"""
_run_install(False)
_run_coverage_html(False)
_run_typecheck_xml(False)
_run_lint(True)
def _run_ci_publish():
"""
Calls the commands required for a continuous integration publishing job.
"""
_run_install(False)
_run_tests(False)
_run_publish(True)
if __name__ == "__main__":
if not sys.version_info >= (3, 9):
logger.error("This program requires Python 3.9 or newer.", file=sys.stderr)
exit(-1)
if len(sys.argv) < 3:
_display_help()
switcher = {
"install": _run_install,
"test": _run_tests,
"coverage": _run_coverage,
"coverage:html": _run_coverage_html,
"coverage:xml": _run_coverage_xml,
"lint": _run_lint,
"typecheck": _run_typecheck,
"typecheck:xml": _run_typecheck_xml,
"build": _run_build,
"publish": _run_publish,
"ci:test": _run_ci_test,
"ci:publish": _run_ci_publish,
}
switcher.get(sys.argv[1], _display_help)()