Revert "Port Checker to python 3" am: 5409408a41

Original change: https://android-review.googlesource.com/c/platform/art/+/1453039

Change-Id: I39b62a739f98a6494afe76f44f7d5fd23b8fd3d7
diff --git a/tools/checker/checker.py b/tools/checker/checker.py
index 236a2ab..65b01a7 100755
--- a/tools/checker/checker.py
+++ b/tools/checker/checker.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python2
 #
 # Copyright (C) 2014 The Android Open Source Project
 #
@@ -99,7 +99,7 @@
   args = ParseArguments()
 
   if args.quiet:
-    Logger.Verbosity = Logger.Level.ERROR
+    Logger.Verbosity = Logger.Level.Error
 
   if args.list_passes:
     ListPasses(args.tested_file)
diff --git a/tools/checker/common/logger.py b/tools/checker/common/logger.py
index 67bb674..aa3a92f 100644
--- a/tools/checker/common/logger.py
+++ b/tools/checker/common/logger.py
@@ -12,50 +12,42 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from __future__ import print_function
 import collections
-import enum
 import sys
 
+class Logger(object):
 
-class Logger:
+  class Level(object):
+    NoOutput, Error, Info = range(3)
 
-  class Level(enum.IntEnum):
-    NO_OUTPUT = 0
-    ERROR = 1
-    INFO = 2
-
-  class Color(enum.Enum):
-    DEFAULT = 0
-    BLUE = 1
-    GRAY = 2
-    PURPLE = 3
-    RED = 4
-    GREEN = 5
+  class Color(object):
+    Default, Blue, Gray, Purple, Red, Green = range(6)
 
     @staticmethod
     def terminalCode(color, out=sys.stdout):
       if not out.isatty():
         return ''
-      elif color == Logger.Color.BLUE:
+      elif color == Logger.Color.Blue:
         return '\033[94m'
-      elif color == Logger.Color.GRAY:
+      elif color == Logger.Color.Gray:
         return '\033[37m'
-      elif color == Logger.Color.PURPLE:
+      elif color == Logger.Color.Purple:
         return '\033[95m'
-      elif color == Logger.Color.RED:
+      elif color == Logger.Color.Red:
         return '\033[91m'
-      elif color == Logger.Color.GREEN:
+      elif color == Logger.Color.Green:
         return '\033[32m'
       else:
         return '\033[0m'
 
-  Verbosity = Level.INFO
+  Verbosity = Level.Info
 
   @staticmethod
-  def log(content, level=Level.INFO, color=Color.DEFAULT, newLine=True, out=sys.stdout):
+  def log(content, level=Level.Info, color=Color.Default, newLine=True, out=sys.stdout):
     if level <= Logger.Verbosity:
       content = Logger.Color.terminalCode(color, out) + str(content) + \
-             Logger.Color.terminalCode(Logger.Color.DEFAULT, out)
+             Logger.Color.terminalCode(Logger.Color.Default, out)
       if newLine:
         print(content, file=out)
       else:
@@ -64,8 +56,8 @@
 
   @staticmethod
   def fail(msg, file=None, line=-1, lineText=None, variables=None):
-    Logger.log("error: ", Logger.Level.ERROR, color=Logger.Color.RED, newLine=False, out=sys.stderr)
-    Logger.log(msg, Logger.Level.ERROR, out=sys.stderr)
+    Logger.log("error: ", Logger.Level.Error, color=Logger.Color.Red, newLine=False, out=sys.stderr)
+    Logger.log(msg, Logger.Level.Error, out=sys.stderr)
 
     if lineText:
       loc = ""
@@ -75,8 +67,8 @@
         loc += str(line) + ":"
       if loc:
         loc += " "
-      Logger.log(loc, Logger.Level.ERROR, color=Logger.Color.GRAY, newLine=False, out=sys.stderr)
-      Logger.log(lineText, Logger.Level.ERROR, out=sys.stderr)
+      Logger.log(loc, Logger.Level.Error, color=Logger.Color.Gray, newLine=False, out=sys.stderr)
+      Logger.log(lineText, Logger.Level.Error, out=sys.stderr)
 
     if variables:
       longestName = 0
