Skip to content

Commit 404ca53

Browse files
committed
create preflight scripts for local CI
1 parent dcb7726 commit 404ca53

File tree

3 files changed

+138
-13
lines changed

3 files changed

+138
-13
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ docs/build
1212
docs/_build
1313

1414
# local testing
15-
_api*.ya?ml
15+
_*.yaml
16+
_*.yml
1617
_*.py
1718
_*.json
19+
preflight.log
1820
.vscode/settings.json
1921

2022
# media files

_preflight.py

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,73 @@
44
It is NOT part of the Hondana API or for public use. No support will be provided for this file. Use at your own risk.
55
"""
66

7+
import argparse
78
import asyncio
89
import pathlib
9-
import subprocess
10+
import subprocess # noqa: S404
1011
import sys
1112

1213
import hondana
1314

1415
TAG_PATH = pathlib.Path("./hondana/extras/tags.json")
1516
REPORT_PATH = pathlib.Path("./hondana/extras/report_reasons.json")
1617

17-
client = hondana.Client()
1818

19+
class ProgramNamespace(argparse.Namespace):
20+
tags: bool
21+
reports: bool
1922

20-
def __update_tags() -> None:
21-
asyncio.run(client.update_tags())
22-
asyncio.run(client.close())
23+
def _parsed(self) -> bool:
24+
return any([self.tags, self.reports])
2325

24-
diff = subprocess.run(["git", "diff", "--exit-code", str(TAG_PATH)], capture_output=False)
25-
sys.exit(diff.returncode)
2626

27+
parser = argparse.ArgumentParser(description="Small pre-flight CI script for hondana")
28+
parser.add_argument("-t", "--tags", action="store_true", dest="tags", help="Whether to run the 'update tags' action.")
29+
parser.add_argument(
30+
"-r", "--reports", action="store_true", dest="reports", help="Whether to run the 'update report reasons' action."
31+
)
2732

28-
def __update_report_reasons() -> None:
29-
asyncio.run(client.update_report_reasons())
30-
asyncio.run(client.close())
3133

32-
diff = subprocess.run(["git", "diff", "--exit-code", str(REPORT_PATH)], capture_output=False)
33-
sys.exit(diff.returncode)
34+
async def __update_tags(client: hondana.Client, /) -> int:
35+
await client.update_tags()
36+
37+
prog: asyncio.subprocess.Process = await asyncio.create_subprocess_exec("git", "diff", "--exit-code", str(REPORT_PATH))
38+
return await prog.wait()
39+
40+
41+
async def __update_report_reasons(client: hondana.Client, /) -> int:
42+
await client.update_report_reasons()
43+
44+
prog: asyncio.subprocess.Process = await asyncio.create_subprocess_exec(
45+
"git",
46+
"diff",
47+
"--exit-code",
48+
str(REPORT_PATH),
49+
)
50+
return await prog.wait()
51+
52+
53+
async def main(args: ProgramNamespace) -> None:
54+
if not args._parsed():
55+
msg = "At least one argument must be specified."
56+
raise RuntimeError(msg)
57+
58+
client = hondana.Client()
59+
60+
if args.tags:
61+
ret = await __update_tags(client)
62+
if ret == 1:
63+
msg = "Tags updated."
64+
raise RuntimeError(msg)
65+
if args.reports:
66+
ret = await __update_report_reasons(client)
67+
if ret == 1:
68+
msg = "Reports updated."
69+
raise RuntimeError(msg)
70+
71+
await client.close()
72+
73+
74+
if __name__ == "__main__":
75+
args = parser.parse_args(namespace=ProgramNamespace())
76+
asyncio.run(main(args))

_preflight.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
set -eux -o pipefail
4+
5+
VENV_PATH=${VENV_PATH:-".venv/"}
6+
STRICT_DOCS=${STRICT_DOCS:-"false"}
7+
PROJECT=${PROJECT:-"hondana"}
8+
API_YAML="_api.yaml"
9+
OLD_API_YAML="_api.old.yaml"
10+
11+
die() { echo "$*" 1>&2; exit 1; }
12+
13+
if [[ ! -d "${VENV_PATH}" ]]; then
14+
die "virtualenvironment does not exist."
15+
fi
16+
17+
# activate the venv to get proper env vars
18+
source .venv/bin/activate
19+
20+
run_ruff(){
21+
if ! command -v ruff > /dev/null 2>&1; then
22+
die "ruff is not installed."
23+
fi
24+
25+
if ! ruff check .; then
26+
die "Ruff check failed."
27+
fi
28+
if ! ruff format --check .; then
29+
die "Ruff format failed."
30+
fi
31+
}
32+
33+
api_diff(){
34+
if [[ -f "${API_YAML}" ]]; then
35+
mv "${API_YAML}" "${OLD_API_YAML}"
36+
else
37+
echo "No API file, skipping to downloading new one."
38+
fi
39+
40+
curl -SsL "https://api.mangadex.org/docs/static/api.yaml" -o _api.yaml
41+
42+
if ! cmp -s "${API_YAML}" "${OLD_API_YAML}"; then
43+
die "There's an API change, check the diff: \`diff ${API_YAML} ${OLD_API_YAML}\`"
44+
fi
45+
}
46+
47+
preflight_tags() {
48+
python _preflight.py -t
49+
}
50+
51+
preflight_reports() {
52+
python _preflight.py -r
53+
}
54+
55+
run_pyright(){
56+
pyright
57+
pyright --ignoreexternal --verifytypes "${PROJECT}"
58+
}
59+
60+
build_docs() {
61+
if [[ "${STRICT_DOCS}" != "false" ]]; then
62+
sphinx-build -anEWT --keep-going docs/ docs/build
63+
else
64+
sphinx-build -aEWT --keep-going docs/ docs/build
65+
fi
66+
}
67+
68+
run_tests() {
69+
pytest
70+
}
71+
72+
{
73+
run_ruff
74+
api_diff
75+
preflight_tags
76+
preflight_reports
77+
run_pyright
78+
build_docs
79+
run_tests
80+
} 2>&1 | tee preflight.log

0 commit comments

Comments
 (0)