Newer
Older
# Version of the Namespace package "dummy"
__version__ = "2021.01.a1"
# contains also script to auto-compile .proto files into python classes
# https://stackoverflow.com/questions/23585450/automate-compilation-of-protobuf-specs-into-python-classes-in-setup-py
import os, sys, subprocess
from pathlib import Path
from distutils.spawn import find_executable
# find protoc installation
# TODO note somewhere that protobuf-compiler needed to compile them
if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
protoc = os.environ['PROTOC']
else:
protoc = find_executable('protoc')
# search all proto files in this package
this_dir = os.path.dirname(os.path.realpath(__file__)) # this dir
working_dir = os.path.join(this_dir, 'identification')
proto_files_g = list(Path(working_dir).rglob("*.[pP][rR][oO][tT][oO]"))
python_out = os.path.join(working_dir, '_proto_gen')
# find global protobuf identification file
for path in proto_files_g:
path_dir = os.path.dirname(path)
if path_dir.endswith("/feature/identification"):
id_path_dir = path_dir
id_path = id_path_dir + "/identification.proto"
if not os.path.exists(id_path):
print("Could not find identification.proto main file.")
# create pb2 and put them in the correct directory
for path in proto_files_g:
path_dir = os.path.dirname(path)
output_file = os.path.join(python_out, os.path.basename(path).split('.')[0] + '_pb2.py')
if path_dir == id_path_dir:
continue
# generate if source file newer or output file non existent
if not os.path.exists(output_file) or (os.path.getmtime(path) > os.path.getmtime(output_file)):
if protoc is None:
print("Protoc recompilation needed, but no proto-compiler found. Install it, e.g. protobuf-compiler via apt.")
sys.exit(1)
# merge path_dir and id_path_dir files (ObjProperties and base file)
with tempfile.NamedTemporaryFile() as tmp:
files = [path, id_path]
for fname in files:
print(fname)
with open(fname, 'rb') as infile:
tmp.write(infile.read())
for line in tmp:
print(line)
# '--proto_path=' + working_dir,
protoc_command = [protoc, '-I=' + os.path.dirname(tmp.name), '--python_out=' + python_out, tmp.name]
# print("Execute protoc for: " + str(path))
print(protoc_command)
if subprocess.call(protoc_command) != 0:
sys.exit(1)
os.rename(python_out + "/" + os.path.basename(tmp.name) + "_pb2.py", python_out + "/" + os.path.basename(path)[:-6] + "_pb2.py") # TODO use join?