aboutsummaryrefslogtreecommitdiff
path: root/scripts/qapi.py
diff options
context:
space:
mode:
authorMarkus Armbruster <armbru@redhat.com>2015-04-02 14:46:39 +0200
committerMarkus Armbruster <armbru@redhat.com>2015-05-14 18:41:32 +0200
commit12f8e1b9ff57e99dafbb13f89cd5a99ad5c28527 (patch)
tree53c4f5a3e185e53f94475b8990dfaea0b8717c3a /scripts/qapi.py
parent16d80f61814745bd3f5bb9f47ae3b00edf9e1e45 (diff)
qapi: Factor open_output(), close_output() out of generators
Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'scripts/qapi.py')
-rw-r--r--scripts/qapi.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 186ec3b39f..fbfe0508a0 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -13,6 +13,7 @@
import re
from ordereddict import OrderedDict
+import errno
import getopt
import os
import sys
@@ -1020,3 +1021,52 @@ def parse_command_line(extra_options = "", extra_long_options = []):
input_file = args[0]
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()