31 """Implements the generate_py_protobufs command."""
33 __author__ =
'dlj@google.com (David L. Jones)'
38 import distutils.spawn
as spawn
39 from distutils.cmd
import Command
40 from distutils.errors
import DistutilsOptionError, DistutilsExecError
43 """Generates Python sources for .proto files."""
45 description =
'Generate Python sources for .proto files'
47 (
'extra-proto-paths=',
None,
48 'Additional paths to resolve imports in .proto files.'),
51 'Path to a specific `protoc` command to use.'),
53 boolean_options = [
'recurse']
56 """Sets the defaults for the command options."""
66 """Sets the final values for the command options.
68 Defaults were set in `initialize_options`, but could have been changed
69 by command-line options or by other commands.
71 self.ensure_dirname(
'source_dir')
72 self.ensure_string_list(
'extra_proto_paths')
76 self.ensure_dirname(
'output_dir')
110 root_candidate = os.path.normpath(root_candidate)
114 self.announce(
'using computed proto_root_path: ' + self.
proto_root_path, level=2)
117 raise DistutilsOptionError(
'source_dir ' + self.
source_dir +
121 files = glob.glob(os.path.join(self.
source_dir,
'*.proto'))
123 files.extend(glob.glob(os.path.join(self.
source_dir,
'**',
'*.proto'), recursive=
True))
126 raise DistutilsOptionError(
'no .proto files were found under ' + self.
source_dir)
128 self.ensure_string_list(
'proto_files')
131 self.
protoc = os.getenv(
'PROTOC')
133 self.
protoc = spawn.find_executable(
'protoc')