Skip to content

Commit d93a146

Browse files
laszlocsomorcopybara-github
authored andcommitted
py2to3: update tools/android/*.py to PY3
This is a rollback of commit 91b26dc and a roll-forward of commit ad628ec. Original commit message is below. Details: - use six.ensure_str / six.ensure_bytes - use absl.flags instead of gflags - put third_party/py/abseil:srcs into @bazel_tools so the embedded Android tools can also use it - remove python_version = "PY2" from py_binary and py_test rules (the default is "PY3"), except in BUILD.tools, to preserve PY2 compatibility for running Bazel (its embedded Python tools) - added srcs_version = "PY2AND3" to BUILD.tools to make it explicit that these libraries are meant to be py2 and py3 compatible See #10127 PiperOrigin-RevId: 278394040
1 parent afc7581 commit d93a146

15 files changed

+261
-180
lines changed

src/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ JAVA_TOOLS = [
127127
"//third_party/ijar:embedded_zipper_sources",
128128
"//third_party/ijar:zipper",
129129
"//third_party/java/j2objc:embedded_tools_srcs",
130+
"//third_party/py/abseil:srcs",
130131
"//third_party/py/concurrent:srcs",
132+
# TODO(laszlocsomor): delete "//third_party/py/gflags:srcs" after
133+
# every script in @bazel_tools was migrated to use Abseil.
131134
"//third_party/py/gflags:srcs",
132135
"//third_party/py/six:srcs",
133136
"//src/conditions:embedded_tools",

tools/android/BUILD

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
load("//tools/python:private/defs.bzl", "py_library", "py_binary", "py_test")
1+
load("//tools/python:private/defs.bzl", "py_binary", "py_library", "py_test")
22

33
package(default_visibility = ["//tools:__pkg__"])
44

55
py_binary(
66
name = "build_incremental_dexmanifest",
77
srcs = [":build_incremental_dexmanifest.py"],
8-
python_version = "PY2",
98
)
109

1110
sh_test(
@@ -27,16 +26,14 @@ sh_test(
2726
py_binary(
2827
name = "build_split_manifest",
2928
srcs = ["build_split_manifest.py"],
30-
python_version = "PY2",
3129
deps = [
32-
"//third_party/py/gflags",
30+
"//third_party/py/abseil",
3331
],
3432
)
3533

3634
py_test(
3735
name = "build_split_manifest_test",
3836
srcs = ["build_split_manifest_test.py"],
39-
python_version = "PY2",
4037
deps = [
4138
":build_split_manifest",
4239
],
@@ -45,17 +42,15 @@ py_test(
4542
py_binary(
4643
name = "incremental_install",
4744
srcs = ["incremental_install.py"],
48-
python_version = "PY2",
4945
deps = [
46+
"//third_party/py/abseil",
5047
"//third_party/py/concurrent:futures",
51-
"//third_party/py/gflags",
5248
],
5349
)
5450

5551
py_test(
5652
name = "incremental_install_test",
5753
srcs = ["incremental_install_test.py"],
58-
python_version = "PY2",
5954
tags = [
6055
# TODO(laszlocsomor): fix on Windows or describe why it cannot pass.
6156
"no_windows",
@@ -69,8 +64,7 @@ py_test(
6964
py_binary(
7065
name = "strip_resources",
7166
srcs = ["strip_resources.py"],
72-
python_version = "PY2",
73-
deps = ["//third_party/py/gflags"],
67+
deps = ["//third_party/py/abseil"],
7468
)
7569

7670
sh_test(
@@ -92,10 +86,9 @@ py_binary(
9286
srcs = [
9387
"aar_native_libs_zip_creator.py",
9488
],
95-
python_version = "PY2",
9689
deps = [
9790
":junction_lib",
98-
"//third_party/py/gflags",
91+
"//third_party/py/abseil",
9992
],
10093
)
10194

@@ -104,7 +97,6 @@ py_test(
10497
srcs = [
10598
"aar_native_libs_zip_creator_test.py",
10699
],
107-
python_version = "PY2",
108100
deps = [
109101
":aar_native_libs_zip_creator",
110102
],
@@ -113,16 +105,14 @@ py_test(
113105
py_binary(
114106
name = "stubify_manifest",
115107
srcs = ["stubify_manifest.py"],
116-
python_version = "PY2",
117108
deps = [
118-
"//third_party/py/gflags",
109+
"//third_party/py/abseil",
119110
],
120111
)
121112

122113
py_test(
123114
name = "stubify_manifest_test",
124115
srcs = ["stubify_manifest_test.py"],
125-
python_version = "PY2",
126116
deps = [
127117
":stubify_manifest",
128118
],
@@ -131,61 +121,53 @@ py_test(
131121
py_binary(
132122
name = "aar_embedded_jars_extractor",
133123
srcs = ["aar_embedded_jars_extractor.py"],
134-
python_version = "PY2",
135124
deps = [
136125
":junction_lib",
137-
"//third_party/py/gflags",
126+
"//third_party/py/abseil",
138127
],
139128
)
140129

141130
py_test(
142131
name = "aar_embedded_jars_extractor_test",
143132
srcs = ["aar_embedded_jars_extractor_test.py"],
144-
python_version = "PY2",
145133
deps = [":aar_embedded_jars_extractor"],
146134
)
147135

148136
py_binary(
149137
name = "aar_resources_extractor",
150138
srcs = ["aar_resources_extractor.py"],
151-
python_version = "PY2",
152139
deps = [
153140
":junction_lib",
154-
"//third_party/py/gflags",
141+
"//third_party/py/abseil",
155142
],
156143
)
157144

158145
py_test(
159146
name = "aar_resources_extractor_test",
160147
srcs = ["aar_resources_extractor_test.py"],
161-
python_version = "PY2",
162148
deps = [":aar_resources_extractor"],
163149
)
164150

165151
py_binary(
166152
name = "resource_extractor",
167153
srcs = ["resource_extractor.py"],
168-
python_version = "PY2",
169154
)
170155

171156
py_test(
172157
name = "resource_extractor_test",
173158
srcs = ["resource_extractor_test.py"],
174-
python_version = "PY2",
175159
deps = [":resource_extractor"],
176160
)
177161

178162
py_binary(
179163
name = "instrumentation_test_check",
180164
srcs = ["instrumentation_test_check.py"],
181-
python_version = "PY2",
182-
deps = ["//third_party/py/gflags"],
165+
deps = ["//third_party/py/abseil"],
183166
)
184167

185168
py_test(
186169
name = "instrumentation_test_check_test",
187170
srcs = ["instrumentation_test_check_test.py"],
188-
python_version = "PY2",
189171
deps = [":instrumentation_test_check"],
190172
)
191173

@@ -205,7 +187,6 @@ py_test(
205187
"//src/conditions:windows": "junction_test.py",
206188
"//conditions:default": "dummy_test.py",
207189
}),
208-
python_version = "PY2",
209190
deps = [
210191
":junction_lib",
211192
"//src/test/py/bazel:test_base",

tools/android/BUILD.tools

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,42 +216,47 @@ alias(
216216
py_binary(
217217
name = "instrumentation_test_check",
218218
srcs = ["instrumentation_test_check.py"],
219+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
219220
python_version = "PY2",
220-
deps = ["//third_party/py/gflags"],
221+
deps = ["//third_party/py/abseil"],
221222
)
222223

223224
py_binary(
224225
name = "build_incremental_dexmanifest",
225226
srcs = [":build_incremental_dexmanifest.py"],
227+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
226228
python_version = "PY2",
227229
deps = [],
228230
)
229231

230232
py_binary(
231233
name = "build_split_manifest",
232234
srcs = ["build_split_manifest.py"],
235+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
233236
python_version = "PY2",
234237
deps = [
235-
"//third_party/py/gflags",
238+
"//third_party/py/abseil",
236239
],
237240
)
238241

239242
py_binary(
240243
name = "incremental_install",
241244
srcs = ["incremental_install.py"],
245+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
242246
python_version = "PY2",
243247
deps = [
244248
"//third_party/py/concurrent:futures",
245-
"//third_party/py/gflags",
249+
"//third_party/py/abseil",
246250
],
247251
)
248252

249253
py_binary(
250254
name = "strip_resources",
251255
srcs = ["strip_resources.py"],
256+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
252257
python_version = "PY2",
253258
deps = [
254-
"//third_party/py/gflags",
259+
"//third_party/py/abseil",
255260
],
256261
)
257262

@@ -260,51 +265,58 @@ py_binary(
260265
srcs = [
261266
"aar_native_libs_zip_creator.py",
262267
],
268+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
263269
python_version = "PY2",
264270
deps = [
265271
":junction_lib",
266-
"//third_party/py/gflags",
272+
"//third_party/py/abseil",
267273
],
268274
)
269275

270276
py_binary(
271277
name = "stubify_manifest",
272278
srcs = ["stubify_manifest.py"],
279+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
273280
python_version = "PY2",
274281
deps = [
275-
"//third_party/py/gflags",
282+
"//third_party/py/abseil",
276283
],
277284
)
278285

279286
py_binary(
280287
name = "aar_embedded_jars_extractor",
281288
srcs = ["aar_embedded_jars_extractor.py"],
289+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
282290
python_version = "PY2",
283291
deps = [
284292
":junction_lib",
285-
"//third_party/py/gflags",
293+
"//third_party/py/abseil",
286294
],
287295
)
288296

289297
py_binary(
290298
name = "aar_resources_extractor",
291299
srcs = ["aar_resources_extractor.py"],
300+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
292301
python_version = "PY2",
293302
deps = [
294303
":junction_lib",
295-
"//third_party/py/gflags",
304+
"//third_party/py/abseil",
296305
],
297306
)
298307

299308
py_binary(
300309
name = "resource_extractor",
301310
srcs = ["resource_extractor.py"],
311+
# TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127.
302312
python_version = "PY2",
303313
)
304314

305315
py_library(
306316
name = "junction_lib",
307317
srcs = ["junction.py"],
318+
# TODO(bazel-team): remove srcs_version = "PY2AND3" while fixing https://github.com/bazelbuild/bazel/issues/10127.
319+
srcs_version = "PY2AND3",
308320
visibility = ["//visibility:private"],
309321
)
310322

tools/android/aar_embedded_jars_extractor.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Lint as: python2, python3
12
# pylint: disable=g-direct-third-party-import
23
# Copyright 2016 The Bazel Authors. All rights reserved.
34
#
@@ -19,23 +20,31 @@
1920
of the jars and creates a param file for singlejar to merge them into one jar.
2021
"""
2122

23+
from __future__ import absolute_import
24+
from __future__ import division
25+
from __future__ import print_function
26+
2227
import os
2328
import re
2429
import sys
2530
import zipfile
2631

32+
# Do not edit this line. Copybara replaces it with PY2 migration helper.
33+
from absl import app
34+
from absl import flags
35+
import six
36+
2737
from tools.android import junction
28-
from third_party.py import gflags
2938

30-
FLAGS = gflags.FLAGS
39+
FLAGS = flags.FLAGS
3140

32-
gflags.DEFINE_string("input_aar", None, "Input AAR")
33-
gflags.MarkFlagAsRequired("input_aar")
34-
gflags.DEFINE_string(
35-
"output_singlejar_param_file", None, "Output parameter file for singlejar")
36-
gflags.MarkFlagAsRequired("output_singlejar_param_file")
37-
gflags.DEFINE_string("output_dir", None, "Output directory to extract jars in")
38-
gflags.MarkFlagAsRequired("output_dir")
41+
flags.DEFINE_string("input_aar", None, "Input AAR")
42+
flags.mark_flag_as_required("input_aar")
43+
flags.DEFINE_string("output_singlejar_param_file", None,
44+
"Output parameter file for singlejar")
45+
flags.mark_flag_as_required("output_singlejar_param_file")
46+
flags.DEFINE_string("output_dir", None, "Output directory to extract jars in")
47+
flags.mark_flag_as_required("output_dir")
3948

4049

4150
def ExtractEmbeddedJars(aar,
@@ -52,7 +61,7 @@ def ExtractEmbeddedJars(aar,
5261
# output_dir may be a temporary junction, so write the original
5362
# (unshortened) path to the params file
5463
singlejar_param_file.write(
55-
(output_dir_orig + "/" + name + "\n").encode("utf-8"))
64+
six.ensure_binary((output_dir_orig + "/" + name + "\n"), "utf-8"))
5665
aar.extract(name, output_dir)
5766

5867

@@ -68,7 +77,7 @@ def _Main(input_aar,
6877
output_dir_orig)
6978

7079

71-
def main():
80+
def main(unused_argv):
7281
if os.name == "nt":
7382
# Shorten paths unconditionally, because the extracted paths in
7483
# ExtractEmbeddedJars (which we cannot yet predict, because they depend on
@@ -90,4 +99,4 @@ def main():
9099

91100
if __name__ == "__main__":
92101
FLAGS(sys.argv)
93-
main()
102+
app.run(main)

0 commit comments

Comments
 (0)