From 76fe732dd29d8b87ca8e2315fd915876659c4130 Mon Sep 17 00:00:00 2001
From: Mitchel Humpherys <mitchelh@codeaurora.org>
Date: Fri, 18 Apr 2014 14:26:42 -0700
Subject: [PATCH] lrdp-v2: improve handling of unsupported python versions

We don't support python3 at all and python2.6 can only be used if the
user installs an extra package (OrderedDict [1]). Ask for python2
explicitly in the shebang line to resolve the python3 issues and print
some instructions to the user regarding python2.6 when that is used.

If the user insists on using python2.6, provide a command line
switch (--force-26) to skip the version check. To reduce clutter, don't
document this switch in the --help text but inform the user about its
existence when we error out due to the python2.6 check.

In order to even print an error message in python3 we have to first
ensure that our code is valid in python3, otherwise the interpreter
fails to even start executing our code (so we can't print any
messages). Use 2to3 to patch up some print statements, etc. Also fix
inconsistent whitespace, which is a syntax error on python3.

[1] https://pypi.python.org/pypi/ordereddict

Change-Id: Ie2c0a200e60ec90bf6cf49789f2cc75f181fa94b
---
 linux-ramdump-parser-v2/gdbmi.py        | 18 ++++++-------
 linux-ramdump-parser-v2/lpaeiommulib.py |  5 +++-
 linux-ramdump-parser-v2/mmu.py          |  4 +--
 linux-ramdump-parser-v2/ramparse.py     | 34 ++++++++++++++++++++++---
 linux-ramdump-parser-v2/sizes.py        |  5 +++-
 5 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/linux-ramdump-parser-v2/gdbmi.py b/linux-ramdump-parser-v2/gdbmi.py
index e7cec10..282f48f 100644
--- a/linux-ramdump-parser-v2/gdbmi.py
+++ b/linux-ramdump-parser-v2/gdbmi.py
@@ -211,7 +211,7 @@ class GdbMI(object):
     def get_enum_lookup_table(self, enum, upperbound):
         """Return a table translating enum values to human readable strings."""
         table = []
-        for i in xrange(0, upperbound):
+        for i in range(0, upperbound):
             result = self._run_for_first(
                 'print ((enum {0}){1})'.format(enum, i))
             parts = result.split(' ')
@@ -239,18 +239,18 @@ class GdbMI(object):
 
 if __name__ == '__main__':
     if len(sys.argv) != 3:
-        print 'Usage: gdbmi.py gdb_path elf'
+        print('Usage: gdbmi.py gdb_path elf')
         sys.exit(1)
 
     gdb_path, elf = sys.argv[1:]
 
     with GdbMI(gdb_path, elf) as g:
-        print 'GDB Version:', g.version()
-        print 'ion_buffer.heap offset:', g.field_offset('struct ion_buffer', 'heap')
-        print 'atomic_t.counter offset:', g.field_offset('atomic_t', 'counter')
-        print 'sizeof(struct ion_buffer):', g.sizeof('struct ion_buffer')
+        print('GDB Version: ' + g.version())
+        print('ion_buffer.heap offset: ' + str(g.field_offset('struct ion_buffer', 'heap')))
+        print('atomic_t.counter offset: ' + str(g.field_offset('atomic_t', 'counter')))
+        print('sizeof(struct ion_buffer): ' + str(g.sizeof('struct ion_buffer')))
         addr = g.address_of('kernel_config_data')
-        print 'address of kernel_config_data:', hex(addr)
+        print('address of kernel_config_data: ' + hex(addr))
         symbol = g.get_symbol_info(addr)
-        print 'symbol at', hex(addr), ':', symbol.symbol, \
-            'which is in section', symbol.section
+        print('symbol at ' + hex(addr) + ' : ' + symbol.symbol + \
+            ' which is in section ' + symbol.section)
diff --git a/linux-ramdump-parser-v2/lpaeiommulib.py b/linux-ramdump-parser-v2/lpaeiommulib.py
index 85edcd9..64fe5ee 100644
--- a/linux-ramdump-parser-v2/lpaeiommulib.py
+++ b/linux-ramdump-parser-v2/lpaeiommulib.py
@@ -9,7 +9,10 @@
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 
-from collections import OrderedDict
+try:
+    from collections import OrderedDict
+except ImportError:
+    from ordereddict import OrderedDict
 
 from print_out import print_out_str
 from register import Register
