lint.py raw
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 "--platform=linux",
48 f"--tag={container}",
49 *shlex.split(os.environ.get("DOCKER_BUILD_CACHE_ARG", "")),
50 f"--file={repo_root}/ci/lint_imagefile",
51 str(repo_root),
52 ]
53 if run(build_cmd, check=False).returncode != 0:
54 if is_ci:
55 print("Retry building image after failure")
56 time.sleep(3)
57 run(build_cmd)
58
59 extra_env = []
60 if is_ci:
61 if os.environ.get("GITHUB_EVENT_NAME") == "pull_request":
62 extra_env = ["--env", "LINT_CI_IS_PR=1"]
63 elif os.environ.get("GITHUB_REPOSITORY") == "bitcoin/bitcoin":
64 extra_env = ["--env", "LINT_CI_SANITY_CHECK_COMMIT_SIG=1"]
65
66 run(
67 [
68 "docker",
69 "run",
70 "--rm",
71 *extra_env,
72 f"--volume={repo_root}:/bitcoin",
73 *get_worktree_mounts(repo_root),
74 *([] if is_ci else ["-it"]),
75 container,
76 "./ci/lint/06_script.sh",
77 *sys.argv[1:],
78 ]
79 )
80
81
82 if __name__ == "__main__":
83 main()
84