From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43613) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WCwmK-0003OK-EK for qemu-devel@nongnu.org; Mon, 10 Feb 2014 14:44:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WCwmE-0000hL-T0 for qemu-devel@nongnu.org; Mon, 10 Feb 2014 14:44:28 -0500 Received: from roura.ac.upc.es ([147.83.33.10]:48231) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WCwmE-0000h5-8E for qemu-devel@nongnu.org; Mon, 10 Feb 2014 14:44:22 -0500 From: =?utf-8?Q?Llu=C3=ADs_Vilanova?= References: <20140109105950.23406.55209.stgit@fimbulvetr.bsc.es> <20140109105955.23406.84903.stgit@fimbulvetr.bsc.es> <20140210134726.5529f58b@redhat.com> Date: Mon, 10 Feb 2014 20:44:18 +0100 In-Reply-To: <20140210134726.5529f58b@redhat.com> (Luiz Capitulino's message of "Mon, 10 Feb 2014 13:47:26 -0500") Message-ID: <87bnye4ust.fsf@fimbulvetr.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 1/2] qapi: Add a primitive to include other files from a QAPI schema file List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Luiz Capitulino Cc: Stefan Hajnoczi , qemu-devel@nongnu.org Luiz Capitulino writes: > On Thu, 9 Jan 2014 11:59:55 +0100 > Llu=C3=ADs Vilanova wrote: >> Adds the "include(...)" primitive to the syntax of QAPI schema files. > I don't know whether/how we want to break existing schema files, but a > include() primitive will be needed soon or later. > My only suggestion is that, _maybe_ we should change the scripts to > take the schema file from the command-line (instead of stdin) and then > we could use the schema's file path for relative paths (instead having > the -i option). That's simple enough, and looks much saner. I'll send a new version later. Thanks, Lluis >>=20 >> Signed-off-by: Llu=C3=ADs Vilanova >> --- >> scripts/qapi-commands.py | 10 +++++++--- >> scripts/qapi-types.py | 9 ++++++--- >> scripts/qapi-visit.py | 9 ++++++--- >> scripts/qapi.py | 25 ++++++++++++++++++++++--- >> 4 files changed, 41 insertions(+), 12 deletions(-) >>=20 >> diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py >> index b12b696..c1d6461 100644 >> --- a/scripts/qapi-commands.py >> +++ b/scripts/qapi-commands.py >> @@ -389,13 +389,15 @@ def gen_command_def_prologue(prefix=3D"", proxy=3D= False): >>=20 >>=20 >> try: >> - opts, args =3D getopt.gnu_getopt(sys.argv[1:], "chp:o:m", >> + opts, args =3D getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m", >> ["source", "header", "prefix=3D", >> - "output-dir=3D", "type=3D", "middle= "]) >> + "input-dir=3D", "output-dir=3D", >> + "type=3D", "middle"]) >> except getopt.GetoptError, err: >> print str(err) >> sys.exit(1) >>=20 >> +input_dir =3D "" >> output_dir =3D "" >> prefix =3D "" >> dispatch_type =3D "sync" >> @@ -409,6 +411,8 @@ do_h =3D False >> for o, a in opts: >> if o in ("-p", "--prefix"): >> prefix =3D a >> + elif o in ("-i", "--input-dir"): >> + input_dir =3D a >> elif o in ("-o", "--output-dir"): >> output_dir =3D a + "/" >> elif o in ("-t", "--type"): >> @@ -440,7 +444,7 @@ except os.error, e: >> if e.errno !=3D errno.EEXIST: >> raise >>=20 >> -exprs =3D parse_schema(sys.stdin) >> +exprs =3D parse_schema(sys.stdin, input_dir) >> commands =3D filter(lambda expr: expr.has_key('command'), exprs) >> commands =3D filter(lambda expr: not expr.has_key('gen'), commands) >>=20 >> diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py >> index 4a1652b..36ff820 100644 >> --- a/scripts/qapi-types.py >> +++ b/scripts/qapi-types.py >> @@ -282,14 +282,15 @@ void qapi_free_%(type)s(%(c_type)s obj) >>=20 >>=20 >> try: >> - opts, args =3D getopt.gnu_getopt(sys.argv[1:], "chbp:o:", >> + opts, args =3D getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:", >> ["source", "header", "builtins", >> - "prefix=3D", "output-dir=3D"]) >> + "prefix=3D", "input-dir=3D", "outpu= t-dir=3D"]) >> except getopt.GetoptError, err: >> print str(err) >> sys.exit(1) >>=20 >> output_dir =3D "" >> +input_dir =3D "" >> prefix =3D "" >> c_file =3D 'qapi-types.c' >> h_file =3D 'qapi-types.h' >> @@ -301,6 +302,8 @@ do_builtins =3D False >> for o, a in opts: >> if o in ("-p", "--prefix"): >> prefix =3D a >> + elif o in ("-i", "--input-dir"): >> + input_dir =3D a >> elif o in ("-o", "--output-dir"): >> output_dir =3D a + "/" >> elif o in ("-c", "--source"): >> @@ -381,7 +384,7 @@ fdecl.write(mcgen(''' >> ''', >> guard=3Dguardname(h_file))) >>=20 >> -exprs =3D parse_schema(sys.stdin) >> +exprs =3D parse_schema(sys.stdin, input_dir) >> exprs =3D filter(lambda expr: not expr.has_key('gen'), exprs) >>=20 >> fdecl.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL")) >> diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py >> index 65f1a54..6667e17 100644 >> --- a/scripts/qapi-visit.py >> +++ b/scripts/qapi-visit.py >> @@ -383,13 +383,14 @@ void visit_type_%(name)s(Visitor *m, %(name)s * ob= j, const char *name, Error **e >> name=3Dname) >>=20 >> try: >> - opts, args =3D getopt.gnu_getopt(sys.argv[1:], "chbp:o:", >> + opts, args =3D getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:", >> ["source", "header", "builtins", "prefix=3D", >> - "output-dir=3D"]) >> + "input-dir=3D", "output-dir=3D"]) >> except getopt.GetoptError, err: >> print str(err) >> sys.exit(1) >>=20 >> +input_dir =3D "" >> output_dir =3D "" >> prefix =3D "" >> c_file =3D 'qapi-visit.c' >> @@ -402,6 +403,8 @@ do_builtins =3D False >> for o, a in opts: >> if o in ("-p", "--prefix"): >> prefix =3D a >> + elif o in ("-i", "--input-dir"): >> + input_dir =3D a >> elif o in ("-o", "--output-dir"): >> output_dir =3D a + "/" >> elif o in ("-c", "--source"): >> @@ -480,7 +483,7 @@ fdecl.write(mcgen(''' >> ''', >> prefix=3Dprefix, guard=3Dguardname(h_file))) >>=20 >> -exprs =3D parse_schema(sys.stdin) >> +exprs =3D parse_schema(sys.stdin, input_dir) >>=20 >> # 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 750e9fb..9491269 100644 >> --- a/scripts/qapi.py >> +++ b/scripts/qapi.py >> @@ -11,9 +11,13 @@ >> # This work is licensed under the terms of the GNU GPLv2. >> # See the COPYING.LIB file in the top-level directory. >>=20 >> +import os >> +import re >> from ordereddict import OrderedDict >> import sys >>=20 >> +include_cre =3D re.compile("include\(\"([^\"]*)\"\)") >> + >> builtin_types =3D [ >> 'str', 'int', 'number', 'bool', >> 'int8', 'int16', 'int32', 'int64', >> @@ -54,8 +58,9 @@ class QAPISchemaError(Exception): >>=20 >> class QAPISchema: >>=20 >> - def __init__(self, fp): >> + def __init__(self, fp, input_dir): >> self.fp =3D fp >> + self.input_dir =3D input_dir >> self.src =3D fp.read() >> if self.src =3D=3D '' or self.src[-1] !=3D '\n': >> self.src +=3D '\n' >> @@ -100,6 +105,20 @@ class QAPISchema: >> if self.cursor =3D=3D len(self.src): >> self.tok =3D None >> return >> + elif self.tok =3D=3D 'i': >> + include_src =3D self.src[self.cursor-1:] >> + include_match =3D include_cre.match(include_src) >> + if include_match is not None: >> + include_path =3D os.path.join(self.input_dir, >> + include_match.group(1)) >> + if not os.path.isfile(include_path): >> + raise QAPISchemaError( >> + self, >> + 'Non-existing included file "%s"' % include= _path) >> + include_schema =3D QAPISchema(open(include_path), >> + self.input_dir) >> + self.exprs +=3D include_schema.exprs >> + self.cursor +=3D include_match.span()[1] - 1 >> elif not self.tok.isspace(): >> raise QAPISchemaError(self, 'Stray "%s"' % self.tok) >>=20 >> @@ -158,9 +177,9 @@ class QAPISchema: >> raise QAPISchemaError(self, 'Expected "{", "[" or string') >> return expr >>=20 >> -def parse_schema(fp): >> +def parse_schema(fp, input_dir): >> try: >> - schema =3D QAPISchema(fp) >> + schema =3D QAPISchema(fp, input_dir) >> except QAPISchemaError, e: >> print >>sys.stderr, e >> exit(1) >>=20 >>=20 --=20 "And it's much the same thing with knowledge, for whenever you learn something new, the whole world becomes that much richer." -- The Princess of Pure Reason, as told by Norton Juster in The Phantom Tollbooth