add clean dev command

This commit is contained in:
Izalia Mae 2024-04-19 10:27:09 -04:00
parent 4c753fc512
commit e6a119c871
2 changed files with 33 additions and 20 deletions

47
dev.py
View file

@ -1,23 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import asyncio import asyncio
import platform
import shlex import shlex
import subprocess import subprocess
import sys import sys
import time import time
import tomllib import tomllib
import traceback
from collections.abc import Callable from collections.abc import Callable
from datetime import datetime, timedelta from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from shutil import rmtree
from typing import TypedDict from typing import TypedDict
try: try:
import watchfiles import watchfiles
from click import Context, echo, group, option, pass_context from click import echo, group, option
from tomlkit.items import Array, String, StringType, Trivia from tomlkit.items import Array, String, StringType, Trivia
from tomlkit.toml_file import TOMLFile from tomlkit.toml_file import TOMLFile
@ -32,6 +30,9 @@ except ImportError:
REPO = Path(__file__).resolve().parent REPO = Path(__file__).resolve().parent
CLEAN_DIRS = ["build", "dist", "dist-pypi", "barkshark_asgi.egg-info", "docs/_build"]
IGNORE_DIRS = ["build", "dist", "dist-pypi", "docs", ".git", "barkshark_asgi.egg-info"]
IGNORE_PATHS = tuple(str(REPO.joinpath(path)) for path in IGNORE_DIRS)
class WatchfilesOptions(TypedDict): class WatchfilesOptions(TypedDict):
@ -46,6 +47,19 @@ def cli() -> None:
... ...
@cli.command("clean")
def cli_clean() -> None:
for directory in CLEAN_DIRS:
try:
rmtree(REPO.joinpath(directory))
except FileNotFoundError:
pass
echo("Cleaned up build files")
@cli.command("install") @cli.command("install")
def cli_install_deps() -> None: def cli_install_deps() -> None:
with open("pyproject.toml", "rb") as fd: with open("pyproject.toml", "rb") as fd:
@ -79,8 +93,8 @@ def cli_lint(path: Path, watch: bool) -> None:
run_python("-m", "mypy", str(path)) run_python("-m", "mypy", str(path))
@cli.command("build-package") @cli.command("build")
def cli_build_package() -> None: def cli_build() -> None:
cli_update_files.callback() # type: ignore cli_update_files.callback() # type: ignore
run_python("-m", " build", "--outdir", "dist-pypi") run_python("-m", " build", "--outdir", "dist-pypi")
@ -106,17 +120,6 @@ def cli_update_files() -> None:
project_file.write(project) project_file.write(project)
@cli.command("exec", context_settings = {"allow_extra_args": True})
@option("--watch", "-w", is_flag = True)
@pass_context
def cli_run(ctx: Context, watch: bool = False) -> None:
if watch:
handle_run_watcher("-m", "barkshark_social", "run")
return
run_python("-m", "basgi", *ctx.args)
def run_python(*arguments: str) -> subprocess.CompletedProcess[bytes]: def run_python(*arguments: str) -> subprocess.CompletedProcess[bytes]:
return subprocess.run([sys.executable, *arguments]) return subprocess.run([sys.executable, *arguments])
@ -137,6 +140,16 @@ async def _handle_run_watcher(*command: str) -> None:
} }
async for changes in watchfiles.awatch(REPO.joinpath("basgi"), **options): async for changes in watchfiles.awatch(REPO.joinpath("basgi"), **options):
skip = False
for _, path in changes:
if path.startswith(IGNORE_PATHS):
skip = True
if skip:
continue
if datetime.now() - timedelta(seconds = 3) < last_restart: if datetime.now() - timedelta(seconds = 3) < last_restart:
continue continue

View file

@ -57,7 +57,7 @@ granian = [
"granian[reload] == 1.2.2" "granian[reload] == 1.2.2"
] ]
uvicorn = [ uvicorn = [
"uvicorn == 0.29.0" "uvicorn[standard] == 0.29.0"
] ]
docs = [ docs = [
"furo == 2024.1.29", "furo == 2024.1.29",
@ -65,6 +65,7 @@ docs = [
"sphinx-external-toc == 1.0.1" "sphinx-external-toc == 1.0.1"
] ]
dev = [ dev = [
"build == 1.2.1",
"mypy == 1.9.0", "mypy == 1.9.0",
"flake8 == 7.0.0", "flake8 == 7.0.0",
"pyinstaller == 6.5.0", "pyinstaller == 6.5.0",
@ -81,7 +82,7 @@ platforms = ["any"]
license-files = ["LICENSE.md"] license-files = ["LICENSE.md"]
[tool.setuptools.package-data] [tool.setuptools.package-data]
basgi = ["frontend"] basgi = ["framework"]
[tool.distutils.bdist_wheel] [tool.distutils.bdist_wheel]
universal = false universal = false
@ -97,7 +98,6 @@ disallow_untyped_decorators = true
warn_redundant_casts = true warn_redundant_casts = true
warn_unreachable = true warn_unreachable = true
warn_unused_ignores = true warn_unused_ignores = true
ignore_missing_imports = true
follow_imports = "silent" follow_imports = "silent"
strict = true strict = true
implicit_reexport = true implicit_reexport = true