| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import csv | 5 import csv |
| 6 import inspect | 6 import inspect |
| 7 import logging | |
| 8 import os | 7 import os |
| 9 import sys | 8 import sys |
| 10 | 9 |
| 11 from telemetry.core import util | 10 from telemetry.core import util |
| 12 from telemetry.page import cloud_storage | |
| 13 from telemetry.page import page as page_module | 11 from telemetry.page import page as page_module |
| 14 from telemetry.page import page_set_archive_info | 12 from telemetry.page import page_set_archive_info |
| 15 from telemetry.page.actions.navigate import NavigateAction | 13 from telemetry.page.actions.navigate import NavigateAction |
| 16 | 14 |
| 17 # TODO(nednguyen): Remove this when crbug.com/239179 is marked fixed | 15 # TODO(nednguyen): Remove this when crbug.com/239179 is marked fixed |
| 18 LEGACY_NAME_CONVERSION_DICT = { | 16 LEGACY_NAME_CONVERSION_DICT = { |
| 19 'endure' : 'RunEndure', | 17 'endure' : 'RunEndure', |
| 20 'navigate_steps' : 'RunNavigateSteps', | 18 'navigate_steps' : 'RunNavigateSteps', |
| 21 'media_metrics' : 'RunMediaMetrics', | 19 'media_metrics' : 'RunMediaMetrics', |
| 22 'stress_memory' : 'RunStressMemory', | 20 'stress_memory' : 'RunStressMemory', |
| 23 'no_op' : 'RunNoOp', | 21 'no_op' : 'RunNoOp', |
| 24 'repaint' : 'RunRepaint', | 22 'repaint' : 'RunRepaint', |
| 25 'smoothness' : 'RunSmoothness', | 23 'smoothness' : 'RunSmoothness', |
| 26 'webrtc' : 'RunWebrtc' | 24 'webrtc' : 'RunWebrtc' |
| 27 } | 25 } |
| 28 | 26 |
| 29 | 27 |
| 30 class PageSetError(Exception): | 28 class PageSetError(Exception): |
| 31 pass | 29 pass |
| 32 | 30 |
| 33 | 31 |
| 34 class PageSet(object): | 32 class PageSet(object): |
| 35 def __init__(self, file_path='', description='', archive_data_file='', | 33 def __init__(self, file_path='', description='', archive_data_file='', |
| 36 credentials_path=None, user_agent_type=None, | 34 credentials_path=None, user_agent_type=None, |
| 37 make_javascript_deterministic=True, startup_url='', pages=None, | 35 make_javascript_deterministic=True, startup_url='', |
| 38 serving_dirs=None): | 36 serving_dirs=None): |
| 39 self.file_path = file_path | 37 self.file_path = file_path |
| 40 # These attributes can be set dynamically by the page set. | 38 # These attributes can be set dynamically by the page set. |
| 41 self.description = description | 39 self.description = description |
| 42 self.archive_data_file = archive_data_file | 40 self.archive_data_file = archive_data_file |
| 43 self.credentials_path = credentials_path | 41 self.credentials_path = credentials_path |
| 44 self.user_agent_type = user_agent_type | 42 self.user_agent_type = user_agent_type |
| 45 self.make_javascript_deterministic = make_javascript_deterministic | 43 self.make_javascript_deterministic = make_javascript_deterministic |
| 46 self.wpr_archive_info = None | 44 self._wpr_archive_info = None |
| 47 self.startup_url = startup_url | 45 self.startup_url = startup_url |
| 48 if pages: | 46 self.pages = [] |
| 49 self.pages = pages | |
| 50 else: | |
| 51 self.pages = [] | |
| 52 if serving_dirs: | 47 if serving_dirs: |
| 53 self.serving_dirs = serving_dirs | 48 self.serving_dirs = serving_dirs |
| 54 else: | 49 else: |
| 55 self.serving_dirs = set() | 50 self.serving_dirs = set() |
| 56 | 51 |
| 57 def _InitializeFromDict(self, attributes, ignore_archive=False): | 52 def _InitializeFromDict(self, attributes): |
| 58 if attributes: | 53 if attributes: |
| 59 for k, v in attributes.iteritems(): | 54 for k, v in attributes.iteritems(): |
| 60 if k in LEGACY_NAME_CONVERSION_DICT: | 55 if k in LEGACY_NAME_CONVERSION_DICT: |
| 61 setattr(self, LEGACY_NAME_CONVERSION_DICT[k], v) | 56 setattr(self, LEGACY_NAME_CONVERSION_DICT[k], v) |
| 62 else: | 57 else: |
| 63 setattr(self, k, v) | 58 setattr(self, k, v) |
| 64 | 59 |
| 65 # Create a Page object for every page. | 60 # Create a Page object for every page. |
| 66 self.pages = [] | 61 self.pages = [] |
| 67 if attributes and 'pages' in attributes: | 62 if attributes and 'pages' in attributes: |
| 68 for page_attributes in attributes['pages']: | 63 for page_attributes in attributes['pages']: |
| 69 url = page_attributes.pop('url') | 64 url = page_attributes.pop('url') |
| 70 page = page_module.Page( | 65 page = page_module.Page( |
| 71 url, self, base_dir=self._base_dir) | 66 url, self, base_dir=self.base_dir) |
| 72 for k, v in page_attributes.iteritems(): | 67 for k, v in page_attributes.iteritems(): |
| 73 setattr(page, k, v) | 68 setattr(page, k, v) |
| 74 page._SchemeErrorCheck() # pylint: disable=W0212 | 69 page._SchemeErrorCheck() # pylint: disable=W0212 |
| 75 for legacy_name in LEGACY_NAME_CONVERSION_DICT: | 70 for legacy_name in LEGACY_NAME_CONVERSION_DICT: |
| 76 if hasattr(page, legacy_name): | 71 if hasattr(page, legacy_name): |
| 77 setattr(page, LEGACY_NAME_CONVERSION_DICT[legacy_name], | 72 setattr(page, LEGACY_NAME_CONVERSION_DICT[legacy_name], |
| 78 getattr(page, legacy_name)) | 73 getattr(page, legacy_name)) |
| 79 delattr(page, legacy_name) | 74 delattr(page, legacy_name) |
| 80 self.AddPage(page) | 75 self.AddPage(page) |
| 81 | 76 |
| 82 # Prepend _base_dir to our serving dirs. | 77 # Prepend base_dir to our serving dirs. |
| 83 # Always use realpath to ensure no duplicates in set. | 78 # Always use realpath to ensure no duplicates in set. |
| 84 self.serving_dirs = set() | 79 self.serving_dirs = set() |
| 85 if attributes and 'serving_dirs' in attributes: | 80 if attributes and 'serving_dirs' in attributes: |
| 86 if not isinstance(attributes['serving_dirs'], list): | 81 if not isinstance(attributes['serving_dirs'], list): |
| 87 raise ValueError('serving_dirs must be a list.') | 82 raise ValueError('serving_dirs must be a list.') |
| 88 for serving_dir in attributes['serving_dirs']: | 83 for serving_dir in attributes['serving_dirs']: |
| 89 self.serving_dirs.add( | 84 self.serving_dirs.add( |
| 90 os.path.realpath(os.path.join(self._base_dir, serving_dir))) | 85 os.path.realpath(os.path.join(self.base_dir, serving_dir))) |
| 91 if not ignore_archive: | |
| 92 self._InitializeArchive() | |
| 93 | |
| 94 def _InitializeArchive(self): | |
| 95 # Create a PageSetArchiveInfo object. | |
| 96 if self.archive_data_file: | |
| 97 self.wpr_archive_info = page_set_archive_info.PageSetArchiveInfo.FromFile( | |
| 98 os.path.join(self._base_dir, self.archive_data_file)) | |
| 99 | |
| 100 # Attempt to download the credentials file. | |
| 101 if self.credentials_path: | |
| 102 try: | |
| 103 cloud_storage.GetIfChanged( | |
| 104 os.path.join(self._base_dir, self.credentials_path)) | |
| 105 except (cloud_storage.CredentialsError, | |
| 106 cloud_storage.PermissionError): | |
| 107 logging.warning('Cannot retrieve credential file: %s', | |
| 108 self.credentials_path) | |
| 109 | |
| 110 # Scan every serving directory for .sha1 files | |
| 111 # and download them from Cloud Storage. Assume all data is public. | |
| 112 all_serving_dirs = self.serving_dirs.copy() | |
| 113 # Add individual page dirs to all serving dirs. | |
| 114 for page in self: | |
| 115 if page.is_file: | |
| 116 all_serving_dirs.add(page.serving_dir) | |
| 117 # Scan all serving dirs. | |
| 118 for serving_dir in all_serving_dirs: | |
| 119 if os.path.splitdrive(serving_dir)[1] == '/': | |
| 120 raise ValueError('Trying to serve root directory from HTTP server.') | |
| 121 for dirpath, _, filenames in os.walk(serving_dir): | |
| 122 for filename in filenames: | |
| 123 path, extension = os.path.splitext( | |
| 124 os.path.join(dirpath, filename)) | |
| 125 if extension != '.sha1': | |
| 126 continue | |
| 127 cloud_storage.GetIfChanged(path) | |
| 128 | 86 |
| 129 def AddPage(self, page): | 87 def AddPage(self, page): |
| 130 assert page.page_set is self | 88 assert page.page_set is self |
| 131 self.pages.append(page) | 89 self.pages.append(page) |
| 132 | 90 |
| 91 def AddPageWithDefaultRunNavigate(self, page_url): |
| 92 """ Add a simple page with url equals to page_url that contains only default |
| 93 RunNavigateSteps. |
| 94 """ |
| 95 self.AddPage(page_module.PageWithDefaultRunNavigate( |
| 96 page_url, self, self.base_dir)) |
| 97 |
| 133 # In json page_set, a page inherits attributes from its page_set. With | 98 # In json page_set, a page inherits attributes from its page_set. With |
| 134 # python page_set, this property will no longer be needed since pages can | 99 # python page_set, this property will no longer be needed since pages can |
| 135 # share property through a common ancestor class. | 100 # share property through a common ancestor class. |
| 136 # TODO(nednguyen): move this to page when crbug.com/239179 is marked fixed | 101 # TODO(nednguyen): move this to page when crbug.com/239179 is marked fixed |
| 137 def RunNavigateSteps(self, action_runner): | 102 def RunNavigateSteps(self, action_runner): |
| 138 action_runner.RunAction(NavigateAction()) | 103 action_runner.RunAction(NavigateAction()) |
| 139 | 104 |
| 140 @staticmethod | 105 @staticmethod |
| 141 def FromFile(file_path, ignore_archive=False): | 106 def FromFile(file_path): |
| 142 _, ext_name = os.path.splitext(file_path) | 107 _, ext_name = os.path.splitext(file_path) |
| 143 if ext_name == '.py': | 108 if ext_name == '.py': |
| 144 return PageSet.FromPythonFile(file_path, ignore_archive) | 109 return PageSet.FromPythonFile(file_path) |
| 145 else: | 110 else: |
| 146 raise PageSetError("Pageset %s has unsupported file type" % file_path) | 111 raise PageSetError("Pageset %s has unsupported file type" % file_path) |
| 147 | 112 |
| 148 @staticmethod | 113 @staticmethod |
| 149 def FromPythonFile(file_path, ignore_archive=False): | 114 def FromPythonFile(file_path): |
| 150 page_set_classes = [] | 115 page_set_classes = [] |
| 151 module = util.GetPythonPageSetModule(file_path) | 116 module = util.GetPythonPageSetModule(file_path) |
| 152 for m in dir(module): | 117 for m in dir(module): |
| 153 if m.endswith('PageSet') and m != 'PageSet': | 118 if m.endswith('PageSet') and m != 'PageSet': |
| 154 page_set_classes.append(getattr(module, m)) | 119 page_set_classes.append(getattr(module, m)) |
| 155 if len(page_set_classes) != 1: | 120 if len(page_set_classes) != 1: |
| 156 raise PageSetError("Pageset file needs to contain exactly 1 pageset class" | 121 raise PageSetError("Pageset file needs to contain exactly 1 pageset class" |
| 157 " with prefix 'PageSet'") | 122 " with prefix 'PageSet'") |
| 158 page_set = page_set_classes[0]() | 123 page_set = page_set_classes[0]() |
| 159 page_set.file_path = file_path | 124 page_set.file_path = file_path |
| 160 # Makes sure that page_set's serving_dirs are absolute paths | 125 # Makes sure that page_set's serving_dirs are absolute paths |
| 161 if page_set.serving_dirs: | 126 if page_set.serving_dirs: |
| 162 abs_serving_dirs = set() | 127 abs_serving_dirs = set() |
| 163 for serving_dir in page_set.serving_dirs: | 128 for serving_dir in page_set.serving_dirs: |
| 164 abs_serving_dirs.add(os.path.realpath(os.path.join( | 129 abs_serving_dirs.add(os.path.realpath(os.path.join( |
| 165 page_set._base_dir, # pylint: disable=W0212 | 130 page_set.base_dir, # pylint: disable=W0212 |
| 166 serving_dir))) | 131 serving_dir))) |
| 167 page_set.serving_dirs = abs_serving_dirs | 132 page_set.serving_dirs = abs_serving_dirs |
| 168 for page in page_set.pages: | 133 for page in page_set.pages: |
| 169 page_class = page.__class__ | 134 page_class = page.__class__ |
| 170 | 135 |
| 171 for method_name, method in inspect.getmembers(page_class, | 136 for method_name, method in inspect.getmembers(page_class, |
| 172 predicate=inspect.ismethod): | 137 predicate=inspect.ismethod): |
| 173 if method_name.startswith("Run"): | 138 if method_name.startswith("Run"): |
| 174 args, _, _, _ = inspect.getargspec(method) | 139 args, _, _, _ = inspect.getargspec(method) |
| 175 if not (args[0] == "self" and args[1] == "action_runner"): | 140 if not (args[0] == "self" and args[1] == "action_runner"): |
| 176 raise PageSetError("""Definition of Run<...> method of all | 141 raise PageSetError("""Definition of Run<...> method of all |
| 177 pages in %s must be in the form of def Run<...>(self, action_runner):""" | 142 pages in %s must be in the form of def Run<...>(self, action_runner):""" |
| 178 % file_path) | 143 % file_path) |
| 179 # Set page's _base_dir attribute. | 144 # Set page's base_dir attribute. |
| 180 page_file_path = sys.modules[page_class.__module__].__file__ | 145 page_file_path = sys.modules[page_class.__module__].__file__ |
| 181 page._base_dir = os.path.dirname(page_file_path) | 146 page._base_dir = os.path.dirname(page_file_path) |
| 182 | 147 |
| 183 if not ignore_archive: | |
| 184 page_set._InitializeArchive() # pylint: disable=W0212 | |
| 185 return page_set | 148 return page_set |
| 186 | 149 |
| 187 @staticmethod | 150 @staticmethod |
| 188 def FromDict(attributes, file_path='', ignore_archive=False): | 151 def FromDict(attributes, file_path=''): |
| 189 page_set = PageSet(file_path) | 152 page_set = PageSet(file_path) |
| 190 # pylint: disable=W0212 | 153 # pylint: disable=W0212 |
| 191 page_set._InitializeFromDict(attributes, ignore_archive) | 154 page_set._InitializeFromDict(attributes) |
| 192 return page_set | 155 return page_set |
| 193 | 156 |
| 194 @property | 157 @property |
| 195 def _base_dir(self): | 158 def base_dir(self): |
| 196 if os.path.isfile(self.file_path): | 159 if os.path.isfile(self.file_path): |
| 197 return os.path.dirname(self.file_path) | 160 return os.path.dirname(self.file_path) |
| 198 else: | 161 else: |
| 199 return self.file_path | 162 return self.file_path |
| 200 | 163 |
| 164 @property |
| 165 def wpr_archive_info(self): # pylint: disable=E0202 |
| 166 """Lazily constructs wpr_archive_info if it's not set and returns it.""" |
| 167 if self.archive_data_file and not self._wpr_archive_info: |
| 168 self._wpr_archive_info = ( |
| 169 page_set_archive_info.PageSetArchiveInfo.FromFile( |
| 170 os.path.join(self.base_dir, self.archive_data_file))) |
| 171 return self._wpr_archive_info |
| 172 |
| 173 @wpr_archive_info.setter |
| 174 def wpr_archive_info(self, value): # pylint: disable=E0202 |
| 175 self._wpr_archive_info = value |
| 176 |
| 201 def ContainsOnlyFileURLs(self): | 177 def ContainsOnlyFileURLs(self): |
| 202 for page in self.pages: | 178 for page in self.pages: |
| 203 if not page.is_file: | 179 if not page.is_file: |
| 204 return False | 180 return False |
| 205 return True | 181 return True |
| 206 | 182 |
| 207 def ReorderPageSet(self, results_file): | 183 def ReorderPageSet(self, results_file): |
| 208 """Reorders this page set based on the results of a past run.""" | 184 """Reorders this page set based on the results of a past run.""" |
| 209 page_set_dict = {} | 185 page_set_dict = {} |
| 210 for page in self.pages: | 186 for page in self.pages: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 237 return self.pages.__iter__() | 213 return self.pages.__iter__() |
| 238 | 214 |
| 239 def __len__(self): | 215 def __len__(self): |
| 240 return len(self.pages) | 216 return len(self.pages) |
| 241 | 217 |
| 242 def __getitem__(self, key): | 218 def __getitem__(self, key): |
| 243 return self.pages[key] | 219 return self.pages[key] |
| 244 | 220 |
| 245 def __setitem__(self, key, value): | 221 def __setitem__(self, key, value): |
| 246 self.pages[key] = value | 222 self.pages[key] = value |
| OLD | NEW |