blob: ba69ba5079513123d4be0adf3b00dc5eb67a676d [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
32from chromite.lib import gs
33from chromite.lib import osutils
34
35
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070036UPLOAD_URL_BASE = 'gs://chromeos-localmirror-private/distfiles'
37
38
Don Garrettec5cf902013-09-05 15:49:59 -070039def DownloadCrx(ext, extension, crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070040 """Download .crx file from WebStore and update entry."""
41 cros_build_lib.Info('Extension "%s"(%s)...', extension['name'], ext)
42
Dmitry Polukhin97992a52014-06-17 13:10:56 +040043 update_url = ('%s?x=prodversion%%3D35.1.1.1%%26id%%3D%s%%26uc' %
Mike Frysingere65f3752014-12-08 00:46:39 -050044 (extension['external_update_url'], ext))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070045 response = urllib.urlopen(update_url)
46 if response.getcode() != 200:
47 cros_build_lib.Error('Cannot get update response, URL: %s, error: %d',
48 update_url, response.getcode())
49 return False
50
51 dom = xml.dom.minidom.parse(response)
52 status = dom.getElementsByTagName('app')[0].getAttribute('status')
53 if status != 'ok':
54 cros_build_lib.Error('Cannot fetch extension, status: %s', status)
55 return False
56
57 node = dom.getElementsByTagName('updatecheck')[0]
58 url = node.getAttribute('codebase')
59 version = node.getAttribute('version')
60 filename = '%s-%s.crx' % (ext, version)
61 response = urllib.urlopen(url)
62 if response.getcode() != 200:
63 cros_build_lib.Error('Cannot download extension, URL: %s, error: %d',
64 url, response.getcode())
65 return False
66
Don Garrettec5cf902013-09-05 15:49:59 -070067 osutils.WriteFile(os.path.join(crxdir, 'extensions', filename),
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070068 response.read())
69
Dmitry Polukhine9d8fac2013-09-20 13:11:21 -070070 # Keep external_update_url in json file, ExternalCache will take care about
71 # replacing it with proper external_crx path and version.
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -070072
73 cros_build_lib.Info('Downloaded, current version %s', version)
74 return True
75
76
Don Garrettec5cf902013-09-05 15:49:59 -070077def CreateValidationFiles(validationdir, crxdir, identifier):
78 """Create validationfiles for all extensions in |crxdir|."""
79
80 verified_files = []
81
82 # Discover all extensions to be validated (but not JSON files).
83 for directory, _, filenames in os.walk(os.path.join(crxdir, 'extensions')):
84
85 # Make directory relative to output dir by removing crxdir and /.
86 for filename in filenames:
Mike Frysingere65f3752014-12-08 00:46:39 -050087 verified_files.append(os.path.join(directory[len(crxdir) + 1:],
Don Garrettec5cf902013-09-05 15:49:59 -070088 filename))
89
90 validation_file = os.path.join(validationdir, '%s.validation' % identifier)
91
92 osutils.SafeMakedirs(validationdir)
93 cros_build_lib.RunCommand(['sha256sum'] + verified_files,
94 log_stdout_to_file=validation_file,
95 cwd=crxdir, print_cmd=False)
96 cros_build_lib.Info('Hashes created.')
97
98
99def CreateCacheTarball(extensions, outputdir, identifier, tarball):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700100 """Cache |extensions| in |outputdir| and pack them in |tarball|."""
Don Garrettec5cf902013-09-05 15:49:59 -0700101
102 crxdir = os.path.join(outputdir, 'crx')
103 jsondir = os.path.join(outputdir, 'json')
104 validationdir = os.path.join(outputdir, 'validation')
105
106 osutils.SafeMakedirs(os.path.join(crxdir, 'extensions', 'managed_users'))
107 osutils.SafeMakedirs(os.path.join(jsondir, 'extensions', 'managed_users'))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700108 was_errors = False
109 for ext in extensions:
110 managed_users = extensions[ext].get('managed_users', 'no')
111 cache_crx = extensions[ext].get('cache_crx', 'yes')
112
113 # Remove fields that shouldn't be in the output file.
114 for key in ('cache_crx', 'managed_users'):
115 extensions[ext].pop(key, None)
116
117 if cache_crx == 'yes':
Don Garrettec5cf902013-09-05 15:49:59 -0700118 if not DownloadCrx(ext, extensions[ext], crxdir):
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700119 was_errors = True
120 elif cache_crx == 'no':
121 pass
122 else:
123 cros_build_lib.Die('Unknown value for "cache_crx" %s for %s',
124 cache_crx, ext)
125
126 if managed_users == 'yes':
Don Garrettec5cf902013-09-05 15:49:59 -0700127 json_file = os.path.join(jsondir,
Mike Frysingere65f3752014-12-08 00:46:39 -0500128 'extensions/managed_users/%s.json' % ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700129 json.dump(extensions[ext],
130 open(json_file, 'w'),
131 sort_keys=True,
132 indent=2,
133 separators=(',', ': '))
134
135 if managed_users != 'only':
Don Garrettec5cf902013-09-05 15:49:59 -0700136 json_file = os.path.join(jsondir, 'extensions/%s.json' % ext)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700137 json.dump(extensions[ext],
138 open(json_file, 'w'),
139 sort_keys=True,
140 indent=2,
141 separators=(',', ': '))
142
143 if was_errors:
144 cros_build_lib.Die('FAIL to download some extensions')
145
Don Garrettec5cf902013-09-05 15:49:59 -0700146 CreateValidationFiles(validationdir, crxdir, identifier)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700147 cros_build_lib.CreateTarball(tarball, outputdir)
148 cros_build_lib.Info('Tarball created %s', tarball)
149
150
151def main(argv):
152 parser = commandline.ArgumentParser(
David James9374aac2013-10-08 16:00:17 -0700153 '%%(prog)s [options] <version>\n\n%s' % __doc__, caching=True)
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700154 parser.add_argument('version', nargs=1)
155 parser.add_argument('--path', default=None, type='path',
156 help='Path of files dir with external_extensions.json')
157 parser.add_argument('--create', default=False, action='store_true',
158 help='Create cache tarball with specified name')
159 parser.add_argument('--upload', default=False, action='store_true',
160 help='Upload cache tarball with specified name')
161 options = parser.parse_args(argv)
162
163 if options.path:
164 os.chdir(options.path)
165
166 if not (options.create or options.upload):
167 cros_build_lib.Die('Need at least --create or --upload args')
168
169 if not os.path.exists('external_extensions.json'):
170 cros_build_lib.Die('No external_extensions.json in %s. Did you forget the '
171 '--path option?', os.getcwd())
172
Don Garrettec5cf902013-09-05 15:49:59 -0700173 identifier = options.version[0]
174 tarball = '%s.tar.xz' % identifier
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700175 if options.create:
176 extensions = json.load(open('external_extensions.json', 'r'))
177 with osutils.TempDir() as tempdir:
Don Garrettec5cf902013-09-05 15:49:59 -0700178 CreateCacheTarball(extensions, tempdir, identifier,
179 os.path.abspath(tarball))
Dmitry Polukhincbdd21c2013-08-13 10:42:04 -0700180
181 if options.upload:
182 ctx = gs.GSContext()
183 url = os.path.join(UPLOAD_URL_BASE, tarball)
184 if ctx.Exists(url):
185 cros_build_lib.Die('This version already exists on Google Storage (%s)!\n'
186 'NEVER REWRITE EXISTING FILE. IT WILL BREAK CHROME OS '
187 'BUILD!!!', url)
188 ctx.Copy(os.path.abspath(tarball), url, acl='project-private')
189 cros_build_lib.Info('Tarball uploaded %s', url)
190 osutils.SafeUnlink(os.path.abspath(tarball))