Make mk_combined_img.py compatible with py3

* py2 is EOL, make it future-proof

Change-Id: I86b33214a8f93b1d52c3ec0603ba06e00ead5f58
diff --git a/tools/mk_combined_img.py b/tools/mk_combined_img.py
index 8c74436..1e35dc6 100755
--- a/tools/mk_combined_img.py
+++ b/tools/mk_combined_img.py
@@ -1,27 +1,33 @@
 #!/usr/bin/python
-import sys
+
+from __future__ import print_function
+
+import argparse
+import codecs
+import operator
 import os
+import sys
 from subprocess import Popen, PIPE
 from tempfile import mkstemp
-import argparse
-import operator
 
 
 def check_sparse(filename):
     magic = 3978755898
     with open(filename, 'rb') as i:
         word = i.read(4)
-        if magic == int(word[::-1].encode('hex'), 16):
+        if magic == int(codecs.encode(word[::-1], 'hex'), 16):
             return True
     return False
 
+
 def shell_command(comm_list):
     command = Popen(comm_list, stdout=PIPE, stderr=PIPE)
     command.communicate()
-    execute = command.wait()
+    command.wait()
     if command.returncode != 0:
         sys.exit(1)
 
+
 def parse_input(input_file):
     parsed_lines = list()
     lines = input_file.readlines()
@@ -40,7 +46,7 @@
     num_used = set()
     for line in parsed_lines:
         partition_info = dict()
-        partition_info["path"] =  line[0]
+        partition_info["path"] = line[0]
         partition_info["label"] = line[1]
         # round up by 1M
         sizeByMb = str((1024 * 1024 - 1 + os.path.getsize(line[0])) / 1024 / 1024)
@@ -49,18 +55,18 @@
         try:
             partition_info["num"] = int(line[2])
         except ValueError:
-            print "'%s' cannot be converted to int" % (line[2])
+            print("'%s' cannot be converted to int" % (line[2]))
             sys.exit(1)
 
         # check if the partition number is out of range
         if partition_info["num"] > len(lines) or partition_info["num"] < 0:
-            print "Invalid partition number: %d, range [1..%d]" % \
-                    (partition_info["num"], len(lines))
+            print("Invalid partition number: %d, range [1..%d]" % \
+                  (partition_info["num"], len(lines)))
             sys.exit(1)
 
         # check if the partition number is duplicated
         if partition_info["num"] in num_used:
-            print "Duplicated partition number:%d" % (partition["num"])
+            print("Duplicated partition number:%d" % (partition_info["num"]))
             sys.exit(1)
         num_used.add(partition_info["num"])
         partitions.append(partition_info)
@@ -68,42 +74,47 @@
     partitions.sort(key=operator.itemgetter("num"))
     return partitions
 
+
 def write_partition(partition, output_file, offset):
     # $ dd if=/path/to/image of=/path/to/output conv=notrunc,sync \
     #   ibs=1024k obs=1024k seek=<offset>
-    dd_comm = ['dd', 'if='+partition["path"], 'of='+output_file,'conv=notrunc,sync',
-                'ibs=1024k','obs=1024k', 'seek='+str(offset)]
+    dd_comm = ['dd', 'if=' + partition["path"], 'of=' + output_file, 'conv=notrunc,sync',
+               'ibs=1024k', 'obs=1024k', 'seek=' + str(offset)]
     shell_command(dd_comm)
     return
 
+
 def unsparse_partition(partition):
     # if the input image is in sparse format, unsparse it
     simg2img = os.environ.get('SIMG2IMG', 'simg2img')
-    print "Unsparsing %s" % (partition["path"]),
+    print("Unsparsing %s" % (partition["path"]), end=' ')
     partition["fd"], temp_file = mkstemp()
     shell_command([simg2img, partition["path"], temp_file])
     partition["path"] = temp_file
-    print "Done"
+    print("Done")
     return
 
+
 def clear_partition_table(filename):
     sgdisk = os.environ.get('SGDISK', 'sgdisk')
-    print "%s --clear %s" % (sgdisk, filename)
+    print("%s --clear %s" % (sgdisk, filename))
     shell_command([sgdisk, '--clear', filename])
     return
 
+
 def add_partition(partition, output_file):
     sgdisk = os.environ.get('SGDISK', 'sgdisk')
     num = str(partition["num"])
-    new_comm = '--new='+num+':'+partition["start"]+':'+partition["end"]
-    type_comm = '--type='+num+':8300'
-    name_comm = '--change-name='+num+':'+partition["label"]
+    new_comm = '--new=' + num + ':' + partition["start"] + ':' + partition["end"]
+    type_comm = '--type=' + num + ':8300'
+    name_comm = '--change-name=' + num + ':' + partition["label"]
     # build partition table in order. for example:
     # $ sgdisk --new=1:2048:5244927 --type=1:8300 --change-name=1:system \
     #   /path/to/output
     shell_command([sgdisk, new_comm, type_comm, name_comm, output_file])
     return
 
