blob: 02f1048be118977d61badbb326499fdf6702d1a8 [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
27import urllib
28import xml.dom.minidom
29
30from chromite.lib import commandline
31from chromite.lib import cros_build_lib
Ralph Nathan03047282015-03-23 11:09:32 -070032from chromite.lib import cros_logging as logging
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070033from chromite.lib import gs
34from chromite.lib import osutils
35
36
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070037UPLOAD_URL_BASE = 'gs://chromeos-localmirror-private/distfiles'
38
39
Don Garrettec5cf902013-09-05 15:49:59 -070040def DownloadCrx(ext, extension, crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070041 """Download .crx file from WebStore and update entry."""
Ralph Nathan03047282015-03-23 11:09:32 -070042 logging.info('Extension "%s"(%s)...', extension['name'], ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070043
Dmitry Polukhin97992a52014-06-17 13:10:56 +040044 update_url = ('%s?x=prodversion%%3D35.1.1.1%%26id%%3D%s%%26uc' %
Mike Frysingere65f3752014-12-08 00:46:39 -050045 (extension['external_update_url'], ext))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070046 response = urllib.urlopen(update_url)
47 if response.getcode() != 200:
Ralph Nathan59900422015-03-24 10:41:17 -070048 logging.error('Cannot get update response, URL: %s, error: %d', update_url,
49 response.getcode())
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070050 return False
51
52 dom = xml.dom.minidom.parse(response)
53 status = dom.getElementsByTagName('app')[0].getAttribute('status')
54 if status != 'ok':
Ralph Nathan59900422015-03-24 10:41:17 -070055 logging.error('Cannot fetch extension, status: %s', status)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070056 return False
57
58 node = dom.getElementsByTagName('updatecheck')[0]
59 url = node.getAttribute('codebase')
60 version = node.getAttribute('version')
61 filename = '%s-%s.crx' % (ext, version)
62 response = urllib.urlopen(url)
63 if response.getcode() != 200:
Ralph Nathan59900422015-03-24 10:41:17 -070064 logging.error('Cannot download extension, URL: %s, error: %d', url,
65 response.getcode())
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070066 return False
67
Don Garrettec5cf902013-09-05 15:49:59 -070068 osutils.WriteFile(os.path.join(crxdir, 'extensions', filename),
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070069 response.read())
70
Dmitry Polukhine9d8fac2013-09-20 13:11:21 -070071 # Keep external_update_url in json file, ExternalCache will take care about
72 # replacing it with proper external_crx path and version.
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070073
Ralph Nathan03047282015-03-23 11:09:32 -070074 logging.info('Downloaded, current version %s', version)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070075 return True
76
77
Don Garrettec5cf902013-09-05 15:49:59 -070078def CreateValidationFiles(validationdir, crxdir, identifier):
79 """Create validationfiles for all extensions in |crxdir|."""
80
81 verified_files = []
82
83 # Discover all extensions to be validated (but not JSON files).
84 for directory, _, filenames in os.walk(os.path.join(crxdir, 'extensions')):
85
86 # Make directory relative to output dir by removing crxdir and /.
87 for filename in filenames:
Mike Frysingere65f3752014-12-08 00:46:39 -050088 verified_files.append(os.path.join(directory[len(crxdir) + 1:],
Don Garrettec5cf902013-09-05 15:49:59 -070089 filename))
90
91 validation_file = os.path.join(validationdir, '%s.validation' % identifier)
92
93 osutils.SafeMakedirs(validationdir)
94 cros_build_lib.RunCommand(['sha256sum'] + verified_files,
95 log_stdout_to_file=validation_file,
96 cwd=crxdir, print_cmd=False)
Ralph Nathan03047282015-03-23 11:09:32 -070097 logging.info('Hashes created.')
Don Garrettec5cf902013-09-05 15:49:59 -070098
99
100def CreateCacheTarball(extensions, outputdir, identifier, tarball):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700101 """Cache |extensions| in |outputdir| and pack them in |tarball|."""
Don Garrettec5cf902013-09-05 15:49:59 -0700102
103 crxdir = os.path.join(outputdir, 'crx')
104 jsondir = os.path.join(outputdir, 'json')
105 validationdir = os.path.join(outputdir, 'validation')
106
107 osutils.SafeMakedirs(os.path.join(crxdir, 'extensions', 'managed_users'))
108 osutils.SafeMakedirs(os.path.join(jsondir, 'extensions', 'managed_users'))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700109 was_errors = False
110 for ext in extensions:
111 managed_users = extensions[ext].get('managed_users', 'no')
112 cache_crx = extensions[ext].get('cache_crx', 'yes')
113
114 # Remove fields that shouldn't be in the output file.
115 for key in ('cache_crx', 'managed_users'):
116 extensions[ext].pop(key, None)
117
118 if cache_crx == 'yes':
Don Garrettec5cf902013-09-05 15:49:59 -0700119 if not DownloadCrx(ext, extensions[ext], crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700120 was_errors = True
121 elif cache_crx == 'no':
122 pass
123 else:
124 cros_build_lib.Die('Unknown value for "cache_crx" %s for %s',
125 cache_crx, ext)
126
127 if managed_users == 'yes':
Don Garrettec5cf902013-09-05 15:49:59 -0700128 json_file = os.path.join(jsondir,
Mike Frysingere65f3752014-12-08 00:46:39 -0500129 'extensions/managed_users/%s.json' % ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700130 json.dump(extensions[ext],
131 open(json_file, 'w'),
132 sort_keys=True,
133 indent=2,
134 separators=(',', ': '))
135
136 if managed_users != 'only':
Don Garrettec5cf902013-09-05 15:49:59 -0700137 json_file = os.path.join(jsondir, 'extensions/%s.json' % ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700138 json.dump(extensions[ext],
139 open(json_file, 'w'),
140 sort_keys=True,
141 indent=2,
142 separators=(',', ': '))
143
144 if was_errors:
145 cros_build_lib.Die('FAIL to download some extensions')
146
Don Garrettec5cf902013-09-05 15:49:59 -0700147 CreateValidationFiles(validationdir, crxdir, identifier)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700148 cros_build_lib.CreateTarball(tarball, outputdir)
Ralph Nathan03047282015-03-23 11:09:32 -0700149 logging.info('Tarball created %s', tarball)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700150
151
152def main(argv):
153 parser = commandline.ArgumentParser(
David James9374aac2013-10-08 16:00:17 -0700154 '%%(prog)s [options] <version>\n\n%s' % __doc__, caching=True)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700155 parser.add_argument('version', nargs=1)
156 parser.add_argument('--path', default=None, type='path',
157 help='Path of files dir with external_extensions.json')
158 parser.add_argument('--create', default=False, action='store_true',
159 help='Create cache tarball with specified name')
160 parser.add_argument('--upload', default=False, action='store_true',
161 help='Upload cache tarball with specified name')
162 options = parser.parse_args(argv)
163
164 if options.path:
165 os.chdir(options.path)
166
167 if not (options.create or options.upload):
168 cros_build_lib.Die('Need at least --create or --upload args')
169
170 if not os.path.exists('external_extensions.json'):
171 cros_build_lib.Die('No external_extensions.json in %s. Did you forget the '
172 '--path option?', os.getcwd())
173
Don Garrettec5cf902013-09-05 15:49:59 -0700174 identifier = options.version[0]
175 tarball = '%s.tar.xz' % identifier
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700176 if options.create:
177 extensions = json.load(open('external_extensions.json', 'r'))
178 with osutils.TempDir() as tempdir:
Don Garrettec5cf902013-09-05 15:49:59 -0700179 CreateCacheTarball(extensions, tempdir, identifier,
180 os.path.abspath(tarball))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700181
182 if options.upload:
183 ctx = gs.GSContext()
184 url = os.path.join(UPLOAD_URL_BASE, tarball)
185 if ctx.Exists(url):
186 cros_build_lib.Die('This version already exists on Google Storage (%s)!\n'
187 'NEVER REWRITE EXISTING FILE. IT WILL BREAK CHROME OS '
188 'BUILD!!!', url)
189 ctx.Copy(os.path.abspath(tarball), url, acl='project-private')
Ralph Nathan03047282015-03-23 11:09:32 -0700190 logging.info('Tarball uploaded %s', url)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700191 osutils.SafeUnlink(os.path.abspath(tarball))