Verified Commit b1a6cfdb authored by anarcat's avatar anarcat
Browse files

simplify boostrap code by using a Namespace

This reduces the number of globals we have to pass around and
generally makes the code more readable and less magic.
parent 71e7f81c
Loading
Loading
Loading
Loading
+15 −18
Original line number Diff line number Diff line
@@ -18,9 +18,10 @@ import re
import configparser


SUDO: str | None = "sudo"
NOOP = False
CONFIRM = True
args = argparse.Namespace(
    sudo_command="sudo",
    confirm=True,
)


def install_packages(names: list[str]):
@@ -34,15 +35,15 @@ def install_packages(names: list[str]):
            "installing %d packages: %s%s",
            len(install_list),
            " ".join(install_list),
            " (simulated)" if NOOP else "",
            " (simulated)" if args.noop else "",
        )
        if NOOP:
        if args.noop:
            return
        cmd = ["apt", "install"]

        if SUDO:
            cmd = [SUDO] + cmd
        if not CONFIRM:
        if not args.no_sudo:
            cmd = [args.sudo_command] + cmd
        if args.yes:
            cmd.append("-y")
        cmd += install_list
        run(cmd, check=True)
@@ -81,7 +82,9 @@ def install_python_requirements():


def install_git_hooks():
    logging.info("installing pre-commit git hook%s", " (simulated)" if NOOP else "")
    logging.info(
        "installing pre-commit git hook%s", " (simulated)" if args.noop else ""
    )
    try:
        symlink("../../lint.sh", ".git/hooks/pre-push")
    except FileExistsError:
@@ -118,7 +121,6 @@ class LoggingAction(argparse.Action):


def main() -> None:
    global SUDO, NOOP, CONFIRM
    parser = argparse.ArgumentParser(description=__doc__, epilog=__epilog__)
    logging.basicConfig(level="INFO", format="%(message)s")
    parser.add_argument(
@@ -137,6 +139,7 @@ def main() -> None:
    )
    parser.add_argument(
        "-n",
        "--noop",
        "--nothing",
        action="store_true",
        help="do nothing permanent",
@@ -150,7 +153,7 @@ def main() -> None:
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "--sudo-command",
        default=SUDO,
        default=args.sudo_command,
        help="command to use to become root, default: %(default)s",
    )
    group.add_argument(
@@ -158,13 +161,7 @@ def main() -> None:
        action="store_true",
        help="do not use a sudo command",
    )
    args = parser.parse_args()
    if args.no_sudo:
        SUDO = None
    else:
        SUDO = args.sudo_command
    NOOP = args.nothing
    CONFIRM = not args.yes
    parser.parse_args(namespace=args)

    install_python_requirements()
    install_git_hooks()
+5 −2
Original line number Diff line number Diff line
import os

from pathlib import Path

# 3.10 and above
from platform import freedesktop_os_release
import shlex
@@ -58,8 +59,10 @@ def main():
    )
    with open(cidfile) as fp:
        cid = fp.read().strip()
    print(f"""ran test suite. to rerun, try: podman run -v {gitroot}:/srv/src -w /srv/src -it \
              $(podman container commit -q {cid}) tox -e upgrades""")
    print(
        f"""ran test suite. to rerun, try: podman run -v {gitroot}:/srv/src -w /srv/src -it \
              $(podman container commit -q {cid}) tox -e upgrades"""
    )
    print(f"otherwise delete container with: podman rm {cid}")