scheduler-tools / rt-app

rt-app emulates typical mobile and real-time systems use cases and gives runtime information
GNU General Public License v2.0
125 stars 102 forks source link

Python 3 compatibility #103

Open bgermann opened 4 years ago

bgermann commented 4 years ago

Please make all the Python scripts compatible with recent versions of Python 3. Python 2 is end of life.

vingu-linaro commented 4 years ago

Added to my todo list

wookey commented 3 years ago

2to3 seemed to do an adequate job of this. I've fixed this in the debian package

(This patch also switches the shebang line #!/usr/bin/env python to #!/usr/bin/python3 which is correct in debian, but probably isn't upstream?)

Index: rt-app-1.0/doc/workgen
===================================================================
--- rt-app-1.0.orig/doc/workgen
+++ rt-app-1.0/doc/workgen
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3

 import os
 import sys
@@ -8,12 +8,12 @@ import signal

 def check_unikid_json(infile, outfile, verbose=0):
     if not os.path.exists(infile):
-        print "WARN: %s does not exist", infile
+        print("WARN: %s does not exist", infile)

     try:
         fi = open(infile, "r")
     except IOError:
-        print "WARN: Unable to open %s", infile
+        print("WARN: Unable to open %s", infile)
         sys.exit(2)

     lines = fi.readlines()
@@ -22,7 +22,7 @@ def check_unikid_json(infile, outfile, v
     try:
         fo = open(outfile, "w+")
     except IOError:
-        print "WARN: Unable to open %s", f
+        print("WARN: Unable to open %s", f)
         sys.exit(2)

     curid = 1
@@ -59,7 +59,7 @@ def check_unikid_json(infile, outfile, v
                 newkey_id = key_id + str(curid)

             if verbose:
-                print "level ", refcount, " : key ", key_id, " changed into ", newkey_id
+                print("level ", refcount, " : key ", key_id, " changed into ", newkey_id)

             myline = myline.replace(key_id, newkey_id, 1)
             key_id = newkey_id
@@ -74,12 +74,12 @@ def check_unikid_json(infile, outfile, v

 def check_suspend_json(infile, outfile, verbose=0):
     if not os.path.exists(infile):
-        print "WARN: %s does not exist", infile
+        print("WARN: %s does not exist", infile)

     try:
         fi = open(infile, "r")
     except IOError:
-        print "WARN: Unable to open %s", infile
+        print("WARN: Unable to open %s", infile)
         sys.exit(2)

     lines = fi.readlines()
@@ -88,7 +88,7 @@ def check_suspend_json(infile, outfile,
     try:
         fo = open(outfile, "w+")
     except IOError:
-        print "WARN: Unable to open %s", f
+        print("WARN: Unable to open %s", f)
         sys.exit(2)

@@ -125,7 +125,7 @@ def check_suspend_json(infile, outfile,
            exception == 1:

             if verbose:
-                print "value ", curid, " added to suspend key"
+                print("value ", curid, " added to suspend key")

             if "," in myline:
                 myline = myline.replace(",", " : " + "\"" + curid + "\",", 1)
@@ -155,7 +155,7 @@ if __name__ == '__main__':
     try:
         opts, args = getopt.getopt(sys.argv[1:], "o:avd")
     except getopt.GetoptError as err:
-        print str(err) # will print something like "option -a not recognized"
+        print(str(err)) # will print something like "option -a not recognized"
         sys.exit(2)

     for o, a in opts:
Index: rt-app-1.0/doc/merge.py
===================================================================
--- rt-app-1.0.orig/doc/merge.py
+++ rt-app-1.0/doc/merge.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3

 import os
 import sys
@@ -11,7 +11,7 @@ try:
     opts, args = getopt.getopt(sys.argv[1:], "o:")

 except getopt.GetoptError as err:
-    print str(err) # will print something like "option -a not recognized"
+    print(str(err)) # will print something like "option -a not recognized"
     sys.exit(2)
 for o, a in opts:
     if o == "-o":
@@ -20,22 +20,22 @@ for o, a in opts:
 merged = dict()
 for f in args:
     if not os.path.exists(f):
-        print "WARN: %s does not exist", f
+        print("WARN: %s does not exist", f)

     fp = open(f, "r")
     d = json.load(fp)
     fp.close()

     for key in d:
-        print key,
-        if merged.has_key(key):
-            print "WARNING: merged already has key", key
+        print(key, end=' ')
+        if key in merged:
+            print("WARNING: merged already has key", key)
             merged[key].update(d[key])
         else:
             merged[key] = d[key]

-    print merged
-    print
+    print(merged)
+    print()

 fp = open(outfile, "w")
 json.dump(merged, fp, indent=4, sort_keys=True)
Index: rt-app-1.0/doc/tune_json.py
===================================================================
--- rt-app-1.0.orig/doc/tune_json.py
+++ rt-app-1.0/doc/tune_json.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3

 import argparse
 import collections
@@ -56,7 +56,7 @@ def load_json_file(filename):
     try:
         f = open(filename, 'r')
     except:
-        print 'ERROR: Unable to open %s' %filename
+        print('ERROR: Unable to open %s' %filename)
         sys.exit(2)

     comment_re = re.compile(
@@ -100,7 +100,7 @@ if __name__ == '__main__':
     args = parser.parse_args()

     if not os.path.isfile(args.infile):
-        print 'ERROR: input file %s does not exist\n' %args.infile
+        print('ERROR: input file %s does not exist\n' %args.infile)
         parser.print_help()
         sys.exit(2)

@@ -109,7 +109,7 @@ if __name__ == '__main__':
     if args.key:
         target = find_dict_by_key(doc, args.key)
         if not target:
-            print 'ERROR: key id %s is not found' %args.key
+            print('ERROR: key id %s is not found' %args.key)
             sys.exit(2)

     if args.instance > 0: