Skip to content

Commit

Permalink
Migrate all Python scripts to Python 3 (#464)
Browse files Browse the repository at this point in the history
This migrates all checked in Python scripts to run under Python 3.x.
Python 2.7 reached end-of-life in January of 2020 and has been removed
from may OS distributions at this point.
  • Loading branch information
cbracken committed Jun 9, 2021
1 parent 3ae5da2 commit 607277f
Show file tree
Hide file tree
Showing 55 changed files with 438 additions and 381 deletions.
5 changes: 2 additions & 3 deletions build/android/gyp/jar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
Expand Down Expand Up @@ -48,8 +48,7 @@ def Jar(class_files, classes_dir, jar_path, manifest_file=None, additional_jar_f
def JarDirectory(classes_dir, excluded_classes, jar_path, manifest_file=None, additional_jar_files=None):
class_files = build_utils.FindInDirectory(classes_dir, '*.class')
for exclude in excluded_classes:
class_files = filter(
lambda f: not fnmatch.fnmatch(f, exclude), class_files)
class_files = [f for f in class_files if not fnmatch.fnmatch(f, exclude)]

Jar(class_files, classes_dir, jar_path, manifest_file=manifest_file,
additional_jar_files=additional_jar_files)
Expand Down
4 changes: 2 additions & 2 deletions build/android/gyp/javac.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
Expand Down Expand Up @@ -301,7 +301,7 @@ def main(argv):
if options.jar_path:
if options.main_class or options.manifest_entry:
if options.manifest_entry:
entries = map(lambda e: e.split(":"), options.manifest_entry)
entries = [e.split(":") for e in options.manifest_entry]
else:
entries = []
manifest_file = os.path.join(temp_dir, 'manifest')
Expand Down
22 changes: 12 additions & 10 deletions build/android/gyp/util/build_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3
#
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand Down Expand Up @@ -297,10 +299,10 @@ def Node(dep):

# Then: simple, slow topological sort.
sorted_deps = []
unsorted_deps = dict(map(Node, all_deps))
unsorted_deps = dict(list(map(Node, all_deps)))
while unsorted_deps:
for library, dependencies in unsorted_deps.items():
if not dependencies.intersection(unsorted_deps.keys()):
for library, dependencies in list(unsorted_deps.items()):
if not dependencies.intersection(list(unsorted_deps.keys())):
sorted_deps.append(library)
del unsorted_deps[library]

Expand All @@ -314,9 +316,9 @@ def GetPythonDependencies():
src/. The paths will be relative to the current directory.
"""
_ForceLazyModulesToLoad()
module_paths = (m.__file__ for m in sys.modules.values()
module_paths = (m.__file__ for m in list(sys.modules.values())
if m is not None and hasattr(m, '__file__'))
abs_module_paths = map(os.path.abspath, module_paths)
abs_module_paths = list(map(os.path.abspath, module_paths))

assert os.path.isabs(DIR_SOURCE_ROOT)
non_system_module_paths = [
Expand All @@ -326,8 +328,8 @@ def ConvertPycToPy(s):
return s[:-1]
return s

non_system_module_paths = map(ConvertPycToPy, non_system_module_paths)
non_system_module_paths = map(os.path.relpath, non_system_module_paths)
non_system_module_paths = list(map(ConvertPycToPy, non_system_module_paths))
non_system_module_paths = list(map(os.path.relpath, non_system_module_paths))
return sorted(set(non_system_module_paths))


Expand All @@ -339,11 +341,11 @@ def _ForceLazyModulesToLoad():
over the values until sys.modules stabilizes so that no modules are missed.
"""
while True:
num_modules_before = len(sys.modules.keys())
for m in sys.modules.values():
num_modules_before = len(list(sys.modules.keys()))
for m in list(sys.modules.values()):
if m is not None and hasattr(m, '__file__'):
_ = m.__file__
num_modules_after = len(sys.modules.keys())
num_modules_after = len(list(sys.modules.keys()))
if num_modules_before == num_modules_after:
break

Expand Down
7 changes: 4 additions & 3 deletions build/apply_locales.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand All @@ -20,7 +21,7 @@ def main(argv):
(options, arglist) = parser.parse_args(argv)

if len(arglist) < 3:
print 'ERROR: need string and list of locales'
print('ERROR: need string and list of locales')
return 1

str_template = arglist[1]
Expand All @@ -39,7 +40,7 @@ def main(argv):

# Quote each element so filename spaces don't mess up GYP's attempt to parse
# it into a list.
print ' '.join(["'%s'" % x for x in results])
print(' '.join(["'%s'" % x for x in results]))

if __name__ == '__main__':
sys.exit(main(sys.argv))
7 changes: 4 additions & 3 deletions build/check_return_value.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand All @@ -12,6 +13,6 @@

devnull = open(os.devnull, 'wb')
if not subprocess.call(sys.argv[1:], stdout=devnull, stderr=devnull):
print 1
print(1)
else:
print 0
print(0)
19 changes: 10 additions & 9 deletions build/compiler_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand All @@ -19,9 +20,9 @@


def Usage(program_name):
print '%s MODE TOOL' % os.path.basename(program_name)
print 'MODE: host or target.'
print 'TOOL: assembler or compiler or linker.'
print('%s MODE TOOL' % os.path.basename(program_name))
print('MODE: host or target.')
print('TOOL: assembler or compiler or linker.')
return 1


Expand Down Expand Up @@ -91,24 +92,24 @@ def GetVersion(compiler, tool):
result = parsed_output.group(1) + parsed_output.group(2)
compiler_version_cache[cache_key] = result
return result
except Exception, e:
except Exception as e:
if tool_error:
sys.stderr.write(tool_error)
print >> sys.stderr, "compiler_version.py failed to execute:", compiler
print >> sys.stderr, e
print("compiler_version.py failed to execute:", compiler, file=sys.stderr)
print(e, file=sys.stderr)
return ""


def main(args):
try:
(mode, tool) = ParseArgs(args[1:])
except Exception, e:
except Exception as e:
sys.stderr.write(e.message + '\n\n')
return Usage(args[0])

ret_code, result = ExtractVersion(mode, tool)
if ret_code == 0:
print result
print(result)
return ret_code


Expand Down
5 changes: 3 additions & 2 deletions build/config/linux/pkg-config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import print_function


import json
import os
Expand Down
2 changes: 2 additions & 0 deletions build/config/linux/sysroot_ld_path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3
#
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand Down
9 changes: 5 additions & 4 deletions build/copy_test_data_ios.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand Down Expand Up @@ -94,11 +95,11 @@ def DoMain(argv):
def main(argv):
try:
result = DoMain(argv[1:])
except WrongNumberOfArgumentsException, e:
print >>sys.stderr, e
except WrongNumberOfArgumentsException as e:
print(e, file=sys.stderr)
return 1
if result:
print result
print(result)
return 0

if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions build/detect_host_arch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand Down Expand Up @@ -37,4 +38,4 @@ def DoMain(_):
return HostArch()

if __name__ == '__main__':
print DoMain([])
print(DoMain([]))
11 changes: 6 additions & 5 deletions build/download_nacl_toolchains.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand All @@ -21,9 +22,9 @@ def Main(args):
package_version_dir = os.path.join(nacl_build_dir, 'package_version')
package_version = os.path.join(package_version_dir, 'package_version.py')
if not os.path.exists(package_version):
print "Can't find '%s'" % package_version
print 'Presumably you are intentionally building without NativeClient.'
print 'Skipping NativeClient toolchain download.'
print("Can't find '%s'" % package_version)
print('Presumably you are intentionally building without NativeClient.')
print('Skipping NativeClient toolchain download.')
sys.exit(0)
sys.path.insert(0, package_version_dir)
import package_version
Expand All @@ -40,7 +41,7 @@ def Main(args):
if 'pnacl' in buildbot_name and 'sdk' in buildbot_name:
use_pnacl = True
if use_pnacl:
print '\n*** DOWNLOADING PNACL TOOLCHAIN ***\n'
print('\n*** DOWNLOADING PNACL TOOLCHAIN ***\n')
else:
args = ['--exclude', 'pnacl_newlib'] + args

Expand Down
5 changes: 3 additions & 2 deletions build/env_dump.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand Down Expand Up @@ -44,7 +45,7 @@ def main():

env_diff = {}
new_env = json.loads(output)
for k, val in new_env.items():
for k, val in list(new_env.items()):
if k == '_' or (k in os.environ and os.environ[k] == val):
continue
env_diff[k] = val
Expand Down
7 changes: 4 additions & 3 deletions build/extract_from_cab.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand All @@ -17,12 +18,12 @@ def run_quiet(*args):
out, _ = popen.communicate()
if popen.returncode:
# expand emits errors to stdout, so if we fail, then print that out.
print out
print(out)
return popen.returncode

def main():
if len(sys.argv) != 4:
print 'Usage: extract_from_cab.py cab_path archived_file output_dir'
print('Usage: extract_from_cab.py cab_path archived_file output_dir')
return 1

[cab_path, archived_file, output_dir] = sys.argv[1:]
Expand Down
4 changes: 2 additions & 2 deletions build/fuchsia/fidl_gen_cpp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
Expand Down Expand Up @@ -59,7 +59,7 @@ def main():
args.json
]

for _, fidl_files in fidl_files_by_name.iteritems():
for _, fidl_files in fidl_files_by_name.items():
fidlc_command.append('--files')
for fidl_file in fidl_files:
fidl_abspath = os.path.abspath('%s/%s' % (args.sdk_base, fidl_file))
Expand Down
5 changes: 3 additions & 2 deletions build/get_landmines.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand Down Expand Up @@ -30,7 +31,7 @@ def print_landmines():
# dependency problems, fix the dependency problems instead of adding a
# landmine.

print 'Lets start a new landmines file.'
print('Lets start a new landmines file.')


def main():
Expand Down
5 changes: 3 additions & 2 deletions build/get_sdk_extras_packages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand All @@ -18,7 +19,7 @@ def main():
for package in packages:
out.append(package['package_id'])

print ','.join(out)
print(','.join(out))

if __name__ == '__main__':
sys.exit(main())
Loading

0 comments on commit 607277f

Please sign in to comment.