X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=ovsdb%2Fovsdb-idlc.in;h=2dd25d8029cd9acb636b70f5799a51e44e558ab8;hb=36d802ae1fd61d5ae3bfa1b114b8f3a911d987e5;hp=d6fc14ab1437a84aaae8c0dd1c55a5525697b9f5;hpb=8bc915de7a08d5b4210ba768d56f15211927d1e7;p=openvswitch diff --git a/ovsdb/ovsdb-idlc.in b/ovsdb/ovsdb-idlc.in index d6fc14ab..2dd25d80 100755 --- a/ovsdb/ovsdb-idlc.in +++ b/ovsdb/ovsdb-idlc.in @@ -1,6 +1,7 @@ #! @PYTHON@ import getopt +import os import re import sys @@ -43,8 +44,9 @@ class DbSchema: comment = getMember(json, 'comment', [unicode], 'database') tablesJson = mustGetMember(json, 'tables', [dict], 'database') tables = {} - for name, tableJson in tablesJson.iteritems(): - tables[name] = TableSchema.fromJson(tableJson, "%s table" % name) + for tableName, tableJson in tablesJson.iteritems(): + tables[tableName] = TableSchema.fromJson(tableJson, + "%s table" % tableName) idlPrefix = mustGetMember(json, 'idlPrefix', [unicode], 'database') idlHeader = mustGetMember(json, 'idlHeader', [unicode], 'database') return DbSchema(name, comment, tables, idlPrefix, idlHeader) @@ -93,7 +95,7 @@ class ColumnSchema: type = Type.fromJson(mustGetMember(json, 'type', [dict, unicode], description), 'type of %s' % description) - ephemeral = getMember(json, 'ephemeral', [True,False], description) + ephemeral = getMember(json, 'ephemeral', [bool], description) persistent = ephemeral != True return ColumnSchema(comment, type, persistent) @@ -141,13 +143,54 @@ class Type: d["max"] = self.max return d + def isScalar(self): + return self.min == 1 and self.max == 1 and not self.value + + def isOptional(self): + return self.min == 0 and self.max == 1 + + def toEnglish(self): + keyName = atomicTypeToEnglish(self.key, self.keyRefTable) + if self.value: + valueName = atomicTypeToEnglish(self.value, self.valueRefTable) + + if self.isScalar(): + return atomicTypeToEnglish(self.key, self.keyRefTable) + elif self.isOptional(): + if self.value: + return "optional %s-%s pair" % (keyName, valueName) + else: + return "optional %s" % keyName + else: + if self.max == "unlimited": + if self.min: + quantity = "%d or more " % self.min + else: + quantity = "" + elif self.min: + quantity = "%d to %d " % (self.min, self.max) + else: + quantity = "up to %d " % self.max + + if self.value: + return "map of %s%s-%s pairs" % (quantity, keyName, valueName) + else: + return "set of %s%s" % (quantity, keyName) + + +def atomicTypeToEnglish(base, refTable): + if base == 'uuid' and refTable: + return refTable + else: + return base + def parseSchema(filename): - file = open(filename, "r") - s = "" - for line in file: - if not line.startswith('//'): - s += line - return DbSchema.fromJson(json.loads(s)) + return DbSchema.fromJson(json.load(open(filename, "r"))) + +def annotateSchema(schemaFile, annotationFile): + schemaJson = json.load(open(schemaFile, "r")) + execfile(annotationFile, globals(), {"s": schemaJson}) + json.dump(schemaJson, sys.stdout) def cBaseType(prefix, type, refTable=None): if type == 'uuid' and refTable: @@ -183,7 +226,15 @@ def cDeclComment(type): else: return "" -def cMembers(prefix, columnName, column): +def constify(cType, const): + if (const + and cType.endswith('*') and not cType.endswith('**') + and (cType.startswith('struct uuid') or cType.startswith('char'))): + return 'const %s' % cType + else: + return cType + +def cMembers(prefix, columnName, column, const): type = column.type if type.min == 1 and type.max == 1: singleton = True @@ -197,16 +248,15 @@ def cMembers(prefix, columnName, column): if type.value: key = {'name': "key_%s" % columnName, - 'type': cBaseType(prefix, type.key, type.keyRefTable) + pointer, + 'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const), 'comment': ''} value = {'name': "value_%s" % columnName, - 'type': (cBaseType(prefix, type.value, type.valueRefTable) - + pointer), + 'type': constify(cBaseType(prefix, type.value, type.valueRefTable) + pointer, const), 'comment': ''} members = [key, value] else: m = {'name': columnName, - 'type': cBaseType(prefix, type.key, type.keyRefTable) + pointer, + 'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const), 'comment': cDeclComment(type)} members = [m] @@ -216,7 +266,8 @@ def cMembers(prefix, columnName, column): 'comment': ''}) return members -def printCIDLHeader(schema): +def printCIDLHeader(schemaFile): + schema = parseSchema(schemaFile) prefix = schema.idlPrefix print '''\ /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */ @@ -237,7 +288,7 @@ def printCIDLHeader(schema): print "\tstruct ovsdb_idl_row header_;" for columnName, column in table.columns.iteritems(): print "\n\t/* %s column. */" % columnName - for member in cMembers(prefix, columnName, column): + for member in cMembers(prefix, columnName, column, False): print "\t%(type)s%(name)s;%(comment)s" % member print '''\ }; @@ -258,7 +309,7 @@ struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *); print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName}, args = ['%(type)s%(name)s' % member for member - in cMembers(prefix, columnName, column)] + in cMembers(prefix, columnName, column, True)] print '%s);' % ', '.join(args) print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix @@ -274,7 +325,8 @@ def printEnum(members): print " %s" % members[-1] print "};" -def printCIDLSource(schema): +def printCIDLSource(schemaFile): + schema = parseSchema(schemaFile) prefix = schema.idlPrefix print '''\ /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */ @@ -479,7 +531,7 @@ void for columnName, column in table.columns.iteritems(): type = column.type print '\nvoid' - members = cMembers(prefix, columnName, column) + members = cMembers(prefix, columnName, column, True) keyVar = members[0]['name'] nVar = None valueVar = None @@ -602,20 +654,36 @@ def ovsdb_escape(string): return '\\x%02x' % ord(c) return re.sub(r'["\\\000-\037]', escape, string) -def printOVSDBSchema(schema): - json.dump(schema.toJson(), sys.stdout, sort_keys=True, indent=2) +def printDoc(schemaFile): + schema = parseSchema(schemaFile) + print schema.name + if schema.comment: + print schema.comment + + for tableName, table in sorted(schema.tables.iteritems()): + title = "%s table" % tableName + print + print title + print '-' * len(title) + if table.comment: + print table.comment + + for columnName, column in sorted(table.columns.iteritems()): + print + print "%s (%s)" % (columnName, column.type.toEnglish()) + if column.comment: + print "\t%s" % column.comment def usage(): print """\ %(argv0)s: ovsdb schema compiler -usage: %(argv0)s [OPTIONS] ACTION SCHEMA -where SCHEMA is the ovsdb schema to read (in JSON format). +usage: %(argv0)s [OPTIONS] COMMAND ARG... -One of the following actions must specified: - validate validate schema without taking any other action - c-idl-header print C header file for IDL - c-idl-source print C source file for IDL implementation - ovsdb-schema print ovsdb parseable schema +The following commands are supported: + annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS + c-idl-header IDL print C header file for IDL + c-idl-source IDL print C source file for IDL implementation + doc IDL print schema documentation The following options are also available: -h, --help display this help message @@ -626,40 +694,49 @@ The following options are also available: if __name__ == "__main__": try: try: - options, args = getopt.gnu_getopt(sys.argv[1:], 'hV', - ['help', + options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV', + ['directory', + 'help', 'version']) except getopt.GetoptError, geo: sys.stderr.write("%s: %s\n" % (argv0, geo.msg)) sys.exit(1) + for key, value in options: + if key in ['-h', '--help']: + usage() + elif key in ['-V', '--version']: + print "ovsdb-idlc (Open vSwitch) @VERSION@" + elif key in ['-C', '--directory']: + os.chdir(value) + else: + sys.exit(0) + optKeys = [key for key, value in options] - if '-h' in optKeys or '--help' in optKeys: - usage() - elif '-V' in optKeys or '--version' in optKeys: - print "ovsdb-idlc (Open vSwitch) @VERSION@" - sys.exit(0) - - if len(args) != 2: - sys.stderr.write("%s: exactly two non-option arguments are " - "required (use --help for help)\n" % argv0) + + if not args: + sys.stderr.write("%s: missing command argument " + "(use --help for help)\n" % argv0) sys.exit(1) - action, inputFile = args - schema = parseSchema(inputFile) - if action == 'validate': - pass - elif action == 'ovsdb-schema': - printOVSDBSchema(schema) - elif action == 'c-idl-header': - printCIDLHeader(schema) - elif action == 'c-idl-source': - printCIDLSource(schema) - else: - sys.stderr.write( - "%s: unknown action '%s' (use --help for help)\n" % - (argv0, action)) + commands = {"annotate": (annotateSchema, 2), + "c-idl-header": (printCIDLHeader, 1), + "c-idl-source": (printCIDLSource, 1), + "doc": (printDoc, 1)} + + if not args[0] in commands: + sys.stderr.write("%s: unknown command \"%s\" " + "(use --help for help)\n" % (argv0, args[0])) sys.exit(1) + + func, n_args = commands[args[0]] + if len(args) - 1 != n_args: + sys.stderr.write("%s: \"%s\" requires %d arguments but %d " + "provided\n" + % (argv0, args[0], n_args, len(args) - 1)) + sys.exit(1) + + func(*args[1:]) except Error, e: sys.stderr.write("%s: %s\n" % (argv0, e.msg)) sys.exit(1)