-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakefile.py
More file actions
225 lines (175 loc) · 6.33 KB
/
makefile.py
File metadata and controls
225 lines (175 loc) · 6.33 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
224
225
from __future__ import annotations
import datetime as dt
import re
import shutil
import sys
import tempfile
from itertools import chain
from pathlib import Path
from subprocess import PIPE, CalledProcessError, Popen
venv_name = "venv310"
activate = rf".\{venv_name}\Scripts\activate.bat & "
wheelhouse_folder = r"\\md-man.biz\project-cph\bwcph\wheelhouse_3_10"
pip_ini = Path(venv_name) / "pip.ini"
pip_ini_txt = f"[install]\nno-index = true\nfind-links = {wheelhouse_folder}"
py_exe = sys.executable
venv_name_build = "venv310b"
venv_build_activate_path = Path(venv_name_build).absolute() / "Scripts" / "activate.bat"
venv_build_activate = f'"{venv_build_activate_path}" && '
pip_ini_build = Path(venv_name_build) / "pip.ini"
def venv():
"""Create or update venv"""
if Path(venv_name).exists():
print("venv exists, exiting")
else:
run(f"{py_exe} -m venv {venv_name}", venv_activate=None)
pip_ini.write_text(pip_ini_txt)
run(f"python -m pip install -U pip")
run(f"python -m pip install -e .[dev]")
if Path(venv_name_build).exists():
print("venv exists, exiting")
else:
run(f"{py_exe} -m venv {venv_name_build}", venv_activate=None)
pip_ini_build.write_text(pip_ini_txt)
run(f"python -m pip install -U pip", venv_activate=venv_build_activate)
run(f"python -m pip install .", venv_activate=venv_build_activate)
def build():
"""Re-build wheel"""
run(f"python -m build --wheel --no-isolation")
def build_exe():
"""Build exe file"""
clean()
# single file build
cmd = f"""
pyinstaller.exe pyinstaller_main.py
--name packaging-example
--collect-data "packaging_example.data"
--noconfirm --console --clean --onefile
"""
cmd = re.sub(r"\s+", " ", cmd)
run(cmd)
# multifile build to inspect what datafiles are included - everything in packaging_example.data
cmd = f"""
pyinstaller.exe pyinstaller_main.py
--name packaging-example-non-single
--collect-data "packaging_example.data"
--noconfirm --console --clean
"""
cmd = re.sub(r"\s+", " ", cmd)
run(cmd)
# multifile build where the package is installed, now the installer and the wheel are in sync
cmd = rf"""
pyinstaller.exe ..\pyinstaller_main.py
--name packaging-example-non-single-dedicated-venv
--collect-data "packaging_example.data"
--noconfirm --console --clean --distpath ..\dist
"""
cmd = re.sub(r"\s+", " ", cmd)
tmp_dir = Path("tmp")
tmp_dir.mkdir(exist_ok=True)
run(f"python -m pip install -U .", venv_activate=venv_build_activate)
run(cmd, venv_activate=venv_build_activate, cwd=tmp_dir)
def pytest():
"""Run the tests"""
run("pytest")
def clean_test():
tmp_dir = Path(tempfile.gettempdir())
now_str = dt.datetime.now().strftime("%Y%m%d_%H%M")
checkout_folder = tmp_dir / f"{Path.cwd().name}-{now_str}"
git_url = get_url_from_git_config()
try:
run(f"git clone {git_url} {checkout_folder.name}", cwd=tmp_dir)
run(f"py -3.10 makefile.py venv", cwd=checkout_folder)
run(f"py -3.10 makefile.py pytest", cwd=checkout_folder)
status = "OK"
except CalledProcessError:
status = "NOK"
run(f'git tag -a TEST_{status}_{now_str} -m "Test run {now_str} - {status}"', cwd=checkout_folder)
run(f"git push --follow-tags", cwd=checkout_folder)
rm(checkout_folder)
def clean():
"""Cleanup build artifacts"""
src = Path("src")
to_remove = chain(
("dist", "build", "pytest_cache", "__pycache__"),
src.glob("**/__pycache__"),
src.glob("**/*.egg-info"),
)
for d in to_remove:
rm(d)
def clean_all():
"""Cleanup build artifacts and venv"""
clean()
rm("venv")
def nbclean_all():
"""Remove output from all notebooks"""
all_notebooks = (nb for nb in Path.cwd().glob("*.ipynb") if ".ipynb_checkpoints" not in nb.parts)
for nb in all_notebooks:
run(f'jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace "{nb}"')
actions = {
"venv": venv,
"build": build,
"build-exe": build_exe,
"pytest": pytest,
"clean-test": clean_test,
"clean": clean,
"nbclean-all": nbclean_all,
"clean-all": clean_all,
}
####################################
##### Boilerplate starts here
####################################
def get_url_from_git_config(conf: Path = Path.cwd() / ".git" / "config") -> str:
"""Get the url from the git config file"""
lines = conf.read_text().splitlines()
urls = [line.split(" = ")[1].strip() for line in lines if line.startswith("\turl = ")]
assert len(urls) == 1, "More than one url found in git config"
return urls[0]
def run(
cmd: str,
echo_cmd=True,
echo_stdout=True,
cwd: Path | None = None,
venv_activate: str | None = rf".\{venv_name}\Scripts\activate.bat && ",
) -> str:
"""Run shell command with option to print stdout incrementally"""
if venv_activate:
cmd = venv_activate + cmd
if echo_cmd:
print(f"##\n## Running: {cmd}", end="")
if cwd:
print(f"\n## cwd: {cwd}")
if echo_cmd:
print(f"\n")
res = []
proc = Popen(cmd, stdout=PIPE, stderr=sys.stderr, shell=True, encoding=sys.getfilesystemencoding(), cwd=cwd)
while proc.poll() is None:
if proc.stdout is None:
continue
line = proc.stdout.readline()
if echo_stdout:
print(line, end="")
res.append(line)
if proc.returncode != 0:
raise CalledProcessError(proc.returncode, cmd)
return "".join(res)
def rm(path: Path | str, echo_cmd: bool = True):
"""Remove file or folder"""
if isinstance(path, str):
path = Path(path)
if echo_cmd:
print(f"## Removing: {path}")
if path.is_file():
path.unlink()
elif path.is_dir():
shutil.rmtree(path)
if __name__ == "__main__":
cmd = sys.argv[0]
sub_cmd = sys.argv[1] if len(sys.argv) == 2 else None
if sub_cmd in actions and sub_cmd:
actions[sub_cmd]()
else:
print(f"Options for '{sys.executable} {cmd}':")
for arg, func in actions.items():
doc_str = func.__doc__.split("\n")[0] if func.__doc__ else ""
print(f" {arg: <18}{doc_str}")