[ovs-dev] [PATCH 23/55] extract-ofp: Python 3 compatibility.

Russell Bryant russell at ovn.org
Mon Dec 21 20:47:23 UTC 2015


Fix build-aux/extract-ofp-* scripts to be compatible with both Python 2
and Python 3.  The fixes do not require any new dependencies.

Signed-off-by: Russell Bryant <russell at ovn.org>
---
 build-aux/extract-ofp-actions | 72 ++++++++++++++++++++++---------------------
 build-aux/extract-ofp-errors  | 38 ++++++++++++-----------
 build-aux/extract-ofp-fields  | 10 +++---
 build-aux/extract-ofp-msgs    | 12 +++++---
 4 files changed, 70 insertions(+), 62 deletions(-)

diff --git a/build-aux/extract-ofp-actions b/build-aux/extract-ofp-actions
index 3a72349..48e39de 100755
--- a/build-aux/extract-ofp-actions
+++ b/build-aux/extract-ofp-actions
@@ -1,5 +1,7 @@
 #! /usr/bin/python
 
+from __future__ import print_function
+
 import sys
 import os.path
 import re
@@ -13,7 +15,7 @@ version_map = {"1.0": 0x01,
                "1.3": 0x04,
                "1.4": 0x05,
                "1.5": 0x06}
-version_reverse_map = dict((v, k) for (k, v) in version_map.iteritems())
+version_reverse_map = dict((v, k) for (k, v) in version_map.items())
 
 # Map from vendor name to the length of the action header.
 vendor_map = {"OF": (0x00000000,  4),
@@ -67,7 +69,7 @@ def fatal(msg):
 
 def usage():
     argv0 = os.path.basename(sys.argv[0])
-    print ('''\
+    print(('''\
 %(argv0)s, for extracting OpenFlow action data
 usage: %(argv0)s OFP_ACTIONS.C [--prototypes | --definitions]
 
@@ -77,7 +79,7 @@ actions.  With --prototypes, it outputs on stdout a set of prototypes to
 a set of definitions to #include late in ofp-actions.c
 
 OFP_ACTIONS.C should point to lib/ofp-actions.c.\
-''' % {"argv0": argv0})
+''' % {"argv0": argv0}))
     sys.exit(0)
 
 def extract_ofp_actions(fn, definitions):
@@ -231,36 +233,36 @@ def extract_ofp_actions(fn, definitions):
     if n_errors:
         sys.exit(1)
 
-    print """\
+    print("""\
 /* Generated automatically; do not modify!     -*- buffer-read-only: t -*- */
-"""
+""")
 
     if definitions:
-        print "/* Verify that structs used as actions are reasonable sizes. */"
+        print("/* Verify that structs used as actions are reasonable sizes. */")
         for s in sorted(arg_structs):
-            print "BUILD_ASSERT_DECL(sizeof(%s) %% OFP_ACTION_ALIGN == 0);" % s
+            print("BUILD_ASSERT_DECL(sizeof(%s) %% OFP_ACTION_ALIGN == 0);" % s)
 
-        print "\nstatic struct ofpact_raw_instance all_raw_instances[] = {"
+        print("\nstatic struct ofpact_raw_instance all_raw_instances[] = {")
         for vendor in domain:
             for type_ in domain[vendor]:
                 for version in domain[vendor][type_]:
                     d = domain[vendor][type_][version]
-                    print "    { { 0x%08x, %2d, 0x%02x }, " % (
-                        vendor, type_, version)
-                    print "      %s," % d["enum"]
-                    print "      HMAP_NODE_NULL_INITIALIZER,"
-                    print "      HMAP_NODE_NULL_INITIALIZER,"
-                    print "      %s," % d["min_length"]
-                    print "      %s," % d["max_length"]
-                    print "      %s," % d["arg_ofs"]
-                    print "      %s," % d["arg_len"]
-                    print "      \"%s\"," % re.sub('_RAW[0-9]*', '', d["enum"], 1)
+                    print("    { { 0x%08x, %2d, 0x%02x }, " % (
+                        vendor, type_, version))
+                    print("      %s," % d["enum"])
+                    print("      HMAP_NODE_NULL_INITIALIZER,")
+                    print("      HMAP_NODE_NULL_INITIALIZER,")
+                    print("      %s," % d["min_length"])
+                    print("      %s," % d["max_length"])
+                    print("      %s," % d["arg_ofs"])
+                    print("      %s," % d["arg_len"])
+                    print("      \"%s\"," % re.sub('_RAW[0-9]*', '', d["enum"], 1))
                     if d["deprecation"]:
-                        print "      \"%s\"," % re.sub(r'(["\\])', r'\\\1', d["deprecation"])
+                        print("      \"%s\"," % re.sub(r'(["\\])', r'\\\1', d["deprecation"]))
                     else:
-                        print "      NULL,"
-                    print "    },"
-        print "};";
+                        print("      NULL,")
+                    print("    },")
+        print("};");
 
     for versions in enums.values():
         need_ofp_version = False
