From patchwork Fri Oct 19 02:41:58 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 192487 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 23E662C0098 for ; Fri, 19 Oct 2012 13:43:09 +1100 (EST) Received: from localhost ([::1]:53207 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TP2YF-00041B-3e for incoming@patchwork.ozlabs.org; Thu, 18 Oct 2012 22:43:07 -0400 Received: from eggs.gnu.org ([208.118.235.92]:42466) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TP2Xm-0003i2-Js for qemu-devel@nongnu.org; Thu, 18 Oct 2012 22:42:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TP2Xl-0002fv-A1 for qemu-devel@nongnu.org; Thu, 18 Oct 2012 22:42:38 -0400 Received: from mail-ob0-f173.google.com ([209.85.214.173]:58994) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TP2Xl-0002eS-4x for qemu-devel@nongnu.org; Thu, 18 Oct 2012 22:42:37 -0400 Received: by mail-ob0-f173.google.com with SMTP id wc18so3049obb.4 for ; Thu, 18 Oct 2012 19:42:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=jBwCwbzUVsPQ1OqVaMAuh+yixmkhIQyWBoUynFmcO6o=; b=F6c0UYq8z9LmzmWAZzOjGCyRben/d14d49YeB5byp7FURDF757pP+dwbKomBznIM2F m1pYUEDNH9XXKhquBuPRQBz/did6/ZzCY0hnFhrmby0bdtlv1myJZwZrKyy+Q2WVZsZz bsRLDfDMQ6qETrHyhvTX26NoSkk/iofJzcP+0bBsmLEAbmRUDtEsVS7HTQk4k3FzOkCU Vgy+FSVkP/oamaBfB2Q7P6uudMxCh/P28nQh4Xpy7bflZmE29GAXcZovBP7VzvyTOixB jMrsaqdXdF3iPMB7T4zvDkj2biUuzMRbk+aFm7xb9qaG1AKyGgUR67nuvLXL30Y4d3MH QTUg== Received: by 10.60.11.162 with SMTP id r2mr20390042oeb.114.1350614556842; Thu, 18 Oct 2012 19:42:36 -0700 (PDT) Received: from loki.austin.ibm.com ([32.97.110.59]) by mx.google.com with ESMTPS id m6sm475144obk.3.2012.10.18.19.42.35 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 18 Oct 2012 19:42:35 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Thu, 18 Oct 2012 21:41:58 -0500 Message-Id: <1350614540-28583-5-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1350614540-28583-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1350614540-28583-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.214.173 Cc: kwolf@redhat.com, peter.maydell@linaro.org, aliguori@us.ibm.com, blauwirbel@gmail.com, pbonzini@redhat.com Subject: [Qemu-devel] [PATCH 04/26] qapi: qapi_visit.py, make code useable as module X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Reviewed-by: Anthony Liguori Reviewed-by: Paolo Bonzini Signed-off-by: Michael Roth --- scripts/qapi_visit.py | 157 +++++++++++++++++++++++++------------------------ 1 file changed, 81 insertions(+), 76 deletions(-) diff --git a/scripts/qapi_visit.py b/scripts/qapi_visit.py index a360de7..f98abf2 100644 --- a/scripts/qapi_visit.py +++ b/scripts/qapi_visit.py @@ -234,55 +234,57 @@ void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **e ''', name=name) -try: - opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:", - ["source", "header", "prefix=", "output-dir="]) -except getopt.GetoptError, err: - print str(err) - sys.exit(1) - -output_dir = "" -prefix = "" -c_file = 'qapi-visit.c' -h_file = 'qapi-visit.h' - -do_c = False -do_h = False - -for o, a in opts: - if o in ("-p", "--prefix"): - prefix = a - elif o in ("-o", "--output-dir"): - output_dir = a + "/" - elif o in ("-c", "--source"): +def main(argv=[]): + try: + opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:", + ["source", "header", "prefix=", + "output-dir="]) + except getopt.GetoptError, err: + print str(err) + sys.exit(1) + + output_dir = "" + prefix = "" + c_file = 'qapi-visit.c' + h_file = 'qapi-visit.h' + + do_c = False + do_h = False + + for o, a in opts: + if o in ("-p", "--prefix"): + prefix = a + elif o in ("-o", "--output-dir"): + output_dir = a + "/" + elif o in ("-c", "--source"): + do_c = True + elif o in ("-h", "--header"): + do_h = True + + if not do_c and not do_h: do_c = True - elif o in ("-h", "--header"): do_h = True -if not do_c and not do_h: - do_c = True - do_h = True + c_file = output_dir + prefix + c_file + h_file = output_dir + prefix + h_file -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 -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() + 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 = maybe_open(do_c, c_file, 'w') + fdecl = maybe_open(do_h, h_file, 'w') -fdef.write(mcgen(''' + fdef.write(mcgen(''' /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ /* @@ -302,7 +304,7 @@ fdef.write(mcgen(''' ''', header=basename(h_file))) -fdecl.write(mcgen(''' + fdecl.write(mcgen(''' /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ /* @@ -326,39 +328,42 @@ fdecl.write(mcgen(''' ''', prefix=prefix, guard=guardname(h_file))) -exprs = parse_schema(sys.stdin) - -for expr in exprs: - if expr.has_key('type'): - ret = generate_visit_struct(expr['type'], expr['data']) - ret += generate_visit_list(expr['type'], expr['data']) - fdef.write(ret) - - ret = generate_declaration(expr['type'], expr['data']) - fdecl.write(ret) - elif expr.has_key('union'): - ret = generate_visit_union(expr['union'], expr['data']) - ret += generate_visit_list(expr['union'], expr['data']) - fdef.write(ret) - - ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys()) - ret += generate_declaration(expr['union'], expr['data']) - fdecl.write(ret) - elif expr.has_key('enum'): - ret = generate_visit_list(expr['enum'], expr['data']) - ret += generate_visit_enum(expr['enum'], expr['data']) - fdef.write(ret) - - ret = generate_decl_enum(expr['enum'], expr['data']) - ret += generate_enum_declaration(expr['enum'], expr['data']) - fdecl.write(ret) - -fdecl.write(''' + exprs = parse_schema(sys.stdin) + + for expr in exprs: + if expr.has_key('type'): + ret = generate_visit_struct(expr['type'], expr['data']) + ret += generate_visit_list(expr['type'], expr['data']) + fdef.write(ret) + + ret = generate_declaration(expr['type'], expr['data']) + fdecl.write(ret) + elif expr.has_key('union'): + ret = generate_visit_union(expr['union'], expr['data']) + ret += generate_visit_list(expr['union'], expr['data']) + fdef.write(ret) + + ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys()) + ret += generate_declaration(expr['union'], expr['data']) + fdecl.write(ret) + elif expr.has_key('enum'): + ret = generate_visit_list(expr['enum'], expr['data']) + ret += generate_visit_enum(expr['enum'], expr['data']) + fdef.write(ret) + + ret = generate_decl_enum(expr['enum'], expr['data']) + ret += generate_enum_declaration(expr['enum'], expr['data']) + fdecl.write(ret) + + fdecl.write(''' #endif -''') + ''') + + fdecl.flush() + fdecl.close() -fdecl.flush() -fdecl.close() + fdef.flush() + fdef.close() -fdef.flush() -fdef.close() +if __name__ == '__main__': + sys.exit(main(sys.argv))