diff mbox

[1/2] qapi: Add a primitive to include other files from a QAPI schema file

Message ID 20140109105955.23406.84903.stgit@fimbulvetr.bsc.es
State New
Headers show

Commit Message

Lluís Vilanova Jan. 9, 2014, 10:59 a.m. UTC
Adds the "include(...)" primitive to the syntax of QAPI schema files.

Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
---
 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(-)

Comments

Luiz Capitulino Feb. 10, 2014, 6:47 p.m. UTC | #1
On Thu,  9 Jan 2014 11:59:55 +0100
Lluís Vilanova <vilanova@ac.upc.edu> 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).

> 
> Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
> ---
>  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(-)
> 
> 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="", proxy=False):
>  
>  
>  try:
> -    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:m",
> +    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
>                                     ["source", "header", "prefix=",
> -                                    "output-dir=", "type=", "middle"])
> +                                    "input-dir=", "output-dir=",
> +                                    "type=", "middle"])
>  except getopt.GetoptError, err:
>      print str(err)
>      sys.exit(1)
>  
> +input_dir = ""
>  output_dir = ""
>  prefix = ""
>  dispatch_type = "sync"
> @@ -409,6 +411,8 @@ do_h = False
>  for o, a in opts:
>      if o in ("-p", "--prefix"):
>          prefix = a
> +    elif o in ("-i", "--input-dir"):
> +        input_dir = a
>      elif o in ("-o", "--output-dir"):
>          output_dir = a + "/"
>      elif o in ("-t", "--type"):
> @@ -440,7 +444,7 @@ except os.error, e:
>      if e.errno != errno.EEXIST:
>          raise
>  
> -exprs = parse_schema(sys.stdin)
> +exprs = parse_schema(sys.stdin, input_dir)
>  commands = filter(lambda expr: expr.has_key('command'), exprs)
>  commands = filter(lambda expr: not expr.has_key('gen'), commands)
>  
> 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)
>  
>  
>  try:
> -    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:o:",
> +    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:",
>                                     ["source", "header", "builtins",
> -                                    "prefix=", "output-dir="])
> +                                    "prefix=", "input-dir=", "output-dir="])
>  except getopt.GetoptError, err:
>      print str(err)
>      sys.exit(1)
>  
>  output_dir = ""
> +input_dir = ""
>  prefix = ""
>  c_file = 'qapi-types.c'
>  h_file = 'qapi-types.h'
> @@ -301,6 +302,8 @@ do_builtins = False
>  for o, a in opts:
>      if o in ("-p", "--prefix"):
>          prefix = a
> +    elif o in ("-i", "--input-dir"):
> +        input_dir = a
>      elif o in ("-o", "--output-dir"):
>          output_dir = a + "/"
>      elif o in ("-c", "--source"):
> @@ -381,7 +384,7 @@ fdecl.write(mcgen('''
>  ''',
>                    guard=guardname(h_file)))
>  
> -exprs = parse_schema(sys.stdin)
> +exprs = parse_schema(sys.stdin, input_dir)
>  exprs = 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..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 * obj, const char *name, Error **e
>                  name=name)
>  
>  try:
> -    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:o:",
> +    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:",
>                                     ["source", "header", "builtins", "prefix=",
> -                                    "output-dir="])
> +                                    "input-dir=", "output-dir="])
>  except getopt.GetoptError, err:
>      print str(err)
>      sys.exit(1)
>  
> +input_dir = ""
>  output_dir = ""
>  prefix = ""
>  c_file = 'qapi-visit.c'
> @@ -402,6 +403,8 @@ do_builtins = False
>  for o, a in opts:
>      if o in ("-p", "--prefix"):
>          prefix = a
> +    elif o in ("-i", "--input-dir"):
> +        input_dir = a
>      elif o in ("-o", "--output-dir"):
>          output_dir = a + "/"
>      elif o in ("-c", "--source"):
> @@ -480,7 +483,7 @@ fdecl.write(mcgen('''
>  ''',
>                    prefix=prefix, guard=guardname(h_file)))
>  
> -exprs = parse_schema(sys.stdin)
> +exprs = parse_schema(sys.stdin, input_dir)
>  
>  # 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.
>  
> +import os
> +import re
>  from ordereddict import OrderedDict
>  import sys
>  
> +include_cre = re.compile("include\(\"([^\"]*)\"\)")
> +
>  builtin_types = [
>      'str', 'int', 'number', 'bool',
>      'int8', 'int16', 'int32', 'int64',
> @@ -54,8 +58,9 @@ class QAPISchemaError(Exception):
>  
>  class QAPISchema:
>  
> -    def __init__(self, fp):
> +    def __init__(self, fp, input_dir):
>          self.fp = fp
> +        self.input_dir = input_dir
>          self.src = fp.read()
>          if self.src == '' or self.src[-1] != '\n':
>              self.src += '\n'
> @@ -100,6 +105,20 @@ class QAPISchema:
>                  if self.cursor == len(self.src):
>                      self.tok = None
>                      return
> +            elif self.tok == 'i':
> +                include_src = self.src[self.cursor-1:]
> +                include_match = include_cre.match(include_src)
> +                if include_match is not None:
> +                    include_path = 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 = QAPISchema(open(include_path),
> +                                                self.input_dir)
> +                    self.exprs += include_schema.exprs
> +                    self.cursor += include_match.span()[1] - 1
>              elif not self.tok.isspace():
>                  raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
>  
> @@ -158,9 +177,9 @@ class QAPISchema:
>              raise QAPISchemaError(self, 'Expected "{", "[" or string')
>          return expr
>  
> -def parse_schema(fp):
> +def parse_schema(fp, input_dir):
>      try:
> -        schema = QAPISchema(fp)
> +        schema = QAPISchema(fp, input_dir)
>      except QAPISchemaError, e:
>          print >>sys.stderr, e
>          exit(1)
> 
>
Lluís Vilanova Feb. 10, 2014, 7:44 p.m. UTC | #2
Luiz Capitulino writes:

> On Thu,  9 Jan 2014 11:59:55 +0100
> Lluís Vilanova <vilanova@ac.upc.edu> 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


>> 
>> Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
>> ---
>> 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(-)
>> 
>> 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="", proxy=False):
>> 
>> 
>> try:
>> -    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:m",
>> +    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
>> ["source", "header", "prefix=",
>> -                                    "output-dir=", "type=", "middle"])
>> +                                    "input-dir=", "output-dir=",
>> +                                    "type=", "middle"])
>> except getopt.GetoptError, err:
>> print str(err)
>> sys.exit(1)
>> 
>> +input_dir = ""
>> output_dir = ""
>> prefix = ""
>> dispatch_type = "sync"
>> @@ -409,6 +411,8 @@ do_h = False
>> for o, a in opts:
>> if o in ("-p", "--prefix"):
>> prefix = a
>> +    elif o in ("-i", "--input-dir"):
>> +        input_dir = a
>> elif o in ("-o", "--output-dir"):
>> output_dir = a + "/"
>> elif o in ("-t", "--type"):
>> @@ -440,7 +444,7 @@ except os.error, e:
>> if e.errno != errno.EEXIST:
>> raise
>> 
>> -exprs = parse_schema(sys.stdin)
>> +exprs = parse_schema(sys.stdin, input_dir)
>> commands = filter(lambda expr: expr.has_key('command'), exprs)
>> commands = filter(lambda expr: not expr.has_key('gen'), commands)
>> 
>> 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)
>> 
>> 
>> try:
>> -    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:o:",
>> +    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:",
>> ["source", "header", "builtins",
>> -                                    "prefix=", "output-dir="])
>> +                                    "prefix=", "input-dir=", "output-dir="])
>> except getopt.GetoptError, err:
>> print str(err)
>> sys.exit(1)
>> 
>> output_dir = ""
>> +input_dir = ""
>> prefix = ""
>> c_file = 'qapi-types.c'
>> h_file = 'qapi-types.h'
>> @@ -301,6 +302,8 @@ do_builtins = False
>> for o, a in opts:
>> if o in ("-p", "--prefix"):
>> prefix = a
>> +    elif o in ("-i", "--input-dir"):
>> +        input_dir = a
>> elif o in ("-o", "--output-dir"):
>> output_dir = a + "/"
>> elif o in ("-c", "--source"):
>> @@ -381,7 +384,7 @@ fdecl.write(mcgen('''
>> ''',
>> guard=guardname(h_file)))
>> 
>> -exprs = parse_schema(sys.stdin)
>> +exprs = parse_schema(sys.stdin, input_dir)
>> exprs = 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..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 * obj, const char *name, Error **e
>> name=name)
>> 
>> try:
>> -    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:o:",
>> +    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:",
>> ["source", "header", "builtins", "prefix=",
>> -                                    "output-dir="])
>> +                                    "input-dir=", "output-dir="])
>> except getopt.GetoptError, err:
>> print str(err)
>> sys.exit(1)
>> 
>> +input_dir = ""
>> output_dir = ""
>> prefix = ""
>> c_file = 'qapi-visit.c'
>> @@ -402,6 +403,8 @@ do_builtins = False
>> for o, a in opts:
>> if o in ("-p", "--prefix"):
>> prefix = a
>> +    elif o in ("-i", "--input-dir"):
>> +        input_dir = a
>> elif o in ("-o", "--output-dir"):
>> output_dir = a + "/"
>> elif o in ("-c", "--source"):
>> @@ -480,7 +483,7 @@ fdecl.write(mcgen('''
>> ''',
>> prefix=prefix, guard=guardname(h_file)))
>> 
>> -exprs = parse_schema(sys.stdin)
>> +exprs = parse_schema(sys.stdin, input_dir)
>> 
>> # 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.
>> 
>> +import os
>> +import re
>> from ordereddict import OrderedDict
>> import sys
>> 
>> +include_cre = re.compile("include\(\"([^\"]*)\"\)")
>> +
>> builtin_types = [
>> 'str', 'int', 'number', 'bool',
>> 'int8', 'int16', 'int32', 'int64',
>> @@ -54,8 +58,9 @@ class QAPISchemaError(Exception):
>> 
>> class QAPISchema:
>> 
>> -    def __init__(self, fp):
>> +    def __init__(self, fp, input_dir):
>> self.fp = fp
>> +        self.input_dir = input_dir
>> self.src = fp.read()
>> if self.src == '' or self.src[-1] != '\n':
>> self.src += '\n'
>> @@ -100,6 +105,20 @@ class QAPISchema:
>> if self.cursor == len(self.src):
>> self.tok = None
>> return
>> +            elif self.tok == 'i':
>> +                include_src = self.src[self.cursor-1:]
>> +                include_match = include_cre.match(include_src)
>> +                if include_match is not None:
>> +                    include_path = 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 = QAPISchema(open(include_path),
>> +                                                self.input_dir)
>> +                    self.exprs += include_schema.exprs
>> +                    self.cursor += include_match.span()[1] - 1
>> elif not self.tok.isspace():
>> raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
>> 
>> @@ -158,9 +177,9 @@ class QAPISchema:
>> raise QAPISchemaError(self, 'Expected "{", "[" or string')
>> return expr
>> 
>> -def parse_schema(fp):
>> +def parse_schema(fp, input_dir):
>> try:
>> -        schema = QAPISchema(fp)
>> +        schema = QAPISchema(fp, input_dir)
>> except QAPISchemaError, e:
>> print >>sys.stderr, e
>> exit(1)
>> 
>>
diff mbox

Patch

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="", proxy=False):
 
 
 try:
