From 76c4e232ad8117ebd15fb635184f6cd2181f0406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thi=C3=A9baud=20Weksteen?= Date: Thu, 10 Jun 2021 07:35:19 +0200 Subject: [PATCH 1/3] update_crate_tests: Use subprocess.DEVNULL Test: update_crate_tests.py in external/rust/crates/libc Change-Id: I0beac6e7e4f795bf8581f9c6448968f721da4b75 --- scripts/update_crate_tests.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/scripts/update_crate_tests.py b/scripts/update_crate_tests.py index 53bfd83e0..9221626c2 100755 --- a/scripts/update_crate_tests.py +++ b/scripts/update_crate_tests.py @@ -76,27 +76,25 @@ class Bazel(object): # Return all modules for a given path. def query_modules(self, path): - with open(os.devnull, 'wb') as DEVNULL: - cmd = self.path() + " query --config=queryview /" + path + ":all" - out = subprocess.check_output(cmd, shell=True, stderr=DEVNULL, text=True).strip().split("\n") - modules = set() - for line in out: - # speed up by excluding unused modules. - if "windows_x86" in line: - continue - modules.add(line) - return modules + cmd = self.path() + " query --config=queryview /" + path + ":all" + out = subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, text=True).strip().split("\n") + modules = set() + for line in out: + # speed up by excluding unused modules. + if "windows_x86" in line: + continue + modules.add(line) + return modules # Return all reverse dependencies for a single module. def query_rdeps(self, module): - with open(os.devnull, 'wb') as DEVNULL: - cmd = (self.path() + " query --config=queryview \'rdeps(//..., " + - module + ")\' --output=label_kind") - out = (subprocess.check_output(cmd, shell=True, stderr=DEVNULL, text=True) - .strip().split("\n")) - if '' in out: - out.remove('') - return out + cmd = (self.path() + " query --config=queryview \'rdeps(//..., " + + module + ")\' --output=label_kind") + out = (subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, text=True) + .strip().split("\n")) + if '' in out: + out.remove('') + return out def exclude_module(self, module): for path in exclude_paths: From 3e32afc5a66f1e0591669f2f73c8df3625dc27af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thi=C3=A9baud=20Weksteen?= Date: Thu, 10 Jun 2021 07:56:06 +0200 Subject: [PATCH 2/3] update_crate_tests: Use prebuilt bazel Bug: 181953100 Test: update_crate_tests.py in external/rust/crates/libc Change-Id: I3c0477cf8d5aac0ae9e444d1b95b8670c9e2e588 --- scripts/update_crate_tests.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/scripts/update_crate_tests.py b/scripts/update_crate_tests.py index 9221626c2..38f50ceae 100755 --- a/scripts/update_crate_tests.py +++ b/scripts/update_crate_tests.py @@ -55,6 +55,9 @@ class Env(object): class Bazel(object): # set up the Bazel queryview def __init__(self, env): + if platform.system() != 'Linux': + sys.exit('ERROR: this script has only been tested on Linux.') + self.path = os.path.join(env.ANDROID_BUILD_TOP, "tools", "bazel") os.chdir(env.ANDROID_BUILD_TOP) print("Building Bazel Queryview. This can take a couple of minutes...") cmd = "./build/soong/soong_ui.bash --build-mode --all-modules --dir=. queryview" @@ -68,15 +71,9 @@ class Bazel(object): self.setup = False os.chdir(env.cwd) - def path(self): - # Only tested on Linux. - if platform.system() != 'Linux': - sys.exit('ERROR: this script has only been tested on Linux.') - return "/usr/bin/bazel" - # Return all modules for a given path. def query_modules(self, path): - cmd = self.path() + " query --config=queryview /" + path + ":all" + cmd = self.path + " query --config=queryview /" + path + ":all" out = subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, text=True).strip().split("\n") modules = set() for line in out: @@ -88,7 +85,7 @@ class Bazel(object): # Return all reverse dependencies for a single module. def query_rdeps(self, module): - cmd = (self.path() + " query --config=queryview \'rdeps(//..., " + + cmd = (self.path + " query --config=queryview \'rdeps(//..., " + module + ")\' --output=label_kind") out = (subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, text=True) .strip().split("\n")) From 5212f8a43f5374d5013fd434ff81ac1212efddca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thi=C3=A9baud=20Weksteen?= Date: Thu, 10 Jun 2021 08:18:32 +0200 Subject: [PATCH 3/3] update_crate_tests: Add init exceptions Normalize the object initializations by raising exceptions and catching them within main. Test: update_crate_tests.py in external/rust/crates/libc Change-Id: I6080fa7f22d740f5dd384c15ef74097e61dbb210 --- scripts/update_crate_tests.py | 43 ++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/scripts/update_crate_tests.py b/scripts/update_crate_tests.py index 38f50ceae..477c65d9b 100755 --- a/scripts/update_crate_tests.py +++ b/scripts/update_crate_tests.py @@ -35,40 +35,45 @@ exclude_paths = [ "//external/vm_tools" ] + +class UpdaterException(Exception): + pass + + class Env(object): def __init__(self, path): try: self.ANDROID_BUILD_TOP = os.environ['ANDROID_BUILD_TOP'] - except: - sys.exit('ERROR: this script must be run from an Android tree.') + except KeyError: + raise UpdaterException('$ANDROID_BUILD_TOP is not defined; you ' + 'must first source build/envsetup.sh and ' + 'select a target.') if path == None: self.cwd = os.getcwd() else: self.cwd = path try: self.cwd_relative = self.cwd.split(self.ANDROID_BUILD_TOP)[1] - self.setup = True - except: - # Mark setup as failed if a path to a rust crate is not provided. - self.setup = False + except IndexError: + raise UpdaterException('The path ' + self.cwd + ' is not under ' + + self.ANDROID_BUILD_TOP + '; You must be in the ' + 'directory of a crate or pass its absolute path ' + 'as first argument.') + class Bazel(object): # set up the Bazel queryview def __init__(self, env): if platform.system() != 'Linux': - sys.exit('ERROR: this script has only been tested on Linux.') + raise UpdaterException('This script has only been tested on Linux.') self.path = os.path.join(env.ANDROID_BUILD_TOP, "tools", "bazel") os.chdir(env.ANDROID_BUILD_TOP) print("Building Bazel Queryview. This can take a couple of minutes...") cmd = "./build/soong/soong_ui.bash --build-mode --all-modules --dir=. queryview" try: - out = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) - self.setup = True + subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - print("Error: Unable to update TEST_MAPPING due to the following build error:") - print(e.output) - # Mark setup as failed if the Bazel queryview fails to build. - self.setup = False + raise UpdaterException('Unable to update TEST_MAPPING: ' + e.output) os.chdir(env.cwd) # Return all modules for a given path. @@ -125,12 +130,7 @@ class TestMapping(object): self.env = Env(path) self.bazel = Bazel(self.env) - def is_setup(self): - return self.env.setup and self.bazel.setup - def create_test_mapping(self, path): - if not self.is_setup(): - return tests = self.get_tests(path) if not bool(tests): return @@ -168,10 +168,11 @@ def main(): path = sys.argv[1] else: path = None - test_mapping = TestMapping(path) + try: + test_mapping = TestMapping(path) + except UpdaterException as err: + sys.exit("Error: " + str(err)) test_mapping.create_test_mapping(None) - if not test_mapping.is_setup(): - raise ValueError('Error getting crate tests.') if __name__ == '__main__': main()