Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed Jan 31, 2020
1 parent 9795bff commit d0fb02b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions sdks/python/gen_protos.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ def _import(m):
sys.path.pop(0)


def _find_protoc_gen_mypy():
# NOTE: this shouldn't be necessary if the virtualenv and test environment
# is setup correctly, since protoc will search the PATH itself
fname = 'protoc-gen-mypy'

pathstr = os.environ.get('PATH')
search_paths = pathstr.split(os.pathsep) if pathstr else []
# should typically be installed into the venv's bin dir
search_paths.insert(0, os.path.dirname(sys.executable))
for path in search_paths:
fullpath = os.path.join(path, fname)
if os.path.exists(fullpath):
return fullpath
raise RuntimeError("Could not find %s in %s" %
(fname, ', '.join(search_paths)))


def generate_proto_files(force=False, log=None):

try:
Expand All @@ -225,6 +242,7 @@ def generate_proto_files(force=False, log=None):

if log is None:
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

py_sdk_root = os.path.dirname(os.path.abspath(__file__))
common = os.path.join(py_sdk_root, '..', 'common')
Expand Down Expand Up @@ -292,11 +310,18 @@ def generate_proto_files(force=False, log=None):
else:
log.info('Regenerating Python proto definitions (%s).' % regenerate)
builtin_protos = pkg_resources.resource_filename('grpc_tools', '_proto')

protoc_gen_mypy = _find_protoc_gen_mypy()

log.info('Found protoc at %s' % protoc.__file__)
log.info('Found protoc_gen_mypy at %s' % protoc_gen_mypy)

args = (
[sys.executable] + # expecting to be called from command line
['--proto_path=%s' % builtin_protos] +
['--proto_path=%s' % d for d in proto_dirs] +
['--python_out=%s' % out_dir] +
['--plugin=protoc-gen-mypy=%s' % protoc_gen_mypy] +
['--mypy_out=%s' % out_dir] +
# TODO(robertwb): Remove the prefix once it's the default.
['--grpc_python_out=grpc_2_0:%s' % out_dir] +
Expand All @@ -311,11 +336,6 @@ def generate_proto_files(force=False, log=None):
for path in MODEL_RESOURCES:
shutil.copy2(os.path.join(py_sdk_root, path), out_dir)

ret_code = subprocess.call(["pip", "install", "future==0.16.0"])
if ret_code:
raise RuntimeError(
'Error installing future during proto generation')

ret_code = subprocess.call(
["futurize", "--both-stages", "--write", "--no-diff", out_dir])
if ret_code:
Expand Down
2 changes: 1 addition & 1 deletion sdks/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def generate_protos_first(original_cmd):

class cmd(original_cmd, object):
def run(self):
gen_protos.generate_proto_files(log=log)
gen_protos.generate_proto_files()
super(cmd, self).run()
return cmd
except ImportError:
Expand Down

0 comments on commit d0fb02b

Please sign in to comment.