Port gen_entries.py script to Python 3. am: fe6cfbfe3a am: eb9b5ea0d8 am: 5e54e9de8e

Original change: https://android-review.googlesource.com/c/device/generic/opengl-transport/+/2351583

Change-Id: Id8bf9f1ec73138bab5b03dfdc1e18282e7c3fc94
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/host/libs/virglrenderer/Android.bp b/host/libs/virglrenderer/Android.bp
index c05b1e5..4c7de86 100644
--- a/host/libs/virglrenderer/Android.bp
+++ b/host/libs/virglrenderer/Android.bp
@@ -102,7 +102,7 @@
 genrule {
   name: "gles1_core_functions_hdr",
   tool_files: ["gen_entries.py"],
-  cmd: "python $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
+  cmd: "python3 $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
   srcs: ["OpenGLESDispatch/gles1_core.entries"],
   out: ["gles1_core_functions.h"],
 }
@@ -110,7 +110,7 @@
 genrule {
   name: "gles1_extensions_functions_hdr",
   tool_files: ["gen_entries.py"],
-  cmd: "python $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
+  cmd: "python3 $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
   srcs: ["OpenGLESDispatch/gles1_extensions.entries"],
   out: ["gles1_extensions_functions.h"],
 }
@@ -118,7 +118,7 @@
 genrule {
   name: "egl_functions_hdr",
   tool_files: ["gen_entries.py"],
-  cmd: "python $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
+  cmd: "python3 $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
   srcs: ["OpenGLESDispatch/egl.entries"],
   out: ["egl_functions.h"],
 }
@@ -126,7 +126,7 @@
 genrule {
   name: "gles3_only_functions_hdr",
   tool_files: ["gen_entries.py"],
-  cmd: "python $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
+  cmd: "python3 $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
   srcs: ["OpenGLESDispatch/gles3_only.entries"],
   out: ["gles3_only_functions.h"],
 }
@@ -134,7 +134,7 @@
 genrule {
   name: "gles31_only_functions_hdr",
   tool_files: ["gen_entries.py"],
-  cmd: "python $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
+  cmd: "python3 $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
   srcs: ["OpenGLESDispatch/gles31_only.entries"],
   out: ["gles31_only_functions.h"],
 }
@@ -142,7 +142,7 @@
 genrule {
   name: "gles2_extensions_functions_hdr",
   tool_files: ["gen_entries.py"],
-  cmd: "python $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
+  cmd: "python3 $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
   srcs: ["OpenGLESDispatch/gles2_extensions.entries"],
   out: ["gles2_extensions_functions.h"],
 }
@@ -150,7 +150,7 @@
 genrule {
   name: "egl_extensions_functions_hdr",
   tool_files: ["gen_entries.py"],
-  cmd: "python $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
+  cmd: "python3 $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
   srcs: ["OpenGLESDispatch/egl_extensions.entries"],
   out: ["egl_extensions_functions.h"],
 }
@@ -158,7 +158,7 @@
 genrule {
   name: "gles2_core_functions_hdr",
   tool_files: ["gen_entries.py"],
-  cmd: "python $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
+  cmd: "python3 $(location gen_entries.py) --mode=funcargs $(in) --output $(out)",
   srcs: ["OpenGLESDispatch/gles2_core.entries"],
   out: ["gles2_core_functions.h"],
 }
diff --git a/host/libs/virglrenderer/gen_entries.py b/host/libs/virglrenderer/gen_entries.py
index fbfdb4d..f0959c4 100755
--- a/host/libs/virglrenderer/gen_entries.py
+++ b/host/libs/virglrenderer/gen_entries.py
@@ -25,15 +25,17 @@
 #
 # Anything else is an error.
 
+import argparse
 import re
 import sys
-import argparse
 
 re_func = re.compile(r"""^(.*[\* ])([A-Za-z_][A-Za-z0-9_]*)\((.*)\);$""")
 re_param = re.compile(r"""^(.*[\* ])([A-Za-z_][A-Za-z0-9_]*)$""")
 
+
 class Entry:
     """Small class used to model a single DLL entry point."""
