From 716121c74a62adb96eb846ea05f886f2874bf0fd Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Fri, 10 Jan 2020 12:53:02 +0200 Subject: [PATCH] make_distrib: Add Python 3 support (see issue #2856) --- tools/combine_libs.py | 4 +++- tools/file_util.py | 25 +++++++++---------- tools/make_cmake.py | 1 + tools/make_distrib.py | 56 +++++++++++++++++++++++++++---------------- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/tools/combine_libs.py b/tools/combine_libs.py index 41103c349..ec3fb6c58 100644 --- a/tools/combine_libs.py +++ b/tools/combine_libs.py @@ -9,6 +9,8 @@ optionally removing some of the object files in the input libs by regular expression matching. For usage information, run the script with a --help argument. ''' +from __future__ import absolute_import +from __future__ import print_function import optparse import os import re @@ -51,7 +53,7 @@ def CombineLibraries(output, remove_re, inputs): removals = CollectRemovals(remove_re, inputs) if len(removals) > 0: - print 'Removals: ', removals + print('Removals: ', removals) args = ['lib.exe', '/out:%s' % output] args += ['/remove:%s' % obj for obj in removals] diff --git a/tools/file_util.py b/tools/file_util.py index 3c55a9924..a31f1334b 100644 --- a/tools/file_util.py +++ b/tools/file_util.py @@ -4,6 +4,7 @@ from __future__ import absolute_import from glob import iglob +from io import open import os import shutil import sys @@ -13,33 +14,29 @@ import time def read_file(name, normalize=True): """ Read a file. """ try: - f = open(name, 'r') - # read the data - data = f.read() - if normalize: - # normalize line endings - data = data.replace("\r\n", "\n") - return data + with open(name, 'r', encoding='utf-8') as f: + # read the data + data = f.read() + if normalize: + # normalize line endings + data = data.replace("\r\n", "\n") + return data except IOError as e: (errno, strerror) = e.args sys.stderr.write('Failed to read file ' + name + ': ' + strerror) raise - else: - f.close() def write_file(name, data): """ Write a file. """ try: - f = open(name, 'w') - # write the data - f.write(data) + with open(name, 'w', encoding='utf-8') as f: + # write the data + f.write(data) except IOError as e: (errno, strerror) = e.args sys.stderr.write('Failed to write file ' + name + ': ' + strerror) raise - else: - f.close() def path_exists(name): diff --git a/tools/make_cmake.py b/tools/make_cmake.py index 93f139561..5e4d2d22c 100644 --- a/tools/make_cmake.py +++ b/tools/make_cmake.py @@ -2,6 +2,7 @@ # reserved. Use of this source code is governed by a BSD-style license that # can be found in the LICENSE file. +from __future__ import absolute_import import os from file_util import * import sys diff --git a/tools/make_distrib.py b/tools/make_distrib.py index ad7845d37..8dac87272 100644 --- a/tools/make_distrib.py +++ b/tools/make_distrib.py @@ -2,17 +2,20 @@ # reserved. Use of this source code is governed by a BSD-style license that # can be found in the LICENSE file. +from __future__ import absolute_import +from __future__ import print_function from cef_version import VersionFormatter from date_util import * from exec_util import exec_cmd from file_util import * +import git_util as git +from io import open from make_cmake import process_cmake_template from optparse import OptionParser import os import re import shlex import subprocess -import git_util as git import sys import tarfile import zipfile @@ -246,14 +249,22 @@ def eval_transfer_file(cef_dir, script_dir, transfer_cfg, output_dir, quiet): if not path_exists(readme): copy_file( os.path.join(script_dir, 'distrib/README-TRANSFER.txt'), readme) - open(readme, 'ab').write(cfg['source'] + "\n") + + str = cfg['source'] + "\n" + with open(readme, 'a', encoding='utf-8') as fp: + try: + # Python 2 + fp.write(str.decode('utf-8')) + except Exception as e: + # Python 3 + fp.write(str) # perform any required post-processing if 'post-process' in cfg: post = cfg['post-process'] if post == 'normalize_headers': new_path = '' - if cfg.has_key('new_header_path'): + if 'new_header_path' in cfg: new_path = cfg['new_header_path'] normalize_headers(dst, new_path) @@ -345,8 +356,8 @@ def combine_libs(platform, build_dir, libs, dest_lib): """ Combine multiple static libraries into a single static library. """ intermediate_obj = None if platform == 'windows': - cmdline = 'msvs_env.bat win%s python combine_libs.py -o "%s"' % ( - platform_arch, dest_lib) + cmdline = 'msvs_env.bat win%s "%s" combine_libs.py -o "%s"' % ( + platform_arch, sys.executable, dest_lib) elif platform == 'macosx': # Find CEF_EXPORT symbols from libcef_sandbox.a (include/cef_sandbox_mac.h) # Export only symbols that include these strings. @@ -355,7 +366,7 @@ def combine_libs(platform, build_dir, libs, dest_lib): 'Cef', # C++ symbols ] - print 'Finding exported symbols...' + print('Finding exported symbols...') assert 'libcef_sandbox.a' in libs[0], libs[0] symbols = [] for symbol in get_exported_symbols(os.path.join(build_dir, libs[0])): @@ -387,22 +398,22 @@ def combine_libs(platform, build_dir, libs, dest_lib): remove_file(intermediate_obj) # Verify that only the expected symbols are exported from the archive file. - print 'Verifying exported symbols...' + print('Verifying exported symbols...') result_symbols = get_exported_symbols(dest_lib) if set(symbols) != set(result_symbols): - print 'Expected', symbols - print 'Got', result_symbols + print('Expected', symbols) + print('Got', result_symbols) raise Exception('Failure verifying exported symbols') # Verify that no C++ symbols are imported by the archive file. If the # archive imports C++ symbols and the client app links an incompatible C++ # library, the result will be undefined behavior. - print 'Verifying imported (undefined) symbols...' + print('Verifying imported (undefined) symbols...') undefined_symbols = get_undefined_symbols(dest_lib) cpp_symbols = list( filter(lambda symbol: symbol.startswith('__Z'), undefined_symbols)) if cpp_symbols: - print 'Found C++ symbols:', cpp_symbols + print('Found C++ symbols:', cpp_symbols) raise Exception('Failure verifying imported (undefined) symbols') @@ -415,6 +426,10 @@ def run(command_line, working_dir): args, cwd=working_dir, env=os.environ, shell=(sys.platform == 'win32')) +def print_error(msg): + print('Error: %s\nSee --help for usage.' % msg) + + # cannot be loaded as a module if __name__ != "__main__": sys.stderr.write('This file cannot be loaded as a module!') @@ -528,33 +543,31 @@ elif sys.platform.startswith('linux'): # the outputdir option is required if options.outputdir is None: - parser.print_help(sys.stderr) + print_error('--output-dir is required.') sys.exit() if options.minimal and options.client: - print 'Cannot specify both --minimal and --client' - parser.print_help(sys.stderr) + print_error('Cannot specify both --minimal and --client.') sys.exit() if options.x64build + options.armbuild + options.arm64build > 1: - print 'Invalid combination of build options.' - parser.print_help(sys.stderr) + print_error('Invalid combination of build options.') sys.exit() if (options.armbuild or options.arm64build) and platform != 'linux': - print '--arm-build and --arm64-build only supported on Linux.' + print_error('--arm-build and --arm64-build are only supported on Linux.') sys.exit() if options.sandbox and not platform in ('macosx', 'windows'): - print '--sandbox is only supported on macOS and Windows.' + print_error('--sandbox is only supported on macOS and Windows.') sys.exit() if not options.ninjabuild: - print 'Ninja build is required on all platforms' + print_error('--ninja-build is required.') sys.exit() if options.ozone and platform != 'linux': - print '--ozone is only supported on Linux.' + print_error('--ozone is only supported on Linux.') sys.exit() # script directory @@ -712,7 +725,8 @@ if mode == 'standard' or mode == 'minimal': mode, output_dir, options.quiet) # process cmake templates - variables = dict(cef_paths.items() + cef_paths2.items()) + variables = cef_paths.copy() + variables.update(cef_paths2) process_cmake_template(os.path.join(cef_dir, 'CMakeLists.txt.in'), \ os.path.join(output_dir, 'CMakeLists.txt'), \ variables, options.quiet)