+
 def main():
     # check usage:
     parser = argparse.ArgumentParser()
@@ -112,19 +123,19 @@
                         default="image_config")
     parser.add_argument("-o", "--output",
                         type=str, help="output filename",
-                        default=os.environ.get("OUT", ".")+"/combined.img")
+                        default=os.environ.get("OUT", ".") + "/combined.img")
     args = parser.parse_args()
 
     output_filename = os.path.expandvars(args.output)
 
     # remove the output_filename.qcow2
-    print "removing " + output_filename + ".qcow2"
+    print("removing " + output_filename + ".qcow2")
     shell_command(['rm', '-rf', output_filename + ".qcow2"])
 
     # check input file
     config_filename = args.input
     if not os.path.exists(config_filename):
-        print "Invalid config file name " + config_filename
+        print("Invalid config file name " + config_filename)
         sys.exit(1)
 
     # read input file
@@ -134,12 +145,12 @@
 
     # take a shortcut in build environment
     if os.path.exists(output_filename) and len(partitions) == 2:
-        print "updating " + output_filename + " ..."
+        print("updating " + output_filename + " ...")
         shell_command(['dd', "if=" + partitions[0]["path"], "of=" + output_filename,
-                "conv=notrunc,sync", "ibs=1024k", "obs=1024k", "seek=1"])
+                       "conv=notrunc,sync", "ibs=1024k", "obs=1024k", "seek=1"])
         shell_command(['dd', "if=" + partitions[1]["path"], "of=" + output_filename,
-                "conv=notrunc,sync", "ibs=1024k", "obs=1024k", "seek=2"])
-        print "done"
+                       "conv=notrunc,sync", "ibs=1024k", "obs=1024k", "seek=2"])
+        print("done")
         sys.exit(0)
     elif len(partitions) == 2:
         gptprefix = partitions[0]["sizeByMb"] + "_" + partitions[1]["sizeByMb"]
@@ -147,7 +158,7 @@
         gpt_head = prebuilt_gpt_dir + "/head.img"
         gpt_tail = prebuilt_gpt_dir + "/head.img"
         if os.path.exists(gpt_head) and os.path.exists(gpt_tail):
-            print "found prebuilt gpt header and footer, use it"
+            print("found prebuilt gpt header and footer, use it")
             shell_command(['dd', "if=" + gpt_head, "of=" + output_filename, "bs=1024k",
                     "conv=notrunc,sync", "count=1"])
             shell_command(['dd', "if=" + partitions[0]["path"], "of=" + output_filename,
@@ -157,32 +168,32 @@
             shell_command(['dd', "if=" + gpt_tail, "of=" + output_filename,
                     "bs=1024k", "conv=notrunc,sync",
                     "seek=" + str(1 + int(partitions[0]["sizeByMb"]) + int(partitions[1]["sizeByMb"]))])
-            print "done"
+            print("done")
             sys.exit(0)
 
     # combine the images
     # add padding
-    shell_command(['dd', 'if=/dev/zero', 'of='+output_filename, 'ibs=1024k', 'count=1'])
+    shell_command(['dd', 'if=/dev/zero', 'of=' + output_filename, 'ibs=1024k', 'count=1'])
 
     for partition in partitions:
         offset = os.path.getsize(output_filename)
-        partition["start"] = str(offset / 512)
+        partition["start"] = str(offset // 512)
         # dectect sparse file format
         if check_sparse(partition["path"]):
             unsparse_partition(partition)
 
         # TODO: extract the partition if the image file is already formatted
 
-        write_partition(partition, output_filename, offset/1024/1024)
+        write_partition(partition, output_filename, offset // 1024 // 1024)
         offset = os.path.getsize(output_filename)
-        partition["end"] = str(offset / 512 - 1)
+        partition["end"] = str(offset // 512 - 1)
 
     # add padding
     # $ dd if=/dev/zero of=/path/to/output conv=notrunc bs=1 \
     #   count=1024k seek=<offset>
-    offset = os.path.getsize(output_filename) / 1024 / 1024
-    shell_command(['dd', 'if=/dev/zero', 'of='+output_filename,
-                'conv=notrunc', 'bs=1024k', 'count=1', 'seek='+str(offset)])
+    offset = os.path.getsize(output_filename) // 1024 // 1024
+    shell_command(['dd', 'if=/dev/zero', 'of=' + output_filename,
+                   'conv=notrunc', 'bs=1024k', 'count=1', 'seek=' + str(offset)])
 
     # make partition table
     # $ sgdisk --clear /path/to/output
@@ -195,6 +206,6 @@
             os.close(partition["fd"])
             os.remove(partition["path"])
 
+
 if __name__ == "__main__":
     main()
-