Commit 12150b43 authored by Marshall Greenblatt's avatar Marshall Greenblatt

Apply yapf formatting to all Python files (issue #2171)

parent 59606b88
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -36,6 +36,7 @@ import sys
# - Some global variables like "$year$" will be replaced in the whole template
# before further parsing occurs.
class cef_html_builder:
""" Class used to build the cefbuilds HTML file. """
......@@ -47,7 +48,7 @@ class cef_html_builder:
def clear(self):
""" Clear the contents of this object. """
self._parts = {}
return;
return
@staticmethod
def _token(key):
......@@ -87,7 +88,9 @@ class cef_html_builder:
top = str[:start_pos]
middle = str[start_pos + len(start_tag):end_pos]
bottom = str[end_pos + len(end_tag):]
return (top + cef_html_builder._token(cef_html_builder._section_key(section)) + bottom, middle)
return (
top + cef_html_builder._token(cef_html_builder._section_key(section)) +
bottom, middle)
def load(self, html_template):
""" Load the specified |html_template| string. """
......@@ -107,32 +110,32 @@ class cef_html_builder:
(version, file) = self._extract(version, 'file')
self._parts = {
'root': root,
'platform_link': platform_link,
'platform': platform,
'version': version,
'file': file
'root': root,
'platform_link': platform_link,
'platform': platform,
'version': version,
'file': file
}
@staticmethod
def _get_platform_name(platform):
return {
'linux32': 'Linux 32-bit',
'linux64': 'Linux 64-bit',
'linuxarm': 'Linux ARM',
'macosx64': 'Mac OS X 64-bit',
'windows32': 'Windows 32-bit',
'windows64': 'Windows 64-bit'
'linux32': 'Linux 32-bit',
'linux64': 'Linux 64-bit',
'linuxarm': 'Linux ARM',
'macosx64': 'Mac OS X 64-bit',
'windows32': 'Windows 32-bit',
'windows64': 'Windows 64-bit'
}[platform]
@staticmethod
def _get_type_name(type):
return {
'standard': 'Standard Distribution',
'minimal': 'Minimal Distribution',
'client': 'Sample Application',
'debug_symbols': 'Debug Symbols',
'release_symbols': 'Release Symbols'
'standard': 'Standard Distribution',
'minimal': 'Minimal Distribution',
'client': 'Sample Application',
'debug_symbols': 'Debug Symbols',
'release_symbols': 'Release Symbols'
}[type]
@staticmethod
......@@ -146,7 +149,7 @@ class cef_html_builder:
size_name = ('B', 'KB', 'MB', 'GB')
i = int(math.floor(math.log(size, 1024)))
p = math.pow(1024, i)
s = round(size/p, 2)
s = round(size / p, 2)
return '%.2f %s' % (s, size_name[i])
@staticmethod
......@@ -175,11 +178,17 @@ class cef_html_builder:
else:
sample_app = 'cefclient'
return {
'standard': 'Standard binary distribution. Includes header files, libcef_dll_wrapper source code, binary files, CMake configuration files and source code for the cefclient and cefsimple sample applications. See the included README.txt file for usage and build requirements.',
'minimal': 'Minimal binary distribution. Includes header files, libcef_dll_wrapper source code, Release build binary files and CMake configuration files. Does not include Debug build binary files or sample application source code. See the included README.txt file for usage and build requirements.',
'client': 'Release build of the ' + sample_app + ' sample application. See the included README.txt file for usage requirements.',
'debug_symbols': 'Debug build symbols. Must be extracted and placed next to the CEF Debug binary file with the same name and version.',
'release_symbols': 'Release build symbols. Must be extracted and placed next to the CEF Release binary file with the same name and version.'
'standard':
'Standard binary distribution. Includes header files, libcef_dll_wrapper source code, binary files, CMake configuration files and source code for the cefclient and cefsimple sample applications. See the included README.txt file for usage and build requirements.',
'minimal':
'Minimal binary distribution. Includes header files, libcef_dll_wrapper source code, Release build binary files and CMake configuration files. Does not include Debug build binary files or sample application source code. See the included README.txt file for usage and build requirements.',
'client':
'Release build of the ' + sample_app +
' sample application. See the included README.txt file for usage requirements.',
'debug_symbols':
'Debug build symbols. Must be extracted and placed next to the CEF Debug binary file with the same name and version.',
'release_symbols':
'Release build symbols. Must be extracted and placed next to the CEF Release binary file with the same name and version.'
}[file['type']]
def generate(self, json_builder):
......@@ -189,8 +198,8 @@ class cef_html_builder:
# Substitution values are augmented at each nesting level.
subs = {
'year': '2016',
'branding': self._branding,
'year': '2016',
'branding': self._branding,
}
# Substitute variables.
......@@ -210,9 +219,12 @@ class cef_html_builder:
for version in json_builder.get_versions(platform):
subs['cef_version'] = version['cef_version']
subs['chromium_version'] = version['chromium_version']
subs['last_modified'] = self._get_date(version['files'][0]['last_modified'])
subs['cef_source_url'] = self._get_cef_source_url(version['cef_version'])
subs['chromium_source_url'] = self._get_chromium_source_url(version['chromium_version'])
subs['last_modified'] = self._get_date(
version['files'][0]['last_modified'])
subs['cef_source_url'] = self._get_cef_source_url(
version['cef_version'])
subs['chromium_source_url'] = self._get_chromium_source_url(
version['chromium_version'])
# Substitute variables.
version_str = self._replace_all(self._parts['version'], subs)
......@@ -225,9 +237,12 @@ class cef_html_builder:
subs['size'] = self._get_file_size(file['size'])
subs['type'] = file['type']
subs['type_name'] = self._get_type_name(file['type'])
subs['file_url'] = self._get_file_url(platform, version['cef_version'], file)
subs['sha1_url'] = self._get_sha1_url(platform, version['cef_version'], file)
subs['tooltip_text'] = self._get_tooltip_text(platform, version['cef_version'], file)
subs['file_url'] = self._get_file_url(platform,
version['cef_version'], file)
subs['sha1_url'] = self._get_sha1_url(platform,
version['cef_version'], file)
subs['tooltip_text'] = self._get_tooltip_text(
platform, version['cef_version'], file)
# Substitute variables.
file_str = self._replace_all(self._parts['file'], subs)
......@@ -236,25 +251,35 @@ class cef_html_builder:
if len(file_strs) > 0:
# Always output file types in the same order.
file_out = ''
type_order = ['standard', 'minimal', 'client', 'debug_symbols', 'release_symbols']
type_order = [
'standard', 'minimal', 'client', 'debug_symbols',
'release_symbols'
]
for type in type_order:
if type in file_strs:
file_out = file_out + file_strs[type]
# Insert files.
version_str = self._replace(version_str, self._section_key('file'), file_out)
version_str = self._replace(version_str,
self._section_key('file'), file_out)
version_strs.append(version_str)
if len(version_strs) > 0:
# Insert versions.
platform_str = self._replace(platform_str, self._section_key('version'), "".join(version_strs))
platform_str = self._replace(platform_str,
self._section_key('version'),
"".join(version_strs))
platform_strs.append(platform_str)
platform_link_strs.append(platform_link_str)
if len(platform_strs) > 0:
# Insert platforms.
root_str = self._replace(root_str, self._section_key('platform_link'), "".join(platform_link_strs))
root_str = self._replace(root_str, self._section_key('platform'), "".join(platform_strs))
root_str = self._replace(root_str,
self._section_key('platform_link'),
"".join(platform_link_strs))
root_str = self._replace(root_str,
self._section_key('platform'),
"".join(platform_strs))
return root_str
......@@ -263,7 +288,8 @@ class cef_html_builder:
if __name__ == '__main__':
# Verify command-line arguments.
if len(sys.argv) < 4:
sys.stderr.write('Usage: %s <json_file_in> <html_file_in> <html_file_out>' % sys.argv[0])
sys.stderr.write(
'Usage: %s <json_file_in> <html_file_in> <html_file_out>' % sys.argv[0])
sys.exit()
json_file_in = sys.argv[1]
......
......@@ -43,19 +43,24 @@ import urllib
# directory listings.
_CEF_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
def parse_date(date):
return datetime.datetime.strptime(date, _CEF_DATE_FORMAT)
def format_date(date):
return date.strftime(_CEF_DATE_FORMAT)
# Helpers to format datetime values on JSON read/write.
def cef_from_json(json_object):
if 'last_modified' in json_object:
json_object['last_modified'] = parse_date(json_object['last_modified'])
return json_object
class cef_json_encoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, datetime.datetime):
return format_date(o)
......@@ -75,7 +80,8 @@ class cef_json_builder:
@staticmethod
def get_platforms():
""" Returns the list of supported platforms. """
return ('linux32', 'linux64', 'linuxarm', 'macosx64', 'windows32', 'windows64')
return ('linux32', 'linux64', 'linuxarm', 'macosx64', 'windows32',
'windows64')
@staticmethod
def get_distrib_types():
......@@ -85,7 +91,8 @@ class cef_json_builder:
@staticmethod
def is_valid_version(version):
""" Returns true if the specified CEF version is fully qualified and valid. """
return bool(re.compile('^3.[0-9]{4,5}.[0-9]{4,5}.g[0-9a-f]{7}$').match(version))
return bool(
re.compile('^3.[0-9]{4,5}.[0-9]{4,5}.g[0-9a-f]{7}$').match(version))
@staticmethod
def is_valid_chromium_version(version):
......@@ -117,8 +124,12 @@ class cef_json_builder:
# Return a string representation of this object.
self._sort_versions()
if self._prettyprint:
return json.dumps(self._data, cls=cef_json_encoder, sort_keys=True,
indent=2, separators=(',', ': '))
return json.dumps(
self._data,
cls=cef_json_encoder,
sort_keys=True,
indent=2,
separators=(',', ': '))
else:
return json.dumps(self._data, cls=cef_json_encoder, sort_keys=True)
......@@ -197,7 +208,7 @@ class cef_json_builder:
self._fatalerrors = fatalerrors
new_data = json.JSONDecoder(object_hook = cef_from_json).decode(json_string)
new_data = json.JSONDecoder(object_hook=cef_from_json).decode(json_string)
# Validate the new data's structure.
for platform in self._data.keys():
......@@ -224,34 +235,41 @@ class cef_json_builder:
not 'size' in file or \
not 'last_modified' in file or \
not 'sha1' in file:
self._print('load: Missing file key(s) for %s %s' % (platform, version['cef_version']))
self._print('load: Missing file key(s) for %s %s' %
(platform, version['cef_version']))
continue
(expected_platform, expected_version, expected_type) = self._parse_name(file['name'])
(expected_platform, expected_version,
expected_type) = self._parse_name(file['name'])
if expected_platform != platform or \
expected_version != version['cef_version'] or \
expected_type != file['type']:
self._print('load: File name/attribute mismatch for %s %s %s' %
(platform, version['cef_version'], file['name']))
(platform, version['cef_version'], file['name']))
continue
self._validate_args(platform, version['cef_version'], file['type'],
file['size'], file['last_modified'], file['sha1'])
if file['type'] in found_types:
self._print('load: Duplicate %s type for %s %s' % (file['type'], platform, version['cef_version']))
self._print('load: Duplicate %s type for %s %s' %
(file['type'], platform, version['cef_version']))
continue
found_types.append(file['type'])
valid_files.append({
'type': file['type'],
'name': file['name'],
'size': file['size'],
'last_modified': file['last_modified'],
'sha1': file['sha1'],
'type': file['type'],
'name': file['name'],
'size': file['size'],
'last_modified': file['last_modified'],
'sha1': file['sha1'],
})
if len(valid_files) > 0:
valid_versions.append({
'cef_version': version['cef_version'],
'chromium_version': self.set_chromium_version(version['cef_version'], version['chromium_version']),
'files': self._sort_files(valid_files)
'cef_version':
version['cef_version'],
'chromium_version':
self.set_chromium_version(version['cef_version'],
version['chromium_version']),
'files':
self._sort_files(valid_files)
})
if len(valid_versions) > 0:
......@@ -280,7 +298,8 @@ class cef_json_builder:
if name_no_ext[-4:] == '.tar':
name_no_ext = name_no_ext[:-4]
name_parts = name_no_ext.split('_')
if len(name_parts) < 4 or name_parts[0] != 'cef' or name_parts[1] != 'binary':
if len(
name_parts) < 4 or name_parts[0] != 'cef' or name_parts[1] != 'binary':
raise Exception('Invalid filename: %s' % name)
# Remove 'cef' and 'binary'.
......@@ -356,7 +375,7 @@ class cef_json_builder:
self._validate_args(platform, version, type, size, last_modified, sha1)
# Find the existing version record.
version_idx = -1;
version_idx = -1
for i in range(0, len(self._data[platform]['versions'])):
if self._data[platform]['versions'][i]['cef_version'] == version:
# Check the version record.
......@@ -368,17 +387,20 @@ class cef_json_builder:
# Add a new version record.
self._print('add_file: Add %s %s' % (platform, version))
self._data[platform]['versions'].append({
'cef_version': version,
'chromium_version': self.get_chromium_version(version),
'files': []
'cef_version': version,
'chromium_version': self.get_chromium_version(version),
'files': []
})
version_idx = len(self._data[platform]['versions']) - 1
# Find the existing file record with matching type.
file_changed = True
for i in range(0, len(self._data[platform]['versions'][version_idx]['files'])):
if self._data[platform]['versions'][version_idx]['files'][i]['type'] == type:
existing_sha1 = self._data[platform]['versions'][version_idx]['files'][i]['sha1']
for i in range(0,
len(self._data[platform]['versions'][version_idx]['files'])):
if self._data[platform]['versions'][version_idx]['files'][i][
'type'] == type:
existing_sha1 = self._data[platform]['versions'][version_idx]['files'][
i]['sha1']
if existing_sha1 != sha1:
# Remove the existing file record.
self._print(' Remove %s %s' % (name, existing_sha1))
......@@ -391,11 +413,11 @@ class cef_json_builder:
# Add a new file record.
self._print(' Add %s %s' % (name, sha1))
self._data[platform]['versions'][version_idx]['files'].append({
'type': type,
'name': name,
'size': size,
'last_modified': last_modified,
'sha1': sha1
'type': type,
'name': name,
'size': size,
'last_modified': last_modified,
'sha1': sha1
})
# Sort file records by last_modified.
......@@ -420,7 +442,7 @@ class cef_json_builder:
if version is None or version_obj['cef_version'].find(version) == 0:
for file_obj in version_obj['files']:
if type is None or type == file_obj['type']:
result_obj = file_obj;
result_obj = file_obj
# Add additional metadata.
result_obj['platform'] = platform
result_obj['cef_version'] = version_obj['cef_version']
......
......@@ -23,23 +23,32 @@ import random
import string
import sys
# Create a fake sha1 checksum value.
def make_fake_sha1():
return ''.join(random.SystemRandom().choice('abcdef' + string.digits) for _ in range(40))
return ''.join(random.SystemRandom().choice('abcdef' + string.digits)
for _ in range(40))
# Create a fake file size value.
def make_fake_size():
return random.randint(30000000, 60000000)
# Create fake file info based on |platform| and |version|.
def make_fake_file_info(platform, version, type):
return {
'name': cef_json_builder.get_file_name(version, platform, type) + '.tar.gz',
'size': make_fake_size(),
'lastModified': datetime.datetime.now(),
'sha1': make_fake_sha1()
'name':
cef_json_builder.get_file_name(version, platform, type) + '.tar.gz',
'size':
make_fake_size(),
'lastModified':
datetime.datetime.now(),
'sha1':
make_fake_sha1()
}
# Returns a list of fake files based on |platform| and |version|.
def create_fake_files(platform, version):
files = []
......@@ -61,7 +70,8 @@ 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 <platform> <cef_version> <chromium_version>' % sys.argv[0])
sys.stderr.write('Usage: %s add <platform> <cef_version> <chromium_version>'
% sys.argv[0])
sys.exit()
# Requested platform.
......@@ -124,7 +134,8 @@ if __name__ == '__main__':
# Add new files to the builder.
changed_files = []
for file in new_files:
if builder.add_file(file['name'], file['size'], file['lastModified'], file['sha1']):
if builder.add_file(file['name'], file['size'], file['lastModified'],
file['sha1']):
changed_files.append(file)
if len(changed_files) > 0:
......
......@@ -6,6 +6,7 @@ from cef_json_builder import cef_json_builder
import datetime
import unittest
class TestCefJSONBuilder(unittest.TestCase):
# Write builder contents to string and then read in.
......@@ -16,33 +17,35 @@ class TestCefJSONBuilder(unittest.TestCase):
self.assertEqual(output, str(builder2))
# Add a file record for testing purposes.
def _add_test_file(self, builder, platform='linux32', version='3.2704.1414.g185cd6c',
type='standard', attrib_idx=0, shouldfail=False):
def _add_test_file(self,
builder,
platform='linux32',
version='3.2704.1414.g185cd6c',
type='standard',
attrib_idx=0,
shouldfail=False):
name = cef_json_builder.get_file_name(version, platform, type) + '.tar.gz'
# Some random attribute information. sha1 must be different to trigger replacement.
attribs = [
{
attribs = [{
'date_str': '2016-05-18T22:42:15.487Z',
'date_val': datetime.datetime(2016, 5, 18, 22, 42, 15, 487000),
'sha1': '2d48ee05ea6385c8fe80879c98c5dd505ad4b100',
'size': 48395610
},
{
}, {
'date_str': '2016-05-14T22:42:15.487Z',
'date_val': datetime.datetime(2016, 5, 14, 22, 42, 15, 487000),
'sha1': '2d48ee05ea6385c8fe80879c98c5dd505ad4b200',
'size': 48395620
}
]
}]
# Populate the Chromium version to avoid queries.
chromium_version = '49.0.2705.50'
self.assertEqual(chromium_version, builder.set_chromium_version(version, chromium_version))
self.assertEqual(chromium_version,
builder.set_chromium_version(version, chromium_version))
self.assertEqual(0, builder.get_query_count())
result = builder.add_file(name,
attribs[attrib_idx]['size'],
result = builder.add_file(name, attribs[attrib_idx]['size'],
attribs[attrib_idx]['date_str'],
attribs[attrib_idx]['sha1'])
# Failure should be expected when adding the same file multiple times with the same sha1.
......@@ -50,14 +53,14 @@ class TestCefJSONBuilder(unittest.TestCase):
# Return the result expected from get_files().
return {
'chromium_version': chromium_version,
'sha1': attribs[attrib_idx]['sha1'],
'name': name,
'platform': platform,
'last_modified': attribs[attrib_idx]['date_val'],
'cef_version': version,
'type': type,
'size': attribs[attrib_idx]['size']
'chromium_version': chromium_version,
'sha1': attribs[attrib_idx]['sha1'],
'name': name,
'platform': platform,
'last_modified': attribs[attrib_idx]['date_val'],
'cef_version': version,
'type': type,
'size': attribs[attrib_idx]['size']
}
# Test with no file contents.
......@@ -105,8 +108,11 @@ class TestCefJSONBuilder(unittest.TestCase):
builder = cef_json_builder()
# Specify all values just in case the defaults change.
expected = self._add_test_file(builder,
platform='linux32', version='3.2704.1414.g185cd6c', type='standard')
expected = self._add_test_file(
builder,
platform='linux32',
version='3.2704.1414.g185cd6c',
type='standard')
# No filter.
files = builder.get_files()
......@@ -140,14 +146,18 @@ class TestCefJSONBuilder(unittest.TestCase):
self.assertEqual(len(files), 0)
# All filters.
files = builder.get_files(platform='linux32', version='3.2704', type='standard')
files = builder.get_files(
platform='linux32', version='3.2704', type='standard')
self.assertEqual(len(files), 1)
self.assertEqual(expected, files[0])
files = builder.get_files(platform='linux32', version='3.2704', type='minimal')
files = builder.get_files(
platform='linux32', version='3.2704', type='minimal')
self.assertEqual(len(files), 0)
files = builder.get_files(platform='linux32', version='3.2623', type='standard')
files = builder.get_files(
platform='linux32', version='3.2623', type='standard')
self.assertEqual(len(files), 0)
files = builder.get_files(platform='linux64', version='3.2704', type='standard')
files = builder.get_files(
platform='linux64', version='3.2704', type='standard')
self.assertEqual(len(files), 0)
# Test add/get of multiple files.
......@@ -162,7 +172,9 @@ class TestCefJSONBuilder(unittest.TestCase):
for platform in platforms:
for version in versions:
for type in types:
expected.append(self._add_test_file(builder, platform=platform, type=type, version=version))
expected.append(
self._add_test_file(
builder, platform=platform, type=type, version=version))
self._verify_write_read(builder)
......@@ -187,7 +199,8 @@ class TestCefJSONBuilder(unittest.TestCase):
for platform in platforms:
for version in versions:
for type in types:
files = builder.get_files(platform=platform, type=type, version=version)
files = builder.get_files(
platform=platform, type=type, version=version)
self.assertEqual(len(files), 1)
self.assertEqual(expected[idx], files[0])
idx = idx + 1
......@@ -203,7 +216,8 @@ class TestCefJSONBuilder(unittest.TestCase):
# Initial file versions.
for platform in platforms:
for type in types:
self._add_test_file(builder, platform=platform, type=type, version=version)
self._add_test_file(
builder, platform=platform, type=type, version=version)
# No filter.
files = builder.get_files()
......@@ -214,8 +228,13 @@ class TestCefJSONBuilder(unittest.TestCase):
# Replace all file versions (due to new sha1).
for platform in platforms:
for type in types:
expected.append(self._add_test_file(builder,
platform=platform, type=type, version=version, attrib_idx=1))
expected.append(
self._add_test_file(
builder,
platform=platform,
type=type,
version=version,
attrib_idx=1))
# No filter.
files = builder.get_files()
......@@ -241,7 +260,8 @@ class TestCefJSONBuilder(unittest.TestCase):
# Initial file versions.
for platform in platforms:
for type in types:
self._add_test_file(builder, platform=platform, type=type, version=version)
self._add_test_file(
builder, platform=platform, type=type, version=version)
# No filter.
files = builder.get_files()
......@@ -252,8 +272,13 @@ class TestCefJSONBuilder(unittest.TestCase):
# Replace no file versions (due to same sha1).
for platform in platforms: