From c70db47d70ce45b1c8c94d423f26da74bbe2922d Mon Sep 17 00:00:00 2001 From: Joe Kaufeld Date: Thu, 27 Feb 2025 11:50:17 -0500 Subject: [PATCH] update for new release pattern --- .gitea/workflows/build_and_release.yml.jinja | 5 +- Makefile.jinja | 3 + README.md.jinja | 2 +- pyproject.toml.jinja | 5 +- src/__init__.py | 1 - src/cli.py.jinja | 96 +++++++------------- 6 files changed, 42 insertions(+), 70 deletions(-) delete mode 100644 src/__init__.py diff --git a/.gitea/workflows/build_and_release.yml.jinja b/.gitea/workflows/build_and_release.yml.jinja index d85139f..ea4f610 100644 --- a/.gitea/workflows/build_and_release.yml.jinja +++ b/.gitea/workflows/build_and_release.yml.jinja @@ -14,7 +14,7 @@ jobs: - uses: https://github.com/actions/checkout@v2 - uses: https://github.com/actions/setup-python@v4 with: - python-version: '3.11.x' + python-version: '3.12.x' - name: Install Env # this should be all we need because shiv will download the deps itself run: | @@ -27,7 +27,8 @@ jobs: echo ${{ env.VERSION }} - name: Build the sucker run: | - sed -i -e "s/?????/${{ env.VERSION }}/g" src/__init__.py + sed -i -e "s/??version??/${{ env.VERSION }}/g" src/__init__.py + sed -i -e "s/??GITEA_TOKEN??/${{ secrets.READ_KEY }}/g" src/__init__.py make build - name: Create release! run: | diff --git a/Makefile.jinja b/Makefile.jinja index d80be07..cafdb27 100644 --- a/Makefile.jinja +++ b/Makefile.jinja @@ -1,3 +1,6 @@ +activate: + source .venv/bin/activate + setup: python src/poetry2setup.py > setup.py diff --git a/README.md.jinja b/README.md.jinja index 8b6c145..df62bb1 100644 --- a/README.md.jinja +++ b/README.md.jinja @@ -4,7 +4,7 @@ Use this template by installing `copier` and running: ```shell -copier https://git.joekaufeld.com/jkaufeld/copier-shiv.git [dir] +copier copy https://git.joekaufeld.com/jkaufeld/copier-shiv.git [dir] ``` After that, you can set up the system with: diff --git a/pyproject.toml.jinja b/pyproject.toml.jinja index 467e478..ef5e261 100644 --- a/pyproject.toml.jinja +++ b/pyproject.toml.jinja @@ -11,8 +11,9 @@ httpx = "^0.23.0" click = "^8.1.3" rich = "^12.5.1" art = "^5.9" +python-dotenv = "^1.0.1" -[tool.poetry.dev-dependencies] +[poetry.group.dev.dependencies] poetry = "^1.1.14" black = "^22.6.0" @@ -21,4 +22,4 @@ requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" [tool.poetry.plugins."console_scripts"] -"[[ module_name ]]" = "src.cli:main" +"[[ module_name ]]" = "src.cli:entrypoint" diff --git a/src/__init__.py b/src/__init__.py deleted file mode 100644 index 47e608a..0000000 --- a/src/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = "?????" # will be replaced during build by CI diff --git a/src/cli.py.jinja b/src/cli.py.jinja index f83346f..ede128c 100644 --- a/src/cli.py.jinja +++ b/src/cli.py.jinja @@ -1,28 +1,16 @@ import code import io -import random -import string import sys -import uuid import art import click -import httpx -from rich import console as rich_console, pretty -from rich.status import Status -from rich.traceback import install -from shiv.bootstrap import current_zipfile +from rich import console as rich_console, pretty, traceback import src +from src.utils import _install, _uninstall, update_from_gitea -def print_help(): - ctx = click.get_current_context() - click.echo(ctx.get_help()) - ctx.exit() - - -class RichGroup(click.Group): +class RichCommand(click.Group): def format_usage(self, ctx, formatter): sio = io.StringIO() console = rich_console.Console(file=sio, force_terminal=True) @@ -32,8 +20,8 @@ class RichGroup(click.Group): formatter.write(sio.getvalue()) -@click.group( - cls=RichGroup, +@click.command( + cls=RichCommand, context_settings=dict(help_option_names=["-h", "--help", "--halp"]), ) @click.pass_context @@ -50,7 +38,21 @@ class RichGroup(click.Group): default=False, help="Launch a REPL for testing.", ) -def main(ctx, update, shell): +@click.option( + "--install", + "install", + is_flag=True, + default=False, + help="Install as a service on Ubuntu systems." +) +@click.option( + "--uninstall", + "uninstall", + is_flag=True, + default=False, + help="Uninstall as an Ubuntu service." +) +def entrypoint(update, shell, install, uninstall): """ Launch [[ project_name ]] or drop into a command line REPL. """ @@ -61,57 +63,23 @@ def main(ctx, update, shell): banner = art.text2art("[[ module_name ]]") pretty.install() # type: ignore - install() # traceback handler + traceback.install() # traceback handler code.interact(local=globals(), banner=banner) sys.exit() - elif ctx.invoked_subcommand is None: - print_help() + if install: + _install() + sys.exit() + if uninstall: + _uninstall() + sys.exit() + main() -@main.command() -def uuid4(): - """Generate a random UUID4.""" - click.echo(uuid.uuid4()) - - -def update_from_gitea(): - """Get the newest release from Gitea and install it.""" - status = Status("Checking for new release...") - status.start() - - response = httpx.get( - "https://git.joekaufeld.com/api/v1/repos/jkaufeld/[[ repo_name ]]/releases/latest" - ) - if response.status_code != 200: - status.stop() - click.echo( - f"Something went wrong when talking to Gitea; got a" - f" {response.status_code} with the following content:\n" - f"{response.content}" - ) - return - status.update("Checking for new release...") - release_data = response.json() - if release_data["tag_name"] == src.__version__: - status.stop() - click.echo( - "Server version is the same as current version; nothing to update." - ) - return - status.update("Updating...") - - url = release_data["assets"][0]["browser_download_url"] - with current_zipfile() as archive: - with open(archive.filename, "wb") as f, httpx.stream( - "GET", url, follow_redirects=True - ) as r: - for line in r.iter_bytes(): - f.write(line) - - status.stop() - click.echo(f"Updated to {release_data['tag_name']}! 🎉") +def main() -> None: + """Put main program functionality here.""" + print("Nothing here...") if __name__ == "__main__": - main() + entrypoint()