update_crate_tests: Remove cwd

Env.cwd is misleading as it captures the location of the crate/package
being updated. Move the detection logic to the Package class and rename
the attribute.

There is the expectation that before running soong_ui, cwd is set to
ANDROID_BUILD_TOP; and before running write_mapping, that cwd is set to
the package directory. Add comments to capture this.

Test: update_crate_tests.py in external/rust/crates/libc
Change-Id: I5c4f16afe0b92b7c85173e4149ee17dd39c773c2
This commit is contained in:
Thiébaud Weksteen
2021-06-10 13:30:20 +02:00
parent 2e532bb1c1
commit fc485b2384

View File

@@ -41,24 +41,13 @@ class UpdaterException(Exception):
class Env(object): class Env(object):
def __init__(self, path): def __init__(self):
try: try:
self.ANDROID_BUILD_TOP = os.environ['ANDROID_BUILD_TOP'] self.ANDROID_BUILD_TOP = os.environ['ANDROID_BUILD_TOP']
except KeyError: except KeyError:
raise UpdaterException('$ANDROID_BUILD_TOP is not defined; you ' raise UpdaterException('$ANDROID_BUILD_TOP is not defined; you '
'must first source build/envsetup.sh and ' 'must first source build/envsetup.sh and '
'select a target.') '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]
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): class Bazel(object):
@@ -68,6 +57,8 @@ class Bazel(object):
raise UpdaterException('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")
soong_ui = os.path.join(env.ANDROID_BUILD_TOP, "build", "soong", "soong_ui.bash") soong_ui = os.path.join(env.ANDROID_BUILD_TOP, "build", "soong", "soong_ui.bash")
# soong_ui requires to be at the root of the repository.
os.chdir(env.ANDROID_BUILD_TOP) os.chdir(env.ANDROID_BUILD_TOP)
print("Generating Bazel files...") print("Generating Bazel files...")
cmd = [soong_ui, "--make-mode", "GENERATE_BAZEL_FILES=1", "nothing"] cmd = [soong_ui, "--make-mode", "GENERATE_BAZEL_FILES=1", "nothing"]
@@ -82,7 +73,6 @@ class Bazel(object):
subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True) subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
raise UpdaterException('Unable to update TEST_MAPPING: ' + e.output) raise UpdaterException('Unable to update TEST_MAPPING: ' + e.output)
os.chdir(env.cwd)
# Return all modules for a given path. # Return all modules for a given path.
def query_modules(self, path): def query_modules(self, path):
@@ -125,8 +115,22 @@ class Bazel(object):
class Package(object): class Package(object):
def __init__(self, path, bazel): def __init__(self, path, env, bazel):
modules = bazel.query_modules(path) if path == None:
self.dir = os.getcwd()
else:
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.')
# Move to the package_directory.
os.chdir(self.dir)
modules = bazel.query_modules(self.dir_rel)
self.rdep_tests = bazel.query_rdep_tests(modules) self.rdep_tests = bazel.query_rdep_tests(modules)
def get_rdep_tests(self): def get_rdep_tests(self):
@@ -135,11 +139,12 @@ class Package(object):
class TestMapping(object): class TestMapping(object):
def __init__(self, path): def __init__(self, path):
self.env = Env(path) env = Env()
self.bazel = Bazel(self.env) bazel = Bazel(env)
self.package = Package(path, env, bazel)
def create(self): def create(self):
tests = Package(self.env.cwd_relative, self.bazel).get_rdep_tests() tests = self.package.get_rdep_tests()
if not bool(tests): if not bool(tests):
return return
test_mapping = self.tests_to_mapping(tests) test_mapping = self.tests_to_mapping(tests)
@@ -164,6 +169,7 @@ class TestMapping(object):
json_file.write("\n") json_file.write("\n")
print("TEST_MAPPING successfully updated!") print("TEST_MAPPING successfully updated!")
def main(): def main():
if len(sys.argv) == 2: if len(sys.argv) == 2:
path = sys.argv[1] path = sys.argv[1]