OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import csv | |
6 import optparse | |
7 import os | |
8 import shutil | |
9 import sys | |
10 import tempfile | |
11 import urllib2 | |
12 import zipfile | |
13 | |
14 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | |
15 'telemetry')) | |
16 | |
17 from catapult_base import cloud_storage | |
18 | |
19 | |
20 # Remote directory to upload extensions to in cloud storage. | |
21 REMOTE_DIR = "extension_set" | |
22 # Name of zip archive to write to | |
23 ZIP_NAME = "extensions.zip" | |
24 | |
25 def _DownloadCRXFromCWS(ext_id, dst): | |
26 """ Downloads CRX specified from Chrome Web Store. | |
27 | |
28 Retrieves CRX (Chrome extension file) sepcified by ext_id from Chrome Web | |
29 Store, into directory specified by dst. | |
30 | |
31 Args: | |
32 ext_id: id of extension to retrieve. | |
33 dst: directory to download CRX into | |
34 | |
35 Returns: | |
36 If retreiving CRX fails, returns None. Otherwise returns local path | |
37 to downloaded CRX. """ | |
38 dst_path = os.path.join(dst, "%s.crx" % ext_id) | |
39 cws_url = ("https://clients2.google.com/service/update2/crx?response=" | |
40 "redirect&prodversion=38.0&x=id%%3D%s%%26installsource%%3D" | |
41 "ondemand%%26uc" % ext_id) | |
42 response = urllib2.urlopen(cws_url) | |
43 if response.getcode() is not 200: | |
44 return None | |
erikchen
2015/07/16 00:33:28
nit: too many spaces.
sydli
2015/07/16 00:53:14
Done.
| |
45 with open(dst_path, "w") as f: | |
46 f.write(response.read()) | |
47 return dst_path | |
48 | |
49 def _UpdateExtensionsInCloud(local_extensions_dir, extensions_csv, remote_dir): | |
50 """ Updates set of extensions in Cloud Storage from CSV of extension ids. | |
51 | |
52 From well-formatted CSV file containing some set of extensions | |
53 (extensions_csv), download them, compress into archive, and update | |
54 the remote extension archive under REMOTE_DIR in CHROME-PARTNER-TELEMETRY | |
55 bucket. This script expects 2nd column of CSV file to contain extension ids. | |
56 | |
57 Args: | |
58 local_extensions_dir: directory to download CRX files into. | |
59 extension_csv: CSV to pull extension_ids from. | |
60 remote_dir: remote directory to put extension archive in cloud storage. | |
61 """ | |
62 # Download CRX to temp files and compress into archive | |
63 zip_path = os.path.join(local_extensions_dir, ZIP_NAME) | |
64 extension_zip = zipfile.ZipFile(zip_path, 'w') | |
65 with open(extensions_csv, 'rb') as csvfile: | |
66 reader = csv.reader(csvfile) | |
67 reader.next() # skip header line | |
68 for row in reader: | |
69 extension_id = row[1] | |
70 print "Fetching extension %s..." % extension_id | |
71 crx_path = _DownloadCRXFromCWS(extension_id, local_extensions_dir) | |
72 if crx_path is None: | |
erikchen
2015/07/16 00:33:28
Under this code, it's possible that there's a good
sydli
2015/07/16 00:53:14
I'll raise an error instead. In the case that one
| |
73 print "\tCould not fetch %s" % extension_id | |
74 continue | |
75 extension_zip.write(crx_path, arcname="%s.crx" % extension_id) | |
76 extension_zip.close() | |
77 print "Uploading extensions to cloud..." | |
78 remote_zip_path = os.path.join(remote_dir, ZIP_NAME) | |
79 cloud_storage.Insert(cloud_storage.PARTNER_BUCKET, remote_zip_path, zip_path) | |
80 | |
81 def _GetCSVFromArgs(): | |
82 """ Parse options to retrieve name of CSV file. """ | |
83 parser = optparse.OptionParser() | |
84 parser.add_option("-e", "--extension-csv", dest="extension_csv", | |
85 help="CSV of extensions to load.") | |
86 (options, _) = parser.parse_args() | |
87 if not options.extension_csv: | |
88 parser.error("Must specify --extension-csv option.") | |
89 return options.extension_csv | |
90 | |
91 def main(): | |
92 extension_csv = _GetCSVFromArgs() | |
93 local_extensions_dir = tempfile.mkdtemp() | |
94 try: | |
95 _UpdateExtensionsInCloud(local_extensions_dir, | |
96 extension_csv, REMOTE_DIR) | |
97 finally: | |
98 shutil.rmtree(local_extensions_dir) | |
99 | |
100 if __name__=="__main__": | |
101 main() | |
102 | |
OLD | NEW |