From 48e7f7d7d0f13f3521f463d05e7e9840b79f1242 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Thu, 17 Mar 2022 17:39:57 +0000 Subject: [PATCH] Decouple population of the dist dir from the SnapshotBuilder Previously, the code for populating the dist directory, e.g. populate_dist and create_legacy_dist_structures assumed that the sdk snapshot files they needed could be found in the directory referenced by SnapshotBuilder.mainline_sdks_dir. This change removes that assumption and instead passes the snapshot_dirs directory into the populate_dist method and uses it in the create_legacy_dist_structures function. This refactoring is needed to allow a follow up change to construct a set of sdk snapshot files in a different directory while still using the populate_dist method to copy them into the correct location in the dist directory. Bug: 218685706 Test: atest --host mainline_modules_sdks_test packages/modules/common/build/mainline_modules_sdks.sh pyformat -s 4 --force_quote_type double -i build/mainline_modules_sdks*.py /usr/bin/pylint --rcfile $ANDROID_BUILD_TOP/tools/repohooks/tools/pylintrc build/mainline_modules_sdks*.py Change-Id: I9fb659df28d7688b3f081bf84f5fae8ee85dc534 --- build/mainline_modules_sdks.py | 39 ++++++++++++++++++----------- build/mainline_modules_sdks_test.py | 9 ++++--- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/build/mainline_modules_sdks.py b/build/mainline_modules_sdks.py index 86038fe..5a02dae 100755 --- a/build/mainline_modules_sdks.py +++ b/build/mainline_modules_sdks.py @@ -197,6 +197,11 @@ class SubprocessRunner: *args, check=True, stdout=self.stdout, stderr=self.stderr, **kwargs) +def sdk_snapshot_zip_file(snapshots_dir, sdk_name, sdk_version): + """Get the path to the sdk snapshot zip file.""" + return os.path.join(snapshots_dir, f"{sdk_name}-{sdk_version}.zip") + + @dataclasses.dataclass() class SnapshotBuilder: """Builds sdk snapshots""" @@ -207,13 +212,16 @@ class SnapshotBuilder: # The OUT_DIR environment variable. out_dir: str - def get_mainline_sdks_path(self): - """Get the path to the Soong mainline-sdks directory""" - return os.path.join(self.out_dir, "soong/mainline-sdks") + # The out/soong/mainline-sdks directory. + mainline_sdks_dir: str = "" + + def __post_init__(self): + self.mainline_sdks_dir = os.path.join(self.out_dir, + "soong/mainline-sdks") def get_sdk_path(self, sdk_name, sdk_version): """Get the path to the sdk snapshot zip file produced by soong""" - return os.path.join(self.get_mainline_sdks_path(), + return os.path.join(self.mainline_sdks_dir, f"{sdk_name}-{sdk_version}.zip") def build_snapshots(self, build_release, sdk_versions, modules): @@ -222,7 +230,7 @@ class SnapshotBuilder: # Compute the paths to all the Soong generated sdk snapshot files # required by this script. paths = [ - self.get_sdk_path(sdk, sdk_version) + sdk_snapshot_zip_file(self.mainline_sdks_dir, sdk, sdk_version) for module in modules for sdk in module.sdks ] @@ -261,6 +269,7 @@ class SnapshotBuilder: env = os.environ.copy() env.update(extraEnv) self.subprocess_runner.run(cmd, env=env) + return self.mainline_sdks_dir # A list of the sdk versions to build. Usually just current but can include a @@ -353,7 +362,8 @@ def create_legacy_dist_structures(build_release: BuildRelease, producer: "SdkDistProducer", modules: List["MainlineModule"]): """Creates legacy file structures.""" - producer.produce_dist_for_build_release(build_release, modules) + snapshots_dir = producer.produce_dist_for_build_release( + build_release, modules) # Create the out/dist/mainline-sdks/stubs structure. # TODO(b/199759953): Remove stubs once it is no longer used by gantry. @@ -369,8 +379,7 @@ def create_legacy_dist_structures(build_release: BuildRelease, # If the sdk's name ends with -sdk then extract sdk library # related files from its zip file. if sdk.endswith("-sdk"): - sdk_file = producer.snapshot_builder.get_sdk_path( - sdk, "current") + sdk_file = sdk_snapshot_zip_file(snapshots_dir, sdk, "current") extract_matching_files_from_zip(sdk_file, dest_dir, sdk_library_files_pattern()) @@ -596,11 +605,13 @@ class SdkDistProducer: def produce_dist_for_build_release(self, build_release, modules): sdk_versions = build_release.sdk_versions - self.snapshot_builder.build_snapshots(build_release, sdk_versions, - modules) - self.populate_dist(build_release, sdk_versions, modules) + snapshots_dir = self.snapshot_builder.build_snapshots( + build_release, sdk_versions, modules) + self.populate_dist(build_release, sdk_versions, modules, snapshots_dir) + return snapshots_dir - def populate_dist(self, build_release, sdk_versions, modules): + def populate_dist(self, build_release, sdk_versions, modules, + snapshots_dir): build_release_dist_dir = os.path.join(self.mainline_sdks_dir, build_release.sub_dir) @@ -618,8 +629,8 @@ class SdkDistProducer: sdk_dist_dir = os.path.join(build_release_dist_dir, sdk_version, apex, subdir) - sdk_path = self.snapshot_builder.get_sdk_path( - sdk, sdk_version) + sdk_path = sdk_snapshot_zip_file(snapshots_dir, sdk, + sdk_version) self.dist_sdk_snapshot_zip(sdk_path, sdk_dist_dir, module.transformations()) diff --git a/build/mainline_modules_sdks_test.py b/build/mainline_modules_sdks_test.py index 3560367..38a6e9e 100644 --- a/build/mainline_modules_sdks_test.py +++ b/build/mainline_modules_sdks_test.py @@ -41,8 +41,8 @@ class FakeSnapshotBuilder(mm.SnapshotBuilder): z.writestr(f"sdk_library/public/{name}-stubs.jar", "") z.writestr(f"sdk_library/public/{name}.txt", "") - def create_snapshot_file(self, name, version): - zip_file = Path(self.get_sdk_path(name, version)) + def create_snapshot_file(self, out_dir, name, version): + zip_file = Path(mm.sdk_snapshot_zip_file(out_dir, name, version)) with zipfile.ZipFile(zip_file, "w") as z: z.writestr("Android.bp", "") if name.endswith("-sdk"): @@ -50,13 +50,14 @@ class FakeSnapshotBuilder(mm.SnapshotBuilder): def build_snapshots(self, build_release, sdk_versions, modules): # Create input file structure. - sdks_out_dir = Path(self.get_mainline_sdks_path()) + sdks_out_dir = Path(self.mainline_sdks_dir).joinpath("test") sdks_out_dir.mkdir(parents=True, exist_ok=True) # Create a fake sdk zip file for each module. for module in modules: for sdk in module.sdks: for sdk_version in sdk_versions: - self.create_snapshot_file(sdk, sdk_version) + self.create_snapshot_file(sdks_out_dir, sdk, sdk_version) + return sdks_out_dir class TestProduceDist(unittest.TestCase):