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 json | |
6 import os | |
7 import platform | |
8 import zipfile | |
9 | |
10 from catapult_base import cloud_storage | |
11 from profile_creators import fast_navigation_profile_extender | |
12 import page_sets | |
13 | |
14 from telemetry.core import exceptions | |
robliao
2015/07/16 18:33:24
This import looks like it comes from us. This shou
sydli
2015/07/16 20:01:29
Done.
| |
15 | |
16 # Remote directory to download extensions from in cloud storage. | |
17 REMOTE_DIR = "extension_set" | |
18 # Name of zip archive to download. | |
robliao
2015/07/16 18:33:25
Nit: Add a linebreak above here.
sydli
2015/07/16 20:01:30
Done.
| |
19 ZIP_NAME = "extensions.zip" | |
20 | |
21 | |
22 class InvalidExtensionArchiveError(exceptions.Error): | |
23 """ Exception thrown when remote archive is invalid or malformed. | |
robliao
2015/07/16 18:33:24
Remove leading whitespace in docstring.
sydli
2015/07/16 20:01:30
Done.
| |
24 | |
25 Remote archive should be located at REMOTE_DIR/ZIP_NAME. Upon failure, | |
26 Prompts user to update remote archive using update_remote_extensions | |
robliao
2015/07/16 18:33:24
Nit: Lowercase prompts.
sydli
2015/07/16 20:01:29
Done.
| |
27 script. """ | |
robliao
2015/07/16 18:33:24
The """ should go on the next line for multiline d
sydli
2015/07/16 20:01:30
Done.
| |
28 | |
29 def __init__(self, msg=''): | |
30 msg += "\nTry running" + \ | |
robliao
2015/07/16 18:33:24
Use single-quoted strings and ()'s to span the exp
sydli
2015/07/16 20:01:29
Done.
| |
31 "\n\tpython update_remote_extensions.py -e extension_set.csv\n" + \ | |
32 "in src/tools/perf/profile_creator subdirectory." | |
33 super(InvalidExtensionArchiveError, self).__init__(msg) | |
34 | |
35 | |
36 class ExtensionProfileExtender( | |
37 fast_navigation_profile_extender.FastNavigationProfileExtender): | |
38 """Creates a profile with many extensions. """ | |
robliao
2015/07/16 18:33:24
Removing trailing whitespace in docstring.
sydli
2015/07/16 20:01:29
Done.
| |
39 | |
40 def __init__(self, finder_options): | |
41 maximum_batch_size = 1 | |
42 super(ExtensionProfileExtender, self).__init__(finder_options, | |
43 maximum_batch_size) | |
44 self._page_set = page_sets.BlankPageSet() | |
45 urls = [] | |
robliao
2015/07/16 18:33:24
This can be
urls = [story.url for story in self._p
sydli
2015/07/16 20:01:30
Done.
| |
46 for story in self._page_set.stories: | |
47 urls.append(story.url) | |
48 self._navigation_urls = urls | |
49 finder_options.browser_options.disable_default_apps = False | |
50 | |
51 def _DownloadRemoteExtensions(self, remote_bucket, local_extensions_dir): | |
52 """ Downloads archive of common extensions to disk and unzips. | |
robliao
2015/07/16 18:33:24
Remove leading whitespace.
Maybe Downloads and unz
sydli
2015/07/16 20:01:30
Done.
| |
53 | |
54 Args: | |
55 remote_bucket: bucket to download remote archive from. | |
56 local_extensions_dir: directory to unzip archive into. | |
robliao
2015/07/16 18:33:24
This seems to be more about creating an extensions
sydli
2015/07/16 20:01:30
Done.
| |
57 | |
58 Raises: | |
59 InvalidExtensionArchiveError if cannot find remote archive. | |
robliao
2015/07/16 18:33:24
InvalidExtensionArchiveError if the remote archive
sydli
2015/07/16 20:01:29
Done.
| |
60 """ | |
61 remote_zip_path = os.path.join(REMOTE_DIR, ZIP_NAME) | |
62 local_zip_path = os.path.join(local_extensions_dir, ZIP_NAME) | |
63 try: | |
64 cloud_storage.Get(remote_bucket, remote_zip_path, | |
65 local_zip_path) | |
robliao
2015/07/16 18:33:25
This looks like it should fit on the previous line
sydli
2015/07/16 20:01:30
Done.
| |
66 except: | |
67 raise InvalidExtensionArchiveError("Can't find archive at gs://%s/%s." | |
robliao
2015/07/16 18:33:24
Change the quotes here to single quotes.
sydli
2015/07/16 20:01:30
Done.
| |
68 % (remote_bucket, remote_zip_path)) | |
69 with zipfile.ZipFile(local_zip_path, "r") as extensions_zip: | |
robliao
2015/07/16 18:33:24
Use single quoted 'r'
sydli
2015/07/16 20:01:29
Done.
| |
70 extensions_zip.extractall(local_extensions_dir) | |
71 os.remove(local_zip_path) | |
72 | |
73 def _GetExtensionInfoFromCRX(self, crxfile): | |
74 """ Retrieves version + name of extension from CRX archive. """ | |
robliao
2015/07/16 18:33:24
Remove surrounding whitespace in docstring.
sydli
2015/07/16 20:01:30
Done.
| |
75 crx_zip = zipfile.ZipFile(crxfile) | |
76 manifest_contents = crx_zip.read('manifest.json') | |
77 decoded_manifest = json.loads(manifest_contents) | |
78 crx_version = decoded_manifest['version'] | |
79 extension_name = decoded_manifest['name'] | |
80 return (crx_version, extension_name) | |
81 | |
82 def _LoadExtensions(self, local_extensions_dir, profile_dir): | |
83 """ Loads extensions in _local_extensions_dir into user profile. | |
robliao
2015/07/16 18:33:25
Remove leading whitespace.
sydli
2015/07/16 20:01:30
Done.
| |
84 | |
85 Extensions are loaded according to platform specifications at | |
86 https://developer.chrome.com/extensions/external_extensions.html . | |
robliao
2015/07/16 18:33:24
Optional: Arguably, you could omit the period at t
sydli
2015/07/16 20:01:29
Done.
| |
87 | |
88 Args: | |
89 local_extensions_dir: directory containing *CRX files. | |
robliao
2015/07/16 18:33:24
Nit: *.crx or just CRX files.
sydli
2015/07/16 20:01:30
Done.
| |
90 profile_dir: user profile directory to load extensions into. | |
robliao
2015/07/16 18:33:24
Target profile directory for the extensions.
sydli
2015/07/16 20:01:29
Done.
| |
91 | |
92 Raises: | |
93 InvalidExtensionArchiveError if archive contains a non-CRX file. | |
94 """ | |
95 ext_files = os.listdir(local_extensions_dir) | |
96 external_ext_dir = os.path.join(profile_dir, "External Extensions") | |
robliao
2015/07/16 18:33:24
Use single quotes if possible, from here and forwa
sydli
2015/07/16 20:01:29
Yep! Done.
| |
97 os.makedirs(external_ext_dir) | |
98 for ext_file in ext_files: | |
99 ext_path = os.path.join(local_extensions_dir, ext_file) | |
100 if not ext_file.endswith(".crx"): | |
101 raise InvalidExtensionArchiveError("Archive contains non-crx file %s." | |
102 % ext_file) | |
103 (version, name) = self._GetExtensionInfoFromCRX(ext_path) | |
104 extension_info = { | |
105 'external_crx': ext_path, | |
robliao
2015/07/16 18:33:24
Two spaces for indentation here.
sydli
2015/07/16 20:01:30
Done.
robliao
2015/07/16 21:10:04
I should have been clearer here. This line should
sydli
2015/07/17 01:00:28
Done.
| |
106 'external_version': version, | |
107 '_comment': name | |
108 } | |
109 ext_id = os.path.splitext(os.path.basename(ext_path))[0] | |
110 extension_json_path = os.path.join(external_ext_dir, "%s.json" % ext_id) | |
111 with open(extension_json_path, 'w') as f: | |
112 f.write(json.dumps(extension_info)) | |
113 | |
114 def GetUrlIterator(self): | |
115 """Superclass override.""" | |
116 return iter(self._navigation_urls) | |
117 | |
118 def ShouldExitAfterBatchNavigation(self): | |
119 """Superclass override.""" | |
120 return False | |
121 | |
122 def Run(self): | |
123 # DL extensions from cloud & force-install extensions into profile. | |
124 if platform.system() != 'Darwin': | |
125 raise NotImplementedError('Extension profile generator on %s is not yet ' | |
robliao
2015/07/16 18:33:24
Nit optional: If this string works all on the next
sydli
2015/07/16 20:01:29
Done.
| |
126 'supported' % platform.system()) | |
127 local_extensions_dir = os.path.join(self.profile_path, | |
128 "external_extensions_crx") | |
129 self._DownloadRemoteExtensions(cloud_storage.PARTNER_BUCKET, | |
130 local_extensions_dir) | |
131 self._LoadExtensions(local_extensions_dir, self.profile_path) | |
132 super(ExtensionProfileExtender, self).Run() | |
133 | |
OLD | NEW |