-    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:m",
+    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
                                    ["source", "header", "prefix=",
-                                    "output-dir=", "type=", "middle"])
+                                    "input-dir=", "output-dir=",
+                                    "type=", "middle"])
 except getopt.GetoptError, err:
     print str(err)
     sys.exit(1)
 
+input_dir = ""
 output_dir = ""
 prefix = ""
 dispatch_type = "sync"
@@ -409,6 +411,8 @@  do_h = False
 for o, a in opts:
     if o in ("-p", "--prefix"):
         prefix = a
+    elif o in ("-i", "--input-dir"):
+        input_dir = a
     elif o in ("-o", "--output-dir"):
         output_dir = a + "/"
     elif o in ("-t", "--type"):
@@ -440,7 +444,7 @@  except os.error, e:
     if e.errno != errno.EEXIST:
         raise
 
-exprs = parse_schema(sys.stdin)
+exprs = parse_schema(sys.stdin, input_dir)
 commands = filter(lambda expr: expr.has_key('command'), exprs)
 commands = filter(lambda expr: not expr.has_key('gen'), commands)
 
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)
 
 
 try:
-    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:o:",
+    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:",
                                    ["source", "header", "builtins",
-                                    "prefix=", "output-dir="])
+                                    "prefix=", "input-dir=", "output-dir="])
 except getopt.GetoptError, err:
     print str(err)
     sys.exit(1)
 
 output_dir = ""
