Revert^2 "Port Checker to python 3"

This reverts commit 5409408a41045c7f178e362311eb51dab992d6db.

Reason for revert: The original issue (b/170308859) is fixed by
https://android-review.googlesource.com/c/platform/art/+/1458282.

Test: art/tools/checker/run_unit_tests.py
Test: art/test/testrunner/testrunner.py --host
Bug: 170308859
Bug: 162408889

Change-Id: Ibc4539403ea77a9e718c830466459e6354ef0f23
diff --git a/tools/checker/checker.py b/tools/checker/checker.py
index ad4cdd2..78bd585 100755
--- a/tools/checker/checker.py
+++ b/tools/checker/checker.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 #
 # Copyright (C) 2014 The Android Open Source Project
 #
@@ -103,7 +103,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 aa3a92f..67bb674 100644
--- a/tools/checker/common/logger.py
+++ b/tools/checker/common/logger.py
@@ -12,42 +12,50 @@
 # 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 Level(object):
-    NoOutput, Error, Info = range(3)
+class Logger:
 
-  class Color(object):
-    Default, Blue, Gray, Purple, Red, Green = range(6)
+  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
 
     @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:
@@ -56,8 +64,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 = ""
@@ -67,8 +75,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
@@ -77,23 +85,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 819de24..e44c84d 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 "<%s: %s>" % (type(self).__name__, str(self.__dict__))
+    return '<{}: {}>'.format(type(self).__name__, str(self.__dict__))
diff --git a/tools/checker/common/testing.py b/tools/checker/common/testing.py
deleted file mode 100644
index 1299c07..0000000
--- a/tools/checker/common/testing.py
+++ /dev/null
@@ -1,22 +0,0 @@
-# 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 b25d2b2..e8a6ac8 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("name\s+\"[^\"]+\"", line):
+    if re.match(r"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("method\s+\"[^\"]*\"", line):
+    if re.match(r"method\s+\"[^\"]*\"", line):
       methodName = line.split("\"")[1].strip()
       if not methodName:
         Logger.fail("Empty method name in output", fileName, lineNo)
 
