forked from tensorflow/tensorboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_protos.py
111 lines (87 loc) · 3.57 KB
/
update_protos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Update proto source files based on generated outputs."""
import os
import sys
# Paths to "tensorboard/data/server/" in (a) the Bazel runfiles tree,
# whence we can read data dependencies, and (b) the Git repository,
# whither we can write output files.
_BAZEL_DIR = os.path.join("tensorboard", "data", "server")
_REPO_DIR = os.path.dirname(os.readlink(__file__))
# Basename for the gRPC file descriptor set. The same basename is used
# for the generated file and the source file.
_FILE_DESCRIPTOR_SET = "descriptor.bin"
_RUST_LICENSE = """\
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
"""
def pkg_basename(pkg, rust_extension):
if pkg == _FILE_DESCRIPTOR_SET:
return _FILE_DESCRIPTOR_SET
else:
return "%s%s" % (pkg, rust_extension)
def expected_contents(pkg):
src = os.path.join(_BAZEL_DIR, "genproto", pkg_basename(pkg, ".rs"))
with open(src, "rb") as infile:
contents = infile.read()
if pkg != _FILE_DESCRIPTOR_SET:
contents = _RUST_LICENSE.encode("utf-8") + contents
return contents
def repo_file_path(pkg):
return os.path.join(_REPO_DIR, pkg_basename(pkg, ".pb.rs"))
def runfiles_file_path(pkg):
return os.path.join(_BAZEL_DIR, pkg_basename(pkg, ".pb.rs"))
def update(proto_packages):
for pkg in proto_packages:
with open(repo_file_path(pkg), "wb") as outfile:
outfile.write(expected_contents(pkg))
def check(proto_packages):
failed = False
for pkg in proto_packages:
dst = runfiles_file_path(pkg)
try:
expected = expected_contents(pkg)
with open(dst, "rb") as infile:
actual = infile.read()
except OSError as e:
failed = True
print("Could not read package %s: %s" % (pkg, e))
continue
if expected == actual:
print("%s OK" % dst)
else:
print("%s out of date" % dst)
failed = True
if failed:
print("To update, run //tensorboard/data/server:update_protos")
raise SystemExit(1)
def main():
(mode, *proto_packages) = sys.argv[1:]
if mode == "--update":
return update(proto_packages)
if mode == "--check":
return check(proto_packages)
raise ValueError("unknown mode: %r" % mode)
if __name__ == "__main__":
main()