Skip to content

Commit 22b8126

Browse files
committed
Convert libchrome scripts to python 3
These appear to have already been updated to python 3 in the chromium repository, but it seems that it's rather difficult to update libchrome in android, so just update the scripts to python 3 independently for now. Previous attempts to update libchrome: aosp/1753549, https://groups.google.com/a/google.com/g/chromeos-libchrome/c/gyoJPi3i4Zo/m/PZKWEz-CAAAJ Bug: 203436762 Test: m libchrome, m javac-check, diffed out/target/product/emulator_x86_64/obj/SHARED_LIBRARIES/libchrome_intermediates/libchrome.so and out/soong/.intermediates/external/libchrome/libmojo_jni_headers with and without this cl and got no changes Change-Id: I8d05a73f6b94ce059b7b049480df47021abc4c7d
1 parent 79d51cc commit 22b8126

29 files changed

+154
-183
lines changed

Android.bp

-18
Original file line numberDiff line numberDiff line change
@@ -1169,27 +1169,13 @@ filegroup {
11691169
],
11701170
}
11711171

1172-
// Python in Chrome repository requires still Python 2.
1173-
python_defaults {
1174-
name: "libmojo_scripts",
1175-
version: {
1176-
py2: {
1177-
enabled: true,
1178-
},
1179-
py3: {
1180-
enabled: false,
1181-
},
1182-
},
1183-
}
1184-
11851172
python_binary_host {
11861173
name: "jni_generator",
11871174
main: "base/android/jni_generator/jni_generator.py",
11881175
srcs: [
11891176
"base/android/jni_generator/jni_generator.py",
11901177
"build/**/*.py",
11911178
],
1192-
defaults: ["libmojo_scripts"],
11931179
}
11941180

11951181
python_binary_host {
@@ -1200,7 +1186,6 @@ python_binary_host {
12001186
"base/android/jni_generator/jni_registration_generator.py",
12011187
"build/**/*.py",
12021188
],
1203-
defaults: ["libmojo_scripts"],
12041189
}
12051190

12061191
python_binary_host {
@@ -1218,7 +1203,6 @@ python_binary_host {
12181203
"mojo/public/tools/bindings/generators/java_templates/*.tmpl",
12191204
"mojo/public/tools/bindings/generators/js_templates/*.tmpl",
12201205
],
1221-
defaults: ["libmojo_scripts"],
12221206
}
12231207

12241208
genrule {
@@ -1250,7 +1234,6 @@ python_binary_host {
12501234
"mojo/public/tools/bindings/pylib/mojom/generate/module.py",
12511235
"mojo/public/tools/bindings/pylib/mojom/generate/pack.py",
12521236
],
1253-
defaults: ["libmojo_scripts"],
12541237
}
12551238

12561239
genrule {
@@ -1290,7 +1273,6 @@ python_binary_host {
12901273
srcs: [
12911274
"mojo/public/tools/bindings/mojom_types_downgrader.py",
12921275
],
1293-
defaults: ["libmojo_scripts"],
12941276
}
12951277

12961278
generate_mojom_downgraded_files {

base/android/jni_generator/jni_generator.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@
1717
import textwrap
1818
import zipfile
1919

20-
CHROMIUM_SRC = os.path.join(
21-
os.path.dirname(__file__), os.pardir, os.pardir, os.pardir)
22-
BUILD_ANDROID_GYP = os.path.join(
23-
CHROMIUM_SRC, 'build', 'android', 'gyp')
24-
25-
sys.path.append(BUILD_ANDROID_GYP)
26-
27-
from util import build_utils
20+
from build.android.gyp.util import build_utils
2821

2922

3023
# Match single line comments, multiline comments, character literals, and
@@ -53,7 +46,7 @@
5346
replace_whitespace=False,
5447
subsequent_indent=' ' * (indent + 4),
5548
break_long_words=False)
56-
for indent in xrange(50)] # 50 chosen experimentally.
49+
for indent in range(50)] # 50 chosen experimentally.
5750

5851

5952
class ParseError(Exception):
@@ -799,7 +792,7 @@ def GetContent(self):
799792

