summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYifan Hong <elsk@google.com>2024-04-18 16:15:25 -0700
committerTreehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com>2024-04-30 02:43:34 +0000
commit219ce6e0ecd6bc7be4c9ccb63b50721f7283f9fb (patch)
treea45bdd5718dae5df3f5d53366649e87708383d07
parente54ff4993b26562cc4735331421661b303e12fac (diff)
downloadbuild-main-riscv64.tar.gz
kleaf: refactor BazelWrapper.run()main-riscv64
Keep the function simple by moving the various predicates like run_as_subprocess, filter_regex, epilog_coro to their own function. Bug: 234125794 Test: TH Change-Id: I77572a4e23dc729e3f71e789a66c4c42d93ac609
-rwxr-xr-xkleaf/bazel.py61
1 files changed, 35 insertions, 26 deletions
diff --git a/kleaf/bazel.py b/kleaf/bazel.py
index ff8c973..4285153 100755
--- a/kleaf/bazel.py
+++ b/kleaf/bazel.py
@@ -525,32 +525,41 @@ class BazelWrapper(KleafHelpPrinter):
if self.known_startup_options.help or self.command == "help":
self._print_help()
- # Whether to run bazel comamnd as subprocess
- run_as_subprocess = False
- # Regex to filter output / stderr lines
- filter_regex = None
- # Epilog coroutine after bazel command finishes
- epilog_coro = None
-
- if self.known_args.strip_execroot:
- run_as_subprocess = True
- if self.absolute_user_root.is_relative_to(self.absolute_out_dir):
- filter_regex = re.compile(
- str(self.absolute_out_dir) + r"/\S+?/execroot/__main__/")
- else:
- filter_regex = re.compile(
- str(self.absolute_user_root) + r"/\S+?/execroot/__main__/")
-
- if self.command == "clean":
- run_as_subprocess = True
- epilog_coro = self.remove_gen_bazelrc_dir()
-
- if run_as_subprocess:
+ if self._should_run_as_subprocess():
import asyncio
- asyncio.run(run(final_args, self.env, filter_regex, epilog_coro))
+ asyncio.run(run(
+ command=final_args,
+ env=self.env,
+ filter_regex=self._get_output_filter_regex(),
+ epilog_coroutine=self._get_epilog_coroutine(),
+ ))
else:
os.execve(path=self.bazel_path, argv=final_args, env=self.env)
+ def _should_run_as_subprocess(self):
+ """Returns whether to run bazel command as subprocess"""
+ return any([
+ self.known_args.strip_execroot,
+ self.command == "clean",
+ ])
+
+ def _get_output_filter_regex(self):
+ """Returns regex to filter output / stderr lines"""
+ if not self.known_args.strip_execroot:
+ return None
+ if self.absolute_user_root.is_relative_to(self.absolute_out_dir):
+ prefix = str(self.absolute_out_dir)
+ else:
+ prefix = str(self.absolute_user_root)
+
+ return re.compile(prefix + r"/\S+?/execroot/__main__/")
+
+ def _get_epilog_coroutine(self):
+ """Returns epilog coroutine after bazel command finishes"""
+ if self.command != "clean":
+ return None
+ return self.remove_gen_bazelrc_dir()
+
async def remove_gen_bazelrc_dir(self):
sys.stderr.write("INFO: Deleting generated bazelrc directory.\n")
shutil.rmtree(self.gen_bazelrc_dir, ignore_errors=True)
@@ -569,12 +578,12 @@ async def output_filter(input_stream, output_stream, filter_regex):
output_stream.flush()
-async def run(command, env, filter_regex, epilog_coro):
+async def run(command, env, filter_regex, epilog_coroutine):
"""Runs command with env asynchronously.
Outputs are filtered with filter_regex if it is not None.
- At the end, run the coroutine epilog_coro if it is not None.
+ At the end, run the coroutine epilog_coroutine if it is not None.
"""
import asyncio
process = await asyncio.create_subprocess_exec(
@@ -589,8 +598,8 @@ async def run(command, env, filter_regex, epilog_coro):
output_filter(process.stdout, sys.stdout, filter_regex),
)
await process.wait()
- if epilog_coro:
- await epilog_coro
+ if epilog_coroutine:
+ await epilog_coroutine
if __name__ == "__main__":