OLD | NEW |
1 # Copyright 2012 The Chromium Authors. All rights reserved. | 1 # Copyright 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 os | 7 import os |
8 | 8 |
9 from telemetry.page import page as page_module | 9 from telemetry.page import page as page_module |
10 from telemetry.user_story import user_story_set | 10 from telemetry.user_story import user_story_set |
11 from telemetry.util import cloud_storage | 11 from telemetry.util import cloud_storage |
12 | 12 |
13 PUBLIC_BUCKET = cloud_storage.PUBLIC_BUCKET | 13 PUBLIC_BUCKET = cloud_storage.PUBLIC_BUCKET |
14 PARTNER_BUCKET = cloud_storage.PARTNER_BUCKET | 14 PARTNER_BUCKET = cloud_storage.PARTNER_BUCKET |
15 INTERNAL_BUCKET = cloud_storage.INTERNAL_BUCKET | 15 INTERNAL_BUCKET = cloud_storage.INTERNAL_BUCKET |
16 | 16 |
17 | 17 |
18 class PageSetError(Exception): | 18 class PageSetError(Exception): |
19 pass | 19 pass |
20 | 20 |
21 | 21 |
22 class PageSet(user_story_set.UserStorySet): | 22 class PageSet(user_story_set.UserStorySet): |
23 def __init__(self, file_path=None, archive_data_file='', user_agent_type=None, | 23 def __init__(self, file_path=None, archive_data_file='', user_agent_type=None, |
24 make_javascript_deterministic=True, serving_dirs=None, | 24 make_javascript_deterministic=True, serving_dirs=None, |
25 bucket=None): | 25 bucket=None): |
26 super(PageSet, self).__init__( | |
27 archive_data_file=archive_data_file, cloud_storage_bucket=bucket) | |
28 # The default value of file_path is location of the file that define this | 26 # The default value of file_path is location of the file that define this |
29 # page set instance's class. | 27 # page set instance's class. |
30 # TODO(chrishenry): Move this logic to user_story_set. Consider passing | 28 # TODO(chrishenry): Move this logic to user_story_set. Consider passing |
31 # a base_dir directly. Alternatively, kill this and rely on the default | 29 # a base_dir directly. Alternatively, kill this and rely on the default |
32 # behavior of using the instance's class file location. | 30 # behavior of using the instance's class file location. |
33 if file_path is None: | 31 if file_path is None: |
34 file_path = inspect.getfile(self.__class__) | 32 file_path = inspect.getfile(self.__class__) |
35 # Turn pyc file into py files if we can | 33 # Turn pyc file into py files if we can |
36 if file_path.endswith('.pyc') and os.path.exists(file_path[:-1]): | 34 if file_path.endswith('.pyc') and os.path.exists(file_path[:-1]): |
37 file_path = file_path[:-1] | 35 file_path = file_path[:-1] |
| 36 self.file_path = file_path |
38 | 37 |
39 self.file_path = file_path | 38 super(PageSet, self).__init__( |
| 39 archive_data_file=archive_data_file, cloud_storage_bucket=bucket, |
| 40 serving_dirs=serving_dirs) |
| 41 |
40 # These attributes can be set dynamically by the page set. | 42 # These attributes can be set dynamically by the page set. |
41 self.user_agent_type = user_agent_type | 43 self.user_agent_type = user_agent_type |
42 self.make_javascript_deterministic = make_javascript_deterministic | 44 self.make_javascript_deterministic = make_javascript_deterministic |
43 # Convert any relative serving_dirs to absolute paths. | |
44 self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d)) | |
45 for d in serving_dirs or []) | |
46 | 45 |
47 @property | 46 @property |
48 def pages(self): | 47 def pages(self): |
49 return self.user_stories | 48 return self.user_stories |
50 | 49 |
51 def AddUserStory(self, user_story): | 50 def AddUserStory(self, user_story): |
52 assert isinstance(user_story, page_module.Page) | 51 assert isinstance(user_story, page_module.Page) |
53 assert user_story.page_set is self | 52 assert user_story.page_set is self |
54 super(PageSet, self).AddUserStory(user_story) | 53 super(PageSet, self).AddUserStory(user_story) |
55 | 54 |
56 def AddPage(self, page): | 55 def AddPage(self, page): |
57 self.AddUserStory(page) | 56 self.AddUserStory(page) |
58 | 57 |
59 def AddPageWithDefaultRunNavigate(self, page_url): | 58 def AddPageWithDefaultRunNavigate(self, page_url): |
60 """ Add a simple page with url equals to page_url that contains only default | 59 """ Add a simple page with url equals to page_url that contains only default |
61 RunNavigateSteps. | 60 RunNavigateSteps. |
62 """ | 61 """ |
63 self.AddUserStory(page_module.Page( | 62 self.AddUserStory(page_module.Page( |
64 page_url, self, self.base_dir)) | 63 page_url, self, self.base_dir)) |
65 | 64 |
66 @property | 65 @property |
67 def base_dir(self): | 66 def base_dir(self): |
68 if os.path.isfile(self.file_path): | 67 if os.path.isfile(self.file_path): |
69 return os.path.dirname(self.file_path) | 68 return os.path.dirname(self.file_path) |
70 else: | 69 else: |
71 return self.file_path | 70 return self.file_path |
72 | 71 |
73 @property | |
74 def serving_dirs(self): | |
75 return self._serving_dirs | |
76 | |
77 def ReorderPageSet(self, results_file): | 72 def ReorderPageSet(self, results_file): |
78 """Reorders this page set based on the results of a past run.""" | 73 """Reorders this page set based on the results of a past run.""" |
79 page_set_dict = {} | 74 page_set_dict = {} |
80 for page in self.user_stories: | 75 for page in self.user_stories: |
81 page_set_dict[page.url] = page | 76 page_set_dict[page.url] = page |
82 | 77 |
83 user_stories = [] | 78 user_stories = [] |
84 with open(results_file, 'rb') as csv_file: | 79 with open(results_file, 'rb') as csv_file: |
85 csv_reader = csv.reader(csv_file) | 80 csv_reader = csv.reader(csv_file) |
86 csv_header = csv_reader.next() | 81 csv_header = csv_reader.next() |
87 | 82 |
88 if 'url' not in csv_header: | 83 if 'url' not in csv_header: |
89 raise Exception('Unusable results_file.') | 84 raise Exception('Unusable results_file.') |
90 | 85 |
91 url_index = csv_header.index('url') | 86 url_index = csv_header.index('url') |
92 | 87 |
93 for csv_row in csv_reader: | 88 for csv_row in csv_reader: |
94 if csv_row[url_index] in page_set_dict: | 89 if csv_row[url_index] in page_set_dict: |
95 self.AddPage(page_set_dict[csv_row[url_index]]) | 90 self.AddPage(page_set_dict[csv_row[url_index]]) |
96 else: | 91 else: |
97 raise Exception('Unusable results_file.') | 92 raise Exception('Unusable results_file.') |
98 | 93 |
99 return user_stories | 94 return user_stories |
OLD | NEW |