update_payload: Port scripts to python3

Update the update_payload scripts to be compatible with
python2 and python3. Python2 compatibility is needed since
the repo is shared with Android.

BUG=chromium:1011631

TEST=Executed aosp/system/update_engine/scripts/run_unittests and
cros_generate_update_payload

Cq-Depend: chromium:1904837, chromium:1911499
Change-Id: Ie450b80b5f7550051b38d320173ccc0c915f65e7
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/1904310
Commit-Queue: Andrew Lassalle <andrewlassalle@chromium.org>
Tested-by: Andrew Lassalle <andrewlassalle@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
Auto-Submit: Andrew Lassalle <andrewlassalle@chromium.org>
diff --git a/scripts/payload_info.py b/scripts/payload_info.py
index bb7f8a4..965bb76 100755
--- a/scripts/payload_info.py
+++ b/scripts/payload_info.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 # -*- coding: utf-8 -*-
 #
 # Copyright (C) 2015 The Android Open Source Project
@@ -18,15 +18,17 @@
 
 """payload_info: Show information about an update payload."""
 
+from __future__ import absolute_import
 from __future__ import print_function
 
 import argparse
-import itertools
 import sys
 import textwrap
 
+from six.moves import range
 import update_payload
 
+
 MAJOR_PAYLOAD_VERSION_BRILLO = 2
 
 def DisplayValue(key, value):
@@ -40,12 +42,12 @@
 def DisplayHexData(data, indent=0):
   """Print out binary data as a hex values."""
   for off in range(0, len(data), 16):
-    chunk = data[off:off + 16]
+    chunk = bytearray(data[off:off + 16])
     print(' ' * indent +
-          ' '.join('%.2x' % ord(c) for c in chunk) +
+          ' '.join('%.2x' % c for c in chunk) +
           '   ' * (16 - len(chunk)) +
           ' | ' +
-          ''.join(c if 32 <= ord(c) < 127 else '.' for c in chunk))
+          ''.join(chr(c) if 32 <= c < 127 else '.' for c in chunk))
 
 
 class PayloadCommand(object):
@@ -144,7 +146,7 @@
 
     op_dict = update_payload.common.OpType.NAMES
     print('%s:' % name)
-    for op, op_count in itertools.izip(operations, itertools.count()):
+    for op_count, op in enumerate(operations):
       print('  %d: %s' % (op_count, op_dict[op.type]))
       if op.HasField('data_offset'):
         print('    Data offset: %s' % op.data_offset)
@@ -178,8 +180,8 @@
           last_ext = curr_ext
 
       # Old and new partitions are read once during verification.
-      read_blocks += partition.old_partition_info.size / manifest.block_size
-      read_blocks += partition.new_partition_info.size / manifest.block_size
+      read_blocks += partition.old_partition_info.size // manifest.block_size
+      read_blocks += partition.new_partition_info.size // manifest.block_size
 
     stats = {'read_blocks': read_blocks,
              'written_blocks': written_blocks,
@@ -212,7 +214,7 @@
 def main():
   parser = argparse.ArgumentParser(
       description='Show information about an update payload.')
-  parser.add_argument('payload_file', type=file,
+  parser.add_argument('payload_file', type=argparse.FileType('rb'),
                       help='The update payload file.')
   parser.add_argument('--list_ops', default=False, action='store_true',
                       help='List the install operations and their extents.')
@@ -224,5 +226,6 @@
 
   PayloadCommand(args).Run()
 
+
 if __name__ == '__main__':
   sys.exit(main())