1# Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights 2# reserved. Use of this source code is governed by a BSD-style license that 3# can be found in the LICENSE file. 4 5# This example utility uses the cef_json_builder class to create and update an 6# index.json file in the same directory as this file. Example usage: 7# 8# Add files for macosx64 platform at the specified version: 9# > python cef_json_builder_example.py add macosx64 3.2704.1416.g185cd6c 51.0.2704.47 10# 11# Add files for all platforms at the specified version: 12# > python cef_json_builder_example.py add all 3.2704.1416.g185cd6c 51.0.2704.47 13# 14# See cef_json_builder.get_platforms() for the list of supported platforms. 15 16from __future__ import absolute_import 17from __future__ import print_function 18from cef_json_builder import cef_json_builder 19import datetime 20import os 21import random 22import string 23import sys 24 25 26# Create a fake sha1 checksum value. 27def make_fake_sha1(): 28 return ''.join(random.SystemRandom().choice('abcdef' + string.digits) 29 for _ in range(40)) 30 31 32# Create a fake file size value. 33def make_fake_size(): 34 return random.randint(30000000, 60000000) 35 36 37# Create fake file info based on |platform| and |version|. 38def make_fake_file_info(platform, version, type): 39 return { 40 'name': 41 cef_json_builder.get_file_name(version, platform, type) + '.tar.gz', 42 'size': 43 make_fake_size(), 44 'lastModified': 45 datetime.datetime.now(), 46 'sha1': 47 make_fake_sha1() 48 } 49 50 51# Returns a list of fake files based on |platform| and |version|. 52def create_fake_files(platform, version): 53 files = [] 54 55 # All platforms create standard, minimal and client distributions. 56 files.append(make_fake_file_info(platform, version, 'standard')) 57 files.append(make_fake_file_info(platform, version, 'minimal')) 58 files.append(make_fake_file_info(platform, version, 'client')) 59 60 # Windows and OS X platforms create debug and release symbols. 61 if platform.find('windows') == 0 or platform.find('macosx') == 0: 62 files.append(make_fake_file_info(platform, version, 'debug_symbols')) 63 files.append(make_fake_file_info(platform, version, 'release_symbols')) 64 65 return files 66 67 68# Program entry point. 69if __name__ == '__main__': 70 # Verify command-line arguments. 71 if len(sys.argv) < 5 or sys.argv[1] != 'add': 72 sys.stderr.write( 73 'Usage: %s add <platform> <cef_version> <chromium_version>\n' % 74 sys.argv[0]) 75 sys.exit() 76 77 # Requested platform. 78 if sys.argv[2] == 'all': 79 platforms = cef_json_builder.get_platforms() 80 elif sys.argv[2] in cef_json_builder.get_platforms(): 81 platforms = [sys.argv[2]] 82 else: 83 sys.stderr.write('Invalid platform: %s\n' % platform) 84 sys.exit() 85 86 # Requested CEF version. 87 cef_version = sys.argv[3] 88 if not cef_json_builder.is_valid_version(cef_version): 89 sys.stderr.write('Invalid CEF version: %s\n' % cef_version) 90 sys.exit() 91 92 # Requested Chromium version. 93 chromium_version = sys.argv[4] 94 if not cef_json_builder.is_valid_chromium_version(chromium_version): 95 sys.stderr.write('Invalid Chromium version: %s\n' % chromium_version) 96 sys.exit() 97 98 # Write the JSON file in the same directory as this file. 99 current_dir = os.path.dirname(__file__) 100 json_file = os.path.join(current_dir, 'index.json') 101 102 # Create the builder object. Enable pretty printing and extra output for 103 # example purposes. 104 builder = cef_json_builder(prettyprint=True, silent=False) 105 106 # Map the CEF version to the Chromium version to avoid a remote query. 107 builder.set_chromium_version(cef_version, chromium_version) 108 109 # Load the existing JSON file, if any. Ignore format errors for example 110 # purposes. 111 if os.path.exists(json_file): 112 print('--> Reading index.json') 113 with open(json_file, 'r') as f: 114 builder.load(f.read(), fatalerrors=False) 115 116 # Create fake file info based on |platform| and |version|. A real 117 # implementation should retrieve the list of files from an external source 118 # like a Web or filesystem directory listing. If using Artifactory, for 119 # example, then "size", "lastModified" and "sha1" attributes would be included 120 # in the directory listing metadata. 121 # For this example we: 122 # - Always use now() as the last modified date. Consequently newly added files 123 # will always be listed at the top of the JSON platform versions list. 124 # - Always create a new (fake) sha1 checksum value for each file. Consequently 125 # duplicate calls for the same |platform| + |version| will always replace 126 # the existing entries. In real implementations the sha1 may be the same 127 # across calls if the file contents have not changed, in which case 128 # cef_json_builder.add_file() will return False and upload of the file 129 # should be skipped. 130 new_files = [] 131 for platform in platforms: 132 new_files.extend(create_fake_files(platform, cef_version)) 133 134 # Add new files to the builder. 135 changed_files = [] 136 for file in new_files: 137 if builder.add_file(file['name'], file['size'], file['lastModified'], 138 file['sha1']): 139 changed_files.append(file) 140 141 if len(changed_files) > 0: 142 # Write the updated JSON file. 143 print('--> Writing index.json') 144 with open(json_file, 'w') as f: 145 f.write(str(builder)) 146 147 # A real implementation would now upload the changed files. 148 for file in changed_files: 149 print('--> Upload file %s' % file['name']) 150