From 11ee7e984cdc99b3d41945bbeac2e5afe505f1d4 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Thu, 9 Jan 2020 15:40:13 +0200 Subject: [PATCH] cefbuilds: Add Python 3 support (see issue #2856) --- tools/cefbuilds/cef_html_builder.py | 14 +++++++------ tools/cefbuilds/cef_json_builder.py | 22 ++++++++++++++------- tools/cefbuilds/cef_json_builder_example.py | 19 ++++++++++-------- tools/cefbuilds/cef_json_builder_test.py | 1 + 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/tools/cefbuilds/cef_html_builder.py b/tools/cefbuilds/cef_html_builder.py index 92a8912b8..f8fbc2af4 100644 --- a/tools/cefbuilds/cef_html_builder.py +++ b/tools/cefbuilds/cef_html_builder.py @@ -2,6 +2,8 @@ # 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_json_builder import cef_json_builder import datetime import math @@ -72,7 +74,7 @@ class cef_html_builder: @staticmethod def _replace_all(str, dict): - for key, value in dict.iteritems(): + for (key, value) in dict.items(): str = cef_html_builder._replace(str, key, value) return str @@ -295,8 +297,8 @@ class cef_html_builder: if __name__ == '__main__': # Verify command-line arguments. if len(sys.argv) < 4: - sys.stderr.write( - 'Usage: %s ' % sys.argv[0]) + sys.stderr.write('Usage: %s \n' + % sys.argv[0]) sys.exit() json_file_in = sys.argv[1] @@ -304,18 +306,18 @@ if __name__ == '__main__': html_file_out = sys.argv[3] # Create the HTML builder and load the HTML template. - print '--> Reading %s' % html_file_in + print('--> Reading %s' % html_file_in) html_builder = cef_html_builder() with open(html_file_in, 'r') as f: html_builder.load(f.read()) # Create the JSON builder and load the JSON file. - print '--> Reading %s' % json_file_in + print('--> Reading %s' % json_file_in) json_builder = cef_json_builder(silent=False) with open(json_file_in, 'r') as f: json_builder.load(f.read()) # Write the HTML output file. - print '--> Writing %s' % html_file_out + print('--> Writing %s' % html_file_out) with open(html_file_out, 'w') as f: f.write(html_builder.generate(json_builder)) diff --git a/tools/cefbuilds/cef_json_builder.py b/tools/cefbuilds/cef_json_builder.py index 31d3f259a..ecd835a87 100644 --- a/tools/cefbuilds/cef_json_builder.py +++ b/tools/cefbuilds/cef_json_builder.py @@ -2,11 +2,19 @@ # 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 import datetime import json import os import re -import urllib + +try: + # Python 2 + from urllib2 import urlopen +except Exception as e: + # Python 3 + from urllib.request import urlopen # Class used to build the cefbuilds JSON file. See cef_json_builder_example.py # for example usage. See cef_json_builder_test.py for unit tests. @@ -152,7 +160,7 @@ class cef_json_builder: if self._fatalerrors: raise Exception(msg) if not self._silent: - print msg + print(msg) def get_query_count(self): """ Returns the number of queries sent while building. """ @@ -166,11 +174,11 @@ class cef_json_builder: query_url = 'https://bitbucket.org/chromiumembedded/cef/raw/%s/CHROMIUM_BUILD_COMPATIBILITY.txt' % git_hash self._queryct = self._queryct + 1 if not self._silent: - print 'Reading %s' % query_url + print('Reading %s' % query_url) try: # Read the remote URL contents. - handle = urllib.urlopen(query_url) + handle = urlopen(query_url) compat_value = handle.read().strip() handle.close() @@ -182,8 +190,8 @@ class cef_json_builder: val = config['chromium_checkout'] if val.find('refs/tags/') == 0: chromium_version = val[10:] - except Exception, e: - print 'Failed to read Chromium version information' + except Exception as e: + print('Failed to read Chromium version information') raise return chromium_version @@ -392,7 +400,7 @@ class cef_json_builder: # Parse the file name. (platform, version, type) = self._parse_name(name) - if not isinstance(size, (int, long)): + if not isinstance(size, int): size = int(size) if not isinstance(last_modified, datetime.datetime): last_modified = parse_date(last_modified) diff --git a/tools/cefbuilds/cef_json_builder_example.py b/tools/cefbuilds/cef_json_builder_example.py index d568da4b5..1403edb7c 100644 --- a/tools/cefbuilds/cef_json_builder_example.py +++ b/tools/cefbuilds/cef_json_builder_example.py @@ -16,6 +16,8 @@ # After creating an index.json file you can use the cef_html_builder.py tool to # create an HTML file. +from __future__ import absolute_import +from __future__ import print_function from cef_json_builder import cef_json_builder import datetime import os @@ -70,8 +72,9 @@ def create_fake_files(platform, version): if __name__ == '__main__': # Verify command-line arguments. if len(sys.argv) < 5 or sys.argv[1] != 'add': - sys.stderr.write('Usage: %s add ' - % sys.argv[0]) + sys.stderr.write( + 'Usage: %s add \n' % + sys.argv[0]) sys.exit() # Requested platform. @@ -80,19 +83,19 @@ if __name__ == '__main__': elif sys.argv[2] in cef_json_builder.get_platforms(): platforms = [sys.argv[2]] else: - sys.stderr.write('Invalid platform: %s' % platform) + sys.stderr.write('Invalid platform: %s\n' % platform) sys.exit() # Requested CEF version. cef_version = sys.argv[3] if not cef_json_builder.is_valid_version(cef_version): - sys.stderr.write('Invalid CEF version: %s' % cef_version) + sys.stderr.write('Invalid CEF version: %s\n' % cef_version) sys.exit() # Requested Chromium version. chromium_version = sys.argv[4] if not cef_json_builder.is_valid_chromium_version(chromium_version): - sys.stderr.write('Invalid Chromium version: %s' % chromium_version) + sys.stderr.write('Invalid Chromium version: %s\n' % chromium_version) sys.exit() # Write the JSON file in the same directory as this file. @@ -109,7 +112,7 @@ if __name__ == '__main__': # Load the existing JSON file, if any. Ignore format errors for example # purposes. if os.path.exists(json_file): - print '--> Reading index.json' + print('--> Reading index.json') with open(json_file, 'r') as f: builder.load(f.read(), fatalerrors=False) @@ -140,10 +143,10 @@ if __name__ == '__main__': if len(changed_files) > 0: # Write the updated JSON file. - print '--> Writing index.json' + print('--> Writing index.json') with open(json_file, 'w') as f: f.write(str(builder)) # A real implementation would now upload the changed files. for file in changed_files: - print '--> Upload file %s' % file['name'] + print('--> Upload file %s' % file['name']) diff --git a/tools/cefbuilds/cef_json_builder_test.py b/tools/cefbuilds/cef_json_builder_test.py index 87a9b2cfb..44eb75638 100644 --- a/tools/cefbuilds/cef_json_builder_test.py +++ b/tools/cefbuilds/cef_json_builder_test.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 from cef_json_builder import cef_json_builder import datetime import unittest