@@ -85,23 +77,23 @@
 
       for var in collections.OrderedDict(sorted(variables.items())):
         padding = ' ' * (longestName - len(var))
-        Logger.log(var, Logger.Level.ERROR, color=Logger.Color.GREEN, newLine=False, out=sys.stderr)
-        Logger.log(padding, Logger.Level.ERROR, newLine=False, out=sys.stderr)
-        Logger.log(" = ", Logger.Level.ERROR, newLine=False, out=sys.stderr)
-        Logger.log(variables[var], Logger.Level.ERROR, out=sys.stderr)
+        Logger.log(var, Logger.Level.Error, color=Logger.Color.Green, newLine=False, out=sys.stderr)
+        Logger.log(padding, Logger.Level.Error, newLine=False, out=sys.stderr)
+        Logger.log(" = ", Logger.Level.Error, newLine=False, out=sys.stderr)
+        Logger.log(variables[var], Logger.Level.Error, out=sys.stderr)
 
     sys.exit(1)
 
   @staticmethod
   def startTest(name):
-    Logger.log("TEST ", color=Logger.Color.PURPLE, newLine=False)
+    Logger.log("TEST ", color=Logger.Color.Purple, newLine=False)
     Logger.log(name + "... ", newLine=False)
 
   @staticmethod
   def testPassed():
-    Logger.log("PASS", color=Logger.Color.BLUE)
+    Logger.log("PASS", color=Logger.Color.Blue)
 
   @staticmethod
   def testFailed(msg, statement, variables):
-    Logger.log("FAIL", color=Logger.Color.RED)
+    Logger.log("FAIL", color=Logger.Color.Red)
     Logger.fail(msg, statement.fileName, statement.lineNo, statement.originalText, variables)
diff --git a/tools/checker/common/mixins.py b/tools/checker/common/mixins.py
index e44c84d..819de24 100644
--- a/tools/checker/common/mixins.py
+++ b/tools/checker/common/mixins.py
@@ -23,4 +23,4 @@
   """ Prints object as name-dictionary pair. """
 
   def __repr__(self):
-    return '<{}: {}>'.format(type(self).__name__, str(self.__dict__))
+    return "<%s: %s>" % (type(self).__name__, str(self.__dict__))
diff --git a/tools/checker/common/testing.py b/tools/checker/common/testing.py
new file mode 100644
index 0000000..1299c07
--- /dev/null
+++ b/tools/checker/common/testing.py
@@ -0,0 +1,22 @@
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+def ToUnicode(string):
+  """ Converts a string into Unicode.
+
+  This is a delegate function for the built-in `unicode`. It checks if the input
+  is not `None`, because `unicode` turns it into an actual "None" string.
+  """
+  assert string is not None
+  return unicode(string)
diff --git a/tools/checker/file_format/c1visualizer/parser.py b/tools/checker/file_format/c1visualizer/parser.py
index 9234bde..a31bc56 100644
--- a/tools/checker/file_format/c1visualizer/parser.py
+++ b/tools/checker/file_format/c1visualizer/parser.py
@@ -37,7 +37,7 @@
   if state.currentState == C1ParserState.StartingCfgBlock:
     # Previous line started a new 'cfg' block which means that this one must
     # contain the name of the pass (this is enforced by C1visualizer).
-    if re.match(r"name\s+\"[^\"]+\"", line):
+    if re.match("name\s+\"[^\"]+\"", line):
       # Extract the pass name, prepend it with the name of the method and
       # return as the beginning of a new group.
       state.currentState = C1ParserState.InsideCfgBlock
@@ -54,12 +54,12 @@
 
   elif state.currentState == C1ParserState.InsideCompilationBlock:
     # Search for the method's name. Format: method "<name>"
