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 from telemetry.core import exceptions | |
19 | |
20 | |
21 # Remote directory to upload extensions to in cloud storage. | |
robliao
2015/07/16 18:33:25
Could be Remote target upload directory in cloud s
sydli
2015/07/16 20:01:31
Done.
| |
22 REMOTE_DIR = "extension_set" | |
23 # Name of zip archive to write to | |
robliao
2015/07/16 18:33:25
Linebreak above, add period. Could also be target
sydli
2015/07/16 20:01:30
Done.
| |
24 ZIP_NAME = "extensions.zip" | |
25 | |
26 def _DownloadCRXFromCWS(ext_id, dst): | |
robliao
2015/07/16 18:33:25
Should probably be _DownloadCrxFromCws since we do
sydli
2015/07/16 20:01:30
Done.
| |
27 """ Downloads CRX specified from Chrome Web Store. | |
robliao
2015/07/16 18:33:25
Remove leading whitespace.
sydli
2015/07/16 20:01:30
Done.
| |
28 | |
29 Retrieves CRX (Chrome extension file) sepcified by ext_id from Chrome Web | |
robliao
2015/07/16 18:33:25
specified
sydli
2015/07/16 20:01:31
Done.
| |
30 Store, into directory specified by dst. | |
31 | |
32 Args: | |
33 ext_id: id of extension to retrieve. | |
34 dst: directory to download CRX into | |
35 | |
36 Returns: | |
37 Returns local path to downloaded CRX. | |
38 If download fails, return None. """ | |
robliao
2015/07/16 18:33:25
Remove trailing whitespace. Also the """ should go
sydli
2015/07/16 20:01:30
Done.
| |
39 dst_path = os.path.join(dst, "%s.crx" % ext_id) | |
40 cws_url = ("https://clients2.google.com/service/update2/crx?response=" | |
41 "redirect&prodversion=38.0&x=id%%3D%s%%26installsource%%3D" | |
42 "ondemand%%26uc" % ext_id) | |
43 response = urllib2.urlopen(cws_url) | |
44 if response.getcode() is not 200: | |
45 return None | |
46 with open(dst_path, "w") as f: | |
47 f.write(response.read()) | |
48 return dst_path | |
49 | |
50 def _UpdateExtensionsInCloud(local_extensions_dir, extensions_csv, remote_dir): | |
51 """ Updates set of extensions in Cloud Storage from CSV of extension ids. | |
robliao
2015/07/16 18:33:25
Remove leading white space.
Nit: From a csv
sydli
2015/07/16 20:01:31
Done.
| |
52 | |
53 From well-formatted CSV file containing some set of extensions | |
54 (extensions_csv), download them, compress into archive, and update | |
55 the remote extension archive under REMOTE_DIR in CHROME-PARTNER-TELEMETRY | |
56 bucket. This script expects 2nd column of CSV file to contain extension ids. | |
57 | |
58 Args: | |
59 local_extensions_dir: directory to download CRX files into. | |
60 extension_csv: CSV to pull extension_ids from. | |
61 remote_dir: remote directory to put extension archive in cloud storage. | |
62 | |
63 Raises: | |
64 Exception if a CRX download fails. """ | |
robliao
2015/07/16 18:33:25
Move """ to the next line.
sydli
2015/07/16 20:01:31
Done.
| |
65 # Download CRX to temp files and compress into archive | |
66 zip_path = os.path.join(local_extensions_dir, ZIP_NAME) | |
67 extension_zip = zipfile.ZipFile(zip_path, 'w') | |
68 with open(extensions_csv, 'rb') as csvfile: | |
69 reader = csv.reader(csvfile) | |
70 reader.next() # skip header line | |
71 for row in reader: | |
72 extension_id = row[1] | |
73 print "Fetching extension %s..." % extension_id | |
74 crx_path = _DownloadCRXFromCWS(extension_id, local_extensions_dir) | |
75 if crx_path is None: | |
76 raise exceptions.Error("\tCould not fetch extension %s.\n\n" | |
77 "If this extension dl consistently fails, " | |
78 "remove this entry from %s." | |
79 % (extension_id, extensions_csv)) | |
80 extension_zip.write(crx_path, arcname="%s.crx" % extension_id) | |
81 extension_zip.close() | |
82 print "Uploading extensions to cloud..." | |
83 remote_zip_path = os.path.join(remote_dir, ZIP_NAME) | |
84 cloud_storage.Insert(cloud_storage.PARTNER_BUCKET, remote_zip_path, zip_path) | |
85 | |
86 def _GetCSVFromArgs(): | |
robliao
2015/07/16 18:33:25
This should probably be _GetCsvFromArgs.
If you c
sydli
2015/07/16 20:01:30
I'll just uncaps the function names from here on o
| |
87 """ Parse options to retrieve name of CSV file. """ | |
robliao
2015/07/16 18:33:25
Remove leading and trailing whitespace in the docs
sydli
2015/07/16 20:01:30
Done.
| |
88 parser = optparse.OptionParser() | |
89 parser.add_option("-e", "--extension-csv", dest="extension_csv", | |
90 help="CSV of extensions to load.") | |
91 (options, _) = parser.parse_args() | |
92 if not options.extension_csv: | |
93 parser.error("Must specify --extension-csv option.") | |
94 return options.extension_csv | |
95 | |
96 def main(): | |
97 extension_csv = _GetCSVFromArgs() | |
98 local_extensions_dir = tempfile.mkdtemp() | |
99 try: | |
100 _UpdateExtensionsInCloud(local_extensions_dir, | |
101 extension_csv, REMOTE_DIR) | |
102 finally: | |
103 shutil.rmtree(local_extensions_dir) | |
104 | |
105 if __name__=="__main__": | |
robliao
2015/07/16 18:33:25
Add a around the ==
sydli
2015/07/16 20:01:31
Done.
| |
106 main() | |
OLD | NEW |