From patchwork Tue Jul 24 17:20:36 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 172981 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 BD6152C0083 for ; Wed, 25 Jul 2012 03:34:36 +1000 (EST) Received: from localhost ([::1]:38966 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StioW-00058N-PW for incoming@patchwork.ozlabs.org; Tue, 24 Jul 2012 13:22:28 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49426) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Stind-0003G2-8k for qemu-devel@nongnu.org; Tue, 24 Jul 2012 13:21:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1StinY-0000QM-Lv for qemu-devel@nongnu.org; Tue, 24 Jul 2012 13:21:33 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:53639) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StinY-0000Fl-EA for qemu-devel@nongnu.org; Tue, 24 Jul 2012 13:21:28 -0400 Received: by mail-pb0-f45.google.com with SMTP id ro12so12773613pbb.4 for ; Tue, 24 Jul 2012 10:21:28 -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=irRyMNQYWzXiQKSJA/rCoqLBAG5/CtcLH9SqKovkr98=; b=hmRpqttw8SLZwpQ5r5wH6TUKr+Ywf2Qpq9Kp4hdBn02fe+1rluuhUf8xexs1cfWQpf HuiR3ZTxSf5+TxMJfpmiYYBmP5iyv/wyUf2TjpS31PqvYygQDnSjP4DUeLuwnqgMtwyJ +tv6fdaMqxgHq79yoYuWS4nZsxEnpkoQdxQDm5ctblB1RzOE3wMtwLWAVCmDayPNMYRm p6ilUn+zlzQ/BG6gtZcB9NTXEeU98vpjNx4o4sjOZhpXH4XDp6mN6d/lkexotfHYpgVR L3kNA8//gdjIcNL49rJfk0Us+qGSzk7zOU7LURofVVbPnahyxELU/eHia3262cnfCyx1 5fJA== Received: by 10.68.231.132 with SMTP id tg4mr46863518pbc.159.1343150488130; Tue, 24 Jul 2012 10:21:28 -0700 (PDT) Received: from loki.morrigu.org (cpe-72-179-62-111.austin.res.rr.com. [72.179.62.111]) by mx.google.com with ESMTPS id nh8sm12522083pbc.60.2012.07.24.10.21.25 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 24 Jul 2012 10:21:27 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 24 Jul 2012 12:20:36 -0500 Message-Id: <1343150454-4677-5-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1343150454-4677-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1343150454-4677-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.160.45 Cc: aliguori@us.ibm.com, quintela@redhat.com, owasserm@redhat.com, yamahata@valinux.co.jp, pbonzini@redhat.com, akong@redhat.com, afaerber@suse.de Subject: [Qemu-devel] [PATCH 04/22] 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 Signed-off-by: Michael Roth Reviewed-by: Anthony Liguori --- scripts/qapi_visit.py | 143 +++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 69 deletions(-) diff --git a/scripts/qapi_visit.py b/scripts/qapi_visit.py index 04ef7c4..25707f5 100644 --- a/scripts/qapi_visit.py +++ b/scripts/qapi_visit.py @@ -224,55 +224,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 */ /* @@ -292,7 +294,7 @@ fdef.write(mcgen(''' ''', header=basename(h_file))) -fdecl.write(mcgen(''' + fdecl.write(mcgen(''' /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ /* @@ -316,37 +318,40 @@ fdecl.write(mcgen(''' ''', prefix=prefix, guard=guardname(h_file))) -exprs = parse_schema(sys.stdin) + 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) + 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_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_enum(expr['enum'], 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_enum(expr['enum'], expr['data']) + fdef.write(ret) - ret = generate_decl_enum(expr['enum'], expr['data']) - fdecl.write(ret) + ret = generate_decl_enum(expr['enum'], expr['data']) + fdecl.write(ret) -fdecl.write(''' + 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))