+input_dir = ""
 prefix = ""
 c_file = 'qapi-types.c'
 h_file = 'qapi-types.h'
@@ -301,6 +302,8 @@  do_builtins = False
 for o, a in opts:
     if o in ("-p", "--prefix"):
         prefix = a
+    elif o in ("-i", "--input-dir"):
+        input_dir = a
     elif o in ("-o", "--output-dir"):
         output_dir = a + "/"
     elif o in ("-c", "--source"):
@@ -381,7 +384,7 @@  fdecl.write(mcgen('''
 ''',
                   guard=guardname(h_file)))
 
-exprs = parse_schema(sys.stdin)
+exprs = parse_schema(sys.stdin, input_dir)
 exprs = 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..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 * obj, const char *name, Error **e
                 name=name)
 
 try:
-    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:o:",
+    opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:",
                                    ["source", "header", "builtins", "prefix=",
-                                    "output-dir="])
+                                    "input-dir=", "output-dir="])
 except getopt.GetoptError, err:
     print str(err)
     sys.exit(1)
 
+input_dir = ""
 output_dir = ""
 prefix = ""
 c_file = 'qapi-visit.c'
@@ -402,6 +403,8 @@  do_builtins = False
 for o, a in opts:
     if o in ("-p", "--prefix"):
         prefix = a
