]> Git Repo - qemu.git/blobdiff - scripts/qapi-visit.py
qapi: Factor open_output(), close_output() out of generators
[qemu.git] / scripts / qapi-visit.py
index 75f0cf3db58554b50352f58af27bc71baad730b6..5b993364885e4aaf09e2e8d6481ed5fa8b3c99c7 100644 (file)
@@ -15,8 +15,6 @@
 from ordereddict import OrderedDict
 from qapi import *
 import re
-import os
-import errno
 
 implicit_structs = []
 
@@ -374,8 +372,6 @@ void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **er
 ''',
                  name=c_name(name))
 
-c_file = 'qapi-visit.c'
-h_file = 'qapi-visit.h'
 do_builtins = False
 
 (input_file, output_dir, do_c, do_h, prefix, opts) = \
@@ -385,28 +381,7 @@ for o, a in opts:
     if o in ("-b", "--builtins"):
         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
  *
@@ -419,15 +394,8 @@ fdef.write(mcgen('''
  * 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
  *
@@ -440,15 +408,24 @@ fdecl.write(mcgen('''
  * 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 "%(prefix)sqapi-types.h"
 
 ''',
-                  prefix=prefix, guard=guardname(h_file)))
+                  prefix=prefix))
 
 exprs = parse_schema(input_file)
 
@@ -504,12 +481,4 @@ for expr in exprs:
         ret += generate_enum_declaration(expr['enum'], expr['data'])
         fdecl.write(ret)
 
-fdecl.write('''
-#endif
-''')
-
-fdecl.flush()
-fdecl.close()
-
-fdef.flush()
-fdef.close()
+close_output(fdef, fdecl)
This page took 0.022946 seconds and 4 git commands to generate.