@@ -307,23 +309,23 @@ def extract_ofp_actions(fn, definitions):
             decl += "}"
         else:
             decl += ";"
-        print decl
-        print
+        print(decl)
+        print()
 
     if definitions:
-        print """\
+        print("""\
 static enum ofperr
 ofpact_decode(const struct ofp_action_header *a, enum ofp_raw_action_type raw,
               enum ofp_version version, uint64_t arg, struct ofpbuf *out)
 {
     switch (raw) {\
-"""
+""")
         for versions in enums.values():
             enum = versions[0]["enum"]
-            print "    case %s:" % enum
+            print("    case %s:" % enum)
             base_argtype = versions[0]["base_argtype"]
             if base_argtype == 'void':
-                print "        return decode_%s(out);" % enum
+                print("        return decode_%s(out);" % enum)
             else:
                 if base_argtype.startswith('struct'):
                     arg = "ALIGNED_CAST(const %s *, a)" % base_argtype
@@ -333,14 +335,14 @@ ofpact_decode(const struct ofp_action_header *a, enum ofp_raw_action_type raw,
                         arg = "%s(arg)" % hton
                     else:
                         arg = "arg"
-                print "        return decode_%s(%s, version, out);" % (enum, arg)
-            print
-        print """\
+                print("        return decode_%s(%s, version, out);" % (enum, arg))
+            print()
+        print("""\
     default:
         OVS_NOT_REACHED();
     }
 }\
-"""
+""")
     else:
         for versions in enums.values():
             enum = versions[0]["enum"]
@@ -352,14 +354,14 @@ ofpact_decode(const struct ofp_action_header *a, enum ofp_raw_action_type raw,
                 else:
                     prototype += "%s, enum ofp_version, " % base_argtype
             prototype += "struct ofpbuf *);"
-            print prototype
+            print(prototype)
 
-        print """
+        print("""
 static enum ofperr ofpact_decode(const struct ofp_action_header *,
                                  enum ofp_raw_action_type raw,
                                  enum ofp_version version,
                                  uint64_t arg, struct ofpbuf *out);
-"""
+""")
 
 if __name__ == '__main__':
     if '--help' in sys.argv:
diff --git a/build-aux/extract-ofp-errors b/build-aux/extract-ofp-errors
index 16bfbc7..eeecd50 100755
--- a/build-aux/extract-ofp-errors
+++ b/build-aux/extract-ofp-errors
@@ -1,5 +1,7 @@
 #! /usr/bin/python
 
+from __future__ import print_function
+
 import sys
 import os.path
 import re
@@ -13,7 +15,7 @@ version_map = {"1.0": 0x01,
                "1.3": 0x04,
                "1.4": 0x05,
                "1.5": 0x06}
-version_reverse_map = dict((v, k) for (k, v) in version_map.iteritems())
+version_reverse_map = dict((v, k) for (k, v) in version_map.items())
 
 token = None
 line = ""
@@ -134,25 +136,25 @@ def parseTaggedName():
     return name
 
 def print_enum(tag, constants, storage_class):
