blob: 5df4ffb5d3a5ca5e8e1558c3a1fb66d7f1aaeaec [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 Frysinger1d4752b2014-11-08 04:00:18 -050023# pylint: disable=bad-continuation
24
Mike Frysinger383367e2014-09-16 15:06:17 -040025from __future__ import print_function
26
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070027import json
28import os
29import urllib
30import xml.dom.minidom
31
32from chromite.lib import commandline
33from chromite.lib import cros_build_lib
34from 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."""
43 cros_build_lib.Info('Extension "%s"(%s)...', extension['name'], ext)
44
Dmitry Polukhin97992a52014-06-17 13:10:56 +040045 update_url = ('%s?x=prodversion%%3D35.1.1.1%%26id%%3D%s%%26uc' %
46 (extension['external_update_url'], ext))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070047 response = urllib.urlopen(update_url)
48 if response.getcode() != 200:
49 cros_build_lib.Error('Cannot get update response, URL: %s, error: %d',
50 update_url, response.getcode())
51 return False
52
53 dom = xml.dom.minidom.parse(response)
54 status = dom.getElementsByTagName('app')[0].getAttribute('status')
55 if status != 'ok':
56 cros_build_lib.Error('Cannot fetch extension, status: %s', status)
57 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)
63 response = urllib.urlopen(url)
64 if response.getcode() != 200:
65 cros_build_lib.Error('Cannot download extension, URL: %s, error: %d',
66 url, response.getcode())
67 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
75 cros_build_lib.Info('Downloaded, current version %s', version)
76 return True
77
78
Don Garrettec5cf902013-09-05 15:49:59 -070079def CreateValidationFiles(validationdir, crxdir, identifier):
80 """Create validationfiles for all extensions in |crxdir|."""
81
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:
89 verified_files.append(os.path.join(directory[len(crxdir)+1:],
90 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)
98 cros_build_lib.Info('Hashes created.')
99
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')
105 jsondir = os.path.join(outputdir, 'json')
106 validationdir = os.path.join(outputdir, 'validation')
107
108 osutils.SafeMakedirs(os.path.join(crxdir, 'extensions', 'managed_users'))
109 osutils.SafeMakedirs(os.path.join(jsondir, 'extensions', 'managed_users'))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700110 was_errors = False
111 for ext in extensions:
112 managed_users = extensions[ext].get('managed_users', 'no')
113 cache_crx = extensions[ext].get('cache_crx', 'yes')
114
115 # Remove fields that shouldn't be in the output file.
116 for key in ('cache_crx', 'managed_users'):
117 extensions[ext].pop(key, None)
118
119 if cache_crx == 'yes':
Don Garrettec5cf902013-09-05 15:49:59 -0700120 if not DownloadCrx(ext, extensions[ext], crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700121 was_errors = True
122 elif cache_crx == 'no':
123 pass
124 else:
125 cros_build_lib.Die('Unknown value for "cache_crx" %s for %s',
126 cache_crx, ext)
127
128 if managed_users == 'yes':
Don Garrettec5cf902013-09-05 15:49:59 -0700129 json_file = os.path.join(jsondir,
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700130 'extensions/managed_users/%s.json' % ext)
131 json.dump(extensions[ext],
132 open(json_file, 'w'),
133 sort_keys=True,
134 indent=2,
135 separators=(',', ': '))
136
137 if managed_users != 'only':
Don Garrettec5cf902013-09-05 15:49:59 -0700138 json_file = os.path.join(jsondir, 'extensions/%s.json' % ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700139 json.dump(extensions[ext],
140 open(json_file, 'w'),
141 sort_keys=True,
142 indent=2,
143 separators=(',', ': '))
144
145 if was_errors:
146 cros_build_lib.Die('FAIL to download some extensions')
147
Don Garrettec5cf902013-09-05 15:49:59 -0700148 CreateValidationFiles(validationdir, crxdir, identifier)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700149 cros_build_lib.CreateTarball(tarball, outputdir)
150 cros_build_lib.Info('Tarball created %s', tarball)
151
152
153def main(argv):
154 parser = commandline.ArgumentParser(
David James9374aac2013-10-08 16:00:17 -0700155 '%%(prog)s [options] <version>\n\n%s' % __doc__, caching=True)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700156 parser.add_argument('version', nargs=1)
157 parser.add_argument('--path', default=None, type='path',
158 help='Path of files dir with external_extensions.json')
159 parser.add_argument('--create', default=False, action='store_true',
160 help='Create cache tarball with specified name')
161 parser.add_argument('--upload', default=False, action='store_true',
162 help='Upload cache tarball with specified name')
163 options = parser.parse_args(argv)
164
165 if options.path:
166 os.chdir(options.path)
167
168 if not (options.create or options.upload):
169 cros_build_lib.Die('Need at least --create or --upload args')
170
171 if not os.path.exists('external_extensions.json'):
172 cros_build_lib.Die('No external_extensions.json in %s. Did you forget the '
173 '--path option?', os.getcwd())
174
Don Garrettec5cf902013-09-05 15:49:59 -0700175 identifier = options.version[0]
176 tarball = '%s.tar.xz' % identifier
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700177 if options.create:
178 extensions = json.load(open('external_extensions.json', 'r'))
179 with osutils.TempDir() as tempdir:
Don Garrettec5cf902013-09-05 15:49:59 -0700180 CreateCacheTarball(extensions, tempdir, identifier,
181 os.path.abspath(tarball))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700182
183 if options.upload:
184 ctx = gs.GSContext()
185 url = os.path.join(UPLOAD_URL_BASE, tarball)
186 if ctx.Exists(url):
187 cros_build_lib.Die('This version already exists on Google Storage (%s)!\n'
188 'NEVER REWRITE EXISTING FILE. IT WILL BREAK CHROME OS '
189 'BUILD!!!', url)
190 ctx.Copy(os.path.abspath(tarball), url, acl='project-private')
191 cros_build_lib.Info('Tarball uploaded %s', url)
192 osutils.SafeUnlink(os.path.abspath(tarball))