800793
@staticmethod
801794
def CreateFromFile(java_file_name, options):
802-
contents = file(java_file_name).read()
795+
contents = open(java_file_name).read()
803796
fully_qualified_class = ExtractFullyQualifiedJavaClassName(java_file_name,
804797
contents)
805798
return JNIFromJavaSource(contents, fully_qualified_class, options)
@@ -856,7 +849,7 @@ def GetClassPathLines(self, classes, declare_only=False):
856849
"${JNI_CLASS_PATH}";
857850
""")
858851

859-
for full_clazz in classes.itervalues():
852+
for full_clazz in classes.values():
860853
values = {
861854
'JAVA_CLASS': GetBinaryClassName(full_clazz),
862855
'JNI_CLASS_PATH': full_clazz,
@@ -882,7 +875,7 @@ def GetClassPathLines(self, classes, declare_only=False):
882875
JNI_REGISTRATION_EXPORT base::subtle::AtomicWord g_${JAVA_CLASS}_clazz = 0;
883876
""" + class_getter)
884877

885-
for full_clazz in classes.itervalues():
878+
for full_clazz in classes.values():
886879
values = {
887880
'JAVA_CLASS': GetBinaryClassName(full_clazz),
888881
}
@@ -1311,13 +1304,13 @@ def GenerateJNIHeader(input_file, output_file, options):
13111304
jni_from_java_source = JNIFromJavaSource.CreateFromFile(
13121305
input_file, options)
13131306
content = jni_from_java_source.GetContent()
1314-
except ParseError, e:
1315-
print e
1307+
except ParseError as e:
1308+
print(e)
13161309
sys.exit(1)
13171310
if output_file:
13181311
WriteOutput(output_file, content)
13191312
else:
1320-
print content
1313+
print(content)
13211314

13221315

13231316
def WriteOutput(output_file, content):
@@ -1393,7 +1386,7 @@ def main(argv):
13931386
input_file = options.input_file
13941387
else:
13951388
option_parser.print_help()
1396-
print '\nError: Must specify --jar_file or --input_file.'
1389+
print('\nError: Must specify --jar_file or --input_file.')
13971390
return 1
13981391
output_file = None
13991392
if options.output_dir:

base/android/jni_generator/jni_generator_tests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def assertObjEquals(self, first, second):
5353
dict_first = first.__dict__
5454
dict_second = second.__dict__
5555
self.assertEquals(dict_first.keys(), dict_second.keys())
56-
for key, value in dict_first.iteritems():
56+
for key, value in dict_first.items():
5757
if (type(value) is list and len(value) and
5858
isinstance(type(value[0]), object)):
5959
self.assertListEquals(value, second.__getattribute__(key))
@@ -64,7 +64,7 @@ def assertObjEquals(self, first, second):
6464

6565
def assertListEquals(self, first, second):
6666
self.assertEquals(len(first), len(second))
67-
for i in xrange(len(first)):
67+
for i in range(len(first)):
6868
if isinstance(first[i], object):
6969
self.assertObjEquals(first[i], second[i])
7070
else:

