blob: e1d950c9fe62b1fa80d85004dec9801821bbb345 [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
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070027import xml.dom.minidom
28
Mike Frysinger3dcacee2019-08-23 17:09:11 -040029from six.moves import urllib
30
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070031from chromite.lib import commandline
32from chromite.lib import cros_build_lib
Ralph Nathan03047282015-03-23 11:09:32 -070033from chromite.lib import cros_logging as logging
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070034from chromite.lib import gs
35from chromite.lib import osutils
36
37
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070038UPLOAD_URL_BASE = 'gs://chromeos-localmirror-private/distfiles'
39
40
Don Garrettec5cf902013-09-05 15:49:59 -070041def DownloadCrx(ext, extension, crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070042 """Download .crx file from WebStore and update entry."""
Ralph Nathan03047282015-03-23 11:09:32 -070043 logging.info('Extension "%s"(%s)...', extension['name'], ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070044
Dmitry Polukhin97992a52014-06-17 13:10:56 +040045 update_url = ('%s?x=prodversion%%3D35.1.1.1%%26id%%3D%s%%26uc' %
Mike Frysingere65f3752014-12-08 00:46:39 -050046 (extension['external_update_url'], ext))
Mike Frysinger3dcacee2019-08-23 17:09:11 -040047 response = urllib.request.urlopen(update_url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070048 if response.getcode() != 200:
Ralph Nathan59900422015-03-24 10:41:17 -070049 logging.error('Cannot get update response, URL: %s, error: %d', update_url,
50 response.getcode())
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070051 return False
52
53 dom = xml.dom.minidom.parse(response)
54 status = dom.getElementsByTagName('app')[0].getAttribute('status')
55 if status != 'ok':
Ralph Nathan59900422015-03-24 10:41:17 -070056 logging.error('Cannot fetch extension, status: %s', status)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070057 return False
58
59 node = dom.getElementsByTagName('updatecheck')[0]
60 url = node.getAttribute('codebase')
61 version = node.getAttribute('version')
62 filename = '%s-%s.crx' % (ext, version)
Mike Frysinger3dcacee2019-08-23 17:09:11 -040063 response = urllib.request.urlopen(url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070064 if response.getcode() != 200:
Ralph Nathan59900422015-03-24 10:41:17 -070065 logging.error('Cannot download extension, URL: %s, error: %d', url,
66 response.getcode())
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070067 return False
68
Don Garrettec5cf902013-09-05 15:49:59 -070069 osutils.WriteFile(os.path.join(crxdir, 'extensions', filename),
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070070 response.read())
71
Dmitry Polukhine9d8fac2013-09-20 13:11:21 -070072 # Keep external_update_url in json file, ExternalCache will take care about
73 # replacing it with proper external_crx path and version.
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070074
Ralph Nathan03047282015-03-23 11:09:32 -070075 logging.info('Downloaded, current version %s', version)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070076 return True
77
78
Don Garrettec5cf902013-09-05 15:49:59 -070079def CreateValidationFiles(validationdir, crxdir, identifier):
khmel@google.com37161cf2019-01-23 09:43:44 -080080 """Create validation files for all extensions in |crxdir|."""
Don Garrettec5cf902013-09-05 15:49:59 -070081
82 verified_files = []
83
84 # Discover all extensions to be validated (but not JSON files).
85 for directory, _, filenames in os.walk(os.path.join(crxdir, 'extensions')):
86
87 # Make directory relative to output dir by removing crxdir and /.
88 for filename in filenames:
Mike Frysingere65f3752014-12-08 00:46:39 -050089 verified_files.append(os.path.join(directory[len(crxdir) + 1:],
Don Garrettec5cf902013-09-05 15:49:59 -070090 filename))
91
92 validation_file = os.path.join(validationdir, '%s.validation' % identifier)
93
94 osutils.SafeMakedirs(validationdir)
95 cros_build_lib.RunCommand(['sha256sum'] + verified_files,
96 log_stdout_to_file=validation_file,
97 cwd=crxdir, print_cmd=False)
Ralph Nathan03047282015-03-23 11:09:32 -070098 logging.info('Hashes created.')
Don Garrettec5cf902013-09-05 15:49:59 -070099
100
101def CreateCacheTarball(extensions, outputdir, identifier, tarball):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700102 """Cache |extensions| in |outputdir| and pack them in |tarball|."""
Don Garrettec5cf902013-09-05 15:49:59 -0700103
104 crxdir = os.path.join(outputdir, 'crx')
khmel@google.com37161cf2019-01-23 09:43:44 -0800105 jsondir = os.path.join(outputdir, 'json', 'extensions')
Don Garrettec5cf902013-09-05 15:49:59 -0700106 validationdir = os.path.join(outputdir, 'validation')
107
khmel@google.com37161cf2019-01-23 09:43:44 -0800108 osutils.SafeMakedirs(os.path.join(crxdir, 'extensions'))
109 osutils.SafeMakedirs(jsondir)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700110 was_errors = False
111 for ext in extensions:
khmel@google.com37161cf2019-01-23 09:43:44 -0800112 extension = extensions[ext]
113 # It should not be in use at this moment.
114 if 'managed_users' in extension:
115 cros_build_lib.Die('managed_users is deprecated and not supported. '
116 'Please use user_type.')
117 # In case we work with old type json, use default 'user_type'.
118 # TODO: Update all external_extensions.json files and deprecate this.
119 if 'user_type' not in extension:
120 user_type = ['unmanaged']
121 if extension.get('child_users', 'no') == 'yes':
122 user_type.append('child')
123 logging.warn('user_type filter has to be set explicitly for %s, using '
124 '%s by default.', ext, user_type)
125 extension['user_type'] = user_type
126 else:
127 if 'child_users' in extension:
128 cros_build_lib.Die('child_users is not supported when user_type is '
129 'set.')
130
131 # Verify user type is well-formed.
132 allowed_user_types = {'unmanaged', 'managed', 'child', 'supervised',
133 'guest'}
134 if not extension['user_type']:
135 cros_build_lib.Die('user_type is not set')
136 ext_keys = set(extension['user_type'])
137 unknown_keys = ext_keys - allowed_user_types
138 if unknown_keys:
139 cros_build_lib.Die('user_type %s is not allowed', unknown_keys)
140
141 cache_crx = extension.get('cache_crx', 'yes')
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700142
143 # Remove fields that shouldn't be in the output file.
khmel@google.com37161cf2019-01-23 09:43:44 -0800144 for key in ('cache_crx', 'child_users'):
145 extension.pop(key, None)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700146
147 if cache_crx == 'yes':
khmel@google.com37161cf2019-01-23 09:43:44 -0800148 if not DownloadCrx(ext, extension, crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700149 was_errors = True
150 elif cache_crx == 'no':
151 pass
152 else:
153 cros_build_lib.Die('Unknown value for "cache_crx" %s for %s',
154 cache_crx, ext)
155
khmel@google.com37161cf2019-01-23 09:43:44 -0800156 json_file = os.path.join(jsondir, '%s.json' % ext)
157 json.dump(extension,
158 open(json_file, 'w'),
159 sort_keys=True,
160 indent=2,
161 separators=(',', ': '))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700162
163 if was_errors:
164 cros_build_lib.Die('FAIL to download some extensions')
165
Don Garrettec5cf902013-09-05 15:49:59 -0700166 CreateValidationFiles(validationdir, crxdir, identifier)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700167 cros_build_lib.CreateTarball(tarball, outputdir)
Ralph Nathan03047282015-03-23 11:09:32 -0700168 logging.info('Tarball created %s', tarball)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700169
170
171def main(argv):
172 parser = commandline.ArgumentParser(
David James9374aac2013-10-08 16:00:17 -0700173 '%%(prog)s [options] <version>\n\n%s' % __doc__, caching=True)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700174 parser.add_argument('version', nargs=1)
175 parser.add_argument('--path', default=None, type='path',
176 help='Path of files dir with external_extensions.json')
177 parser.add_argument('--create', default=False, action='store_true',
178 help='Create cache tarball with specified name')
179 parser.add_argument('--upload', default=False, action='store_true',
180 help='Upload cache tarball with specified name')
181 options = parser.parse_args(argv)
182
183 if options.path:
184 os.chdir(options.path)
185
186 if not (options.create or options.upload):
187 cros_build_lib.Die('Need at least --create or --upload args')
188
189 if not os.path.exists('external_extensions.json'):
190 cros_build_lib.Die('No external_extensions.json in %s. Did you forget the '
191 '--path option?', os.getcwd())
192
Don Garrettec5cf902013-09-05 15:49:59 -0700193 identifier = options.version[0]
194 tarball = '%s.tar.xz' % identifier
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700195 if options.create:
196 extensions = json.load(open('external_extensions.json', 'r'))
197 with osutils.TempDir() as tempdir:
Don Garrettec5cf902013-09-05 15:49:59 -0700198 CreateCacheTarball(extensions, tempdir, identifier,
199 os.path.abspath(tarball))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700200
201 if options.upload:
202 ctx = gs.GSContext()
203 url = os.path.join(UPLOAD_URL_BASE, tarball)
204 if ctx.Exists(url):
205 cros_build_lib.Die('This version already exists on Google Storage (%s)!\n'
206 'NEVER REWRITE EXISTING FILE. IT WILL BREAK CHROME OS '
207 'BUILD!!!', url)
208 ctx.Copy(os.path.abspath(tarball), url, acl='project-private')
Ralph Nathan03047282015-03-23 11:09:32 -0700209 logging.info('Tarball uploaded %s', url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700210 osutils.SafeUnlink(os.path.abspath(tarball))