+
     def __init__(self, func_name, return_type, parameters):
         """Initialize Entry instance. |func_name| is the function name,
            |return_type| its return type, and |parameters| is a list of
@@ -53,6 +55,7 @@
             self.call += "%s%s" % (comma, param[1])
             comma = ", "
 
+
 def banner_command(argv):
     """Return sanitized command-line description.
        |argv| must be a list of command-line parameters, e.g. sys.argv.
@@ -62,7 +65,8 @@
     # Remove path from first parameter
     argv = argv[:]
     argv[0] = "host/commands/gen-entries.py"
-    return ' '.join(argv)
+    return " ".join(argv)
+
 
 def parse_entries_file(lines):
     """Parse an .entries file and return a tuple of:
@@ -81,12 +85,12 @@
         line = line.strip()
         if len(line) == 0:  # Ignore empty lines
             continue
-        if line[0] == '#':  # Ignore comments
+        if line[0] == "#":  # Ignore comments
             continue
-        if line[0] == '!':  # Prefix name
+        if line[0] == "!":  # Prefix name
             prefix_name = line[1:]
             continue
-        if line[0] == '%':  # Verbatim line copy
+        if line[0] == "%":  # Verbatim line copy
             verbatim.append(line[1:])
             continue
         # Must be a function signature.
@@ -128,28 +132,28 @@
     """
     prefix_name = prefix_name.upper()
 
-    print "// Auto-generated with: %s" % banner_command(sys.argv)
-    print "// DO NOT EDIT THIS FILE"
-    print ""
-    print "#ifndef %s_FUNCTIONS_H" % prefix_name
-    print "#define %s_FUNCTIONS_H" % prefix_name
-    print ""
+    print("// Auto-generated with: " + banner_command(sys.argv))
+    print("// DO NOT EDIT THIS FILE")
+    print("")
+    print(f"#ifndef {prefix_name}_FUNCTIONS_H")
+    print(f"#define {prefix_name}_FUNCTIONS_H")
+    print("")
     for line in verbatim:
-        print line
+        print(line)
 
-    print "#define LIST_%s_FUNCTIONS(X) \\" % prefix_name
+    print(f"#define LIST_{prefix_name}_FUNCTIONS(X) \\")
     for entry in entries:
         if with_args:
-            print "  X(%s, %s, (%s), (%s)) \\" % \
-                    (entry.return_type, entry.func_name, entry.parameters,
-                     entry.call)
+            print(f"  X({entry.return_type}, {entry.func_name}, "
+                  f"({entry.parameters}), ({entry.call})) \\")
         else:
-            print "  X(%s, %s, (%s)) \\" % \
-                    (entry.return_type, entry.func_name, entry.parameters)
+            print(f"  X({entry.return_type}, {entry.func_name}, "
+                  f"({entry.parameters})) \\")
 
