Invokes the Protocol Compiler to generate a _pb2.py from the given .proto file. Does nothing if the output already exists and is newer than the input.
(source, require = True)
| 49 | |
| 50 | |
| 51 | def generate_proto(source, require = True): |
| 52 | """Invokes the Protocol Compiler to generate a _pb2.py from the given |
| 53 | .proto file. Does nothing if the output already exists and is newer than |
| 54 | the input.""" |
| 55 | |
| 56 | if not require and not os.path.exists(source): |
| 57 | return |
| 58 | |
| 59 | output = source.replace(".proto", "_pb2.py").replace("../src/", "") |
| 60 | |
| 61 | if (not os.path.exists(output) or |
| 62 | (os.path.exists(source) and |
| 63 | os.path.getmtime(source) > os.path.getmtime(output))): |
| 64 | print("Generating %s..." % output) |
| 65 | |
| 66 | if not os.path.exists(source): |
| 67 | sys.stderr.write("Can't find required file: %s\n" % source) |
| 68 | sys.exit(-1) |
| 69 | |
| 70 | if protoc is None: |
| 71 | sys.stderr.write( |
| 72 | "protoc is not installed nor found in ../src. Please compile it " |
| 73 | "or install the binary package.\n") |
| 74 | sys.exit(-1) |
| 75 | |
| 76 | protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ] |
| 77 | if subprocess.call(protoc_command) != 0: |
| 78 | sys.exit(-1) |
| 79 | |
| 80 | def GenerateUnittestProtos(): |
| 81 | generate_proto("../src/google/protobuf/any_test.proto", False) |