1"""Deletes the existing bindings, then rebuild using the source .proto file.""" 2 3import os 4from shutil import copyfile 5 6 7UP = '../' 8PROTO_PATH = 'src/config/proto/chromiumos/config/api/test/tls/' 9PROTO_NAME = 'common.proto' 10BUILD_CMD = ( 11 "python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. {}" 12 .format(PROTO_NAME)) 13 14 15def delete_old_protos(): 16 """Delete any existing protos or built proto bindings.""" 17 for file in os.listdir('.'): 18 if 'common' in file: 19 os.remove(file) 20 21 22def copy_proto_from_src(): 23 """Copy the proto from the src dir to the local dir.""" 24 proto_dir = get_proto_dir() 25 if os.path.isfile(proto_dir): 26 copyfile(proto_dir, PROTO_NAME) 27 else: 28 raise Exception("Protos not found @ {}".format(proto_dir)) 29 30 31def get_proto_dir(): 32 """Return the full path of the common.proto from TLS.""" 33 return "{}{}{}".format(UP * get_current_depth(), PROTO_PATH, PROTO_NAME) 34 35 36def get_current_depth(): 37 """Return the current depth off /src/ within the file structure.""" 38 dirs = os.getcwd().split('/') 39 src_level = dirs.index('src') 40 return len(dirs) - src_level 41 42 43def create_bindings(): 44 os.system(BUILD_CMD) 45 46 47def main(): 48 delete_old_protos() 49 copy_proto_from_src() 50 create_bindings() 51 52if __name__ == "__main__": 53 main() 54