-
Notifications
You must be signed in to change notification settings - Fork 17
/
x.py
110 lines (80 loc) · 2.73 KB
/
x.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
# ruff: noqa: E401, E731
__effect = lambda effect: lambda func: [func, effect(func.__dict__)][0]
cmd = lambda **kw: __effect(lambda d: d.setdefault("@cmd", {}).update(kw))
arg = lambda *a, **kw: __effect(lambda d: d.setdefault("@arg", []).append((a, kw)))
self_path = __import__("pathlib").Path(__file__).parent.resolve()
@cmd()
def precommit():
format()
lint()
docs()
test()
integration()
@cmd()
def release():
_sh(f"{python} -m build -s -w .")
dist_path = self_path / "dist"
max_wheel = max(
dist_path.glob("*.whl"),
key=lambda p: parse_file_version(p, suffix=".whl"),
)
max_sdist = max(
dist_path.glob("*.tar.gz"),
key=lambda p: parse_file_version(p, suffix=".tar.gz"),
)
print(f"Upload {[max_wheel.name, max_sdist.name]}?")
if input("[yN] ") == "y":
_sh(f"{python} -m twine upload -u __token__ {_q(max_wheel)} {_q(max_sdist)}")
@cmd()
def docs():
import minidoc
print("Update documentation")
minidoc.update_docs(self_path / "Readme.md")
@cmd()
def format():
_sh(f"{python} -m ruff format ipytest tests Example.ipynb x.py minidoc.py")
@cmd()
def lint():
_sh(f"{python} -m ruff check {_q(self_path)}")
@cmd()
def test():
_sh(f"{python} -m pytest tests")
@cmd()
def integration():
notebooks = [
"Example.ipynb",
*(
p
for p in self_path.joinpath("tests").glob("**/Test*.ipynb")
if ".ipynb_checkpoints" not in p.parts
),
]
_sh(
f"{python} -m pytest --nbval-lax --nbval-current-env "
f"{' '.join(_q(p) for p in notebooks)}"
)
@cmd()
def lock():
_sh(f"{python} -m poetry lock")
_sh(
f"{python} -m poetry export --with=dev "
"-f requirements.txt --output requirements-dev.txt"
)
def parse_file_version(p, suffix):
from packaging import version
assert p.name.endswith(suffix)
_, version_part, *_ = p.name[: -len(suffix)].split("-")
return version.parse(version_part)
_sh = lambda c, **kw: __import__("subprocess").run(
[args := __import__("shlex").split(c.replace("\n", " ")), print("::", *args)][0],
**{"check": True, "cwd": self_path, "encoding": "utf-8", **kw},
)
_q = lambda arg: __import__("shlex").quote(str(arg))
python = _q(__import__("sys").executable)
if __name__ == "__main__":
_sps = (_p := __import__("argparse").ArgumentParser()).add_subparsers()
for _f in (f for _, f in sorted(globals().items()) if hasattr(f, "@cmd")):
_sp = _sps.add_parser(_f.__name__.replace("_", "-"), **getattr(_f, "@cmd"))
_sp.set_defaults(_=_f)
[_sp.add_argument(*a, **kw) for a, kw in reversed(getattr(_f, "@arg", []))]
(_a := vars(_p.parse_args())).pop("_", _p.print_help)(**_a)