+    elif o in ("-i", "--input-dir"):
+        input_dir = a
     elif o in ("-o", "--output-dir"):
         output_dir = a + "/"
     elif o in ("-c", "--source"):
@@ -480,7 +483,7 @@  fdecl.write(mcgen('''
 ''',
                   prefix=prefix, guard=guardname(h_file)))
 
-exprs = parse_schema(sys.stdin)
+exprs = parse_schema(sys.stdin, input_dir)
 
 # 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.
 
+import os
+import re
 from ordereddict import OrderedDict
 import sys
 
+include_cre = re.compile("include\(\"([^\"]*)\"\)")
+
 builtin_types = [
     'str', 'int', 'number', 'bool',
     'int8', 'int16', 'int32', 'int64',
@@ -54,8 +58,9 @@  class QAPISchemaError(Exception):
 
 class QAPISchema:
 
-    def __init__(self, fp):
+    def __init__(self, fp, input_dir):
         self.fp = fp
+        self.input_dir = input_dir
         self.src = fp.read()
         if self.src == '' or self.src[-1] != '\n':
             self.src += '\n'
@@ -100,6 +105,20 @@  class QAPISchema:
                 if self.cursor == len(self.src):
                     self.tok = None
                     return
+            elif self.tok == 'i':
+                include_src = self.src[self.cursor-1:]
+                include_match = include_cre.match(include_src)
+                if include_match is not None:
+                    include_path = 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 = QAPISchema(open(include_path),
+                                                self.input_dir)
+                    self.exprs += include_schema.exprs
+                    self.cursor += include_match.span()[1] - 1
             elif not self.tok.isspace():
                 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
 
@@ -158,9 +177,9 @@  class QAPISchema:
             raise QAPISchemaError(self, 'Expected "{", "[" or string')
         return expr
 
-def parse_schema(fp):
+def parse_schema(fp, input_dir):
     try:
-        schema = QAPISchema(fp)
+        schema = QAPISchema(fp, input_dir)
     except QAPISchemaError, e:
         print >>sys.stderr, e
         exit(1)