From 184d5a3904b83209793dbf1df4415ea7d4a78015 Mon Sep 17 00:00:00 2001 From: Charly Delay Date: Sat, 20 Apr 2024 23:11:15 +0900 Subject: [PATCH] Update release script to work directly in `bkb-master` --- bastardkb_build_releases.py | 63 +++++++++++++------------------------ requirements.txt | 3 ++ 2 files changed, 24 insertions(+), 42 deletions(-) create mode 100644 requirements.txt diff --git a/bastardkb_build_releases.py b/bastardkb_build_releases.py index d640c23ca575..7a863bd71c83 100644 --- a/bastardkb_build_releases.py +++ b/bastardkb_build_releases.py @@ -16,10 +16,10 @@ from logging.handlers import RotatingFileHandler from operator import iconcat from pathlib import Path, PurePath +from pathvalidate import sanitize_filename from pygit2 import ( GitError, Repository, - Worktree, ) from rich.console import Console, Group from rich.live import Live @@ -125,18 +125,6 @@ class FirmwareList(NamedTuple): # Build the `via` keymap for the Dilemma 3x5_3 and 4x6_4. Firmware(keyboard="dilemma/3x5_3", keymap="via", keymap_alias="vendor"), Firmware(keyboard="dilemma/4x6_4", keymap="via", keymap_alias="vendor"), - # Build the `manna-harbour_miryoku` keymap for compatible keyboards. - *tuple( - Firmware( - keyboard=keyboard, - keyboard_alias=keyboard_alias, - keymap="manna-harbour_miryoku", - keymap_alias="miryoku", - env_vars=( - "MIRYOKU_ALPHAS=QWERTY", - "MIRYOKU_EXTRA=COLEMAKDH", - ), - ) for keyboard, keyboard_alias in MIRYOKU_COMPATIBLE_KEYBOARDS), ), ),) @@ -167,7 +155,7 @@ def __init__(self, verbose: bool): self._progress_status = lambda _: None def log_file(self, basename: str) -> Path: - return Path(self.log_dir, basename).with_suffix(".log") + return Path(self.log_dir, sanitize_filename(basename)).with_suffix(".log") def set_progress_status(self, progress_status: Callable[[str], None]) -> None: self._progress_status = progress_status @@ -216,39 +204,34 @@ def __init__(self, reporter: Reporter, repository: Repository, dry_run: bool, self.reporter = reporter self.repository = repository - def git_ensure_worktree(self, branch: str, - update_submodules: bool) -> Worktree: + def git_prepare_branch(self, branch: str, update_submodules: bool) -> Path: self.reporter.progress_status( f"Checking out [bright_magenta]{branch}[/bright_magenta]…") + if branch not in self.repository.branches: + # If the branch is not available locally, fetch it. + branch = 'origin/' + branch try: - worktree = self.repository.lookup_worktree(branch) - if worktree is None: - raise GitError + branch_ref = self.repository.branches[branch] except GitError: - self.reporter.error(f"Worktree does not exist: {branch}") + self.reporter.error(f"Failed to checkout: {branch}") sys.exit(1) if not self.dry_run: - # TODO: checkout worktree if it does not exist. - # self.repository.checkout(branch_ref) + self.repository.checkout(branch_ref) if update_submodules: self.reporter.progress_status( - f"([bright_magenta]{worktree.name}[/bright_magenta]) Updating submodules…" - ) + f"([bright_magenta]{branch}[/bright_magenta]) Updating submodules…") # TODO: use pygit2 to update submodules. self._run( ("git", "submodule", "update", "--init", "--recursive"), - log_file=self.reporter.log_file( - f"git-submodule-update-{worktree.name}"), - cwd=worktree.path, + log_file=self.reporter.log_file(f"git-submodule-update-{branch}"), + cwd=self.repository.workdir, ) else: self.reporter.progress_status( - f"([bright_magenta]{worktree.name}[/bright_magenta]) Updating submodules…" - ) - return worktree + f"([bright_magenta]{branch}[/bright_magenta]) Updating submodules…") + return self.repository.workdir - def qmk_compile(self, firmware: Firmware, - worktree: Worktree) -> QmkCompletedProcess: + def qmk_compile(self, firmware: Firmware) -> QmkCompletedProcess: self.reporter.progress_status( f"Compiling [bold white]{firmware}[/bold white]") argv = ( @@ -269,7 +252,7 @@ def qmk_compile(self, firmware: Firmware, ) log_file = self.reporter.log_file(f"qmk-compile-{firmware.output_filename}") return QmkCompletedProcess( - self._run(argv, log_file=log_file, cwd=worktree.path), log_file) + self._run(argv, log_file=log_file, cwd=self.repository.workdir), log_file) def _run( self, @@ -278,6 +261,7 @@ def _run( **kwargs, ) -> subprocess.CompletedProcess: self.reporter.debug(f"exec: {shlex.join(argv)}") + self.reporter.debug(f"cwd: {kwargs.get('cwd')}") self.reporter.debug(f"output: {log_file}") if not self.dry_run: with log_file.open("w") as fd: @@ -356,16 +340,15 @@ def build( reporter.info( f" Building off branch [magenta]{branch}[/] ({len(configurations)} firmwares)" ) - worktree = executor.git_ensure_worktree(branch, update_submodules=True) + path = executor.git_prepare_branch(branch, update_submodules=True) # Build firmwares off that branch. for firmware in configurations: - completed_process = executor.qmk_compile(firmware, worktree) + completed_process = executor.qmk_compile(firmware) if completed_process.returncode == 0: try: - on_firmware_compiled(worktree.path / - read_firmware_filename_from_logs( - firmware, completed_process.log_file)) + on_firmware_compiled(path / read_firmware_filename_from_logs( + firmware, completed_process.log_file)) built_firmware_count += 1 reporter.info(f" [not bold white]{firmware}[/] [green]ok[/]") except FileNotFoundError: @@ -456,10 +439,6 @@ def main() -> None: reporter.error("Failed to initialize QMK repository") sys.exit(1) - if not repository.is_bare: - reporter.error(f"Repository must be bare: {repository}") - sys.exit(1) - # Create output dir if needed. try: cmdline_args.output_dir.mkdir(parents=True, exist_ok=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000000..001c56a55be1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pathvalidate +pygit2 +rich