diff --git a/linux-ramdump-parser-v2/mmu.py b/linux-ramdump-parser-v2/mmu.py
index fad2b31..383272c 100644
--- a/linux-ramdump-parser-v2/mmu.py
+++ b/linux-ramdump-parser-v2/mmu.py
@@ -585,7 +585,7 @@ class Armv8MMU(MMU):
             tl_index=(20,12),
             page_index=(11,0))
 
-	fl_desc = self.do_fl_sl_level_lookup(self.ttbr, virt_r.fl_index, 12, 30)
+        fl_desc = self.do_fl_sl_level_lookup(self.ttbr, virt_r.fl_index, 12, 30)
 
         if fl_desc.dtype == Armv8MMU.DESCRIPTOR_BLOCK:
             return self.fl_block_desc_2_phys(fl_desc, virt_r)
@@ -598,7 +598,7 @@ class Armv8MMU(MMU):
         except:
             return None
 
-	if sl_desc.dtype == Armv8MMU.DESCRIPTOR_BLOCK:
+        if sl_desc.dtype == Armv8MMU.DESCRIPTOR_BLOCK:
             r = self.sl_block_desc_2_phys(sl_desc, virt_r)
             return r
 
diff --git a/linux-ramdump-parser-v2/ramparse.py b/linux-ramdump-parser-v2/ramparse.py
index 859308d..1283458 100755
--- a/linux-ramdump-parser-v2/ramparse.py
+++ b/linux-ramdump-parser-v2/ramparse.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python2
 
 # Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
 #
@@ -11,6 +11,13 @@
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 
+# this script requires python2. However, we'd like to be able to print
+# an informative message to a user who might be unknowingly running
+# python3 so we can't allow any python2 print statements to sneak in
+# since they result in syntax errors in python3. By importing
+# print_function we are requiring ourselves to use the python3 syntax.
+from __future__ import print_function
+
 import sys
 import os
 import re
@@ -23,6 +30,27 @@ from print_out import print_out_str, set_outfile, print_out_section
 # Please update version when something is changed!'
 VERSION = '2.0'
 
+# quick check of system requirements:
+major, minor = sys.version_info[:2]
+if major != 2:
+    print("This script requires python2 to run!\n")
+    print("You seem to be running: " + sys.version)
+    print()
+    sys.exit(1)
+if minor != 7 and '--force-26' not in sys.argv:
+    from textwrap import dedent
+    print(dedent("""
+    Warning! This script is developed and tested with Python 2.7.
+    You might be able to get things working on 2.6 by installing
+    a few dependencies (most notably, OrderedDict [1])
+    and then passing --force-26 to bypass this version check, but
+    the recommended and supported approach is to install python2.7.
+
+    [1] https://pypi.python.org/pypi/ordereddict"""))
+    sys.exit(1)
+if '--force-26' in sys.argv:
+    sys.argv.remove('--force-26')
+
 
 def parse_ram_file(option, opt_str, value, parser):
     a = getattr(parser.values, option.dest)
@@ -223,8 +251,8 @@ if __name__ == '__main__':
                    arm64=options.arm64)
 
     if options.shell or options.classic_shell:
-        print "Entering interactive shell mode."
-        print "The RamDump instance is available in the `dump' variable\n"
+        print("Entering interactive shell mode.")
+        print("The RamDump instance is available in the `dump' variable\n")
         do_fallback = options.classic_shell
         if not do_fallback:
             try:
diff --git a/linux-ramdump-parser-v2/sizes.py b/linux-ramdump-parser-v2/sizes.py
index db65602..0f1a937 100644
--- a/linux-ramdump-parser-v2/sizes.py
+++ b/linux-ramdump-parser-v2/sizes.py
@@ -27,7 +27,10 @@ working with common sizes.
 """
 
 import math
-from collections import OrderedDict
+try:
+    from collections import OrderedDict
+except ImportError:
+    from ordereddict import OrderedDict
 
 SZ_1   = 0x00000001
 SZ_2   = 0x00000002
-- 
GitLab