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