Merge "Generating api diff files for each module sdk." am: 6e31252b35 am: 67a4838ca0

Original change: https://android-review.googlesource.com/c/platform/packages/modules/common/+/2120161

Change-Id: I1ab37adbc23f2721e5fcbd35a11b00a69ca813bc
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Gurpreet Singh
2022-06-14 17:16:11 +00:00
committed by Automerger Merge Worker
2 changed files with 123 additions and 20 deletions

View File

@@ -250,6 +250,11 @@ def sdk_snapshot_info_file(snapshots_dir, sdk_name, sdk_version):
return os.path.join(snapshots_dir, f"{sdk_name}-{sdk_version}.info")
def sdk_snapshot_api_diff_file(snapshots_dir, sdk_name, sdk_version):
"""Get the path to the sdk snapshot api diff file."""
return os.path.join(snapshots_dir, f"{sdk_name}-{sdk_version}-api-diff.txt")
@dataclasses.dataclass()
class SnapshotBuilder:
"""Builds sdk snapshots"""
@@ -425,23 +430,35 @@ java_sdk_library_import {{
sdk_info_file_json = json.loads(sdk_info_file_object.read())
target_paths = []
target_dict = dict()
for jsonItem in sdk_info_file_json:
if not jsonItem["@type"] == "java_sdk_library":
continue
if not self.does_sdk_library_support_latest_api(jsonItem["@name"]):
sdk_library = jsonItem["@name"]
if not self.does_sdk_library_support_latest_api(sdk_library):
continue
target_dict[sdk_library] = dict()
for scope in jsonItem["scopes"]:
target_paths.append(jsonItem["scopes"][scope]["latest_api"])
target_paths.append(
jsonItem["scopes"][scope]["latest_removed_api"])
return target_paths
scope_json = jsonItem["scopes"][scope]
target_dict[sdk_library][scope] = dict()
target_list = [
"current_api", "latest_api", "removed_api",
"latest_removed_api"
]
for target in target_list:
target_dict[sdk_library][scope][target] = scope_json[target]
target_paths.append(scope_json["latest_api"])
target_paths.append(scope_json["latest_removed_api"])
return target_paths, target_dict
def build_sdk_scope_targets(self, build_release, sdk_version, modules):
# Build the latest scope targets for each module sdk
# Compute the paths to all the latest scope targets for each module sdk.
target_paths = []
target_dict = dict()
for module in modules:
for sdk in module.sdks:
if "host-exports" in sdk or "test-exports" in sdk:
@@ -449,8 +466,60 @@ java_sdk_library_import {{
sdk_info_file = sdk_snapshot_info_file(self.mainline_sdks_dir,
sdk, sdk_version)
target_paths.extend(self.latest_api_file_targets(sdk_info_file))
paths, dict_item = self.latest_api_file_targets(sdk_info_file)
target_paths.extend(paths)
target_dict[sdk_info_file] = dict_item
self.build_target_paths(build_release, sdk_version, target_paths)
return target_dict
def appendDiffToFile(self, file_object, sdk_zip_file, current_api,
latest_api, snapshots_dir):
"""Extract current api and find its diff with the latest api."""
with zipfile.ZipFile(sdk_zip_file, "r") as zipObj:
extracted_current_api = zipObj.extract(
member=current_api, path=snapshots_dir)
diff = subprocess.run(
["diff", "-u0", latest_api, extracted_current_api],
capture_output=True).stdout.decode("utf-8")
file_object.write(diff)
def create_snapshot_api_diff(self, sdk, sdk_version, target_dict,
snapshots_dir):
"""Creates api diff files for each module sdk.
For each module sdk, the scope targets are obtained for each java sdk
library and the api diff files are generated by performing a diff
operation between the current api file vs the latest api file.
"""
sdk_info_file = sdk_snapshot_info_file(snapshots_dir, sdk, sdk_version)
sdk_zip_file = sdk_snapshot_zip_file(snapshots_dir, sdk, sdk_version)
sdk_api_diff_file = sdk_snapshot_api_diff_file(snapshots_dir, sdk,
sdk_version)
with open(sdk_api_diff_file, "w") as sdk_api_diff_file_object:
for sdk_library in target_dict[sdk_info_file]:
for scope in target_dict[sdk_info_file][sdk_library]:
scope_json = target_dict[sdk_info_file][sdk_library][scope]
current_api = scope_json["current_api"]
latest_api = scope_json["latest_api"]
self.appendDiffToFile(sdk_api_diff_file_object,
sdk_zip_file, current_api, latest_api,
snapshots_dir)
removed_api = scope_json["removed_api"]
latest_removed_api = scope_json["latest_removed_api"]
self.appendDiffToFile(sdk_api_diff_file_object,
sdk_zip_file, removed_api,
latest_removed_api, snapshots_dir)
def build_snapshot_api_diff(self, sdk_version, modules, target_dict,
snapshots_dir):
"""For each module sdk, create the api diff file."""
for module in modules:
for sdk in module.sdks:
if "host-exports" in sdk or "test-exports" in sdk:
continue
self.create_snapshot_api_diff(sdk, sdk_version, target_dict,
snapshots_dir)
# A list of the sdk versions to build. Usually just current but can include a
@@ -1005,8 +1074,10 @@ class SdkDistProducer:
snapshots_dir = self.snapshot_builder.build_snapshots(
build_release, sdk_versions, modules)
if build_release == LATEST:
self.snapshot_builder.build_sdk_scope_targets(
build_release, sdk_versions[0], modules)
target_dict = self.snapshot_builder.build_sdk_scope_targets(
build_release, "current", modules)
self.snapshot_builder.build_snapshot_api_diff(
"current", modules, target_dict, snapshots_dir)
self.populate_unbundled_dist(build_release, sdk_versions, modules,
snapshots_dir)
return snapshots_dir
@@ -1019,6 +1090,18 @@ class SdkDistProducer:
build_release, sdk_versions, modules)
self.populate_bundled_dist(build_release, modules, snapshots_dir)
def dist_sdk_snapshot_api_diff(self, sdk_dist_dir, sdk, sdk_version, module,
snapshots_dir):
"""Copy the sdk snapshot api diff file to a dist directory."""
if "host-exports" in sdk or "test-exports" in sdk:
return
sdk_dist_subdir = os.path.join(sdk_dist_dir, module.apex, "sdk")
os.makedirs(sdk_dist_subdir, exist_ok=True)
sdk_api_diff_path = sdk_snapshot_api_diff_file(snapshots_dir, sdk,
sdk_version)
shutil.copy(sdk_api_diff_path, sdk_dist_subdir)
def populate_unbundled_dist(self, build_release, sdk_versions, modules,
snapshots_dir):
build_release_dist_dir = os.path.join(self.mainline_sdks_dir,
@@ -1026,10 +1109,12 @@ class SdkDistProducer:
for module in modules:
for sdk_version in sdk_versions:
for sdk in module.sdks:
# TODO(b/230609867): create API diff file for each module
# sdk.
sdk_dist_dir = os.path.join(build_release_dist_dir,
sdk_version)
if build_release == LATEST:
self.dist_sdk_snapshot_api_diff(sdk_dist_dir, sdk,
sdk_version, module,
snapshots_dir)
self.populate_dist_snapshot(build_release, module, sdk,
sdk_dist_dir, sdk_version,
snapshots_dir)
@@ -1064,7 +1149,7 @@ class SdkDistProducer:
and then a new zip file is created in the dist directory with the
original files replaced by the newly transformed files.
"""
os.makedirs(sdk_dist_dir)
os.makedirs(sdk_dist_dir, exist_ok=True)
dest_sdk_zip = os.path.join(sdk_dist_dir, os.path.basename(src_sdk_zip))
print(f"Copying sdk snapshot {src_sdk_zip} to {dest_sdk_zip}")