From c2da550520bd34ece11fa8f0f268d565060d5b53 Mon Sep 17 00:00:00 2001 From: Salini Venate Date: Mon, 29 Jul 2024 07:13:41 +0000 Subject: [PATCH] Revert "Migrate python2 scripts to python3" This reverts commit ead4c1f92fb83bf8e4393e3e0e1820f63bb395cf. Reverting this patch since boot issue is seen on MBL Fab-B board. Tracked-On: OAM-122801 Signed-off-by: Salini Venate --- bootloader_from_zip | 8 ++--- create_gpt_image.py | 37 ++++++++++---------- generate_factory_images | 34 +++++++++--------- releasetools/bootloader_from_target_files | 4 +-- releasetools/flash_cmd_generator.py | 8 +---- releasetools/flashfiles_from_target_files | 42 +++++++++++------------ releasetools/intel_common.py | 4 +-- tasks/checkvendor.py | 2 +- 8 files changed, 65 insertions(+), 74 deletions(-) diff --git a/bootloader_from_zip b/bootloader_from_zip index d2cbed0..d24af8f 100755 --- a/bootloader_from_zip +++ b/bootloader_from_zip @@ -83,12 +83,12 @@ def main(argv): sys.exit(1) if not OPTIONS.zipfile: - print ("--zipfile is required") + print "--zipfile is required" common.Usage(__doc__) sys.exit(1) tf = tempfile.NamedTemporaryFile() - tf.write(b"foo") + tf.write("foo") tf.flush() extra_files = OPTIONS.bootimage @@ -106,9 +106,9 @@ if __name__ == '__main__': try: common.CloseInheritedPipes() main(sys.argv[1:]) - except (common.ExternalError,e): + except common.ExternalError, e: print - print (" ERROR: %s" % (e,)) + print " ERROR: %s" % (e,) print sys.exit(1) diff --git a/create_gpt_image.py b/create_gpt_image.py index a925a1e..16ad850 100755 --- a/create_gpt_image.py +++ b/create_gpt_image.py @@ -17,7 +17,7 @@ Script to create a GPT/UEFI image or to show information it contains. """ -from sys import version_info, exit +from sys import version_info if version_info < (2, 7, 3): exit('Python version must be 2.7.3 or higher') @@ -35,7 +35,7 @@ if version_info < (3, 0, 1): from ConfigParser import SafeConfigParser, ParsingError, NoOptionError, NoSectionError else: - from configparser import ConfigParser, ParsingError, NoOptionError, NoSectionError + from configparser import SafeConfigParser, ParsingError, NoOptionError, NoSectionError from math import floor, log @@ -135,8 +135,8 @@ def write(self, img_file, offset=0): Used to write MBR in an image file """ self.raw = pack(MBRInfos._FMT, self.boot, self.os_type, - self.lba_start, self.lba_size, b'', - MBRInfos._PART_ENTRY.encode('utf-8'), b'', self.sign.encode('utf-8')) + self.lba_start, self.lba_size, '', + MBRInfos._PART_ENTRY, '', self.sign) img_file.seek(offset) img_file.write(self.raw) @@ -284,15 +284,15 @@ def write(self, img_file, offset, block_size): """ Used to write GPT header and backup in an image file """ - self.raw = pack(GPTHeaderInfos._FMT, self.sign.encode('utf-8'), self.rev.encode('utf-8'), - self.size, 0, 1, int(self.lba_backup), - int(self.lba_first), int(self.lba_last), self.uuid, + self.raw = pack(GPTHeaderInfos._FMT, self.sign, self.rev, + self.size, 0, 1, self.lba_backup, + self.lba_first, self.lba_last, self.uuid, 2, self.table_length, self.entry_size, 0) - backup_raw = pack(GPTHeaderInfos._FMT, self.sign.encode('utf-8'), self.rev.encode('utf-8'), - self.size, 0, int(self.lba_backup), 1, - int(self.lba_first), int(self.lba_last), self.uuid, - int(self.lba_start), self.table_length, + backup_raw = pack(GPTHeaderInfos._FMT, self.sign, self.rev, + self.size, 0, self.lba_backup, 1, + self.lba_first, self.lba_last, self.uuid, + self.lba_start, self.table_length, self.entry_size, 0) # writes a new GPT header @@ -300,14 +300,14 @@ def write(self, img_file, offset, block_size): img_file.write(self.raw) # writes zero on unused blocks of GPT header - raw_stuffing = b'\x00' * (block_size - len(self.raw)) + raw_stuffing = '\x00' * (block_size - len(self.raw)) img_file.write(raw_stuffing) # saves the end of the GPT header gpt_header_end = img_file.tell() # writes a new GPT backup - backup_position = int(self.lba_backup) * block_size + backup_position = self.lba_backup * block_size img_file.seek(backup_position) img_file.write(backup_raw) @@ -357,7 +357,6 @@ def write(self, img_file, offset, entry_size, tlb_infos, last_usable): """ # erases the partition table entries self = [] - offset=int(offset) # writes all new partition entries in GPT header current_offset = offset @@ -372,7 +371,7 @@ def write(self, img_file, offset, entry_size, tlb_infos, last_usable): img_file.seek(offset) raw_entries_size = current_offset - offset raw_entries = img_file.read(raw_entries_size) - img_file.seek(int(last_usable) + 1) + img_file.seek(last_usable + 1) img_file.write(raw_entries) img_file.seek(current_offset) @@ -739,7 +738,7 @@ def _read_ini(self, block_size): Used to read a INI TLB partition file """ # sets a parser to read the INI TLB partition file - cfg = ConfigParser(strict=False) + cfg = SafeConfigParser() try: cfg.read(self.path) @@ -966,7 +965,7 @@ def _write_crc(self, img_file): img_file.seek(2 * self.block_size) raw_table = img_file.read(self.gpt_header.table_length * self.gpt_header.entry_size) - img_file.seek((int(self.gpt_header.lba_backup) - 32) * self.block_size) + img_file.seek((self.gpt_header.lba_backup - 32) * self.block_size) raw_backup_table = img_file.read(self.gpt_header.table_length * self.gpt_header.entry_size) @@ -1036,7 +1035,7 @@ def _write_partitions(self, img_file, tlb_infos, binaries_path): # no binary file used to build the partition or slot_b case label = tlb_part.label[0:] if bin_path == 'none' or label[len(label)-2:] == '_b': - line = b'\0' + line = '\0' img_file.seek(offset) img_file.write(line) bin_size = 0 @@ -1082,7 +1081,7 @@ def write(self, tlb_infos, binaries_path): # fill output image header with 0x00: MBR size + GPT header size + # (partition table length * entry size) - zero = b'\x00' * (2 * self.block_size + + zero = '\x00' * (2 * self.block_size + self.gpt_header.table_length * self.gpt_header.entry_size) img_file.seek(0) diff --git a/generate_factory_images b/generate_factory_images index 92b2479..4987e81 100755 --- a/generate_factory_images +++ b/generate_factory_images @@ -32,7 +32,7 @@ import os _FLASHALL_FILENAME = "flash-all.sh" # chmod (octal) -rwxr-x--x -_PERMS = 0o751 +_PERMS = 0751 _FLASH_HEADER = """#!/bin/bash # Copyright 2012 The Android Open Source Project @@ -103,23 +103,23 @@ def ConvertToDOSFormat(filename): def AddFlashScript(filename, tar, commands, windows): - print ("Archiving", filename) + print "Archiving", filename tf = tempfile.NamedTemporaryFile(delete=False) if (windows): - tf.write(_WIN_FLASH_HEADER.encode('utf-8')) + tf.write(_WIN_FLASH_HEADER) else: - tf.write(_FLASH_HEADER.encode('utf-8')) + tf.write(_FLASH_HEADER) for c in commands: if windows: - tf.write(c.get_windows_command().encode('utf-8')) + tf.write(c.get_windows_command()) else: - tf.write(c.get_linux_command().encode('utf-8')) + tf.write(c.get_linux_command()) if (windows): - tf.write(_WIN_FLASH_FOOTER.encode('utf-8')) + tf.write(_WIN_FLASH_FOOTER) else: - tf.write(_FLASH_FOOTER.encode('utf-8')) + tf.write(_FLASH_FOOTER) tf.close() if (windows): @@ -146,8 +146,8 @@ class CommandlineParser(ArgumentParser): self.description = __doc__ def error(self, message): - print ("ERROR: {}".format(message), file=sys.stderr) - print ("\n------\n", file=sys.stderr) + print >>stderr, "ERROR: {}".format(message) + print >>stderr, "\n------\n" self.print_help() exit(2) @@ -230,18 +230,18 @@ def main(): archive_name = args.output # Create Archive - print ("Creating archive: " + archive_name) + print "Creating archive: " + archive_name tar = TarOpen(archive_name, "w:gz") for src_path, dst_path in files: - print ("Archiving " + src_path) + print "Archiving " + src_path RequireFile(src_path) tar.add(src_path, arcname=dst_path) # 'fastboot update' covers the additional AOSP pieces, add this to the # command list now commands.append(UpdateCommand(update_fn, True)) - print ("Archiving " + args.update_archive) + print "Archiving " + args.update_archive RequireFile(args.update_archive) tar.add(args.update_archive, update_fn) AddFlashScript(_FLASHALL_FILENAME, tar, commands, windows=False) @@ -249,12 +249,12 @@ def main(): tar.close() - print ("Done.") + print "Done." if __name__ == "__main__": try: exit(main()) - except Usage as err: - print ("ERROR: {}".format(err.msg), file=sys.stderr) - print (" for help use --help", file=sys.stderr) + except Usage, err: + print >>stderr, "ERROR: {}".format(err.msg) + print >>stderr, " for help use --help" exit(2) diff --git a/releasetools/bootloader_from_target_files b/releasetools/bootloader_from_target_files index b35cd40..4162b00 100755 --- a/releasetools/bootloader_from_target_files +++ b/releasetools/bootloader_from_target_files @@ -61,7 +61,7 @@ def main(argv): common.Usage(__doc__) sys.exit(1) - print ("unzipping target-files...") + print "unzipping target-files..." #OPTIONS.input_tmp = common.UnzipTemp(args[0]) OPTIONS.input_tmp = args[0] #input_zip = zipfile.ZipFile(args[0], "r") @@ -91,7 +91,7 @@ if __name__ == '__main__': main(sys.argv[1:]) except common.ExternalError, e: print - print (" ERROR: %s" % (e,)) + print " ERROR: %s" % (e,) print sys.exit(1) finally: diff --git a/releasetools/flash_cmd_generator.py b/releasetools/flash_cmd_generator.py index 64b56a0..bd50b17 100755 --- a/releasetools/flash_cmd_generator.py +++ b/releasetools/flash_cmd_generator.py @@ -330,13 +330,7 @@ def parse_config(ips, variant, platform): results_list = [] for k,v in results.items(): results_list.append((k,v)) - unique_files = [] - for file in files: - # If the number is not already in the unique_numbers list, add it - if file not in unique_files: - unique_files.append(file) - - flist = [f.rsplit(':', 1) for f in unique_files] + flist = [f.rsplit(':', 1) for f in set(files)] return results_list, flist diff --git a/releasetools/flashfiles_from_target_files b/releasetools/flashfiles_from_target_files index cc9b68e..02de5ff 100755 --- a/releasetools/flashfiles_from_target_files +++ b/releasetools/flashfiles_from_target_files @@ -97,14 +97,14 @@ class VariantIpGenerator: def __add_variant_flashfile(self, ip, variant): variant_flashfile = self.flashfile + "_" + variant + ".ini" - print ("Variant flashfile = %s"%variant_flashfile) + print "Variant flashfile = %s"%variant_flashfile # Sanity check to avoid future silent removal eg = self.empty_groups(ip) if eg: raise AssertionError("Unexpected malformed section %s" % eg[0]) if os.path.isfile(variant_flashfile): - print ("Reading INI configuration for %s ..."%variant) + print "Reading INI configuration for %s ..."%variant with open(variant_flashfile, "r") as f: ip.parse(f) self.variant_files = self.variant_files_common @@ -125,7 +125,7 @@ class VariantIpGenerator: # This may happen when a mixin (platform level) disables a feature, while # local flashfile.ini (variant level) is kept and customizes this feature. for s in self.empty_groups(ip): - print ("Removing malformed section : ", s) + print "Removing malformed section : ", s ip.delete_section(s) def empty_groups(self, ip): @@ -214,7 +214,7 @@ def getFromZip(zip_path, filename): with zipfile.ZipFile(zip_path, "r") as zf: data = zf.open(filename).read() info = zf.getinfo(filename) - return (common.File(filename, data), (info.external_attr >> 16) & 0xFFFF) + return (common.File(filename, data), (info.external_attr >> 16L) & 0xFFFF) def getProvdataVariants(unpack_dir): variants = [] @@ -250,7 +250,7 @@ def process_image(unpack_dir, dest_zip, source, target, configs, variant=None, t if target_out in flashfile_content: return else: - print ("-- Adding", target_out) + print "-- Adding", target_out # Default is no special permissions perms = None # retrieve file from target file package based on source & target strings @@ -310,7 +310,7 @@ def process_image_fast(product_out, flashfiles_out, source, target, variant=None if target_out in flashfile_content: return - print ("-- Adding", target_out) + print "-- Adding", target_out outfile = os.path.join(flashfiles_out, target_out) if not os.path.exists(os.path.dirname(outfile)): os.mkdir(os.path.dirname(outfile)) @@ -374,7 +374,7 @@ def main(argv): flashfile = getIntermediates(product_out, "flashfiles", "flashfiles") else: - print ("Unzipping target-files...") + print "Unzipping target-files..." unpack_dir = common.UnzipTemp(args[0]) if OPTIONS.add_image: input_super = os.path.join(unpack_dir, "IMAGES") @@ -392,13 +392,13 @@ def main(argv): # Retrieve "generic" PFT instructions from target file package if os.path.isfile(flashfile + ".ini"): - print ("Reading INI configuration...") + print "Reading INI configuration..." with open(flashfile + ".ini", "r") as f: ip = iniparser.IniParser() ip.parse(f) configs, files = flash_cmd_generator.parse_config([ip], build_type, platform) elif os.path.isfile(flashfile + ".json") and not OPTIONS.unified_variants: - print ("Reading JSON configuration...") + print "Reading JSON configuration..." with open(flashfile + ".json", "r") as f: conf = json.loads(f.read()) configs, files = flashxml.parse_config(conf, build_type, platform) @@ -406,25 +406,25 @@ def main(argv): if not OPTIONS.mv_config_default: common.Usage(__doc__) sys.exit(1) - print ("Reading JSON FLS configuration...") + print "Reading JSON FLS configuration..." with open(flashfile + "_fls.json", "r") as f: conf = json.loads(f.read()) configs, files = flashflsxml.parse_config(conf, build_type, platform, OPTIONS.mv_config_default, system) else: - print ("Exiting, Missing correct flashfile configuration for generating Flashfiles.") + print "Exiting, Missing correct flashfile configuration for generating Flashfiles." sys.exit(1) if OPTIONS.fast: fastff_dir = args[1] # If mega flashfile is enabled, create multi-variant version of PFT instructions if OPTIONS.unified_variants or OPTIONS.variants : - print ("Adding variant specific configurations to ip...") + print "Adding variant specific configurations to ip..." vip = VariantIpGenerator(ip, configs, OPTIONS.variants, variant_files, flashfile) vip.generate_variant_ip() configs, cmd_files = flash_cmd_generator.parse_config(vip.variant_ips, build_type, platform) cmd_files = set([i for _,i in cmd_files]) - print ("Adding required binaries...") + print "Adding required binaries..." for src, target in files: if OPTIONS.variants: for variant in OPTIONS.variants: @@ -442,7 +442,7 @@ def main(argv): src,target = file.split(":") process_image_fast(product_out, fastff_dir, src, target, variant, variantFilename(target, variant)) - print ("Generating JSON flash configuration files...") + print "Generating JSON flash configuration files..." for fn, data in configs: with open(os.path.join(fastff_dir,fn), 'w') as file: file.write(data) @@ -450,14 +450,14 @@ def main(argv): with zipfile.ZipFile(args[1], "w", zipfile.ZIP_DEFLATED,allowZip64=True) as dest_zip: # If mega flashfile is enabled, create multi-variant version of PFT instructions if OPTIONS.unified_variants or OPTIONS.variants : - print ("Adding variant specific configurations to ip...") + print "Adding variant specific configurations to ip..." vip = VariantIpGenerator(ip, configs, OPTIONS.variants, variant_files, flashfile) vip.generate_variant_ip() configs, cmd_files = flash_cmd_generator.parse_config(vip.variant_ips, build_type, platform) cmd_files = set([i for _,i in cmd_files]) # Using "generic" instructions as reference, grab required files & insert into flashfile zip - print ("Adding required binaries...") + print "Adding required binaries..." for src, target in files: if OPTIONS.variants: for variant in OPTIONS.variants: @@ -477,21 +477,19 @@ def main(argv): process_image(unpack_dir, dest_zip, src, target, configs, variant, variantFilename(target, variant)) # Write flash_cmd_generator parsed PFT flashing instructions to file & insert into flashfile zip - print ("Generating JSON flash configuration files...") + print "Generating JSON flash configuration files..." for fn, data in configs: - if isinstance(data, str): - data = data.encode('utf-8') ifile = common.File(fn, data) ifile.AddToZip(dest_zip) - print ("All done.") + print "All done." if __name__ == '__main__': try: common.CloseInheritedPipes() main(sys.argv[1:]) - except common.ExternalError as e: + except common.ExternalError, e: print - print (" ERROR: %s" % (e,)) + print " ERROR: %s" % (e,) print sys.exit(1) finally: diff --git a/releasetools/intel_common.py b/releasetools/intel_common.py index 6c617ba..c2267e9 100644 --- a/releasetools/intel_common.py +++ b/releasetools/intel_common.py @@ -394,7 +394,7 @@ def GetBootloaderImageFromTFP(unpack_dir, autosize=False, extra_files=None, vari block_size=info["block_size"], extra_files=extra_files) - bootloader = open(filename,'rb') + bootloader = open(filename) data = bootloader.read() bootloader.close() os.unlink(filename) @@ -482,7 +482,7 @@ def MakeVFATFilesystem(root_zip, filename, title="ANDROIDIA", size=0, block_size cmd = ["mkdosfs"] if block_size: cmd.extend(["-S", str(block_size)]) - cmd.extend(["-n", title, "-C", filename, str(size // 1024)]) + cmd.extend(["-n", title, "-C", filename, str(size / 1024)]) try: p = common.Run(cmd) except Exception as exc: diff --git a/tasks/checkvendor.py b/tasks/checkvendor.py index b1483c3..86e3ba2 100755 --- a/tasks/checkvendor.py +++ b/tasks/checkvendor.py @@ -186,4 +186,4 @@ def main(): return 0 if __name__ == "__main__": - sys.exit(main()) + exit(main())