-      m = re.search("isa_features:([\w,-]+)", methodName)
+      m = re.search(r"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 e5acd62..a118a92 100644
--- a/tools/checker/file_format/c1visualizer/test.py
+++ b/tools/checker/file_format/c1visualizer/test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 #
 # Copyright (C) 2014 The Android Open Source Project
 #
@@ -15,7 +15,6 @@
 # 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
 
@@ -41,7 +40,7 @@
 
   def assertParsesTo(self, c1Text, expectedData):
     expectedFile = self.createFile(expectedData)
-    actualFile = ParseC1visualizerStream("<c1_file>", io.StringIO(ToUnicode(c1Text)))
+    actualFile = ParseC1visualizerStream("<c1_file>", io.StringIO(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 221c5fb..4c61664 100644
--- a/tools/checker/file_format/checker/test.py
+++ b/tools/checker/file_format/checker/test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 #
 # Copyright (C) 2014 The Android Open Source Project
 #
@@ -15,7 +15,6 @@
 # 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
 
@@ -27,7 +26,7 @@
 class CheckerParser_PrefixTest(unittest.TestCase):
 
   def tryParse(self, string):
-    checkerText = u"/// CHECK-START: pass\n" + ToUnicode(string)
+    checkerText = "/// CHECK-START: pass\n" + string
     return ParseCheckerStream("<test-file>", "CHECK", io.StringIO(checkerText))
 
   def assertParses(self, string):
@@ -76,8 +75,8 @@
 class CheckerParser_TestExpressionTest(unittest.TestCase):
 
   def parseStatement(self, string, variant=""):
-    checkerText = (u"/// CHECK-START: pass\n" +
-                   u"/// CHECK" + ToUnicode(variant) + u": " + ToUnicode(string))
+    checkerText = ("/// CHECK-START: pass\n" +
+                   "/// CHECK" + variant + ": " + string)
     checkerFile = ParseCheckerStream("<test-file>", "CHECK", io.StringIO(checkerText))
     self.assertEqual(len(checkerFile.testCases), 1)
     testCase = checkerFile.testCases[0]
@@ -214,7 +213,7 @@
     return self.assertEqual(expectedFile, actualFile)
 
   def parse(self, checkerText):
-    return ParseCheckerStream("<test_file>", "CHECK", io.StringIO(ToUnicode(checkerText)))
+    return ParseCheckerStream("<test_file>", "CHECK", io.StringIO(checkerText))
 
   def test_EmptyFile(self):
     self.assertParsesTo("", [])
@@ -317,7 +316,7 @@
                 """
 
   def parse(self, checkerText):
-    return ParseCheckerStream("<test_file>", "CHECK", io.StringIO(ToUnicode(checkerText)))
+    return ParseCheckerStream("<test_file>", "CHECK", io.StringIO(checkerText))
 
   def test_NonArchTests(self):
     for arch in [None] + archs_list:
@@ -376,7 +375,7 @@
 
 class CheckerParser_EvalTests(unittest.TestCase):
   def parseTestCase(self, string):
-    checkerText = u"/// CHECK-START: pass\n" + ToUnicode(string)
+    checkerText = "/// CHECK-START: pass\n" + 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 1f68666..6f36313 100644
--- a/tools/checker/match/file.py
+++ b/tools/checker/match/file.py
@@ -336,8 +336,8 @@
     # match a check group against the first output group of the same name.
     c1Pass = c1File.findPass(testCase.name)
     if c1Pass is None:
-      with file(c1File.fullFileName) as cfgFile:
-        Logger.log(''.join(cfgFile), Logger.Level.Error)
+      with open(c1File.fullFileName) as cfgFile:
+        Logger.log(''.join(cfgFile), Logger.Level.ERROR)
       Logger.fail("Test case not found in the CFG file",
                   testCase.fullFileName, testCase.startLineNo, testCase.name)
 
@@ -353,6 +353,6 @@
         msg = "Statement could not be matched starting from line {}"
       msg = msg.format(lineNo)
       if printCfg:
-        with file(c1File.fullFileName) as cfgFile:
+        with open(c1File.fullFileName) as cfgFile:
           Logger.log(''.join(cfgFile), Logger.Level.Error)
       Logger.testFailed(msg, e.statement, e.variables)
diff --git a/tools/checker/match/test.py b/tools/checker/match/test.py
index 16ace56..5e43fa0 100644
--- a/tools/checker/match/test.py
+++ b/tools/checker/match/test.py
@@ -13,9 +13,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
 from file_format.checker.parser      import ParseCheckerStream, ParseCheckerStatement
 from file_format.checker.struct      import CheckerFile, TestCase, TestStatement
 from match.file                      import MatchTestCase, MatchFailedException, \
@@ -35,9 +33,7 @@
     return ParseCheckerStatement(testCase, checkerString, TestStatement.Variant.InOrder, 0)
 
   def tryMatch(self, checkerString, c1String, varState={}):
-    return MatchLines(self.createTestStatement(checkerString),
-                      ToUnicode(c1String),
-                      ImmutableDict(varState))
+    return MatchLines(self.createTestStatement(checkerString), c1String, ImmutableDict(varState))
 
   def assertMatches(self, checkerString, c1String, varState={}):
     self.assertIsNotNone(self.tryMatch(checkerString, c1String, varState))
@@ -145,8 +141,8 @@
       """
         end_cfg
       """
-    checkerFile = ParseCheckerStream("<test-file>", "CHECK", io.StringIO(ToUnicode(checkerString)))
-    c1File = ParseC1visualizerStream("<c1-file>", io.StringIO(ToUnicode(c1String)))
+    checkerFile = ParseCheckerStream("<test-file>", "CHECK", io.StringIO(checkerString))
+    c1File = ParseC1visualizerStream("<c1-file>", io.StringIO(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 a0d274d..9bd3f53 100755
--- a/tools/checker/run_unit_tests.py
+++ b/tools/checker/run_unit_tests.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 #
 # Copyright (C) 2014 The Android Open Source Project
 #
@@ -27,5 +27,5 @@
 import unittest
 
 if __name__ == '__main__':
-  Logger.Verbosity = Logger.Level.NoOutput
+  Logger.Verbosity = Logger.Level.NO_OUTPUT
   unittest.main(verbosity=2)