From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36042) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W050X-0003wl-SR for qemu-devel@nongnu.org; Mon, 06 Jan 2014 02:54:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W050T-0000LY-C9 for qemu-devel@nongnu.org; Mon, 06 Jan 2014 02:53:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50315) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W050T-0000LU-3z for qemu-devel@nongnu.org; Mon, 06 Jan 2014 02:53:53 -0500 Message-ID: <52CA60FD.9080804@redhat.com> Date: Mon, 06 Jan 2014 15:53:33 +0800 From: Fam Zheng MIME-Version: 1.0 References: <1388923351-10556-1-git-send-email-akong@redhat.com> <1388923351-10556-3-git-send-email-akong@redhat.com> In-Reply-To: <1388923351-10556-3-git-send-email-akong@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v3 2/3] qapi: change qapi to convert schema json List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Amos Kong , qemu-devel@nongnu.org Cc: qiaonuohan@cn.fujitsu.com, lcapitulino@redhat.com, mdroth@linux.vnet.ibm.com, xiawenc@linux.vnet.ibm.com On 2014=E5=B9=B401=E6=9C=8805=E6=97=A5 20:02, Amos Kong wrote: > QMP schema is defined in a json file, it will be parsed by > qapi scripts and generate C files. > > We want to return the schema information to management, > this patch converts the json file to a string table in a > C head file, then we can use the json content in QEMU code. > > eg: (qmp-schema.h) > const char *const qmp_schema_table[] =3D { > "{ 'type': 'NameInfo', 'data': {'*name': 'str'} }", > "{ 'command': 'query-name', 'returns': 'NameInfo' }", > ... > } > > Signed-off-by: Amos Kong > --- > Makefile | 5 ++++- > scripts/qapi-commands.py | 2 +- > scripts/qapi-types.py | 48 +++++++++++++++++++++++++++++++++++++++= ++++++--- > scripts/qapi-visit.py | 2 +- > scripts/qapi.py | 20 +++++++++++++++----- > 5 files changed, 66 insertions(+), 11 deletions(-) > > diff --git a/Makefile b/Makefile > index bdff4e4..2c29755 100644 > --- a/Makefile > +++ b/Makefile > @@ -45,7 +45,7 @@ endif > endif > > GENERATED_HEADERS =3D config-host.h qemu-options.def > -GENERATED_HEADERS +=3D qmp-commands.h qapi-types.h qapi-visit.h > +GENERATED_HEADERS +=3D qmp-commands.h qapi-types.h qapi-visit.h qmp-sc= hema.h > GENERATED_SOURCES +=3D qmp-marshal.c qapi-types.c qapi-visit.c > > GENERATED_HEADERS +=3D trace/generated-events.h > @@ -229,6 +229,9 @@ $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qa= pi-visit.py $(qapi-py) > qmp-commands.h qmp-marshal.c :\ > $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(q= api-py) > $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py = $(gen-out-type) -m -o "." < $<, " GEN $@") > +qmp-schema.h:\ > +$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-= py) > + $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py $(ge= n-out-type) -o "." -s "$@" < $<, " GEN $@") > > QGALIB_GEN=3D$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qa= pi-visit.h qga-qmp-commands.h) > $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN) > diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py > index b12b696..5f4fb94 100644 > --- a/scripts/qapi-commands.py > +++ b/scripts/qapi-commands.py > @@ -440,7 +440,7 @@ except os.error, e: > if e.errno !=3D errno.EEXIST: > raise > > -exprs =3D parse_schema(sys.stdin) > +exprs =3D parse_schema(sys.stdin)[0] > commands =3D filter(lambda expr: expr.has_key('command'), exprs) > commands =3D filter(lambda expr: not expr.has_key('gen'), commands) > > diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py > index 4a1652b..0f86b95 100644 > --- a/scripts/qapi-types.py > +++ b/scripts/qapi-types.py > @@ -15,6 +15,7 @@ import sys > import os > import getopt > import errno > +import re > > def generate_fwd_struct(name, members, builtin_type=3DFalse): > if builtin_type: > @@ -282,9 +283,10 @@ void qapi_free_%(type)s(%(c_type)s obj) > > > try: > - opts, args =3D getopt.gnu_getopt(sys.argv[1:], "chbp:o:", > + opts, args =3D getopt.gnu_getopt(sys.argv[1:], "chbs:p:o:", > ["source", "header", "builtins", > - "prefix=3D", "output-dir=3D"]) > + "schema-dump-file=3D", "prefix=3D"= , > + "output-dir=3D"]) > except getopt.GetoptError, err: > print str(err) > sys.exit(1) > @@ -293,6 +295,7 @@ output_dir =3D "" > prefix =3D "" > c_file =3D 'qapi-types.c' > h_file =3D 'qapi-types.h' > +schema_dump_file =3D "" > > do_c =3D False > do_h =3D False > @@ -309,11 +312,17 @@ for o, a in opts: > do_h =3D True > elif o in ("-b", "--builtins"): > do_builtins =3D True > + elif o in ("-s", "--schema-dump-file"): > + schema_dump_file =3D a > > if not do_c and not do_h: > do_c =3D True > do_h =3D True > > +if schema_dump_file: > + do_c =3D False > + do_h =3D False > + > c_file =3D output_dir + prefix + c_file > h_file =3D output_dir + prefix + h_file > > @@ -381,7 +390,40 @@ fdecl.write(mcgen(''' > ''', > guard=3Dguardname(h_file))) > > -exprs =3D parse_schema(sys.stdin) > +exprs_all =3D parse_schema(sys.stdin) > + > +schema_table =3D """/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ > + > +/* > + * Schema json string table converted from qapi-schema.json > + * > + * Copyright (c) 2013 Red Hat, Inc. > + * > + * Authors: > + * Amos Kong > + * > + * This work is licensed under the terms of the GNU LGPL, version 2.1 = or later. > + * See the COPYING.LIB file in the top-level directory. > + * > + */ > + > +const char *const qmp_schema_table[] =3D { > +""" > + > +if schema_dump_file: > + for line in exprs_all[1]: > + line =3D re.sub(r'#.*\n', ' ', line.strip()) > + line =3D re.sub(r'\n', ' ', line.strip()) > + line =3D re.sub(r' +', ' ', line) > + schema_table +=3D ' "%s",\n' % (line) > + > + schema_table +=3D ' NULL };\n' > + f =3D open(schema_dump_file, "w") > + f.write(schema_table) > + f.flush() > + f.close() > + > +exprs =3D exprs_all[0] > exprs =3D filter(lambda expr: not expr.has_key('gen'), exprs) > > fdecl.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL")) > diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py > index 65f1a54..db68084 100644 > --- a/scripts/qapi-visit.py > +++ b/scripts/qapi-visit.py > @@ -480,7 +480,7 @@ fdecl.write(mcgen(''' > ''', > prefix=3Dprefix, guard=3Dguardname(h_file))) > > -exprs =3D parse_schema(sys.stdin) > +exprs =3D parse_schema(sys.stdin)[0] > > # to avoid header dependency hell, we always generate declarations > # for built-in types in our header files and simply guard them > diff --git a/scripts/qapi.py b/scripts/qapi.py > index 4263bbd..43cc401 100644 > --- a/scripts/qapi.py > +++ b/scripts/qapi.py > @@ -61,10 +61,13 @@ class QAPISchema: > self.src +=3D '\n' > self.cursor =3D 0 > self.exprs =3D [] > + self.raw_exprs =3D [] > self.accept() > > while self.tok !=3D None: > - self.exprs.append(self.get_expr(False)) > + self.cur_entry=3D '' Missing whitespace before "=3D". > + self.exprs.append(self.get_expr(False, True)) > + self.raw_exprs.append(self.cur_entry) > > def accept(self): > while True: > @@ -103,9 +106,11 @@ class QAPISchema: > elif not self.tok.isspace(): > raise QAPISchemaError(self, 'Stray "%s"' % self.tok) > > - def get_members(self): > + def get_members(self, start=3DNone): > expr =3D OrderedDict() > if self.tok =3D=3D '}': > + if start !=3D None: > + self.cur_entry =3D self.src[start:self.cursor] > self.accept() > return expr > if self.tok !=3D "'": > @@ -118,6 +123,8 @@ class QAPISchema: > self.accept() > expr[key] =3D self.get_expr(True) > if self.tok =3D=3D '}': > + if start !=3D None: > + self.cur_entry =3D self.src[start:self.cursor] > self.accept() > return expr > if self.tok !=3D ',': > @@ -142,12 +149,15 @@ class QAPISchema: > raise QAPISchemaError(self, 'Expected "," or "]"') > self.accept() > > - def get_expr(self, nested): > + def get_expr(self, nested, first=3DFalse): > if self.tok !=3D '{' and not nested: > raise QAPISchemaError(self, 'Expected "{"') > if self.tok =3D=3D '{': > + start =3D None > + if first: > + start =3D self.cursor - 1 > self.accept() > - expr =3D self.get_members() > + expr =3D self.get_members(start) > elif self.tok =3D=3D '[': > self.accept() > expr =3D self.get_values() > @@ -174,7 +184,7 @@ def parse_schema(fp): > elif expr.has_key('type'): > add_struct(expr) > > - return schema.exprs > + return schema.exprs, schema.raw_exprs > > def parse_args(typeinfo): > if isinstance(typeinfo, basestring): >