Skip to content

Commit

Permalink
🍻 Migrate dev scripts from sh to py
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Oct 24, 2024
1 parent 7b7315d commit bc78b85
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 148 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ jobs:
poetry install
poetry run pip install coveralls
- name: Check code style and Type Hint
run: ./scripts/check.sh
run: ./scripts/check.py
- name: test
run: ./scripts/test.sh
run: ./scripts/test.py
- name: build
run: poetry build
- name: Upload Coverage
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ repos:
hooks:
- id: check
name: check by ruff/mypy
entry: ./scripts/check.sh
language: script
entry: ./scripts/check.py
language: python
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
help:
@echo "FastDevCli development makefile"
@echo
@echo "Usage: make <target>"
@echo "Targets:"
@echo " up Updates dev/test dependencies"
@echo " deps Ensure dev/test dependencies are installed"
@echo " check Checks that build is sane"
@echo " test Runs all tests"
@echo " style Auto-formats the code"
@echo " lint Auto-formats the code and check type hints"

up:
@poetry run fast upgrade

deps:
@poetry install

_check:
./scripts/check.py
check: deps _build _check

_lint:
@poetry run fast lint
lint: deps _build _lint

_test:
./scripts/test.py
test: deps _test

_style:
./scripts/format.py
style: deps _style

_build:
rm -fR dist/
poetry build
build: deps _build
149 changes: 75 additions & 74 deletions poetry.lock

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions scripts/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
"""
Check style by `ruff` and verify type hints by `mypy`,
then run `bandit -r <package>` to find security issue.
Usage::
./scripts/check.py
"""

import os
import sys

CMD = "fast check"
TOOL = ("poetry", "pdm", "")[0]
BANDIT = True
parent = os.path.abspath(os.path.dirname(__file__))
work_dir = os.path.dirname(parent)
if os.getcwd() != work_dir:
os.chdir(work_dir)

cmd = "{} run {}".format(TOOL, CMD) if TOOL else CMD
if os.system(cmd) != 0:
print("\033[1m Please run './scripts/format.py' to auto-fix style issues \033[0m")
sys.exit(1)

if BANDIT:
package_name = os.path.basename(work_dir).replace("-", "_")
cmd = "{}bandit -r {}".format(TOOL and f"{TOOL} run ", package_name)
print("-->", cmd)
if os.system(cmd) != 0:
sys.exit(1)
print("Done. ✨ 🍰 ✨")
28 changes: 0 additions & 28 deletions scripts/check.sh

This file was deleted.

24 changes: 24 additions & 0 deletions scripts/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
"""
Run `ruff format` to make style and `ruff check --fix` to remove unused imports
Usage:
./scripts/format.py
"""

import os
import shlex
import subprocess
import sys
from pathlib import Path

CMD = "fast lint"
TOOL = ("poetry", "pdm", "")[0]
work_dir = Path(__file__).parent.resolve().parent
if Path.cwd() != work_dir:
os.chdir(str(work_dir))

cmd = (TOOL and f"{TOOL} run ") + CMD
r = subprocess.run(shlex.split(cmd), env=dict(os.environ, SKIP_MYPY="1"))
sys.exit(None if r.returncode == 0 else 1)
12 changes: 0 additions & 12 deletions scripts/format.sh

This file was deleted.

76 changes: 76 additions & 0 deletions scripts/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python
import os
import shlex
import subprocess
import sys
import time
from pathlib import Path

TOOL = ("poetry", "pdm", "")[0]
work_dir = Path(__file__).parent.resolve().parent
if Path.cwd() != work_dir:
os.chdir(str(work_dir))

PREPARE = "python tests/prepare_media.py"
CMD = "python scripts/parallel_test.py"
COMBINE = "coverage combine examples/*/.coverage tests/*/.coverage"
REPORT = 'coverage report --omit="tests/*" -m'


def get_result_files():
for folder in ("examples", "tests"):
for file in work_dir.joinpath(folder).rglob(".coverage"):
yield file


def remove_outdate_files(start_time: float) -> None:
for file in get_result_files():
if file.stat().st_mtime < start_time:
file.unlink()
print(f"Removed outdate file: {file}")


def chdir(path: Path) -> None:
os.chdir(str(path))
print("--> cd", path)


def run_command(cmd: str, shell=False, tool=TOOL, env=None) -> None:
prefix = tool + " run "
if tool and not cmd.startswith(prefix):
cmd = prefix + cmd
print("-->", cmd, flush=True)
if env is not None:
env = dict(os.environ, **env)
r = subprocess.run(cmd if shell else shlex.split(cmd), shell=shell, env=None)
r.returncode and sys.exit(1)


def combine_result_files(shell=COMBINE) -> None:
to_be_combine = [i.name for i in get_result_files()]
if to_be_combine:
if sys.platform == "win32":
shell = "coverage combine "
if work_dir.joinpath(".coverage").exists():
shell += ".coverage "
shell += " ".join(to_be_combine)
run_command(shell, True)


started_at = time.time()
run_command(PREPARE)
if "--no-parallel" in sys.argv:
for folder in work_dir.joinpath("tests").glob("*"):
if not folder.is_dir():
continue
chdir(folder)
run_command("coverage run -m pytest -s", tool="")
else:
run_command(CMD)
for eg in ("normal", "offline"):
chdir(work_dir / "examples" / eg)
run_command("coverage run -m pytest -s", tool="")
os.chdir(str(work_dir))
remove_outdate_files(started_at)
combine_result_files()
run_command(REPORT)
30 changes: 0 additions & 30 deletions scripts/test.sh

This file was deleted.

0 comments on commit bc78b85

Please sign in to comment.