base/android/jni_generator/jni_registration_generator.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
to register all native methods that exist within an application."""
1111

1212
import argparse
13-
import jni_generator
1413
import multiprocessing
1514
import string
1615
import sys
17-
from util import build_utils
16+
17+
import jni_generator
18+
from build.android.gyp.util import build_utils
1819

1920

2021
# All but FULL_CLASS_NAME, which is used only for sorting.
@@ -58,7 +59,7 @@ def GenerateJNIHeader(java_file_paths, output_file, args):
5859
if output_file:
5960
jni_generator.WriteOutput(output_file, header_content)
6061
else:
61-
print header_content
62+
print(header_content)
6263

6364

6465
def _DictForPath(path):
@@ -244,7 +245,7 @@ def _SubstituteNativeMethods(self, template):
244245
ret = []
245246
all_classes = self.helper.GetUniqueClasses(self.natives)
246247
all_classes[self.class_name] = self.fully_qualified_class
247-
for clazz, full_clazz in all_classes.iteritems():
248+
for clazz, full_clazz in all_classes.items():
248249
kmethods = self._GetKMethodsString(clazz)
249250
namespace_str = ''
250251
if self.namespace:
@@ -321,7 +322,7 @@ def main(argv):
321322
args.sources_files = build_utils.ParseGnList(args.sources_files)
322323

323324
if not args.sources_files:
324-
print '\nError: Must specify --sources_files.'
325+
print('\nError: Must specify --sources_files.')
325326
return 1
326327

327328
java_file_paths = []

build/android/gyp/util/build_utils.py

+9-20
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,8 @@
2323
# //build/config/android/internal_rules.gni
2424

2525
# Some clients do not add //build/android/gyp to PYTHONPATH.
26-
import md5_check # pylint: disable=relative-import
27-
28-
# pylib conflicts with mojo/public/tools/bindings/pylib. Prioritize
29-
# build/android/pylib.
30-
# PYTHONPATH wouldn't help in this case, because soong put source files under
31-
# temp directory for each build, so the abspath is unknown until the
32-
# execution.
33-
#sys.path.append(os.path.join(os.path.dirname(__file__),
34-
# os.pardir, os.pardir, os.pardir))
35-
sys.path.insert(0, os.path.join(os.path.dirname(__file__),
36-
os.pardir, os.pardir))
37-
38-
import gn_helpers
26+
import build.android.gyp.util.md5_check as md5_check # pylint: disable=relative-import
27+
import build.gn_helpers as gn_helpers
3928

4029
# Definition copied from pylib/constants/__init__.py to avoid adding
4130
# a dependency on pylib.
@@ -44,7 +33,7 @@
4433
os.pardir, os.pardir, os.pardir, os.pardir)))
4534

4635
HERMETIC_TIMESTAMP = (2001, 1, 1, 0, 0, 0)
47-
_HERMETIC_FILE_ATTR = (0644 << 16L)
36+
_HERMETIC_FILE_ATTR = (0o644 << 16)
4837

4938

5039
@contextlib.contextmanager
@@ -246,7 +235,7 @@ def _IsSymlink(zip_file, name):
246235

247236
# The two high-order bytes of ZipInfo.external_attr represent
248237
# UNIX permissions and file type bits.
249-
return stat.S_ISLNK(zi.external_attr >> 16L)
238+
return stat.S_ISLNK(zi.external_attr >> 16)
250239

251240

252241
def ExtractAll(zip_path, path=None, no_clobber=True, pattern=None,
@@ -309,12 +298,12 @@ def AddToZipHermetic(zip_file, zip_path, src_path=None, data=None,
309298

310299
if src_path and os.path.islink(src_path):
311300
zipinfo.filename = zip_path
312-
zipinfo.external_attr |= stat.S_IFLNK << 16L # mark as a symlink
301+
zipinfo.external_attr |= stat.S_IFLNK << 16 # mark as a symlink
313302
zip_file.writestr(zipinfo, os.readlink(src_path))
314303
return
315304

316305
if src_path:
317-
with file(src_path) as f:
306+
with open(src_path) as f:
318307
data = f.read()
319308

320309
# zipfile will deflate even when it makes the file bigger. To avoid
@@ -342,7 +331,7 @@ def DoZip(inputs, output, base_dir=None, compress_fn=None):
342331
"""
343332
input_tuples = []
344333
for tup in inputs:
345-
if isinstance(tup, basestring):
334+
if isinstance(tup, str):
346335
tup = (os.path.relpath(tup, base_dir), tup)
347336
input_tuples.append(tup)
348337

@@ -382,7 +371,7 @@ def MergeZips(output, input_zips, path_transform=None):
382371
path_transform = path_transform or (lambda p: p)
383372
added_names = set()
384373

