소스 검색

qapi: Factor open_output(), close_output() out of generators

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Markus Armbruster 10 년 전
부모
커밋
12f8e1b9ff
5개의 변경된 파일141개의 추가작업 그리고 215개의 파일을 삭제
  1. 37 64
      scripts/qapi-commands.py
  2. 20 55
      scripts/qapi-event.py
  3. 18 49
      scripts/qapi-types.py
  4. 16 47
      scripts/qapi-visit.py
  5. 50 0
      scripts/qapi.py

+ 37 - 64
scripts/qapi-commands.py

@@ -15,8 +15,6 @@
 from ordereddict import OrderedDict
 from ordereddict import OrderedDict
 from qapi import *
 from qapi import *
 import re
 import re
-import os
-import errno
 
 
 def generate_command_decl(name, args, ret_type):
 def generate_command_decl(name, args, ret_type):
     arglist=""
     arglist=""
@@ -311,51 +309,18 @@ def gen_registry(commands):
                 registry=registry.rstrip())
                 registry=registry.rstrip())
     return ret
     return ret
 
 
-def gen_command_decl_prologue(header, guard, prefix=""):
+def gen_command_decl_prologue(prefix=""):
     ret = mcgen('''
     ret = mcgen('''
-/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
-/*
- * schema-defined QAPI function prototypes
- *
- * Copyright IBM, Corp. 2011
- *
- * Authors:
- *  Anthony Liguori   <aliguori@us.ibm.com>
- *
- * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
- * See the COPYING.LIB file in the top-level directory.
- *
- */
-
-#ifndef %(guard)s
-#define %(guard)s
-
 #include "%(prefix)sqapi-types.h"
 #include "%(prefix)sqapi-types.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/error.h"
 #include "qapi/error.h"
 
 
 ''',
 ''',
-                 header=basename(header), guard=guardname(header), prefix=prefix)
+                 prefix=prefix)
     return ret
     return ret
 
 
 def gen_command_def_prologue(prefix="", proxy=False):
 def gen_command_def_prologue(prefix="", proxy=False):
     ret = mcgen('''
     ret = mcgen('''
-/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
-/*
- * schema-defined QMP->QAPI command dispatch
- *
- * Copyright IBM, Corp. 2011
- *
- * Authors:
- *  Anthony Liguori   <aliguori@us.ibm.com>
- *
- * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
- * See the COPYING.LIB file in the top-level directory.
- *
- */
-
 #include "qemu-common.h"
 #include "qemu-common.h"
 #include "qemu/module.h"
 #include "qemu/module.h"
 #include "qapi/qmp/qerror.h"
 #include "qapi/qmp/qerror.h"
@@ -374,8 +339,6 @@ def gen_command_def_prologue(prefix="", proxy=False):
         ret += '#include "%sqmp-commands.h"' % prefix
         ret += '#include "%sqmp-commands.h"' % prefix
     return ret + "\n\n"
     return ret + "\n\n"
 
 
-c_file = 'qmp-marshal.c'
-h_file = 'qmp-commands.h'
 middle_mode = False
 middle_mode = False
 
 
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
@@ -385,29 +348,44 @@ def gen_command_def_prologue(prefix="", proxy=False):
     if o in ("-m", "--middle"):
     if o in ("-m", "--middle"):
         middle_mode = True
         middle_mode = True
 
 
-c_file = output_dir + prefix + c_file
-h_file = output_dir + prefix + h_file
-
-def maybe_open(really, name, opt):
-    if really:
-        return open(name, opt)
-    else:
-        import StringIO
-        return StringIO.StringIO()
-
-try:
-    os.makedirs(output_dir)
-except os.error, e:
-    if e.errno != errno.EEXIST:
-        raise
-
 exprs = parse_schema(input_file)
 exprs = parse_schema(input_file)
 commands = filter(lambda expr: expr.has_key('command'), exprs)
 commands = filter(lambda expr: expr.has_key('command'), exprs)
 commands = filter(lambda expr: not expr.has_key('gen'), commands)
 commands = filter(lambda expr: not expr.has_key('gen'), commands)
 
 
-fdecl = maybe_open(do_h, h_file, 'w')
-fdef = maybe_open(do_c, c_file, 'w')
-ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
+c_comment = '''
+/*
+ * schema-defined QMP->QAPI command dispatch
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+'''
+h_comment = '''
+/*
+ * schema-defined QAPI function prototypes
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+'''
+
+(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
+                            'qmp-marshal.c', 'qmp-commands.h',
+                            c_comment, h_comment)
+
+ret = gen_command_decl_prologue(prefix=prefix)
 fdecl.write(ret)
 fdecl.write(ret)
 ret = gen_command_def_prologue(prefix=prefix)
 ret = gen_command_def_prologue(prefix=prefix)
 fdef.write(ret)
 fdef.write(ret)
@@ -431,13 +409,8 @@ def maybe_open(really, name, opt):
     ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
     ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
     fdef.write(ret)
     fdef.write(ret)
 
 
-fdecl.write("\n#endif\n");
-
 if not middle_mode:
 if not middle_mode:
     ret = gen_registry(commands)
     ret = gen_registry(commands)
     fdef.write(ret)
     fdef.write(ret)
 
 
-fdef.flush()
-fdef.close()
-fdecl.flush()
-fdecl.close()
+close_output(fdef, fdecl)

+ 20 - 55
scripts/qapi-event.py

@@ -11,8 +11,6 @@
 
 
 from ordereddict import OrderedDict
 from ordereddict import OrderedDict
 from qapi import *
 from qapi import *
-import os
-import errno
 
 
 def _generate_event_api_name(event_name, params):
 def _generate_event_api_name(event_name, params):
     api_name = "void qapi_event_send_%s(" % c_name(event_name).lower();
     api_name = "void qapi_event_send_%s(" % c_name(event_name).lower();
@@ -214,36 +212,9 @@ def generate_event_enum_lookup(event_enum_name, event_enum_strings):
 ''')
 ''')
     return ret
     return ret
 
 
