+def process(filenames, options):
+ '''Process the files given on the command line.'''
+
+ if not filenames:
+ optparser.print_help()
+ return False
+
+ if options.quiet:
+ options.verbose = False
+
+ Globals.verbose_options = options.verbose
+
+ toplevel_options = nanopb_pb2.NanoPBOptions()
+ for s in options.settings:
+ text_format.Merge(s, toplevel_options)
+
+ for filename in filenames:
+ data = open(filename, 'rb').read()
+ fdesc = descriptor.FileDescriptorSet.FromString(data)
+
+ # Check if any separate options are specified
+ try:
+ optfilename = options.options_file % os.path.splitext(filename)[0]
+ except TypeError:
+ # No %s specified, use the filename as-is
+ optfilename = options.options_file
+
+ if options.verbose:
+ print 'Reading options from ' + optfilename
+
+ if os.path.isfile(optfilename):
+ Globals.separate_options = read_options_file(open(optfilename, "rU"))
+ else:
+ Globals.separate_options = []
+
+ # Parse the file
+ file_options = get_nanopb_suboptions(fdesc.file[0], toplevel_options, Names([filename]))
+ enums, messages = parse_file(fdesc.file[0], file_options)
+
+ noext = os.path.splitext(filename)[0]
+ headername = noext + '.' + options.extension + '.h'
+ sourcename = noext + '.' + options.extension + '.c'
+ headerbasename = os.path.basename(headername)
+
+ if not options.quiet:
+ print "Writing to " + headername + " and " + sourcename
+
+ # List of .proto files that should not be included in the C header file
+ # even if they are mentioned in the source .proto.
+ excludes = ['nanopb.proto', 'google/protobuf/descriptor.proto'] + options.exclude
+ dependencies = [d for d in fdesc.file[0].dependency if d not in excludes]
+
+ header = open(headername, 'w')
+ for part in generate_header(dependencies, headerbasename, enums, messages, options):
+ header.write(part)
+
+ source = open(sourcename, 'w')
+ for part in generate_source(headerbasename, enums, messages):
+ source.write(part)
+
+ return True
+
+if __name__ == '__main__':
+ options, filenames = optparser.parse_args()
+ status = process(filenames, options)
+
+ if not status:
+ sys.exit(1)