0

I have a directory named protos which holds a single .proto file, but will eventually hold many. This directory has a sibling named app , where I would like to dump the built grpc python files.

I'm trying to write a simple bash script that will invoke the protoc command and build .proto files in protos, and output the built files to app

Below is a tree of where I've placed each file.

├── build_protos.sh
└── trainingInstance (root module directory)
    |
    ├── app
    │   ├── app.py
    |   ├── (where I'd like my built files to go)
    |   |
    |   └──trainingInstance
    |      └──protos
    |         └──(where the built files actually go)
    |
    └── protos
        └── TrainingService.proto (what I'd like to compile)


When I run build_protos.sh, the file TrainingService.proto is detected fine and built, but instead of placing the built file into ...trainingInstance/app/<file> , it instead places it in ...trainingInstance/app/trainingInstance/protos/<file> (see above).

I thought I had solved this problem by explicitly setting the proto_path to protos, but it doesn't seem to care either way. Below is my build_protos.sh file:

#!/bin/bash

working_dir=$(pwd)
proto_dirs=($(pwd)/trainingInstance)

for dir in $proto_dirs
do
    srcdir=$dir/protos #./trainingInstance/protos
    destdir=$dir/app #./trainingInstance/app
    echo Building $dir protos...

    python3 -m grpc_tools.protoc\
        -I $working_dir\
        --proto_path=$srcdir\
        --python_out=$destdir\
        --grpc_python_out=$destdir\
        $srcdir/*.proto #./trainingInstance/protos/*.proto

done

So does anyone know how to stop protoc from making these reflected directories in the output directory? Any help is appreciated.

Alex
  • 183
  • 1
  • 10

1 Answers1

7

Not 5 minutes after posting this question (after previously playing with the script for an hour), I've solved my own problem: the -I flag, which is pointed to my working-directory, forces protoc to build files in this manner, since it aims to keep different .proto packages separate. This issue is fixed by removing the -I flag entirely.

I feel I should leave this here in-case anyone else has a similar issue.

Alex
  • 183
  • 1
  • 10
  • 1
    indeed, you should definitely leave this here for others to benefit from this. – Kevin S Apr 02 '19 at 13:21
  • This doesn't seem to work with `pip install grpcio-tools-1.51.1` as of 2022-01-13. The message is: `File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).` – est Jan 13 '23 at 03:33