-
-# Start the real job
-
-c_file = 'qapi-event.c'
-h_file = 'qapi-event.h'
-
 (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
 (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
 
 
-c_file = output_dir + prefix + c_file
-h_file = output_dir + prefix + h_file
-
-try:
-    os.makedirs(output_dir)
-except os.error, e:
-    if e.errno != errno.EEXIST:
-        raise
-
-def maybe_open(really, name, opt):
-    if really:
-        return open(name, opt)
-    else:
-        import StringIO
-        return StringIO.StringIO()
-
-fdef = maybe_open(do_c, c_file, 'w')
-fdecl = maybe_open(do_h, h_file, 'w')
-
-fdef.write(mcgen('''
-/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
+c_comment = '''
 /*
 /*
  * schema-defined QAPI event functions
  * schema-defined QAPI event functions
  *
  *
@@ -256,19 +227,8 @@ def maybe_open(really, name, opt):
  * See the COPYING.LIB file in the top-level directory.
  * See the COPYING.LIB file in the top-level directory.
  *
  *
  */
  */
-
-#include "qemu-common.h"
-#include "%(header)s"
-#include "%(prefix)sqapi-visit.h"
-#include "qapi/qmp-output-visitor.h"
-#include "qapi/qmp-event.h"
-
-''',
-                 prefix=prefix, header=basename(h_file)))
-
-fdecl.write(mcgen('''
-/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
+'''
+h_comment = '''
 /*
 /*
  * schema-defined QAPI event functions
  * schema-defined QAPI event functions
  *
  *
@@ -281,16 +241,29 @@ def maybe_open(really, name, opt):
  * See the COPYING.LIB file in the top-level directory.
  * See the COPYING.LIB file in the top-level directory.
  *
  *
  */
  */
+'''
 
 
-#ifndef %(guard)s
-#define %(guard)s
+(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
+                            'qapi-event.c', 'qapi-event.h',
+                            c_comment, h_comment)
 
 
+fdef.write(mcgen('''
+#include "qemu-common.h"
+#include "%(prefix)sqapi-event.h"
+#include "%(prefix)sqapi-visit.h"
+#include "qapi/qmp-output-visitor.h"
+#include "qapi/qmp-event.h"
+
+''',
+                 prefix=prefix))
+
+fdecl.write(mcgen('''
 #include "qapi/error.h"
 #include "qapi/error.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qdict.h"
 #include "%(prefix)sqapi-types.h"
 #include "%(prefix)sqapi-types.h"
 
 
 ''',
 ''',
-                  prefix=prefix, guard=guardname(h_file)))
+                  prefix=prefix))
 
 
 exprs = parse_schema(input_file)
 exprs = parse_schema(input_file)
 
 
@@ -323,12 +296,4 @@ def maybe_open(really, name, opt):
 ret = generate_event_enum_lookup(event_enum_name, event_enum_strings)
 ret = generate_event_enum_lookup(event_enum_name, event_enum_strings)
 fdef.write(ret)
 fdef.write(ret)
 
 
-fdecl.write('''
-#endif
-''')
-
-fdecl.flush()
-fdecl.close()
-
-fdef.flush()
-fdef.close()
+close_output(fdef, fdecl)

