|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or https://opensource.org/license/mit/. |
| 5 | + |
| 6 | +import os |
| 7 | +import shlex |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import time |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def run(cmd, **kwargs): |
| 15 | + print("+ " + shlex.join(cmd), flush=True) |
| 16 | + kwargs.setdefault("check", True) |
| 17 | + try: |
| 18 | + return subprocess.run(cmd, **kwargs) |
| 19 | + except Exception as e: |
| 20 | + sys.exit(str(e)) |
| 21 | + |
| 22 | + |
| 23 | +def get_worktree_mounts(repo_root): |
| 24 | + git_path = repo_root / ".git" |
| 25 | + if not git_path.is_file(): |
| 26 | + return [] |
| 27 | + content = git_path.read_text().strip() |
| 28 | + if not content.startswith("gitdir: "): |
| 29 | + return [] |
| 30 | + gitdir = (repo_root / content.removeprefix("gitdir: ")).resolve() |
| 31 | + main_gitdir = gitdir.parent.parent |
| 32 | + return [ |
| 33 | + f"--volume={gitdir}:{gitdir}", |
| 34 | + f"--volume={main_gitdir}:{main_gitdir}", |
| 35 | + ] |
| 36 | + |
| 37 | + |
| 38 | +def main(): |
| 39 | + repo_root = Path(__file__).resolve().parent.parent |
| 40 | + is_ci = os.environ.get("GITHUB_ACTIONS") == "true" |
| 41 | + container = "bitcoin-linter" |
| 42 | + |
| 43 | + build_cmd = [ |
| 44 | + "docker", |
| 45 | + "buildx", |
| 46 | + "build", |
| 47 | + f"--tag={container}", |
| 48 | + *shlex.split(os.environ.get("DOCKER_BUILD_CACHE_ARG", "")), |
| 49 | + f"--file={repo_root}/ci/lint_imagefile", |
| 50 | + str(repo_root), |
| 51 | + ] |
| 52 | + if run(build_cmd, check=False).returncode != 0: |
| 53 | + if is_ci: |
| 54 | + print("Retry building image after failure") |
| 55 | + time.sleep(3) |
| 56 | + run(build_cmd) |
| 57 | + |
| 58 | + extra_env = [] |
| 59 | + if is_ci: |
| 60 | + if os.environ.get("GITHUB_EVENT_NAME") == "pull_request": |
| 61 | + extra_env = ["--env", "LINT_CI_IS_PR=1"] |
| 62 | + elif os.environ.get("GITHUB_REPOSITORY") == "bitcoin/bitcoin": |
| 63 | + extra_env = ["--env", "LINT_CI_SANITY_CHECK_COMMIT_SIG=1"] |
| 64 | + |
| 65 | + run( |
| 66 | + [ |
| 67 | + "docker", |
| 68 | + "run", |
| 69 | + "--rm", |
| 70 | + *extra_env, |
| 71 | + f"--volume={repo_root}:/bitcoin", |
| 72 | + *get_worktree_mounts(repo_root), |
| 73 | + *([] if is_ci else ["-it"]), |
| 74 | + container, |
| 75 | + *sys.argv[1:], |
| 76 | + ] |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
0 commit comments