forked from bazel-contrib/rules_python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplicate_ci
More file actions
Latest commit
executable file
·167 lines (137 loc) · 5.13 KB
/
Copy pathreplicate_ci
File metadata and controls
executable file
·167 lines (137 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
importargparse
importos
importshlex
importsubprocess
importsys
importyaml
defparse_args():
parser=argparse.ArgumentParser(
description="Replicate and emulate BazelCI job configurations from presubmit.yml."
)
parser.add_argument(
"job",
help="The key or name of the CI job to emulate (e.g., ubuntu_workspace).",
)
returnparser.parse_args()
defrun_cmd(cmd, cwd=None, env=None, shell=False):
ifshell:
cmd_str=cmdifisinstance(cmd, str) else" ".join(cmd)
else:
cmd_str=shlex.join(cmd) ifisinstance(cmd, list) elsestr(cmd)
print(f"\n🚀 Executing: {cmd_str}")
ifcwdandcwd!=os.getcwd():
print(f"📁 Directory: {cwd}")
ifenvand"USE_BAZEL_VERSION"inenv:
print(f"🔧 Bazel Version: {env['USE_BAZEL_VERSION']}")
res=subprocess.run(cmd, cwd=cwd, env=env, shell=shell)
ifres.returncode!=0:
print(
f"\n❌ Command failed with return code {res.returncode}: {cmd_str}",
file=sys.stderr,
)
returnFalse
returnTrue
defresolve_bazel_version(task_bazel):
ifnottask_bazelortask_bazel.startswith("${{"):
returnNone
returntask_bazel
defexecute_ci_job(job_key, task, repo_root):
job_name=task.get("name", job_key)
print(f"\n{'='*80}\n🎯 Replicating CI Job: {job_key} ('{job_name}')\n{'='*80}")
# Setup working directory
cwd=repo_root
if"working_directory"intask:
cwd=os.path.join(repo_root, task["working_directory"])
ifnotos.path.exists(cwd):
print(
f"❌ Error: working_directory '{task['working_directory']}' does not exist at '{cwd}'",
file=sys.stderr,
)
returnFalse
# Setup environment
env=os.environ.copy()
bzl_version=resolve_bazel_version(task.get("bazel"))
ifbzl_version:
env["USE_BAZEL_VERSION"] =bzl_version
# Execute pre-commands
is_windows=sys.platform.startswith("win")
pre_cmds=task.get("batch_commands"ifis_windowselse"shell_commands", [])
forpre_cmdinpre_cmds:
ifnotrun_cmd(pre_cmd, cwd=cwd, env=env, shell=True):
returnFalse
# Execute Build Targets
build_targets= [tfortintask.get("build_targets", []) ift!="--"]
ifbuild_targets:
build_flags=task.get("build_flags", [])
cmd= ["bazel", "build"] +build_flags+ ["--"] +build_targets
ifnotrun_cmd(cmd, cwd=cwd, env=env):
returnFalse
# Execute Test Targets
test_targets= [tfortintask.get("test_targets", []) ift!="--"]
iftest_targets:
test_flags=task.get("test_flags", [])
if"--build_tests_only"notintest_flags:
test_flags= ["--build_tests_only"] +test_flags
cmd= ["bazel", "test"] +test_flags+ ["--"] +test_targets
ifnotrun_cmd(cmd, cwd=cwd, env=env):
returnFalse
# Execute Coverage Targets
coverage_targets= [tfortintask.get("coverage_targets", []) ift!="--"]
ifcoverage_targets:
coverage_flags=task.get("test_flags", [])
cmd= ["bazel", "coverage"] +coverage_flags+ ["--"] +coverage_targets
ifnotrun_cmd(cmd, cwd=cwd, env=env):
returnFalse
print(f"\n🎉 Successfully replicated CI Job: {job_key}")
returnTrue
defmain():
args=parse_args()
repo_root=os.path.abspath(os.path.dirname(__file__))
presubmit_path=os.path.join(repo_root, ".bazelci/presubmit.yml")
ifnotos.path.exists(presubmit_path):
print(
f"❌ Error: Presubmit file not found at '{presubmit_path}'",
file=sys.stderr,
)
sys.exit(1)
withopen(presubmit_path) asf:
presubmit=yaml.safe_load(f)
tasks=presubmit.get("tasks", {})
ifnottasks:
print(
f"❌ Error: No tasks found in '{presubmit_path}'",
file=sys.stderr,
)
sys.exit(1)
# If no job specified, print available jobs and exit
ifnotargs.job:
print("❌ Error: No CI job specified. Provide a job key.\n", file=sys.stderr)
print("📋 Available CI Job Keys:", file=sys.stderr)
forkeyinsorted(tasks.keys()):
name=tasks[key].get("name", key)
print(f" • {key} ({name})", file=sys.stderr)
sys.exit(1)
# Match by key or by name
job_key=None
ifargs.jobintasks:
job_key=args.job
else:
forkey, configintasks.items():
ifconfig.get("name") ==args.job:
job_key=key
break
ifnotjob_key:
print(
f"❌ Error: CI job '{args.job}' not found in '{presubmit_path}'\n",
file=sys.stderr,
)
print("📋 Available CI Job Keys:", file=sys.stderr)
forkeyinsorted(tasks.keys()):
name=tasks[key].get("name", key)
print(f" • {key} ({name})", file=sys.stderr)
sys.exit(1)
success=execute_ci_job(job_key, tasks[job_key], repo_root)
sys.exit(0ifsuccesselse1)
if__name__=="__main__":
main()