385-
output_is_already_open = not isinstance(output, basestring)
374+
output_is_already_open = not isinstance(output, str)
386375
if output_is_already_open:
387376
assert isinstance(output, zipfile.ZipFile)
388377
out_zip = output
@@ -445,7 +434,7 @@ def _ComputePythonDependencies():
445434
src/. The paths will be relative to the current directory.
446435
"""
447436
_ForceLazyModulesToLoad()
448-
module_paths = (m.__file__ for m in sys.modules.itervalues()
437+
module_paths = (m.__file__ for m in sys.modules.values()
449438
if m is not None and hasattr(m, '__file__'))
450439
abs_module_paths = map(os.path.abspath, module_paths)
451440

build/android/gyp/util/jar_info_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def WriteJarInfoFile(info_path, info_data, source_file_map=None):
4444
temporary location.
4545
"""
4646
with open(info_path, 'w') as info_file:
47-
for fully_qualified_name, path in info_data.iteritems():
47+
for fully_qualified_name, path in info_data.items():
4848
if source_file_map and path in source_file_map:
4949
path = source_file_map[path]
5050
assert not path.startswith('/tmp'), (

build/android/gyp/util/md5_check.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ def CallAndRecordIfStale(
7878
return
7979

8080
if PRINT_EXPLANATIONS:
81-
print '=' * 80
82-
print 'Target is stale: %s' % record_path
83-
print changes.DescribeDifference()
84-
print '=' * 80
81+
print('=' * 80)
82+
print('Target is stale: %s' % record_path)
83+
print(changes.DescribeDifference())
84+
print('=' * 80)
8585

8686
args = (changes,) if pass_changes else ()
8787
function(*args)

build/android/gyp/util/resource_utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def CreateResourceInfoFile(files_to_zip, zip_path):
5959
"""
6060
info_file_path = zip_path + '.info'
6161
with open(info_file_path, 'w') as info_file:
62-
for archive_path, source_path in files_to_zip.iteritems():
62+
for archive_path, source_path in files_to_zip.items():
6363
info_file.write('{},{}\n'.format(archive_path, source_path))
6464

6565

@@ -260,7 +260,7 @@ def CreateRJavaFiles(srcjar_dir, package, main_r_txt_file,
260260
if entry:
261261
resources_by_type[entry.resource_type].append(entry)
262262

263-
for package, resources_by_type in resources_by_package.iteritems():
263+
for package, resources_by_type in resources_by_package.items():
264264
_CreateRJavaSourceFile(srcjar_dir, package, resources_by_type,
265265
rjava_build_options)
266266

@@ -296,7 +296,7 @@ def _RenderRJavaSource(package, resources_by_type, rjava_build_options):
296296
"""Render an R.java source file. See _CreateRJaveSourceFile for args info."""
297297
final_resources_by_type = collections.defaultdict(list)
298298
non_final_resources_by_type = collections.defaultdict(list)
299-
for res_type, resources in resources_by_type.iteritems():
299+
for res_type, resources in resources_by_type.items():
300300
for entry in resources:
301301
# Entries in stylable that are not int[] are not actually resource ids
302302
# but constants.

build/android/pylib/constants/host_paths_unittest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_GetAaptPath(self):
4040
self.assertEqual(host_paths.GetAaptPath(), _EXPECTED_AAPT_PATH)
4141

4242
def test_ToolPath(self):
43-
for cpu_arch, binprefix in _EXPECTED_NDK_TOOL_SUBDIR_MAP.iteritems():
43+
for cpu_arch, binprefix in _EXPECTED_NDK_TOOL_SUBDIR_MAP.items():
4444
expected_binprefix = os.path.join(constants.ANDROID_NDK_ROOT, binprefix)
4545
expected_path = expected_binprefix + 'foo'
4646
self.assertEqual(host_paths.ToolPath('foo', cpu_arch), expected_path)

build/android/pylib/content_settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _GetTypeBinding(value):
2929
return 's'
3030
raise ValueError('Unsupported type %s' % type(value))
3131

32-
def iteritems(self):
32+
def items(self):
3333
# Example row:
3434
# 'Row: 0 _id=13, name=logging_id2, value=-1fccbaa546705b05'
3535
for row in self._device.RunShellCommand(

build/android/pylib/device_settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def ConfigureContentSettings(device, desired_settings):
3333
for key, value in key_value:
3434
settings[key] = value
3535
logging.info('\n%s %s', table, (80 - len(table)) * '-')
36-
for key, value in sorted(settings.iteritems()):
36+
for key, value in sorted(settings.items()):
3737
logging.info('\t%s: %s', key, value)
3838

3939

build/check_gn_headers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def ParseGNProjectJSON(gn, out_dir, tmp_out):
110110
"""Parse GN output and get the header files"""
111111
all_headers = set()
112112

113-
for _target, properties in gn['targets'].iteritems():
113+
for _target, properties in gn['targets'].items():
114114
sources = properties.get('sources', [])
115115
public = properties.get('public', [])
116116
# Exclude '"public": "*"'.
@@ -292,7 +292,7 @@ def PrintError(msg):
292292
print ' ', cc
293293

294294
print '\nMissing headers sorted by number of affected object files:'
295-
count = {k: len(v) for (k, v) in d.iteritems()}
295+
count = {k: len(v) for (k, v) in d.items()}
296296
for f in sorted(count, key=count.get, reverse=True):
297297
if f in missing:
298298
print count[f], f

0 commit comments

Comments
 (0)