blob: 1891d728c81c5a517a0da84d8d06e7fb525b3cae [file] [log] [blame]
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -07001# -*- 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
8Run 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
12Always increment the version when you update an existing package.
13If no new files are added, increment the third version number.
14 e.g. 1.0.0 -> 1.0.1
15If you change list of default extensions, increment the second version number.
16 e.g. 1.0.0 -> 1.1.0
17
18Also you need to regenerate the Manifest with the new tarball digest.
19Run inside the chroot:
20$ ebuild chromeos-default-apps-1.0.0.ebuild manifest --force
21"""
22
Mike Frysinger383367e2014-09-16 15:06:17 -040023from __future__ import print_function
24
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070025import json
26import os
Mike Frysinger03b983f2020-02-21 02:31:49 -050027import sys
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070028import xml.dom.minidom
29
Mike Frysinger3dcacee2019-08-23 17:09:11 -040030from six.moves import urllib
31
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070032from chromite.lib import commandline
33from chromite.lib import cros_build_lib
Ralph Nathan03047282015-03-23 11:09:32 -070034from chromite.lib import cros_logging as logging
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070035from chromite.lib import gs
36from chromite.lib import osutils
Mike Frysingerd0960812020-06-09 01:53:32 -040037from chromite.lib import pformat
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070038
39
Mike Frysinger03b983f2020-02-21 02:31:49 -050040assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
41
42
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070043UPLOAD_URL_BASE = 'gs://chromeos-localmirror-private/distfiles'
44
45
Don Garrettec5cf902013-09-05 15:49:59 -070046def DownloadCrx(ext, extension, crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070047 """Download .crx file from WebStore and update entry."""
Ralph Nathan03047282015-03-23 11:09:32 -070048 logging.info('Extension "%s"(%s)...', extension['name'], ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070049
Dmitry Polukhin97992a52014-06-17 13:10:56 +040050 update_url = ('%s?x=prodversion%%3D35.1.1.1%%26id%%3D%s%%26uc' %
Mike Frysingere65f3752014-12-08 00:46:39 -050051 (extension['external_update_url'], ext))
Mike Frysinger3dcacee2019-08-23 17:09:11 -040052 response = urllib.request.urlopen(update_url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070053 if response.getcode() != 200:
Ralph Nathan59900422015-03-24 10:41:17 -070054 logging.error('Cannot get update response, URL: %s, error: %d', update_url,
55 response.getcode())
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070056 return False
57
58 dom = xml.dom.minidom.parse(response)
59 status = dom.getElementsByTagName('app')[0].getAttribute('status')
60 if status != 'ok':
Ralph Nathan59900422015-03-24 10:41:17 -070061 logging.error('Cannot fetch extension, status: %s', status)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070062 return False
63
64 node = dom.getElementsByTagName('updatecheck')[0]
Alan Cutterbf4d1662020-10-27 13:32:38 +110065 if node.getAttribute('status') == 'noupdate':
66 logging.info('No CRX available (may have been removed from the webstore).')
67 return True
68
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070069 url = node.getAttribute('codebase')
70 version = node.getAttribute('version')
71 filename = '%s-%s.crx' % (ext, version)
Mike Frysinger3dcacee2019-08-23 17:09:11 -040072 response = urllib.request.urlopen(url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070073 if response.getcode() != 200:
Ralph Nathan59900422015-03-24 10:41:17 -070074 logging.error('Cannot download extension, URL: %s, error: %d', url,
75 response.getcode())
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070076 return False
77
Don Garrettec5cf902013-09-05 15:49:59 -070078 osutils.WriteFile(os.path.join(crxdir, 'extensions', filename),
Alan Cutterbf4d1662020-10-27 13:32:38 +110079 response.read(), mode='wb')
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070080
Dmitry Polukhine9d8fac2013-09-20 13:11:21 -070081 # Keep external_update_url in json file, ExternalCache will take care about
82 # replacing it with proper external_crx path and version.
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070083
Ralph Nathan03047282015-03-23 11:09:32 -070084 logging.info('Downloaded, current version %s', version)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070085 return True
86
87
Don Garrettec5cf902013-09-05 15:49:59 -070088def CreateValidationFiles(validationdir, crxdir, identifier):
khmel@google.com37161cf2019-01-23 09:43:44 -080089 """Create validation files for all extensions in |crxdir|."""
Don Garrettec5cf902013-09-05 15:49:59 -070090
91 verified_files = []
92
93 # Discover all extensions to be validated (but not JSON files).
94 for directory, _, filenames in os.walk(os.path.join(crxdir, 'extensions')):
95
96 # Make directory relative to output dir by removing crxdir and /.
97 for filename in filenames:
Mike Frysingere65f3752014-12-08 00:46:39 -050098 verified_files.append(os.path.join(directory[len(crxdir) + 1:],
Don Garrettec5cf902013-09-05 15:49:59 -070099 filename))
100
101 validation_file = os.path.join(validationdir, '%s.validation' % identifier)
102
103 osutils.SafeMakedirs(validationdir)
Mike Frysinger45602c72019-09-22 02:15:11 -0400104 cros_build_lib.run(['sha256sum'] + verified_files,
Mike Frysingerae3e2c72019-12-07 02:35:12 -0500105 stdout=validation_file,
Mike Frysinger45602c72019-09-22 02:15:11 -0400106 cwd=crxdir, print_cmd=False)
Ralph Nathan03047282015-03-23 11:09:32 -0700107 logging.info('Hashes created.')
Don Garrettec5cf902013-09-05 15:49:59 -0700108
109
110def CreateCacheTarball(extensions, outputdir, identifier, tarball):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700111 """Cache |extensions| in |outputdir| and pack them in |tarball|."""
Don Garrettec5cf902013-09-05 15:49:59 -0700112
113 crxdir = os.path.join(outputdir, 'crx')
khmel@google.com37161cf2019-01-23 09:43:44 -0800114 jsondir = os.path.join(outputdir, 'json', 'extensions')
Don Garrettec5cf902013-09-05 15:49:59 -0700115 validationdir = os.path.join(outputdir, 'validation')
116
khmel@google.com37161cf2019-01-23 09:43:44 -0800117 osutils.SafeMakedirs(os.path.join(crxdir, 'extensions'))
118 osutils.SafeMakedirs(jsondir)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700119 was_errors = False
120 for ext in extensions:
khmel@google.com37161cf2019-01-23 09:43:44 -0800121 extension = extensions[ext]
122 # It should not be in use at this moment.
123 if 'managed_users' in extension:
124 cros_build_lib.Die('managed_users is deprecated and not supported. '
125 'Please use user_type.')
126 # In case we work with old type json, use default 'user_type'.
127 # TODO: Update all external_extensions.json files and deprecate this.
128 if 'user_type' not in extension:
129 user_type = ['unmanaged']
130 if extension.get('child_users', 'no') == 'yes':
131 user_type.append('child')
Mike Frysinger968c1142020-05-09 00:37:56 -0400132 logging.warning('user_type filter has to be set explicitly for %s, using '
133 '%s by default.', ext, user_type)
khmel@google.com37161cf2019-01-23 09:43:44 -0800134 extension['user_type'] = user_type
135 else:
136 if 'child_users' in extension:
137 cros_build_lib.Die('child_users is not supported when user_type is '
138 'set.')
139
140 # Verify user type is well-formed.
141 allowed_user_types = {'unmanaged', 'managed', 'child', 'supervised',
142 'guest'}
143 if not extension['user_type']:
144 cros_build_lib.Die('user_type is not set')
145 ext_keys = set(extension['user_type'])
146 unknown_keys = ext_keys - allowed_user_types
147 if unknown_keys:
148 cros_build_lib.Die('user_type %s is not allowed', unknown_keys)
149
150 cache_crx = extension.get('cache_crx', 'yes')
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700151
152 # Remove fields that shouldn't be in the output file.
khmel@google.com37161cf2019-01-23 09:43:44 -0800153 for key in ('cache_crx', 'child_users'):
154 extension.pop(key, None)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700155
156 if cache_crx == 'yes':
khmel@google.com37161cf2019-01-23 09:43:44 -0800157 if not DownloadCrx(ext, extension, crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700158 was_errors = True
159 elif cache_crx == 'no':
160 pass
161 else:
162 cros_build_lib.Die('Unknown value for "cache_crx" %s for %s',
163 cache_crx, ext)
164
khmel@google.com37161cf2019-01-23 09:43:44 -0800165 json_file = os.path.join(jsondir, '%s.json' % ext)
Mike Frysingerd0960812020-06-09 01:53:32 -0400166 pformat.json(extension, fp=json_file)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700167
168 if was_errors:
169 cros_build_lib.Die('FAIL to download some extensions')
170
Don Garrettec5cf902013-09-05 15:49:59 -0700171 CreateValidationFiles(validationdir, crxdir, identifier)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700172 cros_build_lib.CreateTarball(tarball, outputdir)
Ralph Nathan03047282015-03-23 11:09:32 -0700173 logging.info('Tarball created %s', tarball)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700174
175
176def main(argv):
177 parser = commandline.ArgumentParser(
David James9374aac2013-10-08 16:00:17 -0700178 '%%(prog)s [options] <version>\n\n%s' % __doc__, caching=True)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700179 parser.add_argument('version', nargs=1)
180 parser.add_argument('--path', default=None, type='path',
181 help='Path of files dir with external_extensions.json')
182 parser.add_argument('--create', default=False, action='store_true',
183 help='Create cache tarball with specified name')
184 parser.add_argument('--upload', default=False, action='store_true',
185 help='Upload cache tarball with specified name')
186 options = parser.parse_args(argv)
187
188 if options.path:
189 os.chdir(options.path)
190
191 if not (options.create or options.upload):
192 cros_build_lib.Die('Need at least --create or --upload args')
193
194 if not os.path.exists('external_extensions.json'):
195 cros_build_lib.Die('No external_extensions.json in %s. Did you forget the '
196 '--path option?', os.getcwd())
197
Don Garrettec5cf902013-09-05 15:49:59 -0700198 identifier = options.version[0]
199 tarball = '%s.tar.xz' % identifier
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700200 if options.create:
201 extensions = json.load(open('external_extensions.json', 'r'))
202 with osutils.TempDir() as tempdir:
Don Garrettec5cf902013-09-05 15:49:59 -0700203 CreateCacheTarball(extensions, tempdir, identifier,
204 os.path.abspath(tarball))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700205
206 if options.upload:
207 ctx = gs.GSContext()
208 url = os.path.join(UPLOAD_URL_BASE, tarball)
209 if ctx.Exists(url):
210 cros_build_lib.Die('This version already exists on Google Storage (%s)!\n'
211 'NEVER REWRITE EXISTING FILE. IT WILL BREAK CHROME OS '
212 'BUILD!!!', url)
213 ctx.Copy(os.path.abspath(tarball), url, acl='project-private')
Ralph Nathan03047282015-03-23 11:09:32 -0700214 logging.info('Tarball uploaded %s', url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700215 osutils.SafeUnlink(os.path.abspath(tarball))