Merge "releasetools: Make additional modules Python 3 compatible."

This commit is contained in:
Tao Bao
2019-06-27 23:52:50 +00:00
committed by Gerrit Code Review
6 changed files with 29 additions and 27 deletions

View File

@@ -126,7 +126,7 @@ def GetCareMap(which, imgname):
if not image_size: if not image_size:
return None 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) 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 # 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 return img.name
def output_sink(fn, data): def output_sink(fn, data):
ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") output_file = os.path.join(OPTIONS.input_tmp, "SYSTEM", fn)
ofile.write(data) with open(output_file, "wb") as ofile:
ofile.close() ofile.write(data)
if output_zip: if output_zip:
arc_name = "SYSTEM/" + fn arc_name = "SYSTEM/" + fn
if arc_name in output_zip.namelist(): if arc_name in output_zip.namelist():
OPTIONS.replace_updated_files_list.append(arc_name) OPTIONS.replace_updated_files_list.append(arc_name)
else: 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 if (OPTIONS.rebuild_recovery and recovery_img is not None and
boot_img is not None): 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): 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) image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
fstab = info_dict["fstab"] fstab = info_dict["fstab"]
@@ -778,7 +778,7 @@ def AddImagesToTargetFiles(filename):
partitions = dict() partitions = dict()
def banner(s): def banner(s):
logger.info("\n\n++++ " + s + " ++++\n\n") logger.info("\n\n++++ %s ++++\n\n", s)
boot_image = None boot_image = None
if has_boot: if has_boot:
@@ -901,7 +901,7 @@ def AddImagesToTargetFiles(filename):
ab_partitions_txt = os.path.join(OPTIONS.input_tmp, "META", ab_partitions_txt = os.path.join(OPTIONS.input_tmp, "META",
"ab_partitions.txt") "ab_partitions.txt")
if os.path.exists(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() ab_partitions = f.readlines()
# For devices using A/B update, make sure we have all the needed images # 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( pack_radioimages_txt = os.path.join(
OPTIONS.input_tmp, "META", "pack_radioimages.txt") OPTIONS.input_tmp, "META", "pack_radioimages.txt")
if os.path.exists(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()) AddPackRadioImages(output_zip, f.readlines())
if output_zip: if output_zip:

View File

@@ -54,7 +54,9 @@ class Options(object):
base_search_path = os.path.join(base_out_path, base_search_path = os.path.join(base_out_path,
os.path.basename(os.getcwd())) os.path.basename(os.getcwd()))
# Python >= 3.3 returns 'linux', whereas Python 2.7 gives 'linux2'.
platform_search_path = { platform_search_path = {
"linux": os.path.join(base_search_path, "host/linux-x86"),
"linux2": 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"), "darwin": os.path.join(base_search_path, "host/darwin-x86"),
} }

View File

