Allow passing multiple crates to update_crate_tests.py

Currently the script can only run on a single crate at a time, and
each time it runs it re-initializes Bazel. By passing multiple crates
we can save time by only initializing Bazel once.

Test: Call with zero, one, and multiple crate arguments.
Change-Id: Ic83c16d87066a8555b736b35bc7971586ee26e16
This commit is contained in:
Joel Galenson
2021-06-17 14:59:15 -07:00
parent 6105088a62
commit 177910448c

View File

@@ -181,17 +181,14 @@ class Package(object):
UpdaterException: the package does not appear to belong to the
current repository.
"""
if path == None:
self.dir = os.getcwd()
else:
self.dir = path
self.dir = path
try:
self.dir_rel = self.dir.split(env.ANDROID_BUILD_TOP)[1]
except IndexError:
raise UpdaterException('The path ' + self.dir + ' is not under ' +
env.ANDROID_BUILD_TOP + '; You must be in the '
'directory of a crate or pass its absolute path '
'as first argument.')
'as the argument.')
# Move to the package_directory.
os.chdir(self.dir)
@@ -208,14 +205,14 @@ class TestMapping(object):
Attributes:
package: The package associated with this TEST_MAPPING file.
"""
def __init__(self, path):
def __init__(self, env, bazel, path):
"""Constructor.
Args:
env: An instance of Env.
bazel: An instance of Bazel.
path: The absolute path to the package.
"""
env = Env()
bazel = Bazel(env)
self.package = Package(path, env, bazel)
def create(self):
@@ -245,19 +242,22 @@ class TestMapping(object):
json_file.write("// Generated by update_crate_tests.py for tests that depend on this crate.\n")
json.dump(test_mapping, json_file, indent=2, separators=(',', ': '), sort_keys=True)
json_file.write("\n")
print("TEST_MAPPING successfully updated!")
print("TEST_MAPPING successfully updated for %s!" % self.package.dir_rel)
def main():
if len(sys.argv) == 2:
path = sys.argv[1]
if len(sys.argv) > 1:
paths = sys.argv[1:]
else:
path = None
try:
test_mapping = TestMapping(path)
except UpdaterException as err:
sys.exit("Error: " + str(err))
test_mapping.create()
paths = [os.getcwd()]
env = Env()
bazel = Bazel(env)
for path in paths:
try:
test_mapping = TestMapping(env, bazel, path)
except UpdaterException as err:
sys.exit("Error: " + str(err))
test_mapping.create()
if __name__ == '__main__':
main()