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 json | |
6 import os | |
7 import platform | |
8 import zipfile | |
9 | |
10 from catapult_base import cloud_storage | |
11 import page_sets | |
12 from profile_creators import fast_navigation_profile_extender | |
13 from telemetry.core import exceptions | |
14 | |
15 | |
16 # Remote target upload directory in cloud storage for extensions. | |
17 REMOTE_DIR = 'extension_set' | |
18 | |
19 # Target zip file. | |
20 ZIP_NAME = 'extensions.zip' | |
21 | |
22 | |
23 class InvalidExtensionArchiveError(exceptions.Error): | |
24 """Exception thrown when remote archive is invalid or malformed. | |
25 | |
26 Remote archive should be located at REMOTE_DIR/ZIP_NAME. Upon failure, | |
27 prompts user to update remote archive using update_remote_extensions | |
28 script. | |
29 """ | |
30 | |
31 def __init__(self, msg=''): | |
32 msg += ('\nTry running\n' | |
33 '\tpython update_remote_extensions.py -e extension_set.csv\n' | |
34 'in src/tools/perf/profile_creator subdirectory.') | |
35 super(InvalidExtensionArchiveError, self).__init__(msg) | |
36 | |
37 | |
38 class ExtensionProfileExtender( | |
39 fast_navigation_profile_extender.FastNavigationProfileExtender): | |
40 """Creates a profile with many extensions.""" | |
41 | |
42 def __init__(self, finder_options): | |
43 super(ExtensionProfileExtender, self).__init__(finder_options, | |
44 maximum_batch_size=1) | |
45 self._page_set = page_sets.BlankPageSet() | |
46 urls = [story.url for story in self._page_set.stories] | |
47 self._navigation_urls = urls | |
48 finder_options.browser_options.disable_default_apps = False | |
49 | |
50 def _DownloadRemoteExtensions(self, remote_bucket, local_extensions_dir): | |
51 """Downloads and unzips archive of common extensions to disk. | |
52 | |
53 Args: | |
54 remote_bucket: bucket to download remote archive from. | |
55 local_extensions_dir: destination extensions directory. | |
56 | |
57 Raises: | |
58 InvalidExtensionArchiveError if remote archive is not found. | |
59 """ | |
60 remote_zip_path = os.path.join(REMOTE_DIR, ZIP_NAME) | |
61 local_zip_path = os.path.join(local_extensions_dir, ZIP_NAME) | |
62 try: | |
63 cloud_storage.Get(remote_bucket, remote_zip_path, local_zip_path) | |
64 except: | |
65 raise InvalidExtensionArchiveError('Can\'t find archive at gs://%s/%s..' | |
66 % (remote_bucket, remote_zip_path)) | |
67 try: | |
68 with zipfile.ZipFile(local_zip_path, 'r') as extensions_zip: | |
69 extensions_zip.extractall(local_extensions_dir) | |
70 finally: | |
71 os.remove(local_zip_path) | |
72 | |
73 def _GetExtensionInfoFromCrx(self, crxfile): | |
74 """Retrieves version + name of extension from CRX archive.""" | |
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. | |
84 | |
85 Extensions are loaded according to platform specifications at | |
86 https://developer.chrome.com/extensions/external_extensions.html | |
87 | |
88 Args: | |
89 local_extensions_dir: directory containing CRX files. | |
90 profile_dir: target profile directory for the extensions. | |
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') | |
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, | |
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 # Download extensions from cloud & force-install extensions into profile. | |
124 if platform.system() != 'Darwin': | |
nednguyen
2015/07/17 21:48:07
This part is not correct, the platform telemetry i
sydli
2015/07/17 22:29:27
Would this be the correct query?
https://code.goog
nednguyen
2015/07/17 22:49:08
Yeah, but you need to get the pointer to platform.
erikchen
2015/07/17 22:50:33
Yeah, you want to use the platform of the possible
sydli
2015/07/20 21:33:41
done; additions to profile extender to restrict th
| |
125 raise NotImplementedError( | |
126 'Extension profile generator on %s is not yet supported' | |
127 % platform.system()) | |
128 local_extensions_dir = os.path.join(self.profile_path, | |
129 'external_extensions_crx') | |
130 self._DownloadRemoteExtensions(cloud_storage.PARTNER_BUCKET, | |
131 local_extensions_dir) | |
132 self._LoadExtensions(local_extensions_dir, self.profile_path) | |
133 super(ExtensionProfileExtender, self).Run() | |
134 | |
OLD | NEW |