OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 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 os | |
7 import optparse | |
erikchen
2015/07/14 23:36:15
alphabetize
sydli
2015/07/15 22:56:24
Done.
| |
8 import shutil | |
9 import sys | |
10 import tempfile | |
11 import urllib2 | |
12 | |
13 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | |
14 'telemetry')) | |
15 | |
16 from catapult_base import cloud_storage | |
17 | |
18 | |
19 # Google Cloud Storage bucket to upload to | |
20 BUCKET_NAME = "chrome-partner-telemetry" | |
erikchen
2015/07/14 23:36:15
no need for this variable
sydli
2015/07/15 22:56:24
Done.
| |
21 DEFAULT_REMOTE_DIR = "extension_set" | |
22 | |
23 def DownloadCRXFromCWS(ext_id, dst): | |
24 """ Download CRX (Chrome extension file) specified by extension_id from | |
erikchen
2015/07/14 23:36:15
Please see python style guide for formatting of th
sydli
2015/07/15 22:56:24
Done.
| |
25 Chrome Web Store, into file specified by dst. """ | |
26 dst_path = os.path.join(dst, "%s.crx" % ext_id) | |
27 cws_url = ("https://clients2.google.com/service/update2/crx?response=" | |
28 "redirect&prodversion=38.0&x=id%%3D%s%%26installsource%%3D" | |
29 "ondemand%%26uc" % ext_id) | |
30 response = urllib2.urlopen(cws_url) | |
31 if response.getcode() is not 200: | |
32 return None | |
erikchen
2015/07/14 23:36:15
This seems like a silent failure. I would throw an
sydli
2015/07/15 22:56:24
Documented return value and will handle in caller
| |
33 with open(dst_path, "w") as f: | |
34 f.write(response.read()) | |
35 return dst_path | |
36 | |
37 def UpdateExtensionsInCloud(local_extensions_dir, extensions_csv, remote_dir): | |
38 """ From well-formatted CSV file containing some set of extensions | |
39 (extensions_csv), download them into local temp directory and update | |
40 the remote directory. """ | |
41 # Download CRX to temp files | |
42 with open(extensions_csv, 'rb') as csvfile: | |
43 reader = csv.reader(csvfile) | |
44 reader.next() # skip header line | |
45 for row in reader: | |
46 extension_id = row[1] | |
47 print "Fetching extension %s..." % extension_id | |
48 DownloadCRXFromCWS(extension_id, local_extensions_dir) | |
49 # Delete remote directory if it exists | |
erikchen
2015/07/14 23:36:15
You are deleting, and then uploading. This set of
sydli
2015/07/15 22:56:24
Oh, my bad; there's also toctou problem. Will arch
| |
50 if cloud_storage.Exists(BUCKET_NAME, remote_dir): | |
51 cloud_storage.Delete(BUCKET_NAME, remote_dir, recursive=True) | |
52 print "Uploading extensions to cloud..." | |
53 cloud_storage.Insert(BUCKET_NAME, remote_dir, | |
54 local_extensions_dir, recursive=True) | |
55 | |
56 def GetCSVFromArgs(): | |
57 parser = optparse.OptionParser() | |
58 parser.add_option("-e", "--extension-csv", dest="extension_csv", | |
59 help="CSV of extensions to load.") | |
60 (options, _) = parser.parse_args() | |
61 if not options.extension_csv: | |
62 parser.error("Must specify --extension-csv option.") | |
63 return options.extension_csv | |
64 | |
65 def main(): | |
66 # TODO(sydli) check integrity of CSV | |
67 extension_csv = GetCSVFromArgs() | |
68 local_extensions_dir = tempfile.mkdtemp() | |
69 UpdateExtensionsInCloud(local_extensions_dir, | |
70 extension_csv, DEFAULT_REMOTE_DIR) | |
71 # Clean up | |
72 shutil.rmtree(local_extensions_dir) | |
erikchen
2015/07/14 23:36:15
You probably want to wrap UpdateExtensionsInCloud(
sydli
2015/07/15 22:56:24
Done.
| |
73 | |
74 if __name__=="__main__": | |
75 main() | |
76 | |
OLD | NEW |