blob: 94abaa9437ae262ddd1cf8d0c491b76e9d74d97a [file] [log] [blame]
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -07001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Generate and upload tarballs for default apps cache.
6
7Run inside the 'files' dir containing 'external_extensions.json' file:
8$ chromite/bin/chrome_update_extension_cache --create --upload \\
9 chromeos-default-apps-1.0.0
10
11Always increment the version when you update an existing package.
12If no new files are added, increment the third version number.
13 e.g. 1.0.0 -> 1.0.1
14If you change list of default extensions, increment the second version number.
15 e.g. 1.0.0 -> 1.1.0
16
17Also you need to regenerate the Manifest with the new tarball digest.
18Run inside the chroot:
19$ ebuild chromeos-default-apps-1.0.0.ebuild manifest --force
20"""
21
22import json
23import os
Mike Frysingere852b072021-05-21 12:39:03 -040024import urllib.request
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070025import xml.dom.minidom
26
27from chromite.lib import commandline
28from chromite.lib import cros_build_lib
Ralph Nathan03047282015-03-23 11:09:32 -070029from chromite.lib import cros_logging as logging
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070030from chromite.lib import gs
31from chromite.lib import osutils
Mike Frysingerd0960812020-06-09 01:53:32 -040032from chromite.lib import pformat
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070033
34
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070035UPLOAD_URL_BASE = 'gs://chromeos-localmirror-private/distfiles'
36
37
Don Garrettec5cf902013-09-05 15:49:59 -070038def DownloadCrx(ext, extension, crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070039 """Download .crx file from WebStore and update entry."""
Ralph Nathan03047282015-03-23 11:09:32 -070040 logging.info('Extension "%s"(%s)...', extension['name'], ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070041
Dmitry Polukhin97992a52014-06-17 13:10:56 +040042 update_url = ('%s?x=prodversion%%3D35.1.1.1%%26id%%3D%s%%26uc' %
Mike Frysingere65f3752014-12-08 00:46:39 -050043 (extension['external_update_url'], ext))
Mike Frysinger3dcacee2019-08-23 17:09:11 -040044 response = urllib.request.urlopen(update_url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070045 if response.getcode() != 200:
Ralph Nathan59900422015-03-24 10:41:17 -070046 logging.error('Cannot get update response, URL: %s, error: %d', update_url,
47 response.getcode())
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070048 return False
49
50 dom = xml.dom.minidom.parse(response)
51 status = dom.getElementsByTagName('app')[0].getAttribute('status')
52 if status != 'ok':
Ralph Nathan59900422015-03-24 10:41:17 -070053 logging.error('Cannot fetch extension, status: %s', status)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070054 return False
55
56 node = dom.getElementsByTagName('updatecheck')[0]
Alan Cutterbf4d1662020-10-27 13:32:38 +110057 if node.getAttribute('status') == 'noupdate':
58 logging.info('No CRX available (may have been removed from the webstore).')
59 return True
60
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070061 url = node.getAttribute('codebase')
62 version = node.getAttribute('version')
63 filename = '%s-%s.crx' % (ext, version)
Mike Frysinger3dcacee2019-08-23 17:09:11 -040064 response = urllib.request.urlopen(url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070065 if response.getcode() != 200:
Ralph Nathan59900422015-03-24 10:41:17 -070066 logging.error('Cannot download extension, URL: %s, error: %d', url,
67 response.getcode())
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070068 return False
69
Don Garrettec5cf902013-09-05 15:49:59 -070070 osutils.WriteFile(os.path.join(crxdir, 'extensions', filename),
Alan Cutterbf4d1662020-10-27 13:32:38 +110071 response.read(), mode='wb')
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070072
Dmitry Polukhine9d8fac2013-09-20 13:11:21 -070073 # Keep external_update_url in json file, ExternalCache will take care about
74 # replacing it with proper external_crx path and version.
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070075
Ralph Nathan03047282015-03-23 11:09:32 -070076 logging.info('Downloaded, current version %s', version)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070077 return True
78
79
Don Garrettec5cf902013-09-05 15:49:59 -070080def CreateValidationFiles(validationdir, crxdir, identifier):
khmel@google.com37161cf2019-01-23 09:43:44 -080081 """Create validation files for all extensions in |crxdir|."""
Don Garrettec5cf902013-09-05 15:49:59 -070082
83 verified_files = []
84
85 # Discover all extensions to be validated (but not JSON files).
86 for directory, _, filenames in os.walk(os.path.join(crxdir, 'extensions')):
87
88 # Make directory relative to output dir by removing crxdir and /.
89 for filename in filenames:
Mike Frysingere65f3752014-12-08 00:46:39 -050090 verified_files.append(os.path.join(directory[len(crxdir) + 1:],
Don Garrettec5cf902013-09-05 15:49:59 -070091 filename))
92
93 validation_file = os.path.join(validationdir, '%s.validation' % identifier)
94
95 osutils.SafeMakedirs(validationdir)
Mike Frysinger45602c72019-09-22 02:15:11 -040096 cros_build_lib.run(['sha256sum'] + verified_files,
Mike Frysingerae3e2c72019-12-07 02:35:12 -050097 stdout=validation_file,
Mike Frysinger45602c72019-09-22 02:15:11 -040098 cwd=crxdir, print_cmd=False)
Ralph Nathan03047282015-03-23 11:09:32 -070099 logging.info('Hashes created.')
Don Garrettec5cf902013-09-05 15:49:59 -0700100
101
102def CreateCacheTarball(extensions, outputdir, identifier, tarball):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700103 """Cache |extensions| in |outputdir| and pack them in |tarball|."""
Don Garrettec5cf902013-09-05 15:49:59 -0700104
105 crxdir = os.path.join(outputdir, 'crx')
khmel@google.com37161cf2019-01-23 09:43:44 -0800106 jsondir = os.path.join(outputdir, 'json', 'extensions')
Don Garrettec5cf902013-09-05 15:49:59 -0700107 validationdir = os.path.join(outputdir, 'validation')
108
khmel@google.com37161cf2019-01-23 09:43:44 -0800109 osutils.SafeMakedirs(os.path.join(crxdir, 'extensions'))
110 osutils.SafeMakedirs(jsondir)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700111 was_errors = False
112 for ext in extensions:
khmel@google.com37161cf2019-01-23 09:43:44 -0800113 extension = extensions[ext]
114 # It should not be in use at this moment.
115 if 'managed_users' in extension:
116 cros_build_lib.Die('managed_users is deprecated and not supported. '
117 'Please use user_type.')
118 # In case we work with old type json, use default 'user_type'.
119 # TODO: Update all external_extensions.json files and deprecate this.
120 if 'user_type' not in extension:
121 user_type = ['unmanaged']
122 if extension.get('child_users', 'no') == 'yes':
123 user_type.append('child')
Mike Frysinger968c1142020-05-09 00:37:56 -0400124 logging.warning('user_type filter has to be set explicitly for %s, using '
125 '%s by default.', ext, user_type)
khmel@google.com37161cf2019-01-23 09:43:44 -0800126 extension['user_type'] = user_type
127 else:
128 if 'child_users' in extension:
129 cros_build_lib.Die('child_users is not supported when user_type is '
130 'set.')
131
132 # Verify user type is well-formed.
133 allowed_user_types = {'unmanaged', 'managed', 'child', 'supervised',
134 'guest'}
135 if not extension['user_type']:
136 cros_build_lib.Die('user_type is not set')
137 ext_keys = set(extension['user_type'])
138 unknown_keys = ext_keys - allowed_user_types
139 if unknown_keys:
140 cros_build_lib.Die('user_type %s is not allowed', unknown_keys)
141
142 cache_crx = extension.get('cache_crx', 'yes')
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700143
144 # Remove fields that shouldn't be in the output file.
khmel@google.com37161cf2019-01-23 09:43:44 -0800145 for key in ('cache_crx', 'child_users'):
146 extension.pop(key, None)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700147
148 if cache_crx == 'yes':
khmel@google.com37161cf2019-01-23 09:43:44 -0800149 if not DownloadCrx(ext, extension, crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700150 was_errors = True
151 elif cache_crx == 'no':
152 pass
153 else:
154 cros_build_lib.Die('Unknown value for "cache_crx" %s for %s',
155 cache_crx, ext)
156
khmel@google.com37161cf2019-01-23 09:43:44 -0800157 json_file = os.path.join(jsondir, '%s.json' % ext)
Mike Frysingerd0960812020-06-09 01:53:32 -0400158 pformat.json(extension, fp=json_file)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700159
160 if was_errors:
161 cros_build_lib.Die('FAIL to download some extensions')
162
Don Garrettec5cf902013-09-05 15:49:59 -0700163 CreateValidationFiles(validationdir, crxdir, identifier)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700164 cros_build_lib.CreateTarball(tarball, outputdir)
Ralph Nathan03047282015-03-23 11:09:32 -0700165 logging.info('Tarball created %s', tarball)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700166
167
168def main(argv):
169 parser = commandline.ArgumentParser(
David James9374aac2013-10-08 16:00:17 -0700170 '%%(prog)s [options] <version>\n\n%s' % __doc__, caching=True)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700171 parser.add_argument('version', nargs=1)
172 parser.add_argument('--path', default=None, type='path',
173 help='Path of files dir with external_extensions.json')
174 parser.add_argument('--create', default=False, action='store_true',
175 help='Create cache tarball with specified name')
176 parser.add_argument('--upload', default=False, action='store_true',
177 help='Upload cache tarball with specified name')
178 options = parser.parse_args(argv)
179
180 if options.path:
181 os.chdir(options.path)
182
183 if not (options.create or options.upload):
184 cros_build_lib.Die('Need at least --create or --upload args')
185
186 if not os.path.exists('external_extensions.json'):
187 cros_build_lib.Die('No external_extensions.json in %s. Did you forget the '
188 '--path option?', os.getcwd())
189
Don Garrettec5cf902013-09-05 15:49:59 -0700190 identifier = options.version[0]
191 tarball = '%s.tar.xz' % identifier
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700192 if options.create:
193 extensions = json.load(open('external_extensions.json', 'r'))
194 with osutils.TempDir() as tempdir:
Don Garrettec5cf902013-09-05 15:49:59 -0700195 CreateCacheTarball(extensions, tempdir, identifier,
196 os.path.abspath(tarball))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700197
198 if options.upload:
199 ctx = gs.GSContext()
200 url = os.path.join(UPLOAD_URL_BASE, tarball)
201 if ctx.Exists(url):
202 cros_build_lib.Die('This version already exists on Google Storage (%s)!\n'
203 'NEVER REWRITE EXISTING FILE. IT WILL BREAK CHROME OS '
204 'BUILD!!!', url)
205 ctx.Copy(os.path.abspath(tarball), url, acl='project-private')
Ralph Nathan03047282015-03-23 11:09:32 -0700206 logging.info('Tarball uploaded %s', url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700207 osutils.SafeUnlink(os.path.abspath(tarball))