util: Make cpt_upgraders python3 compatible

It won't be possible to build gem5 in a python3 only environment
otherwise since the cpt_upgrader.py script is automatically run at the
end of compilation

Change-Id: Iea4217e5cd64ca44b99aa0db5dabfdced7aeb3ea
Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/28587
Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/util/cpt_upgrader.py b/util/cpt_upgrader.py
index 4ac7dbf..96dcaf6 100755
--- a/util/cpt_upgrader.py
+++ b/util/cpt_upgrader.py
@@ -1,6 +1,6 @@
-#!/usr/bin/env python2.7
+#!/usr/bin/env python
 
-# Copyright (c) 2012-2013,2015-2016 ARM Limited
+# Copyright (c) 2012-2013,2015-2016, 2020 ARM Limited
 # All rights reserved
 #
 # The license below extends only to copyright in the software and shall
@@ -68,8 +68,9 @@
 # upgrader. This can be especially valuable when maintaining private
 # upgraders in private branches.
 
+from __future__ import print_function
 
-import ConfigParser
+from six.moves import configparser
 import glob, types, sys, os
 import os.path as osp
 
@@ -79,8 +80,8 @@
     if not verbose_print:
         return
     for arg in args:
-        print arg,
-    print
+        print(arg, end=' ')
+    print("\n")
 
 class Upgrader:
     tag_set = set()
@@ -89,7 +90,7 @@
     legacy = {}
     def __init__(self, filename):
         self.filename = filename
-        execfile(filename, {}, self.__dict__)
+        exec(open(filename).read(), {}, self.__dict__)
 
         if not hasattr(self, 'tag'):
             self.tag = osp.basename(filename)[:-3]
@@ -99,7 +100,7 @@
             self.depends = [self.depends]
 
         if not isinstance(self.depends, list):
-            print "Error: 'depends' for %s is the wrong type" % self.tag
+            print("Error: 'depends' for {} is the wrong type".format(self.tag))
             sys.exit(1)
 
         if hasattr(self, 'fwd_depends'):
@@ -109,23 +110,25 @@
             self.fwd_depends = []
 
         if not isinstance(self.fwd_depends, list):
-            print "Error: 'fwd_depends' for %s is the wrong type" % self.tag
+            print("Error: 'fwd_depends' for {} is the wrong type".format(
+                self.tag))
             sys.exit(1)
 
         if hasattr(self, 'upgrader'):
             if not isinstance(self.upgrader, types.FunctionType):
-                print "Error: 'upgrader' for %s is %s, not function" \
-                    % (self.tag, type(self))
+                print("Error: 'upgrader' for {} is {}, not function".format(
+                    self.tag, type(self)))
                 sys.exit(1)
             Upgrader.tag_set.add(self.tag)
         elif hasattr(self, 'downgrader'):
             if not isinstance(self.downgrader, types.FunctionType):
-                print "Error: 'downgrader' for %s is %s, not function" \
-                    % (self.tag, type(self))
+                print("Error: 'downgrader' for {} is {}, not function".format(
+                    self.tag, type(self)))
                 sys.exit(1)
             Upgrader.untag_set.add(self.tag)
         else:
-            print "Error: no upgrader or downgrader method for", self.tag
+            print("Error: no upgrader or downgrader method for".format(
+                self.tag))
             sys.exit(1)
 
         if hasattr(self, 'legacy_version'):
@@ -170,14 +173,14 @@
         for tag, upg in Upgrader.by_tag.items():
             for fd in upg.fwd_depends:
                 if fd not in Upgrader.by_tag:
-                    print "Error: '%s' cannot (forward) depend on "\
-                          "nonexistent tag '%s'" % (fd, tag)
+                    print("Error: '{}' cannot (forward) depend on "
+                          "nonexistent tag '{}'".format(fd, tag))
                     sys.exit(1)
                 Upgrader.by_tag[fd].depends.append(tag)
             for dep in upg.depends:
                 if dep not in Upgrader.by_tag:
-                    print "Error: '%s' cannot depend on "\
-                          "nonexistent tag '%s'" % (tag, dep)
+                    print("Error: '{}' cannot depend on "
+                          "nonexistent tag '{}'".format(tag, dep))
                     sys.exit(1)
 
 def process_file(path, **kwargs):
@@ -191,7 +194,7 @@
         import shutil
         shutil.copyfile(path, path + '.bak')
 
-    cpt = ConfigParser.SafeConfigParser()
+    cpt = configparser.SafeConfigParser()
 
     # gem5 is case sensitive with paramaters
     cpt.optionxform = str
@@ -219,7 +222,7 @@
     elif cpt.has_option('Globals','version_tags'):
         tags = set((''.join(cpt.get('Globals','version_tags'))).split())
     else:
-        print "fatal: no version information in checkpoint"
+        print("fatal: no version information in checkpoint")
         exit(1)
 
     verboseprint("has tags", ' '.join(tags))
@@ -228,8 +231,8 @@
     # simulator support for its changes.
     unknown_tags = tags - (Upgrader.tag_set | Upgrader.untag_set)
     if unknown_tags:
-        print "warning: upgrade script does not recognize the following "\
-              "tags in this checkpoint:", ' '.join(unknown_tags)
+        print("warning: upgrade script does not recognize the following "
+              "tags in this checkpoint:", ' '.join(unknown_tags))
 
     # Apply migrations for tags not in checkpoint and tags present for which
     # downgraders are present, respecting dependences
