Skip to content

Commit 570f338

Browse files
committed
Fix lint issue
Signed-off-by: Yong Tang <[email protected]>
1 parent 882c2dd commit 570f338

File tree

1 file changed

+48
-54
lines changed

1 file changed

+48
-54
lines changed

third_party/toolchains/tf/tf_configure.bzl

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@ _TF_HEADER_DIR = "TF_HEADER_DIR"
44
_TF_SHARED_LIBRARY_DIR = "TF_SHARED_LIBRARY_DIR"
55
_TF_SHARED_LIBRARY_NAME = "TF_SHARED_LIBRARY_NAME"
66

7-
8-
def _tpl(repository_ctx, tpl, substitutions={}, out=None):
7+
def _tpl(repository_ctx, tpl, substitutions = {}, out = None):
98
if not out:
109
out = tpl
1110
repository_ctx.template(
12-
out, Label("//third_party/toolchains/tf:%s.tpl" % tpl), substitutions,
11+
out,
12+
Label("//third_party/toolchains/tf:%s.tpl" % tpl),
13+
substitutions,
1314
)
1415

15-
1616
def _fail(msg):
1717
"""Output failure message when auto configuration fails."""
1818
red = "\033[0;31m"
1919
no_color = "\033[0m"
2020
fail("{}Python Configuration Error:{} {}\n".format(red, no_color, msg))
2121

22-
2322
def _is_windows(repository_ctx):
2423
"""Returns true if the host operating system is windows."""
2524
os_name = repository_ctx.os.name.lower()
2625
if os_name.find("windows") != -1:
2726
return True
2827
return False
2928

30-
3129
def _execute(
32-
repository_ctx, cmdline, error_msg=None, error_details=None, empty_stdout_fine=False
33-
):
30+
repository_ctx,
31+
cmdline,
32+
error_msg = None,
33+
error_details = None,
34+
empty_stdout_fine = False):
3435
"""Executes an arbitrary shell command.
3536
Args:
3637
repository_ctx: the repository_ctx object
@@ -50,12 +51,11 @@ def _execute(
5051
error_msg.strip() if error_msg else "Repository command failed",
5152
result.stderr.strip(),
5253
error_details if error_details else "",
53-
]
54-
)
54+
],
55+
),
5556
)
5657
return result
5758

58-
5959
def _read_dir(repository_ctx, src_dir):
6060
"""Returns a string with all files in a directory.
6161
Finds all files inside a directory, traversing subfolders and following
@@ -67,7 +67,7 @@ def _read_dir(repository_ctx, src_dir):
6767
find_result = _execute(
6868
repository_ctx,
6969
["cmd.exe", "/c", "dir", src_dir, "/b", "/s", "/a-d"],
70-
empty_stdout_fine=True,
70+
empty_stdout_fine = True,
7171
)
7272

7373
# src_files will be used in genrule.outs where the paths must
@@ -77,12 +77,11 @@ def _read_dir(repository_ctx, src_dir):
7777
find_result = _execute(
7878
repository_ctx,
7979
["find", src_dir, "-follow", "-type", "f"],
80-
empty_stdout_fine=True,
80+
empty_stdout_fine = True,
8181
)
8282
result = find_result.stdout
8383
return result
8484

85-
8685
def _genrule(genrule_name, command, outs):
8786
"""Returns a string with a genrule.
8887
@@ -97,35 +96,32 @@ def _genrule(genrule_name, command, outs):
9796
A genrule target.
9897
"""
9998
return (
100-
"genrule(\n"
101-
+ ' name = "'
102-
+ genrule_name
103-
+ '",\n'
104-
+ " outs = [\n"
105-
+ "{}\n".format(outs)
106-
+ " ],\n"
107-
+ ' cmd = """ {} """,\n'.format(command)
108-
+ ")\n"
99+
"genrule(\n" +
100+
' name = "' +
101+
genrule_name +
102+
'",\n' +
103+
" outs = [\n" +
104+
"{}\n".format(outs) +
105+
" ],\n" +
106+
' cmd = """ {} """,\n'.format(command) +
107+
")\n"
109108
)
110109

111-
112110
def _norm_path(path):
113111
"""Returns a path with '/' and remove the trailing slash."""
114112
path = path.replace("\\", "/")
115113
if path[-1] == "/":
116114
path = path[:-1]
117115
return path
118116

