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
This commit is contained in:
@@ -35,40 +35,45 @@ exclude_paths = [
|
|||||||
"//external/vm_tools"
|
"//external/vm_tools"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class UpdaterException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Env(object):
|
class Env(object):
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
try:
|
try:
|
||||||
self.ANDROID_BUILD_TOP = os.environ['ANDROID_BUILD_TOP']
|
self.ANDROID_BUILD_TOP = os.environ['ANDROID_BUILD_TOP']
|
||||||
except:
|
except KeyError:
|
||||||
sys.exit('ERROR: this script must be run from an Android tree.')
|
raise UpdaterException('$ANDROID_BUILD_TOP is not defined; you '
|
||||||
|
'must first source build/envsetup.sh and '
|
||||||
|
'select a target.')
|
||||||
if path == None:
|
if path == None:
|
||||||
self.cwd = os.getcwd()
|
self.cwd = os.getcwd()
|
||||||
else:
|
else:
|
||||||
self.cwd = path
|
self.cwd = path
|
||||||
try:
|
try:
|
||||||
self.cwd_relative = self.cwd.split(self.ANDROID_BUILD_TOP)[1]
|
self.cwd_relative = self.cwd.split(self.ANDROID_BUILD_TOP)[1]
|
||||||
self.setup = True
|
except IndexError:
|
||||||
except:
|
raise UpdaterException('The path ' + self.cwd + ' is not under ' +
|
||||||
# Mark setup as failed if a path to a rust crate is not provided.
|
self.ANDROID_BUILD_TOP + '; You must be in the '
|
||||||
self.setup = False
|
'directory of a crate or pass its absolute path '
|
||||||
|
'as first argument.')
|
||||||
|
|
||||||
|
|
||||||
class Bazel(object):
|
class Bazel(object):
|
||||||
# set up the Bazel queryview
|
# set up the Bazel queryview
|
||||||
def __init__(self, env):
|
def __init__(self, env):
|
||||||
if platform.system() != 'Linux':
|
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")
|
self.path = os.path.join(env.ANDROID_BUILD_TOP, "tools", "bazel")
|
||||||
os.chdir(env.ANDROID_BUILD_TOP)
|
os.chdir(env.ANDROID_BUILD_TOP)
|
||||||
print("Building Bazel Queryview. This can take a couple of minutes...")
|
print("Building Bazel Queryview. This can take a couple of minutes...")
|
||||||
cmd = "./build/soong/soong_ui.bash --build-mode --all-modules --dir=. queryview"
|
cmd = "./build/soong/soong_ui.bash --build-mode --all-modules --dir=. queryview"
|
||||||
try:
|
try:
|
||||||
out = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
|
subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
|
||||||
self.setup = True
|
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print("Error: Unable to update TEST_MAPPING due to the following build error:")
|
raise UpdaterException('Unable to update TEST_MAPPING: ' + e.output)
|
||||||
print(e.output)
|
|
||||||
# Mark setup as failed if the Bazel queryview fails to build.
|
|
||||||
self.setup = False
|
|
||||||
os.chdir(env.cwd)
|
os.chdir(env.cwd)
|
||||||
|
|
||||||
# Return all modules for a given path.
|
# Return all modules for a given path.
|
||||||
@@ -125,12 +130,7 @@ class TestMapping(object):
|
|||||||
self.env = Env(path)
|
self.env = Env(path)
|
||||||
self.bazel = Bazel(self.env)
|
self.bazel = Bazel(self.env)
|
||||||
|
|
||||||
def is_setup(self):
|
|
||||||
return self.env.setup and self.bazel.setup
|
|
||||||
|
|
||||||
def create_test_mapping(self, path):
|
def create_test_mapping(self, path):
|
||||||
if not self.is_setup():
|
|
||||||
return
|
|
||||||
tests = self.get_tests(path)
|
tests = self.get_tests(path)
|
||||||
if not bool(tests):
|
if not bool(tests):
|
||||||
return
|
return
|
||||||
@@ -168,10 +168,11 @@ def main():
|
|||||||
path = sys.argv[1]
|
path = sys.argv[1]
|
||||||
else:
|
else:
|
||||||
path = None
|
path = None
|
||||||
|
try:
|
||||||
test_mapping = TestMapping(path)
|
test_mapping = TestMapping(path)
|
||||||
|
except UpdaterException as err:
|
||||||
|
sys.exit("Error: " + str(err))
|
||||||
test_mapping.create_test_mapping(None)
|
test_mapping.create_test_mapping(None)
|
||||||
if not test_mapping.is_setup():
|
|
||||||
raise ValueError('Error getting crate tests.')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user