@@ -237,8 +240,8 @@
     while to_apply:
         ready = set([ t for t in to_apply if Upgrader.get(t).ready(tags) ])
         if not ready:
-            print "could not apply these upgrades:", ' '.join(to_apply)
-            print "update dependences impossible to resolve; aborting"
+            print("could not apply these upgrades:", ' '.join(to_apply))
+            print("update dependences impossible to resolve; aborting")
             exit(1)
 
         for tag in ready:
@@ -278,14 +281,14 @@
     Upgrader.load_all()
 
     if options.get_cc_file:
-        print "// this file is auto-generated by util/cpt_upgrader.py"
-        print "#include <string>"
-        print "#include <set>"
+        print("// this file is auto-generated by util/cpt_upgrader.py")
+        print("#include <string>")
+        print("#include <set>")
         print
-        print "std::set<std::string> version_tags = {"
+        print("std::set<std::string> version_tags = {")
         for tag in Upgrader.tag_set:
-            print "  \"%s\"," % tag
-        print "};"
+            print("  \"{}\",".format(tag))
+        print("};")
         exit(0)
     elif len(args) != 1:
         parser.error("You must specify a checkpoint file to modify or a "\
@@ -312,8 +315,8 @@
         elif osp.isfile(cpt_file):
             process_file(cpt_file, **vars(options))
         else:
-            print "Error: checkpoint file not found at in %s " % path,
-            print "and recurse not specified"
+            print("Error: checkpoint file not found in {} ".format(path))
+            print("and recurse not specified")
             sys.exit(1)
     sys.exit(0)
 
diff --git a/util/cpt_upgraders/arm-hdlcd-upgrade.py b/util/cpt_upgraders/arm-hdlcd-upgrade.py
index ce4b073..dbddc56 100644
--- a/util/cpt_upgraders/arm-hdlcd-upgrade.py
+++ b/util/cpt_upgraders/arm-hdlcd-upgrade.py
@@ -94,8 +94,8 @@
             cpt.set(sec_dma, "buffer", "")
 
 
-    print "Warning: Assuming that the HDLCD pixel clock and global frequency " \
-        "are still using their default values."
+    print("Warning: Assuming that the HDLCD pixel clock and global frequency "
+        "are still using their default values.")
     sec_osc = "system.realview.realview_io.osc_pxl"
     global_tick = 1E12
     pxl_freq = 137E6
diff --git a/util/cpt_upgraders/arm-miscreg-teehbr.py b/util/cpt_upgraders/arm-miscreg-teehbr.py
index 70359dd..f0174d5 100644
--- a/util/cpt_upgraders/arm-miscreg-teehbr.py
+++ b/util/cpt_upgraders/arm-miscreg-teehbr.py
@@ -7,7 +7,7 @@
             if re.search('.*sys.*\.cpu.*\.isa$', sec):
                 mr = cpt.get(sec, 'miscRegs').split()
                 if len(mr) == 161:
-                    print "MISCREG_TEEHBR already seems to be inserted."
+                    print("MISCREG_TEEHBR already seems to be inserted.")
                 else:
                     mr.insert(51,0); # Add dummy value for MISCREG_TEEHBR
                     cpt.set(sec, 'miscRegs', ' '.join(str(x) for x in mr))
diff --git a/util/cpt_upgraders/arm-sve.py b/util/cpt_upgraders/arm-sve.py
index 53fab7f..aa66045 100644
--- a/util/cpt_upgraders/arm-sve.py
+++ b/util/cpt_upgraders/arm-sve.py
@@ -23,7 +23,8 @@
                 # Updating SVE misc registers (dummy values)
                 mr = cpt.get(sec, 'miscRegs').split()
                 if len(mr) == 820:
-                    print "MISCREG_SVE registers already seems to be inserted."
+                    print("MISCREG_SVE registers already seems "
+                          "to be inserted.")
                 else:
                     # Replace MISCREG_FREESLOT_1 with MISCREG_ID_AA64ZFR0_EL1
                     mr[-1] = 0;
diff --git a/util/cpt_upgraders/armv8.py b/util/cpt_upgraders/armv8.py
index 4e4e2a3..1bb4c2b 100644
--- a/util/cpt_upgraders/armv8.py
+++ b/util/cpt_upgraders/armv8.py
@@ -3,10 +3,10 @@
     if cpt.get('root','isa') != 'arm':
         return
     import re
-    print "Warning: The size of the FP register file has changed. "\
-          "To get similar results you need to adjust the number of "\
-          "physical registers in the CPU you're restoring into by "\
-          "NNNN."
+    print("Warning: The size of the FP register file has changed. "
+          "To get similar results you need to adjust the number of "
+          "physical registers in the CPU you're restoring into by "
+          "NNNN.")
     # Find the CPU context's and upgrade their registers
     for sec in cpt.sections():
         re_xc_match = re.match('^.*?sys.*?\.cpu(\d+)*\.xc\.*', sec)
diff --git a/util/cpt_upgraders/x86-add-tlb.py b/util/cpt_upgraders/x86-add-tlb.py
index bd07a10..db465d5 100644
--- a/util/cpt_upgraders/x86-add-tlb.py
+++ b/util/cpt_upgraders/x86-add-tlb.py
@@ -12,6 +12,6 @@
                 cpt.set(sec, '_size', '0')
                 cpt.set(sec, 'lruSeq', '0')
     else:
-        print "ISA is not x86"
+        print("ISA is not x86")
 
 legacy_version = 6