-    print ""
-    print ""
-    print "#endif  // %s_FUNCTIONS_H" % prefix_name
+    print("")
+    print("")
+    print(f"#endif  // {prefix_name}_FUNCTIONS_H")
+
 
 def gen_dll_wrapper(entries, prefix_name, verbatim, filename):
     """Generate a C source file that contains functions that act as wrappers
@@ -163,77 +167,74 @@
 
     ENTRY_PREFIX = "__dll_"
 
-    print "// Auto-generated with: %s" % banner_command(sys.argv)
-    print "// DO NOT EDIT THIS FILE"
-    print ""
-    print "#include <dlfcn.h>"
+    print("// Auto-generated with: " + banner_command(sys.argv))
+    print("// DO NOT EDIT THIS FILE")
+    print("")
+    print("#include <dlfcn.h>")
     for line in verbatim:
-        print line
+        print(line)
 
-    print ""
-    print "///"
-    print "///  W R A P P E R   P O I N T E R S"
-    print "///"
-    print ""
+    print("")
+    print("///")
+    print("///  W R A P P E R   P O I N T E R S")
+    print("///")
+    print("")
     for entry in entries:
         ptr_name = ENTRY_PREFIX + entry.func_name
-        print "static %s (*%s)(%s) = 0;" % \
-                (entry.return_type, ptr_name, entry.parameters)
+        print(f"static {entry.return_type} "
+              f"(*{ptr_name})({entry.parameters}) = 0;")
 
-    print ""
-    print "///"
-    print "///  W R A P P E R   F U N C T I O N S"
-    print "///"
-    print ""
+    print("")
+    print("///")
+    print("///  W R A P P E R   F U N C T I O N S")
+    print("///")
+    print("")
 
     for entry in entries:
-        print "%s %s(%s) {" % \
-                (entry.return_type, entry.func_name, entry.parameters)
+        print(f"{entry.return_type} {entry.func_name}({entry.parameters}) {{")
         ptr_name = ENTRY_PREFIX + entry.func_name
         if entry.return_type != "void":
-            print "  return %s(%s);" % (ptr_name, entry.call)
+            print(f"  return {ptr_name}({entry.call});")
         else:
-            print "  %s(%s);" % (ptr_name, entry.call)
-        print "}\n"
+            print("  {ptr_name}({entry.call});")
+        print("}\n")
 
-    print ""
-    print "///"
-    print "///  I N I T I A L I Z A T I O N   F U N C T I O N"
-    print "///"
-    print ""
+    print("")
+    print("///")
+    print("///  I N I T I A L I Z A T I O N   F U N C T I O N")
+    print("///")
+    print("")
 
-    print "int %s_dynlink_init(void* lib) {" % prefix_name
+    print(f"int {prefix_name}_dynlink_init(void* lib) {{")
     for entry in entries:
         ptr_name = ENTRY_PREFIX + entry.func_name
-        print "  %s = (%s(*)(%s))dlsym(lib, \"%s\");" % \
-                (ptr_name,
-                 entry.return_type,
-                 entry.parameters,
-                 entry.func_name)
-        print "  if (!%s) return -1;" % ptr_name
-    print "  return 0;"
-    print "}"
+        print(f"  {ptr_name} = ({entry.return_type}(*)({entry.parameters})) "
+              f"dlsym(lib, \"{entry.func_name}\");")
+        print(f"  if (!{ptr_name}) return -1;")
+    print("  return 0;")
+    print("}")
 
 
 def gen_windows_def_file(entries):
     """Generate a windows DLL .def file. |entries| is a list of Entry instances.
     """
-    print "EXPORTS"
+    print("EXPORTS")
     for entry in entries:
-        print "    %s" % entry.func_name
+        print("    " + entry.func_name)
 
 
 def gen_unix_sym_file(entries):
     """Generate an ELF linker version file. |entries| is a list of Entry
        instances.
     """
-    print "VERSION {"
-    print "\tglobal:"
+    print("VERSION {")
+    print("\tglobal:")
     for entry in entries:
-        print "\t\t%s;" % entry.func_name
-    print "\tlocal:"
-    print "\t\t*;"
-    print "};"
+        print(f"\t\t{entry.func_name};")
+    print("\tlocal:")
+    print("\t\t*;")
+    print("};")
+
 
 def gen_symbols(entries, underscore):
     """Generate a list of symbols from |entries|, a list of Entry instances.
@@ -244,7 +245,8 @@
     if underscore:
         prefix = "_"
     for entry in entries:
-        print "%s%s" % (prefix, entry.func_name)
+        print(prefix + entry.func_name)
+
 
 def parse_file(filename, lines, mode):
     """Generate one of possible outputs from |filename|. |lines| must be a list
@@ -254,7 +256,7 @@
     entries, prefix_name, verbatim, errors = parse_entries_file(lines)
     if errors:
         for error in errors:
-            print >> sys.stderr, "ERROR: %s:%s" % (filename, error)
+            print(f"ERROR: {filename}:{error}", file=sys.stderr)
         sys.exit(1)
 
     if not prefix_name:
@@ -305,7 +307,7 @@
 args = parser.parse_args()
 
 if not args.mode:
-    print >> sys.stderr, "ERROR: Please use --mode=<name>, see --help."
+    print("ERROR: Please use --mode=<name>, see --help.", file=sys.stderr)
     sys.exit(1)
 
 if args.output: