From patchwork Wed Oct 31 22:35:48 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 196034 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 1F8162C0199 for ; Thu, 1 Nov 2012 09:50:39 +1100 (EST) Received: from localhost ([::1]:48821 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTguo-0002da-9o for incoming@patchwork.ozlabs.org; Wed, 31 Oct 2012 18:37:38 -0400 Received: from eggs.gnu.org ([208.118.235.92]:34620) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTgtq-0000MQ-Fr for qemu-devel@nongnu.org; Wed, 31 Oct 2012 18:36:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TTgtp-0005ls-4P for qemu-devel@nongnu.org; Wed, 31 Oct 2012 18:36:38 -0400 Received: from mail-ie0-f173.google.com ([209.85.223.173]:33189) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTgto-0005k6-VR for qemu-devel@nongnu.org; Wed, 31 Oct 2012 18:36:37 -0400 Received: by mail-ie0-f173.google.com with SMTP id 17so2893446iea.4 for ; Wed, 31 Oct 2012 15:36: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=HtYMyz0M3Ke64rdiEjn3DFilZwIgjq8EHyGHi3vQlnLJW+kaQMh/mgKVkMmLTL6y/a o36ozxYPc4pDVPHwdPrsTA7foq7RRPaE9AF5nfn5srsjkOn2uCo8gUKzghT+PNZkA4/Z f4SfHGowrniuaDQTaZw6iqnTvguNV3dvHhKIj9G7tLAj0nyPms+SHpJD19bzkRtNGvq9 lLun5esxG5ptd+VfjNQ2dG9oVGq3oqXMMgSupvsWRNhnJv2Xwe5pC02d76WuasygqFKD GdIxfdSVC0RcmCg7IiQKdvI2LDDzhlXJ4uzIQinfS0BRqab2MCI8M5vsnh4OBV2W5X9P WcBg== Received: by 10.50.33.138 with SMTP id r10mr3261334igi.6.1351722996736; Wed, 31 Oct 2012 15:36:36 -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 hg2sm11556858igc.3.2012.10.31.15.36.35 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 31 Oct 2012 15:36:36 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Wed, 31 Oct 2012 17:35:48 -0500 Message-Id: <1351722972-17801-5-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1351722972-17801-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1351722972-17801-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.223.173 Cc: kwolf@redhat.com, peter.maydell@linaro.org, aliguori@us.ibm.com, blauwirbel@gmail.com, pbonzini@redhat.com Subject: [Qemu-devel] [PATCH 04/28] 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))