From 8e332ff3a76c1f90db1e2e71dbc0ef9f1b2d8bd2 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Wed, 29 Jul 2020 17:51:55 -0700 Subject: [PATCH] Add SetHostToolLocation. Add common.SetHostToolLocation so that, when calling various common.Run* functions, replace the first argument with previously set paths. This is needed when the script is executed in a Soong sandbox. Bug: 161563386 Test: pass Change-Id: If3b40b518fc7fe068677d39d604e3f6578a12ea3 --- tools/releasetools/common.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py index 32864ab2c..5dfd12b85 100644 --- a/tools/releasetools/common.py +++ b/tools/releasetools/common.py @@ -96,6 +96,7 @@ class Options(object): self.cache_size = None self.stash_threshold = 0.8 self.logfile = None + self.host_tools = {} OPTIONS = Options() @@ -213,6 +214,10 @@ def InitLogging(): logging.config.dictConfig(config) +def SetHostToolLocation(tool_name, location): + OPTIONS.host_tools[tool_name] = location + + def Run(args, verbose=None, **kwargs): """Creates and returns a subprocess.Popen object. @@ -234,6 +239,14 @@ def Run(args, verbose=None, **kwargs): kwargs['stderr'] = subprocess.STDOUT if 'universal_newlines' not in kwargs: kwargs['universal_newlines'] = True + + # If explicitly set host tool location before, use that location to avoid + # PATH violation. Make a copy of args in case client relies on the content + # of args later. + if args and args[0] in OPTIONS.host_tools: + args = args[:] + args[0] = OPTIONS.host_tools[args[0]] + # Don't log any if caller explicitly says so. if verbose: logger.info(" Running: \"%s\"", " ".join(args))