-    if re.match(r"method\s+\"[^\"]*\"", line):
+    if re.match("method\s+\"[^\"]*\"", line):
       methodName = line.split("\"")[1].strip()
       if not methodName:
         Logger.fail("Empty method name in output", fileName, lineNo)
 
-      m = re.search(r"isa_features:([\w,-]+)", methodName)
+      m = re.search("isa_features:([\w,-]+)", methodName)
       if (m):
         rawFeatures = m.group(1).split(",")
         # Create a map of features in the form {featureName: isEnabled}.
diff --git a/tools/checker/file_format/c1visualizer/test.py b/tools/checker/file_format/c1visualizer/test.py
index a118a92..e5acd62 100644
--- a/tools/checker/file_format/c1visualizer/test.py
+++ b/tools/checker/file_format/c1visualizer/test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python2
 #
 # Copyright (C) 2014 The Android Open Source Project
 #
@@ -15,6 +15,7 @@
 # limitations under the License.
 
 from common.immutables               import ImmutableDict
+from common.testing                  import ToUnicode
 from file_format.c1visualizer.parser import ParseC1visualizerStream
 from file_format.c1visualizer.struct import C1visualizerFile, C1visualizerPass
 
@@ -40,7 +41,7 @@
 
   def assertParsesTo(self, c1Text, expectedData):
     expectedFile = self.createFile(expectedData)
-    actualFile = ParseC1visualizerStream("<c1_file>", io.StringIO(c1Text))
+    actualFile = ParseC1visualizerStream("<c1_file>", io.StringIO(ToUnicode(c1Text)))
     return self.assertEqual(expectedFile, actualFile)
 
   def test_EmptyFile(self):
diff --git a/tools/checker/file_format/checker/test.py b/tools/checker/file_format/checker/test.py
index 4c61664..221c5fb 100644
--- a/tools/checker/file_format/checker/test.py
+++ b/tools/checker/file_format/checker/test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python2
 #
 # Copyright (C) 2014 The Android Open Source Project
 #
@@ -15,6 +15,7 @@
 # limitations under the License.
 
 from common.archs               import archs_list
+from common.testing             import ToUnicode
 from file_format.checker.parser import ParseCheckerStream
 from file_format.checker.struct import CheckerFile, TestCase, TestStatement, TestExpression
 
@@ -26,7 +27,7 @@
 class CheckerParser_PrefixTest(unittest.TestCase):
 
   def tryParse(self, string):
-    checkerText = "/// CHECK-START: pass\n" + string
+    checkerText = u"/// CHECK-START: pass\n" + ToUnicode(string)
     return ParseCheckerStream("<test-file>", "CHECK", io.StringIO(checkerText))
 
   def assertParses(self, string):
@@ -75,8 +76,8 @@
 class CheckerParser_TestExpressionTest(unittest.TestCase):
 
   def parseStatement(self, string, variant=""):
-    checkerText = ("/// CHECK-START: pass\n" +
-                   "/// CHECK" + variant + ": " + string)
+    checkerText = (u"/// CHECK-START: pass\n" +
+                   u"/// CHECK" + ToUnicode(variant) + u": " + ToUnicode(string))
     checkerFile = ParseCheckerStream("<test-file>", "CHECK", io.StringIO(checkerText))
     self.assertEqual(len(checkerFile.testCases), 1)
     testCase = checkerFile.testCases[0]
@@ -213,7 +214,7 @@
     return self.assertEqual(expectedFile, actualFile)
 
   def parse(self, checkerText):
-    return ParseCheckerStream("<test_file>", "CHECK", io.StringIO(checkerText))
+    return ParseCheckerStream("<test_file>", "CHECK", io.StringIO(ToUnicode(checkerText)))
 
   def test_EmptyFile(self):
     self.assertParsesTo("", [])
