xref: /aosp_15_r20/external/autotest/server/hosts/tls_client/buildprotos.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1"""Deletes the existing bindings, then rebuild using the source .proto file."""
2
3import os
4from shutil import copyfile
5
6UP = '../'
7PROTO_PATH = 'src/config/proto/chromiumos/config/api/test/tls/'
8PROTO_NAME = 'commontls.proto'
9DEST_PROTO_NAME = 'autotest_common.proto'
10DEP_PROTO_RELATIVE_PATH = 'dependencies/longrunning/'
11DEP_PROTO_NAME = 'operations.proto'
12
13BUILD_CMD = (
14        'python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. {} {}'
15        .format(DEST_PROTO_NAME,
16                os.path.join(DEP_PROTO_RELATIVE_PATH, DEP_PROTO_NAME)))
17
18
19def delete_old_protos():
20    """Delete any existing protos or built proto bindings."""
21    for file in os.listdir('.'):
22        if 'autotest_common' in file:
23            os.remove(file)
24
25    for file in os.listdir(DEP_PROTO_RELATIVE_PATH):
26        if 'operations' in file:
27            os.remove(os.path.join(DEP_PROTO_RELATIVE_PATH, file))
28
29
30def copy_proto_from_src():
31    """Copy the proto from the src dirs to the local dir."""
32    copy_list = [(get_proto_path(), DEST_PROTO_NAME),
33                 (get_proto_deps_dir(),
34                  os.path.join(DEP_PROTO_RELATIVE_PATH, DEP_PROTO_NAME))]
35
36    for src, dest in copy_list:
37        if os.path.isfile(src):
38            copyfile(src, dest)
39        else:
40            raise Exception('Proto missing at %s' % src)
41
42
43def get_proto_path():
44    """Return the full path of the commontls.proto from TLS."""
45    return os.path.join(UP * get_current_depth(), PROTO_PATH, PROTO_NAME)
46
47
48def get_proto_deps_dir():
49    """Return the full path of the operations.proto from TLS."""
50    return os.path.join(UP * get_current_depth(), PROTO_PATH,
51                        DEP_PROTO_RELATIVE_PATH, DEP_PROTO_NAME)
52
53
54def get_current_depth():
55    """Return the current depth off /src/ within the file structure."""
56    dirs = os.getcwd().split('/')
57    src_level = dirs.index('src')
58    return len(dirs) - src_level
59
60
61def modify_proto():
62    """Change the full path for the dependencies for a local one."""
63    # This is likely a dirty hack, but compiling with the full src in autotest
64    # doesn't work. Open to suggestions for alternatives.
65
66    #TODO (dbeckett@) b/183220746, work on a better thats not a hack...
67    with open(DEST_PROTO_NAME, 'r+') as f:
68        original = f.read()
69    new = original.replace(
70            'import "chromiumos/config/api/test/tls/dependencies/longrunning/operations.proto";',
71            'import "dependencies/longrunning/operations.proto";')
72    with open(DEST_PROTO_NAME, 'w') as wf:
73        wf.write(new)
74
75
76def create_bindings():
77    os.system(BUILD_CMD)
78
79
80def main():
81    delete_old_protos()
82    copy_proto_from_src()
83    modify_proto()
84    create_bindings()
85
86
87if __name__ == "__main__":
88    main()
89