Improve status/error reporting in generator.

Update issue 105
Status: FixedInGit
This commit is contained in:
Petteri Aimonen
2014-02-12 18:51:48 +02:00
parent 057165966c
commit bd22cf2777

View File

@@ -36,7 +36,6 @@ except:
''' + '\n') ''' + '\n')
raise raise
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Generation of single fields # Generation of single fields
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@@ -900,6 +899,7 @@ class Globals:
'''Ugly global variables, should find a good way to pass these.''' '''Ugly global variables, should find a good way to pass these.'''
verbose_options = False verbose_options = False
separate_options = [] separate_options = []
matched_namemasks = set()
def get_nanopb_suboptions(subdesc, options, name): def get_nanopb_suboptions(subdesc, options, name):
'''Get copy of options, and merge information from subdesc.''' '''Get copy of options, and merge information from subdesc.'''
@@ -910,6 +910,7 @@ def get_nanopb_suboptions(subdesc, options, name):
dotname = '.'.join(name.parts) dotname = '.'.join(name.parts)
for namemask, options in Globals.separate_options: for namemask, options in Globals.separate_options:
if fnmatch(dotname, namemask): if fnmatch(dotname, namemask):
Globals.matched_namemasks.add(namemask)
new_options.MergeFrom(options) new_options.MergeFrom(options)
# Handle options defined in .proto # Handle options defined in .proto
@@ -929,8 +930,8 @@ def get_nanopb_suboptions(subdesc, options, name):
new_options.MergeFrom(ext) new_options.MergeFrom(ext)
if Globals.verbose_options: if Globals.verbose_options:
print "Options for " + dotname + ":" sys.stderr.write("Options for " + dotname + ": ")
print text_format.MessageToString(new_options) sys.stderr.write(text_format.MessageToString(new_options) + "\n")
return new_options return new_options
@@ -994,13 +995,14 @@ def process_file(filename, fdesc, options):
# No %s specified, use the filename as-is # No %s specified, use the filename as-is
optfilename = options.options_file optfilename = options.options_file
if options.verbose:
print 'Reading options from ' + optfilename
if os.path.isfile(optfilename): if os.path.isfile(optfilename):
if options.verbose:
sys.stderr.write('Reading options from ' + optfilename + '\n')
Globals.separate_options = read_options_file(open(optfilename, "rU")) Globals.separate_options = read_options_file(open(optfilename, "rU"))
else: else:
Globals.separate_options = [] Globals.separate_options = []
Globals.matched_namemasks = set()
# Parse the file # Parse the file
file_options = get_nanopb_suboptions(fdesc, toplevel_options, Names([filename])) file_options = get_nanopb_suboptions(fdesc, toplevel_options, Names([filename]))
@@ -1023,6 +1025,14 @@ def process_file(filename, fdesc, options):
sourcedata = ''.join(generate_source(headerbasename, enums, sourcedata = ''.join(generate_source(headerbasename, enums,
messages, extensions, options)) messages, extensions, options))
# Check if there were any lines in .options that did not match a member
unmatched = [n for n,o in Globals.separate_options if n not in Globals.matched_namemasks]
if unmatched and not options.quiet:
sys.stderr.write("Following patterns in " + optfilename + " did not match any fields: "
+ ', '.join(unmatched) + "\n")
if not Globals.verbose_options:
sys.stderr.write("Use protoc --nanopb-out=-v:. to see a list of the field names.\n")
return {'headername': headername, 'headerdata': headerdata, return {'headername': headername, 'headerdata': headerdata,
'sourcename': sourcename, 'sourcedata': sourcedata} 'sourcename': sourcename, 'sourcedata': sourcedata}
@@ -1044,7 +1054,8 @@ def main_cli():
results = process_file(filename, None, options) results = process_file(filename, None, options)
if not options.quiet: if not options.quiet:
print "Writing to " + results['headername'] + " and " + results['sourcename'] sys.stderr.write("Writing to " + results['headername'] + " and "
+ results['sourcename'] + "\n")
open(results['headername'], 'w').write(results['headerdata']) open(results['headername'], 'w').write(results['headerdata'])
open(results['sourcename'], 'w').write(results['sourcedata']) open(results['sourcename'], 'w').write(results['sourcedata'])
@@ -1067,10 +1078,7 @@ def main_plugin():
args = shlex.split(request.parameter) args = shlex.split(request.parameter)
options, dummy = optparser.parse_args(args) options, dummy = optparser.parse_args(args)
# We can't go printing stuff to stdout Globals.verbose_options = options.verbose
Globals.verbose_options = False
options.verbose = False
options.quiet = True
response = plugin_pb2.CodeGeneratorResponse() response = plugin_pb2.CodeGeneratorResponse()