+ 18 - 49
scripts/qapi-types.py

@@ -11,8 +11,6 @@
 
 
 from ordereddict import OrderedDict
 from ordereddict import OrderedDict
 from qapi import *
 from qapi import *
-import os
-import errno
 
 
 def generate_fwd_struct(name, members, builtin_type=False):
 def generate_fwd_struct(name, members, builtin_type=False):
     if builtin_type:
     if builtin_type:
@@ -273,8 +271,6 @@ def generate_type_cleanup(name):
                 c_type=c_type(name), name=c_name(name))
                 c_type=c_type(name), name=c_name(name))
     return ret
     return ret
 
 
-c_file = 'qapi-types.c'
-h_file = 'qapi-types.h'
 do_builtins = False
 do_builtins = False
 
 
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
@@ -284,28 +280,7 @@ def generate_type_cleanup(name):
     if o in ("-b", "--builtins"):
     if o in ("-b", "--builtins"):
         do_builtins = True
         do_builtins = True
 
 
-c_file = output_dir + prefix + c_file
-h_file = output_dir + prefix + h_file
-
-try:
-    os.makedirs(output_dir)
-except os.error, e:
-    if e.errno != errno.EEXIST:
-        raise
-
-def maybe_open(really, name, opt):
-    if really:
-        return open(name, opt)
-    else:
-        import StringIO
-        return StringIO.StringIO()
-
-fdef = maybe_open(do_c, c_file, 'w')
-fdecl = maybe_open(do_h, h_file, 'w')
-
-fdef.write(mcgen('''
-/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
+c_comment = '''
 /*
 /*
  * deallocation functions for schema-defined QAPI types
  * deallocation functions for schema-defined QAPI types
  *
  *
@@ -319,16 +294,8 @@ def maybe_open(really, name, opt):
  * See the COPYING.LIB file in the top-level directory.
  * See the COPYING.LIB file in the top-level directory.
  *
  *
  */
  */
-
-#include "qapi/dealloc-visitor.h"
-#include "%(prefix)sqapi-types.h"
-#include "%(prefix)sqapi-visit.h"
-
-''',             prefix=prefix))
-
-fdecl.write(mcgen('''
-/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
+'''
+h_comment = '''
 /*
 /*
  * schema-defined QAPI types
  * schema-defined QAPI types
  *
  *
@@ -341,15 +308,25 @@ def maybe_open(really, name, opt):
  * See the COPYING.LIB file in the top-level directory.
  * See the COPYING.LIB file in the top-level directory.
  *
  *
  */
  */
+'''
 
 
-#ifndef %(guard)s
-#define %(guard)s
+(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
+                            'qapi-types.c', 'qapi-types.h',
+                            c_comment, h_comment)
 
 
+fdef.write(mcgen('''
+#include "qapi/dealloc-visitor.h"
+#include "%(prefix)sqapi-types.h"
+#include "%(prefix)sqapi-visit.h"
+
+''',
+                 prefix=prefix))
+
+fdecl.write(mcgen('''
 #include <stdbool.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdint.h>
 
 
-''',
-                  guard=guardname(h_file)))
+'''))
 
 
 exprs = parse_schema(input_file)
 exprs = parse_schema(input_file)
 exprs = filter(lambda expr: not expr.has_key('gen'), exprs)
 exprs = filter(lambda expr: not expr.has_key('gen'), exprs)
@@ -427,12 +404,4 @@ def maybe_open(really, name, opt):
         continue
         continue
     fdecl.write(ret)
     fdecl.write(ret)
 
 
-fdecl.write('''
-#endif
-''')
-
-fdecl.flush()
-fdecl.close()
-
-fdef.flush()
-fdef.close()
+close_output(fdef, fdecl)

+ 16 - 47
scripts/qapi-visit.py

@@ -15,8 +15,6 @@
 from ordereddict import OrderedDict
 from ordereddict import OrderedDict
 from qapi import *
 from qapi import *
 import re
 import re
-import os
-import errno
 
 
 implicit_structs = []
 implicit_structs = []
 
 
@@ -374,8 +372,6 @@ def generate_decl_enum(name, members):
 ''',
 ''',
                  name=c_name(name))
                  name=c_name(name))
 
 
-c_file = 'qapi-visit.c'
-h_file = 'qapi-visit.h'
 do_builtins = False
 do_builtins = False
 
 
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
@@ -385,28 +381,7 @@ def generate_decl_enum(name, members):
     if o in ("-b", "--builtins"):
     if o in ("-b", "--builtins"):
         do_builtins = True
         do_builtins = True
 
 
