Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Generate and upload tarballs for default apps cache. |
| 7 | |
| 8 | Run inside the 'files' dir containing 'external_extensions.json' file: |
| 9 | $ chromite/bin/chrome_update_extension_cache --create --upload \\ |
| 10 | chromeos-default-apps-1.0.0 |
| 11 | |
| 12 | Always increment the version when you update an existing package. |
| 13 | If no new files are added, increment the third version number. |
| 14 | e.g. 1.0.0 -> 1.0.1 |
| 15 | If you change list of default extensions, increment the second version number. |
| 16 | e.g. 1.0.0 -> 1.1.0 |
| 17 | |
| 18 | Also you need to regenerate the Manifest with the new tarball digest. |
| 19 | Run inside the chroot: |
| 20 | $ ebuild chromeos-default-apps-1.0.0.ebuild manifest --force |
| 21 | """ |
| 22 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 23 | from __future__ import print_function |
| 24 | |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 25 | import json |
| 26 | import os |
Mike Frysinger | 03b983f | 2020-02-21 02:31:49 -0500 | [diff] [blame] | 27 | import sys |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 28 | import xml.dom.minidom |
| 29 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 30 | from six.moves import urllib |
| 31 | |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 32 | from chromite.lib import commandline |
| 33 | from chromite.lib import cros_build_lib |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 34 | from chromite.lib import cros_logging as logging |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 35 | from chromite.lib import gs |
| 36 | from chromite.lib import osutils |
Mike Frysinger | d096081 | 2020-06-09 01:53:32 -0400 | [diff] [blame^] | 37 | from chromite.lib import pformat |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 38 | |
| 39 | |
Mike Frysinger | 03b983f | 2020-02-21 02:31:49 -0500 | [diff] [blame] | 40 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 41 | |
| 42 | |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 43 | UPLOAD_URL_BASE = 'gs://chromeos-localmirror-private/distfiles' |
| 44 | |
| 45 | |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 46 | def DownloadCrx(ext, extension, crxdir): |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 47 | """Download .crx file from WebStore and update entry.""" |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 48 | logging.info('Extension "%s"(%s)...', extension['name'], ext) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 49 | |
Dmitry Polukhin | 97992a5 | 2014-06-17 13:10:56 +0400 | [diff] [blame] | 50 | update_url = ('%s?x=prodversion%%3D35.1.1.1%%26id%%3D%s%%26uc' % |
Mike Frysinger | e65f375 | 2014-12-08 00:46:39 -0500 | [diff] [blame] | 51 | (extension['external_update_url'], ext)) |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 52 | response = urllib.request.urlopen(update_url) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 53 | if response.getcode() != 200: |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 54 | logging.error('Cannot get update response, URL: %s, error: %d', update_url, |
| 55 | response.getcode()) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 56 | return False |
| 57 | |
| 58 | dom = xml.dom.minidom.parse(response) |
| 59 | status = dom.getElementsByTagName('app')[0].getAttribute('status') |
| 60 | if status != 'ok': |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 61 | logging.error('Cannot fetch extension, status: %s', status) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 62 | return False |
| 63 | |
| 64 | node = dom.getElementsByTagName('updatecheck')[0] |
| 65 | url = node.getAttribute('codebase') |
| 66 | version = node.getAttribute('version') |
| 67 | filename = '%s-%s.crx' % (ext, version) |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 68 | response = urllib.request.urlopen(url) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 69 | if response.getcode() != 200: |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 70 | logging.error('Cannot download extension, URL: %s, error: %d', url, |
| 71 | response.getcode()) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 72 | return False |
| 73 | |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 74 | osutils.WriteFile(os.path.join(crxdir, 'extensions', filename), |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 75 | response.read()) |
| 76 | |
Dmitry Polukhin | e9d8fac | 2013-09-20 13:11:21 -0700 | [diff] [blame] | 77 | # Keep external_update_url in json file, ExternalCache will take care about |
| 78 | # replacing it with proper external_crx path and version. |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 79 | |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 80 | logging.info('Downloaded, current version %s', version) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 81 | return True |
| 82 | |
| 83 | |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 84 | def CreateValidationFiles(validationdir, crxdir, identifier): |
khmel@google.com | 37161cf | 2019-01-23 09:43:44 -0800 | [diff] [blame] | 85 | """Create validation files for all extensions in |crxdir|.""" |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 86 | |
| 87 | verified_files = [] |
| 88 | |
| 89 | # Discover all extensions to be validated (but not JSON files). |
| 90 | for directory, _, filenames in os.walk(os.path.join(crxdir, 'extensions')): |
| 91 | |
| 92 | # Make directory relative to output dir by removing crxdir and /. |
| 93 | for filename in filenames: |
Mike Frysinger | e65f375 | 2014-12-08 00:46:39 -0500 | [diff] [blame] | 94 | verified_files.append(os.path.join(directory[len(crxdir) + 1:], |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 95 | filename)) |
| 96 | |
| 97 | validation_file = os.path.join(validationdir, '%s.validation' % identifier) |
| 98 | |
| 99 | osutils.SafeMakedirs(validationdir) |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 100 | cros_build_lib.run(['sha256sum'] + verified_files, |
Mike Frysinger | ae3e2c7 | 2019-12-07 02:35:12 -0500 | [diff] [blame] | 101 | stdout=validation_file, |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 102 | cwd=crxdir, print_cmd=False) |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 103 | logging.info('Hashes created.') |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def CreateCacheTarball(extensions, outputdir, identifier, tarball): |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 107 | """Cache |extensions| in |outputdir| and pack them in |tarball|.""" |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 108 | |
| 109 | crxdir = os.path.join(outputdir, 'crx') |
khmel@google.com | 37161cf | 2019-01-23 09:43:44 -0800 | [diff] [blame] | 110 | jsondir = os.path.join(outputdir, 'json', 'extensions') |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 111 | validationdir = os.path.join(outputdir, 'validation') |
| 112 | |
khmel@google.com | 37161cf | 2019-01-23 09:43:44 -0800 | [diff] [blame] | 113 | osutils.SafeMakedirs(os.path.join(crxdir, 'extensions')) |
| 114 | osutils.SafeMakedirs(jsondir) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 115 | was_errors = False |
| 116 | for ext in extensions: |
khmel@google.com | 37161cf | 2019-01-23 09:43:44 -0800 | [diff] [blame] | 117 | extension = extensions[ext] |
| 118 | # It should not be in use at this moment. |
| 119 | if 'managed_users' in extension: |
| 120 | cros_build_lib.Die('managed_users is deprecated and not supported. ' |
| 121 | 'Please use user_type.') |
| 122 | # In case we work with old type json, use default 'user_type'. |
| 123 | # TODO: Update all external_extensions.json files and deprecate this. |
| 124 | if 'user_type' not in extension: |
| 125 | user_type = ['unmanaged'] |
| 126 | if extension.get('child_users', 'no') == 'yes': |
| 127 | user_type.append('child') |
Mike Frysinger | 968c114 | 2020-05-09 00:37:56 -0400 | [diff] [blame] | 128 | logging.warning('user_type filter has to be set explicitly for %s, using ' |
| 129 | '%s by default.', ext, user_type) |
khmel@google.com | 37161cf | 2019-01-23 09:43:44 -0800 | [diff] [blame] | 130 | extension['user_type'] = user_type |
| 131 | else: |
| 132 | if 'child_users' in extension: |
| 133 | cros_build_lib.Die('child_users is not supported when user_type is ' |
| 134 | 'set.') |
| 135 | |
| 136 | # Verify user type is well-formed. |
| 137 | allowed_user_types = {'unmanaged', 'managed', 'child', 'supervised', |
| 138 | 'guest'} |
| 139 | if not extension['user_type']: |
| 140 | cros_build_lib.Die('user_type is not set') |
| 141 | ext_keys = set(extension['user_type']) |
| 142 | unknown_keys = ext_keys - allowed_user_types |
| 143 | if unknown_keys: |
| 144 | cros_build_lib.Die('user_type %s is not allowed', unknown_keys) |
| 145 | |
| 146 | cache_crx = extension.get('cache_crx', 'yes') |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 147 | |
| 148 | # Remove fields that shouldn't be in the output file. |
khmel@google.com | 37161cf | 2019-01-23 09:43:44 -0800 | [diff] [blame] | 149 | for key in ('cache_crx', 'child_users'): |
| 150 | extension.pop(key, None) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 151 | |
| 152 | if cache_crx == 'yes': |
khmel@google.com | 37161cf | 2019-01-23 09:43:44 -0800 | [diff] [blame] | 153 | if not DownloadCrx(ext, extension, crxdir): |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 154 | was_errors = True |
| 155 | elif cache_crx == 'no': |
| 156 | pass |
| 157 | else: |
| 158 | cros_build_lib.Die('Unknown value for "cache_crx" %s for %s', |
| 159 | cache_crx, ext) |
| 160 | |
khmel@google.com | 37161cf | 2019-01-23 09:43:44 -0800 | [diff] [blame] | 161 | json_file = os.path.join(jsondir, '%s.json' % ext) |
Mike Frysinger | d096081 | 2020-06-09 01:53:32 -0400 | [diff] [blame^] | 162 | pformat.json(extension, fp=json_file) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 163 | |
| 164 | if was_errors: |
| 165 | cros_build_lib.Die('FAIL to download some extensions') |
| 166 | |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 167 | CreateValidationFiles(validationdir, crxdir, identifier) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 168 | cros_build_lib.CreateTarball(tarball, outputdir) |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 169 | logging.info('Tarball created %s', tarball) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 170 | |
| 171 | |
| 172 | def main(argv): |
| 173 | parser = commandline.ArgumentParser( |
David James | 9374aac | 2013-10-08 16:00:17 -0700 | [diff] [blame] | 174 | '%%(prog)s [options] <version>\n\n%s' % __doc__, caching=True) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 175 | parser.add_argument('version', nargs=1) |
| 176 | parser.add_argument('--path', default=None, type='path', |
| 177 | help='Path of files dir with external_extensions.json') |
| 178 | parser.add_argument('--create', default=False, action='store_true', |
| 179 | help='Create cache tarball with specified name') |
| 180 | parser.add_argument('--upload', default=False, action='store_true', |
| 181 | help='Upload cache tarball with specified name') |
| 182 | options = parser.parse_args(argv) |
| 183 | |
| 184 | if options.path: |
| 185 | os.chdir(options.path) |
| 186 | |
| 187 | if not (options.create or options.upload): |
| 188 | cros_build_lib.Die('Need at least --create or --upload args') |
| 189 | |
| 190 | if not os.path.exists('external_extensions.json'): |
| 191 | cros_build_lib.Die('No external_extensions.json in %s. Did you forget the ' |
| 192 | '--path option?', os.getcwd()) |
| 193 | |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 194 | identifier = options.version[0] |
| 195 | tarball = '%s.tar.xz' % identifier |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 196 | if options.create: |
| 197 | extensions = json.load(open('external_extensions.json', 'r')) |
| 198 | with osutils.TempDir() as tempdir: |
Don Garrett | ec5cf90 | 2013-09-05 15:49:59 -0700 | [diff] [blame] | 199 | CreateCacheTarball(extensions, tempdir, identifier, |
| 200 | os.path.abspath(tarball)) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 201 | |
| 202 | if options.upload: |
| 203 | ctx = gs.GSContext() |
| 204 | url = os.path.join(UPLOAD_URL_BASE, tarball) |
| 205 | if ctx.Exists(url): |
| 206 | cros_build_lib.Die('This version already exists on Google Storage (%s)!\n' |
| 207 | 'NEVER REWRITE EXISTING FILE. IT WILL BREAK CHROME OS ' |
| 208 | 'BUILD!!!', url) |
| 209 | ctx.Copy(os.path.abspath(tarball), url, acl='project-private') |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 210 | logging.info('Tarball uploaded %s', url) |
Dmitry Polukhin | cbdd21c | 2013-08-13 10:42:04 -0700 | [diff] [blame] | 211 | osutils.SafeUnlink(os.path.abspath(tarball)) |