From a370545a2c888c03b5a12152cbe84ec7679e7163 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 24 Jun 2019 15:33:41 -0700 Subject: [PATCH] releasetools: Make additional modules Python 3 compatible. Bug: 131631303 Test: `python -m unittest test_sign_target_files_apks` Test: `python3 -m unittest test_sign_target_files_apks` Test: `python -m unittest test_add_img_to_target_files` Test: `python3 -m unittest test_add_img_to_target_files` Test: `python -m unittest test_ota_from_target_files` Test: `python3 -m unittest test_ota_from_target_files` Test: `python -m unittest test_validate_target_files` Test: `python3 -m unittest test_validate_target_files` Test: Run `python3 ota_from_target_files.py` to generate an OTA. Test: Run `python3 sign_target_files_apks.py` to sign a target_files. Change-Id: I56b45bbcbf7aa83e690785a9640c0212e45d12d8 --- tools/releasetools/add_img_to_target_files.py | 18 ++++++------- tools/releasetools/common.py | 2 ++ tools/releasetools/ota_from_target_files.py | 2 +- tools/releasetools/sign_target_files_apks.py | 26 +++++++++---------- .../test_sign_target_files_apks.py | 6 ++--- .../test_validate_target_files.py | 2 +- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/tools/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py index 44719edaa..699443094 100755 --- a/tools/releasetools/add_img_to_target_files.py +++ b/tools/releasetools/add_img_to_target_files.py @@ -126,7 +126,7 @@ def GetCareMap(which, imgname): if not image_size: return None - image_blocks = int(image_size) / 4096 - 1 + image_blocks = int(image_size) // 4096 - 1 assert image_blocks > 0, "blocks for {} must be positive".format(which) # For sparse images, we will only check the blocks that are listed in the care @@ -154,16 +154,16 @@ def AddSystem(output_zip, recovery_img=None, boot_img=None): return img.name def output_sink(fn, data): - ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") - ofile.write(data) - ofile.close() + output_file = os.path.join(OPTIONS.input_tmp, "SYSTEM", fn) + with open(output_file, "wb") as ofile: + ofile.write(data) if output_zip: arc_name = "SYSTEM/" + fn if arc_name in output_zip.namelist(): OPTIONS.replace_updated_files_list.append(arc_name) else: - common.ZipWrite(output_zip, ofile.name, arc_name) + common.ZipWrite(output_zip, output_file, arc_name) if (OPTIONS.rebuild_recovery and recovery_img is not None and boot_img is not None): @@ -290,7 +290,7 @@ def AddDtbo(output_zip): def CreateImage(input_dir, info_dict, what, output_file, block_list=None): - logger.info("creating " + what + ".img...") + logger.info("creating %s.img...", what) image_props = build_image.ImagePropFromGlobalDict(info_dict, what) fstab = info_dict["fstab"] @@ -778,7 +778,7 @@ def AddImagesToTargetFiles(filename): partitions = dict() def banner(s): - logger.info("\n\n++++ " + s + " ++++\n\n") + logger.info("\n\n++++ %s ++++\n\n", s) boot_image = None if has_boot: @@ -901,7 +901,7 @@ def AddImagesToTargetFiles(filename): ab_partitions_txt = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt") if os.path.exists(ab_partitions_txt): - with open(ab_partitions_txt, 'r') as f: + with open(ab_partitions_txt) as f: ab_partitions = f.readlines() # For devices using A/B update, make sure we have all the needed images @@ -916,7 +916,7 @@ def AddImagesToTargetFiles(filename): pack_radioimages_txt = os.path.join( OPTIONS.input_tmp, "META", "pack_radioimages.txt") if os.path.exists(pack_radioimages_txt): - with open(pack_radioimages_txt, 'r') as f: + with open(pack_radioimages_txt) as f: AddPackRadioImages(output_zip, f.readlines()) if output_zip: diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py index 4bebef50b..f8cb7e9a9 100644 --- a/tools/releasetools/common.py +++ b/tools/releasetools/common.py @@ -54,7 +54,9 @@ class Options(object): base_search_path = os.path.join(base_out_path, os.path.basename(os.getcwd())) + # Python >= 3.3 returns 'linux', whereas Python 2.7 gives 'linux2'. platform_search_path = { + "linux": os.path.join(base_search_path, "host/linux-x86"), "linux2": os.path.join(base_search_path, "host/linux-x86"), "darwin": os.path.join(base_search_path, "host/darwin-x86"), } diff --git a/tools/releasetools/ota_from_target_files.py b/tools/releasetools/ota_from_target_files.py index 3442b27f2..0e8432710 100755 --- a/tools/releasetools/ota_from_target_files.py +++ b/tools/releasetools/ota_from_target_files.py @@ -1938,7 +1938,7 @@ def GetTargetFilesZipForRetrofitDynamicPartitions(input_file, target_file = common.MakeTempFile(prefix="targetfiles-", suffix=".zip") shutil.copyfile(input_file, target_file) - with zipfile.ZipFile(input_file, 'r') as input_zip: + with zipfile.ZipFile(input_file) as input_zip: namelist = input_zip.namelist() input_tmp = common.UnzipTemp(input_file, RETROFIT_DAP_UNZIP_PATTERN) diff --git a/tools/releasetools/sign_target_files_apks.py b/tools/releasetools/sign_target_files_apks.py index 668ad9ba3..4a7b34b3a 100755 --- a/tools/releasetools/sign_target_files_apks.py +++ b/tools/releasetools/sign_target_files_apks.py @@ -154,11 +154,11 @@ OPTIONS.avb_extra_args = {} def GetApkCerts(certmap): # apply the key remapping to the contents of the file - for apk, cert in certmap.iteritems(): + for apk, cert in certmap.items(): certmap[apk] = OPTIONS.key_map.get(cert, cert) # apply all the -e options, overriding anything in the file - for apk, cert in OPTIONS.extra_apks.iteritems(): + for apk, cert in OPTIONS.extra_apks.items(): if not cert: cert = "PRESIGNED" certmap[apk] = OPTIONS.key_map.get(cert, cert) @@ -519,14 +519,14 @@ def ProcessTargetFiles(input_tf_zip, output_tf_zip, misc_info, if stat.S_ISLNK(info.external_attr >> 16): new_data = data else: - new_data = RewriteProps(data) + new_data = RewriteProps(data.decode()) common.ZipWriteStr(output_tf_zip, out_info, new_data) # Replace the certs in *mac_permissions.xml (there could be multiple, such # as {system,vendor}/etc/selinux/{plat,nonplat}_mac_permissions.xml). elif filename.endswith("mac_permissions.xml"): print("Rewriting %s with new keys." % (filename,)) - new_data = ReplaceCerts(data) + new_data = ReplaceCerts(data.decode()) common.ZipWriteStr(output_tf_zip, out_info, new_data) # Ask add_img_to_target_files to rebuild the recovery patch if needed. @@ -630,17 +630,17 @@ def ReplaceCerts(data): Raises: AssertionError: On finding duplicate entries. """ - for old, new in OPTIONS.key_map.iteritems(): + for old, new in OPTIONS.key_map.items(): if OPTIONS.verbose: print(" Replacing %s.x509.pem with %s.x509.pem" % (old, new)) try: with open(old + ".x509.pem") as old_fp: old_cert16 = base64.b16encode( - common.ParseCertificate(old_fp.read())).lower() + common.ParseCertificate(old_fp.read())).decode().lower() with open(new + ".x509.pem") as new_fp: new_cert16 = base64.b16encode( - common.ParseCertificate(new_fp.read())).lower() + common.ParseCertificate(new_fp.read())).decode().lower() except IOError as e: if OPTIONS.verbose or e.errno != errno.ENOENT: print(" Error accessing %s: %s.\nSkip replacing %s.x509.pem with " @@ -858,7 +858,7 @@ def ReplaceVerityKeyId(input_zip, output_zip, key_path): writable. key_path: The path to the PEM encoded X.509 certificate. """ - in_cmdline = input_zip.read("BOOT/cmdline") + in_cmdline = input_zip.read("BOOT/cmdline").decode() # Copy in_cmdline to output_zip if veritykeyid is not present. if "veritykeyid" not in in_cmdline: common.ZipWriteStr(output_zip, "BOOT/cmdline", in_cmdline) @@ -891,7 +891,7 @@ def ReplaceMiscInfoTxt(input_zip, output_zip, misc_info): current in-memory dict contains additional items computed at runtime. """ misc_info_old = common.LoadDictionaryFromLines( - input_zip.read('META/misc_info.txt').split('\n')) + input_zip.read('META/misc_info.txt').decode().split('\n')) items = [] for key in sorted(misc_info): if key in misc_info_old: @@ -957,7 +957,7 @@ def BuildKeyMap(misc_info, key_mapping_options): def GetApiLevelAndCodename(input_tf_zip): - data = input_tf_zip.read("SYSTEM/build.prop") + data = input_tf_zip.read("SYSTEM/build.prop").decode() api_level = None codename = None for line in data.split("\n"): @@ -979,7 +979,7 @@ def GetApiLevelAndCodename(input_tf_zip): def GetCodenameToApiLevelMap(input_tf_zip): - data = input_tf_zip.read("SYSTEM/build.prop") + data = input_tf_zip.read("SYSTEM/build.prop").decode() api_level = None codenames = None for line in data.split("\n"): @@ -997,7 +997,7 @@ def GetCodenameToApiLevelMap(input_tf_zip): if codenames is None: raise ValueError("No ro.build.version.all_codenames in SYSTEM/build.prop") - result = dict() + result = {} for codename in codenames: codename = codename.strip() if codename: @@ -1021,7 +1021,7 @@ def ReadApexKeysInfo(tf_zip): key. """ keys = {} - for line in tf_zip.read("META/apexkeys.txt").split("\n"): + for line in tf_zip.read('META/apexkeys.txt').decode().split('\n'): line = line.strip() if not line: continue diff --git a/tools/releasetools/test_sign_target_files_apks.py b/tools/releasetools/test_sign_target_files_apks.py index e142ebb06..9a1d16311 100644 --- a/tools/releasetools/test_sign_target_files_apks.py +++ b/tools/releasetools/test_sign_target_files_apks.py @@ -136,7 +136,7 @@ name="apex.apexd_test_different_app.apex" public_key="system/apex/apexd/apexd_te ReplaceVerityKeyId(input_zip, output_zip, cert_file) with zipfile.ZipFile(output_file) as output_zip: - self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline')) + self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline').decode()) # Test with the second certificate. cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem') @@ -146,7 +146,7 @@ name="apex.apexd_test_different_app.apex" public_key="system/apex/apexd/apexd_te ReplaceVerityKeyId(input_zip, output_zip, cert_file) with zipfile.ZipFile(output_file) as output_zip: - self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline')) + self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline').decode()) def test_ReplaceVerityKeyId_no_veritykeyid(self): BOOT_CMDLINE = ( @@ -164,7 +164,7 @@ name="apex.apexd_test_different_app.apex" public_key="system/apex/apexd/apexd_te ReplaceVerityKeyId(input_zip, output_zip, None) with zipfile.ZipFile(output_file) as output_zip: - self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline')) + self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline').decode()) def test_ReplaceCerts(self): cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem') diff --git a/tools/releasetools/test_validate_target_files.py b/tools/releasetools/test_validate_target_files.py index 70e3b497a..0f0d773c6 100644 --- a/tools/releasetools/test_validate_target_files.py +++ b/tools/releasetools/test_validate_target_files.py @@ -98,7 +98,7 @@ class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase): with open(boot_image, 'r+b') as boot_fp: boot_fp.seek(-1, os.SEEK_END) last_byte = boot_fp.read(1) - last_byte = chr(255 - ord(last_byte)) + last_byte = bytes([255 - ord(last_byte)]) boot_fp.seek(-1, os.SEEK_END) boot_fp.write(last_byte)