-    print ("""
+    print(("""
 %(storage_class)sconst char *
 %(tag)s_to_string(uint16_t value)
 {
     switch (value) {\
 """ % {"tag": tag,
        "bufferlen": len(tag) + 32,
-       "storage_class": storage_class})
+       "storage_class": storage_class}))
     for constant in constants:
-        print ("    case %s: return \"%s\";" % (constant, constant))
-    print ("""\
+        print(("    case %s: return \"%s\";" % (constant, constant)))
+    print(("""\
     }
     return NULL;
 }\
-""" % {"tag": tag})
+""" % {"tag": tag}))
 
 def usage():
     argv0 = os.path.basename(sys.argv[0])
-    print ('''\
+    print(('''\
 %(argv0)s, for extracting OpenFlow error codes from header files
 usage: %(argv0)s ERROR_HEADER VENDOR_HEADER
 
@@ -164,7 +166,7 @@ strings.
 ERROR_HEADER should point to lib/ofp-errors.h.
 VENDOR_HEADER should point to include/openflow/openflow-common.h.
 The output is suitable for use as lib/ofp-errors.inc.\
-''' % {"argv0": argv0})
+''' % {"argv0": argv0}))
     sys.exit(0)
 
 def extract_vendor_ids(fn):
@@ -361,7 +363,7 @@ def extract_ofp_errors(fn):
     if n_errors:
         sys.exit(1)
 
-    print ("""\
+    print(("""\
 /* Generated automatically; do not modify!     -*- buffer-read-only: t -*- */
 
 #define OFPERR_N_ERRORS %d
@@ -383,14 +385,14 @@ static const char *error_comments[OFPERR_N_ERRORS] = {
 """ % (len(names),
        '\n'.join('    "%s",' % name for name in names),
        '\n'.join('    "%s",' % re.sub(r'(["\\])', r'\\\1', comment)
-                 for comment in comments)))
+                 for comment in comments))))
 
     def output_domain(map, name, description, version):
-        print ("""
+        print(("""
 static enum ofperr
 %s_decode(uint32_t vendor, uint16_t type, uint16_t code)
 {
-    switch (((uint64_t) vendor << 32) | (type << 16) | code) {""" % name)
+    switch (((uint64_t) vendor << 32) | (type << 16) | code) {""" % name))
         found = set()
         for enum in names:
             if enum not in map:
@@ -404,28 +406,28 @@ static enum ofperr
                 vendor_s = "(%#xULL << 32) | " % vendor
             else:
                 vendor_s = ""
-            print ("    case %s(%d << 16) | %d:" % (vendor_s, type_, code))
-            print ("        return OFPERR_%s;" % enum)
+            print(("    case %s(%d << 16) | %d:" % (vendor_s, type_, code)))
+            print(("        return OFPERR_%s;" % enum))
         print ("""\
     }
 
     return 0;
 }""")
 
-        print ("""
+        print(("""
 static const struct ofperr_domain %s = {
     "%s",
     %d,
     %s_decode,
-    {""" % (name, description, version, name))
+    {""" % (name, description, version, name)))
         for enum in names:
             if enum in map:
                 vendor, type_, code = map[enum]
                 if code == None:
                     code = -1
-                print "        { %#8x, %2d, %3d }, /* %s */" % (vendor, type_, code, enum)
+                print("        { %#8x, %2d, %3d }, /* %s */" % (vendor, type_, code, enum))
             else:
-                print ("        {       -1, -1,  -1 }, /* %s */" % enum)
+                print(("        {       -1, -1,  -1 }, /* %s */" % enum))
         print ("""\
     },
 };""")
diff --git a/build-aux/extract-ofp-fields b/build-aux/extract-ofp-fields
index 8d43e4b..13541e2 100755
--- a/build-aux/extract-ofp-fields
+++ b/build-aux/extract-ofp-fields
@@ -1,5 +1,7 @@
 #! /usr/bin/python
 
+from __future__ import print_function
+
 import sys
 import os.path
 import re
@@ -116,14 +118,14 @@ def fatal(msg):
 
 def usage():
     argv0 = os.path.basename(sys.argv[0])
-    print('''\
+    print(('''\
 %(argv0)s, for extracting OpenFlow field properties from meta-flow.h
 usage: %(argv0)s INPUT [--meta-flow | --nx-match]
   where INPUT points to lib/meta-flow.h in the source directory.
 Depending on the option given, the output written to stdout is intended to be
 saved either as lib/meta-flow.inc or lib/nx-match.inc for the respective C
 file to #include.\
-''' % {"argv0": argv0})
+''' % {"argv0": argv0}))
     sys.exit(0)
 
 
@@ -384,8 +386,8 @@ def make_nx_match(fields):
     for f in fields:
         # Sort by OpenFlow version number (nx-match.c depends on this).
         for oxm in sorted(f['OXM'], key=lambda x: x[2]):
-            print("""{ .nf = { %s, %d, "%s", %s } },""" % (
-                oxm[0], oxm[2], oxm[1], f['mff']))
+            print(("""{ .nf = { %s, %d, "%s", %s } },""" % (
+                oxm[0], oxm[2], oxm[1], f['mff'])))
     print("};")
     return output
 
diff --git a/build-aux/extract-ofp-msgs b/build-aux/extract-ofp-msgs
index b00039d..e382643 100755
--- a/build-aux/extract-ofp-msgs
+++ b/build-aux/extract-ofp-msgs
@@ -1,5 +1,7 @@
 #! /usr/bin/python
 
+from __future__ import print_function
+
 import sys
 import os.path
 import re
@@ -55,14 +57,14 @@ def fatal(msg):
 
 def usage():
     argv0 = os.path.basename(sys.argv[0])
-    print '''\
+    print('''\
 %(argv0)s, for extracting OpenFlow message types from header files
 usage: %(argv0)s INPUT OUTPUT
   where INPUT is the name of the input header file
     and OUTPUT is the output file name.
 Despite OUTPUT, the output is written to stdout, and the OUTPUT argument
 only controls #line directives in the output.\
-''' % {"argv0": argv0}
+''' % {"argv0": argv0})
     sys.exit(0)
 
 def make_sizeof(s):
@@ -300,7 +302,7 @@ def extract_ofp_msgs(output_file_name):
         for hdrs in r['hdrs']:
             output.append("    { {0, NULL}, {%d, %d, %d, 0x%x, %d}, %s, 0 },"
                           % (hdrs + (raw,)))
-                
+
         output.append("};")
 
     output.append("")
@@ -377,5 +379,5 @@ if __name__ == '__main__':
         line_number = 0
 
         for line in extract_ofp_msgs(sys.argv[2]):
-            print line
-        
+            print(line)
+
-- 
2.5.0




More information about the dev mailing list