119-
120117
def _symlink_genrule_for_dir(
121-
repository_ctx,
122-
src_dir,
123-
dest_dir,
124-
genrule_name,
125-
src_files=[],
126-
dest_files=[],
127-
tf_pip_dir_rename_pair=[],
128-
):
118+
repository_ctx,
119+
src_dir,
120+
dest_dir,
121+
genrule_name,
122+
src_files = [],
123+
dest_files = [],
124+
tf_pip_dir_rename_pair = []):
129125
"""Returns a genrule to symlink(or copy if on Windows) a set of files.
130126
131127
If src_dir is passed, files will be read from the given directory; otherwise
@@ -151,34 +147,35 @@ def _symlink_genrule_for_dir(
151147
tf_pip_dir_rename_pair_len = len(tf_pip_dir_rename_pair)
152148
if tf_pip_dir_rename_pair_len != 0 and tf_pip_dir_rename_pair_len != 2:
153149
_fail(
154-
"The size of argument tf_pip_dir_rename_pair should be either 0 or 2, but %d is given."
155-
% tf_pip_dir_rename_pair_len
150+
"The size of argument tf_pip_dir_rename_pair should be either 0 or 2, but %d is given." %
151+
tf_pip_dir_rename_pair_len,
156152
)
157153

158154
outs = []
159155
command = []
160156
if src_dir != None:
161157
src_dir = _norm_path(src_dir)
162158
dest_dir = _norm_path(dest_dir)
159+
163160
# TODO: manually remove aws headers for now. Not needed after tensorflow drops aws.
164161
files = "\n".join(
165162
sorted(
166163
[
167164
e
168165
for e in _read_dir(repository_ctx, src_dir).splitlines()
169-
if ("/external/aws" not in e)
170-
and ("external/boringssl" not in e)
171-
and ("external/curl" not in e)
172-
]
173-
)
166+
if ("/external/aws" not in e) and
167+
("external/boringssl" not in e) and
168+
("external/curl" not in e)
169+
],
170+
),
174171
)
175172

176173
# Create a list with the src_dir stripped to use for outputs.
177174
if tf_pip_dir_rename_pair_len:
178175
dest_files = (
179176
files.replace(src_dir, "")
180-
.replace(tf_pip_dir_rename_pair[0], tf_pip_dir_rename_pair[1])
181-
.splitlines()
177+
.replace(tf_pip_dir_rename_pair[0], tf_pip_dir_rename_pair[1])
178+
.splitlines()
182179
)
183180
else:
184181
dest_files = files.replace(src_dir, "").splitlines()
@@ -193,37 +190,35 @@ def _symlink_genrule_for_dir(
193190
for i in range(len(dest_files)):
194191
outs.append(' "{}",'.format(dest_files[i]))
195192
command.append(
196-
'cp -r "{}" "{}"'.format(src_files[i], "$(@D)/" + dest_files[i])
193+
'cp -r "{}" "{}"'.format(src_files[i], "$(@D)/" + dest_files[i]),
197194
)
198195
return _genrule(genrule_name, " && ".join(command), "\n".join(outs))
199196

200-
201197
def _tf_pip_impl(repository_ctx):
202198
tf_header_dir = repository_ctx.os.environ[_TF_HEADER_DIR]
203199
tf_header_rule = _symlink_genrule_for_dir(
204200
repository_ctx,
205201
tf_header_dir,
206202
"include",
207203
"tf_header_include",
208-
tf_pip_dir_rename_pair=["tensorflow_core", "tensorflow"],
204+
tf_pip_dir_rename_pair = ["tensorflow_core", "tensorflow"],
209205
)
210206

211207
tf_shared_library_dir = repository_ctx.os.environ[_TF_SHARED_LIBRARY_DIR]
212208
tf_shared_library_name = repository_ctx.os.environ[_TF_SHARED_LIBRARY_NAME]
213209
tf_shared_library_path = "{}/{}".format(
214-
tf_shared_library_dir, tf_shared_library_name
210+
tf_shared_library_dir,
211+
tf_shared_library_name,
215212
)
216213

217214
tf_shared_library_rule = _symlink_genrule_for_dir(
218215
repository_ctx,
219216
None,
220217
"",
221218
"libtensorflow_framework.so",
222-
src_files=[tf_shared_library_path],
223-
dest_files=[
224-
"_pywrap_tensorflow_internal.lib"
225-
if _is_windows(repository_ctx)
226-
else "libtensorflow_framework.so"
219+
src_files = [tf_shared_library_path],
220+
dest_files = [
221+
"_pywrap_tensorflow_internal.lib" if _is_windows(repository_ctx) else "libtensorflow_framework.so",
227222
],
228223
)
229224

@@ -236,8 +231,7 @@ def _tf_pip_impl(repository_ctx):
236231
},
237232
)
238233

239-
240234
tf_configure = repository_rule(
241-
implementation=_tf_pip_impl,
242-
environ=[_TF_HEADER_DIR, _TF_SHARED_LIBRARY_DIR, _TF_SHARED_LIBRARY_NAME,],
235+
implementation = _tf_pip_impl,
236+
environ = [_TF_HEADER_DIR, _TF_SHARED_LIBRARY_DIR, _TF_SHARED_LIBRARY_NAME],
243237
)

0 commit comments

Comments
 (0)