-c_file = output_dir + prefix + c_file
-h_file = output_dir + prefix + h_file
-
-try:
-    os.makedirs(output_dir)
-except os.error, e:
-    if e.errno != errno.EEXIST:
-        raise
-
-def maybe_open(really, name, opt):
-    if really:
-        return open(name, opt)
-    else:
-        import StringIO
-        return StringIO.StringIO()
-
-fdef = maybe_open(do_c, c_file, 'w')
-fdecl = maybe_open(do_h, h_file, 'w')
-
-fdef.write(mcgen('''
-/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
+c_comment = '''
 /*
 /*
  * schema-defined QAPI visitor functions
  * schema-defined QAPI visitor functions
  *
  *
@@ -419,15 +394,8 @@ def maybe_open(really, name, opt):
  * See the COPYING.LIB file in the top-level directory.
  * See the COPYING.LIB file in the top-level directory.
  *
  *
  */
  */
-
-#include "qemu-common.h"
-#include "%(header)s"
-''',
-                 header=basename(h_file)))
-
-fdecl.write(mcgen('''
-/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
+'''
+h_comment = '''
 /*
 /*
  * schema-defined QAPI visitor functions
  * schema-defined QAPI visitor functions
  *
  *
@@ -440,15 +408,24 @@ def maybe_open(really, name, opt):
  * See the COPYING.LIB file in the top-level directory.
  * See the COPYING.LIB file in the top-level directory.
  *
  *
  */
  */
+'''
+
+(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
+                            'qapi-visit.c', 'qapi-visit.h',
+                            c_comment, h_comment)
 
 
-#ifndef %(guard)s
-#define %(guard)s
+fdef.write(mcgen('''
+#include "qemu-common.h"
+#include "%(prefix)sqapi-visit.h"
+''',
+                 prefix = prefix))
 
 
+fdecl.write(mcgen('''
 #include "qapi/visitor.h"
 #include "qapi/visitor.h"
 #include "%(prefix)sqapi-types.h"
 #include "%(prefix)sqapi-types.h"
 
 
 ''',
 ''',
-                  prefix=prefix, guard=guardname(h_file)))
+                  prefix=prefix))
 
 
 exprs = parse_schema(input_file)
 exprs = parse_schema(input_file)
 
 
@@ -504,12 +481,4 @@ def maybe_open(really, name, opt):
         ret += generate_enum_declaration(expr['enum'], expr['data'])
         ret += generate_enum_declaration(expr['enum'], expr['data'])
         fdecl.write(ret)
         fdecl.write(ret)
 
 
-fdecl.write('''
-#endif
-''')
-
-fdecl.flush()
-fdecl.close()
-
-fdef.flush()
-fdef.close()
+close_output(fdef, fdecl)

+ 50 - 0
scripts/qapi.py

@@ -13,6 +13,7 @@
 
 
 import re
 import re
 from ordereddict import OrderedDict
 from ordereddict import OrderedDict
+import errno
 import getopt
 import getopt
 import os
 import os
 import sys
 import sys
@@ -1020,3 +1021,52 @@ def parse_command_line(extra_options = "", extra_long_options = []):
     input_file = args[0]
     input_file = args[0]
 
 
     return (input_file, output_dir, do_c, do_h, prefix, extra_opts)
     return (input_file, output_dir, do_c, do_h, prefix, extra_opts)
+
+def open_output(output_dir, do_c, do_h, prefix, c_file, h_file,
+                c_comment, h_comment):
+    c_file = output_dir + prefix + c_file
+    h_file = output_dir + prefix + h_file
+
+    try:
+        os.makedirs(output_dir)
+    except os.error, e:
+        if e.errno != errno.EEXIST:
+            raise
+
+    def maybe_open(really, name, opt):
+        if really:
+            return open(name, opt)
+        else:
+            import StringIO
+            return StringIO.StringIO()
+
+    fdef = maybe_open(do_c, c_file, 'w')
+    fdecl = maybe_open(do_h, h_file, 'w')
+
+    fdef.write(mcgen('''
+/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
+%(comment)s
+''',
+                     comment = c_comment))
+
+    fdecl.write(mcgen('''
+/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
+%(comment)s
+#ifndef %(guard)s
+#define %(guard)s
+
+''',
+                      comment = h_comment, guard = guardname(h_file)))
+
+    return (fdef, fdecl)
+
+def close_output(fdef, fdecl):
+    fdecl.write('''
+#endif
+''')
+
+    fdecl.flush()
+    fdecl.close()
+
+    fdef.flush()
+    fdef.close()