@@ -1938,7 +1938,7 @@ def GetTargetFilesZipForRetrofitDynamicPartitions(input_file,
target_file = common.MakeTempFile(prefix="targetfiles-", suffix=".zip") target_file = common.MakeTempFile(prefix="targetfiles-", suffix=".zip")
shutil.copyfile(input_file, target_file) 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() namelist = input_zip.namelist()
input_tmp = common.UnzipTemp(input_file, RETROFIT_DAP_UNZIP_PATTERN) input_tmp = common.UnzipTemp(input_file, RETROFIT_DAP_UNZIP_PATTERN)

View File

@@ -154,11 +154,11 @@ OPTIONS.avb_extra_args = {}
def GetApkCerts(certmap): def GetApkCerts(certmap):
# apply the key remapping to the contents of the file # 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) certmap[apk] = OPTIONS.key_map.get(cert, cert)
# apply all the -e options, overriding anything in the file # 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: if not cert:
cert = "PRESIGNED" cert = "PRESIGNED"
certmap[apk] = OPTIONS.key_map.get(cert, cert) 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): if stat.S_ISLNK(info.external_attr >> 16):
new_data = data new_data = data
else: else:
new_data = RewriteProps(data) new_data = RewriteProps(data.decode())
common.ZipWriteStr(output_tf_zip, out_info, new_data) common.ZipWriteStr(output_tf_zip, out_info, new_data)
# Replace the certs in *mac_permissions.xml (there could be multiple, such # Replace the certs in *mac_permissions.xml (there could be multiple, such
# as {system,vendor}/etc/selinux/{plat,nonplat}_mac_permissions.xml). # as {system,vendor}/etc/selinux/{plat,nonplat}_mac_permissions.xml).
elif filename.endswith("mac_permissions.xml"): elif filename.endswith("mac_permissions.xml"):
print("Rewriting %s with new keys." % (filename,)) 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) common.ZipWriteStr(output_tf_zip, out_info, new_data)
# Ask add_img_to_target_files to rebuild the recovery patch if needed. # Ask add_img_to_target_files to rebuild the recovery patch if needed.
@@ -630,17 +630,17 @@ def ReplaceCerts(data):
Raises: Raises:
AssertionError: On finding duplicate entries. AssertionError: On finding duplicate entries.
""" """
for old, new in OPTIONS.key_map.iteritems(): for old, new in OPTIONS.key_map.items():
if OPTIONS.verbose: if OPTIONS.verbose:
print(" Replacing %s.x509.pem with %s.x509.pem" % (old, new)) print(" Replacing %s.x509.pem with %s.x509.pem" % (old, new))
try: try:
with open(old + ".x509.pem") as old_fp: with open(old + ".x509.pem") as old_fp:
old_cert16 = base64.b16encode( 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: with open(new + ".x509.pem") as new_fp:
new_cert16 = base64.b16encode( new_cert16 = base64.b16encode(
common.ParseCertificate(new_fp.read())).lower() common.ParseCertificate(new_fp.read())).decode().lower()
except IOError as e: except IOError as e:
if OPTIONS.verbose or e.errno != errno.ENOENT: if OPTIONS.verbose or e.errno != errno.ENOENT:
print(" Error accessing %s: %s.\nSkip replacing %s.x509.pem with " print(" Error accessing %s: %s.\nSkip replacing %s.x509.pem with "
@@ -858,7 +858,7 @@ def ReplaceVerityKeyId(input_zip, output_zip, key_path):
writable. writable.
key_path: The path to the PEM encoded X.509 certificate. 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. # Copy in_cmdline to output_zip if veritykeyid is not present.
if "veritykeyid" not in in_cmdline: if "veritykeyid" not in in_cmdline:
common.ZipWriteStr(output_zip, "BOOT/cmdline", 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. current in-memory dict contains additional items computed at runtime.
""" """
misc_info_old = common.LoadDictionaryFromLines( 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 = [] items = []
for key in sorted(misc_info): for key in sorted(misc_info):
if key in misc_info_old: if key in misc_info_old:
@@ -957,7 +957,7 @@ def BuildKeyMap(misc_info, key_mapping_options):
def GetApiLevelAndCodename(input_tf_zip): 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 api_level = None
codename = None codename = None
for line in data.split("\n"): for line in data.split("\n"):
@@ -979,7 +979,7 @@ def GetApiLevelAndCodename(input_tf_zip):
def GetCodenameToApiLevelMap(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 api_level = None
codenames = None codenames = None
for line in data.split("\n"): for line in data.split("\n"):
@@ -997,7 +997,7 @@ def GetCodenameToApiLevelMap(input_tf_zip):
if codenames is None: if codenames is None:
raise ValueError("No ro.build.version.all_codenames in SYSTEM/build.prop") raise ValueError("No ro.build.version.all_codenames in SYSTEM/build.prop")
result = dict() result = {}
for codename in codenames: for codename in codenames:
codename = codename.strip() codename = codename.strip()
if codename: if codename:
@@ -1021,7 +1021,7 @@ def ReadApexKeysInfo(tf_zip):
key. key.
""" """
keys = {} 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() line = line.strip()
if not line: if not line:
continue continue

View File

@@ -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) ReplaceVerityKeyId(input_zip, output_zip, cert_file)
with zipfile.ZipFile(output_file) as output_zip: 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. # Test with the second certificate.
cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem') 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) ReplaceVerityKeyId(input_zip, output_zip, cert_file)
with zipfile.ZipFile(output_file) as output_zip: 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): def test_ReplaceVerityKeyId_no_veritykeyid(self):
BOOT_CMDLINE = ( 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) ReplaceVerityKeyId(input_zip, output_zip, None)
with zipfile.ZipFile(output_file) as output_zip: 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): def test_ReplaceCerts(self):
cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem') cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')

View File

@@ -98,7 +98,7 @@ class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase):
with open(boot_image, 'r+b') as boot_fp: with open(boot_image, 'r+b') as boot_fp:
boot_fp.seek(-1, os.SEEK_END) boot_fp.seek(-1, os.SEEK_END)
last_byte = boot_fp.read(1) 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.seek(-1, os.SEEK_END)
boot_fp.write(last_byte) boot_fp.write(last_byte)