@@ -316,7 +317,7 @@
                 """
 
   def parse(self, checkerText):
-    return ParseCheckerStream("<test_file>", "CHECK", io.StringIO(checkerText))
+    return ParseCheckerStream("<test_file>", "CHECK", io.StringIO(ToUnicode(checkerText)))
 
   def test_NonArchTests(self):
     for arch in [None] + archs_list:
@@ -375,7 +376,7 @@
 
 class CheckerParser_EvalTests(unittest.TestCase):
   def parseTestCase(self, string):
-    checkerText = "/// CHECK-START: pass\n" + string
+    checkerText = u"/// CHECK-START: pass\n" + ToUnicode(string)
     checkerFile = ParseCheckerStream("<test-file>", "CHECK", io.StringIO(checkerText))
     self.assertEqual(len(checkerFile.testCases), 1)
     return checkerFile.testCases[0]
diff --git a/tools/checker/match/file.py b/tools/checker/match/file.py
index 1683829..4a0923c 100644
--- a/tools/checker/match/file.py
+++ b/tools/checker/match/file.py
@@ -337,7 +337,7 @@
     c1Pass = c1File.findPass(testCase.name)
     if c1Pass is None:
       with file(c1File.fileName) as cfgFile:
-        Logger.log(''.join(cfgFile), Logger.Level.ERROR)
+        Logger.log(''.join(cfgFile), Logger.Level.Error)
       Logger.fail("Test case not found in the CFG file",
                   testCase.fileName, testCase.startLineNo, testCase.name)
 
diff --git a/tools/checker/match/test.py b/tools/checker/match/test.py
index 5e43fa0..16ace56 100644
--- a/tools/checker/match/test.py
+++ b/tools/checker/match/test.py
@@ -13,7 +13,9 @@
 # limitations under the License.
 
 from common.immutables               import ImmutableDict
+from common.testing                  import ToUnicode
 from file_format.c1visualizer.parser import ParseC1visualizerStream
+from file_format.c1visualizer.struct import C1visualizerFile, C1visualizerPass
 from file_format.checker.parser      import ParseCheckerStream, ParseCheckerStatement
 from file_format.checker.struct      import CheckerFile, TestCase, TestStatement
 from match.file                      import MatchTestCase, MatchFailedException, \
@@ -33,7 +35,9 @@
     return ParseCheckerStatement(testCase, checkerString, TestStatement.Variant.InOrder, 0)
 
   def tryMatch(self, checkerString, c1String, varState={}):
-    return MatchLines(self.createTestStatement(checkerString), c1String, ImmutableDict(varState))
+    return MatchLines(self.createTestStatement(checkerString),
+                      ToUnicode(c1String),
+                      ImmutableDict(varState))
 
   def assertMatches(self, checkerString, c1String, varState={}):
     self.assertIsNotNone(self.tryMatch(checkerString, c1String, varState))
@@ -141,8 +145,8 @@
       """
         end_cfg
       """
-    checkerFile = ParseCheckerStream("<test-file>", "CHECK", io.StringIO(checkerString))
-    c1File = ParseC1visualizerStream("<c1-file>", io.StringIO(c1String))
+    checkerFile = ParseCheckerStream("<test-file>", "CHECK", io.StringIO(ToUnicode(checkerString)))
+    c1File = ParseC1visualizerStream("<c1-file>", io.StringIO(ToUnicode(c1String)))
     assert len(checkerFile.testCases) == 1
     assert len(c1File.passes) == 1
     MatchTestCase(checkerFile.testCases[0], c1File.passes[0], c1File.instructionSetFeatures)
diff --git a/tools/checker/run_unit_tests.py b/tools/checker/run_unit_tests.py
index 9bd3f53..a0d274d 100755
--- a/tools/checker/run_unit_tests.py
+++ b/tools/checker/run_unit_tests.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python2
 #
 # Copyright (C) 2014 The Android Open Source Project
 #
@@ -27,5 +27,5 @@
 import unittest
 
 if __name__ == '__main__':
-  Logger.Verbosity = Logger.Level.NO_OUTPUT
+  Logger.Verbosity = Logger.Level.NoOutput
   unittest.main(verbosity=2)