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.page import page_set_archive_info | |
11 from telemetry.user_story import user_story_set | 10 from telemetry.user_story import user_story_set |
12 from telemetry.util import cloud_storage | 11 from telemetry.util import cloud_storage |
13 | 12 |
14 PUBLIC_BUCKET = cloud_storage.PUBLIC_BUCKET | 13 PUBLIC_BUCKET = cloud_storage.PUBLIC_BUCKET |
15 PARTNER_BUCKET = cloud_storage.PARTNER_BUCKET | 14 PARTNER_BUCKET = cloud_storage.PARTNER_BUCKET |
16 INTERNAL_BUCKET = cloud_storage.INTERNAL_BUCKET | 15 INTERNAL_BUCKET = cloud_storage.INTERNAL_BUCKET |
17 | 16 |
18 | 17 |
19 class PageSetError(Exception): | 18 class PageSetError(Exception): |
20 pass | 19 pass |
21 | 20 |
22 | 21 |
23 class PageSet(user_story_set.UserStorySet): | 22 class PageSet(user_story_set.UserStorySet): |
24 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, |
25 make_javascript_deterministic=True, serving_dirs=None, | 24 make_javascript_deterministic=True, serving_dirs=None, |
26 bucket=None): | 25 bucket=None): |
27 super(PageSet, self).__init__() | 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 | 28 # The default value of file_path is location of the file that define this |
29 # page set instance's class. | 29 # page set instance's class. |
| 30 # TODO(chrishenry): Move this logic to user_story_set. Consider passing |
| 31 # a base_dir directly. Alternatively, kill this and rely on the default |
| 32 # behavior of using the instance's class file location. |
30 if file_path is None: | 33 if file_path is None: |
31 file_path = inspect.getfile(self.__class__) | 34 file_path = inspect.getfile(self.__class__) |
32 # Turn pyc file into py files if we can | 35 # Turn pyc file into py files if we can |
33 if file_path.endswith('.pyc') and os.path.exists(file_path[:-1]): | 36 if file_path.endswith('.pyc') and os.path.exists(file_path[:-1]): |
34 file_path = file_path[:-1] | 37 file_path = file_path[:-1] |
35 | 38 |
36 self.file_path = file_path | 39 self.file_path = file_path |
37 # These attributes can be set dynamically by the page set. | 40 # These attributes can be set dynamically by the page set. |
38 self.archive_data_file = archive_data_file | |
39 self.user_agent_type = user_agent_type | 41 self.user_agent_type = user_agent_type |
40 self.make_javascript_deterministic = make_javascript_deterministic | 42 self.make_javascript_deterministic = make_javascript_deterministic |
41 self._wpr_archive_info = None | |
42 # Convert any relative serving_dirs to absolute paths. | 43 # Convert any relative serving_dirs to absolute paths. |
43 self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d)) | 44 self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d)) |
44 for d in serving_dirs or []) | 45 for d in serving_dirs or []) |
45 if self._IsValidPrivacyBucket(bucket): | |
46 self._bucket = bucket | |
47 else: | |
48 raise ValueError("Pageset privacy bucket %s is invalid" % bucket) | |
49 | 46 |
50 @property | 47 @property |
51 def pages(self): | 48 def pages(self): |
52 return self.user_stories | 49 return self.user_stories |
53 | 50 |
54 def AddUserStory(self, user_story): | 51 def AddUserStory(self, user_story): |
55 assert isinstance(user_story, page_module.Page) | 52 assert isinstance(user_story, page_module.Page) |
56 assert user_story.page_set is self | 53 assert user_story.page_set is self |
57 super(PageSet, self).AddUserStory(user_story) | 54 super(PageSet, self).AddUserStory(user_story) |
58 | 55 |
59 def AddPage(self, page): | 56 def AddPage(self, page): |
60 self.AddUserStory(page) | 57 self.AddUserStory(page) |
61 | 58 |
62 def AddPageWithDefaultRunNavigate(self, page_url): | 59 def AddPageWithDefaultRunNavigate(self, page_url): |
63 """ Add a simple page with url equals to page_url that contains only default | 60 """ Add a simple page with url equals to page_url that contains only default |
64 RunNavigateSteps. | 61 RunNavigateSteps. |
65 """ | 62 """ |
66 self.AddUserStory(page_module.Page( | 63 self.AddUserStory(page_module.Page( |
67 page_url, self, self.base_dir)) | 64 page_url, self, self.base_dir)) |
68 | 65 |
69 @staticmethod | |
70 def _IsValidPrivacyBucket(bucket_name): | |
71 return bucket_name in (None, PUBLIC_BUCKET, PARTNER_BUCKET, INTERNAL_BUCKET) | |
72 | |
73 @property | 66 @property |
74 def base_dir(self): | 67 def base_dir(self): |
75 if os.path.isfile(self.file_path): | 68 if os.path.isfile(self.file_path): |
76 return os.path.dirname(self.file_path) | 69 return os.path.dirname(self.file_path) |
77 else: | 70 else: |
78 return self.file_path | 71 return self.file_path |
79 | 72 |
80 @property | 73 @property |
81 def serving_dirs(self): | 74 def serving_dirs(self): |
82 return self._serving_dirs | 75 return self._serving_dirs |
83 | 76 |
84 @property | |
85 def wpr_archive_info(self): # pylint: disable=E0202 | |
86 """Lazily constructs wpr_archive_info if it's not set and returns it.""" | |
87 if self.archive_data_file and not self._wpr_archive_info: | |
88 self._wpr_archive_info = ( | |
89 page_set_archive_info.PageSetArchiveInfo.FromFile( | |
90 os.path.join(self.base_dir, self.archive_data_file), self._bucket)) | |
91 return self._wpr_archive_info | |
92 | |
93 @property | |
94 def bucket(self): | |
95 return self._bucket | |
96 | |
97 @wpr_archive_info.setter | |
98 def wpr_archive_info(self, value): # pylint: disable=E0202 | |
99 self._wpr_archive_info = value | |
100 | |
101 def ContainsOnlyFileURLs(self): | 77 def ContainsOnlyFileURLs(self): |
102 for page in self.user_stories: | 78 for page in self.user_stories: |
103 if not page.is_file: | 79 if not page.is_file: |
104 return False | 80 return False |
105 return True | 81 return True |
106 | 82 |
107 def ReorderPageSet(self, results_file): | 83 def ReorderPageSet(self, results_file): |
108 """Reorders this page set based on the results of a past run.""" | 84 """Reorders this page set based on the results of a past run.""" |
109 page_set_dict = {} | 85 page_set_dict = {} |
110 for page in self.user_stories: | 86 for page in self.user_stories: |
111 page_set_dict[page.url] = page | 87 page_set_dict[page.url] = page |
112 | 88 |
113 user_stories = [] | 89 user_stories = [] |
114 with open(results_file, 'rb') as csv_file: | 90 with open(results_file, 'rb') as csv_file: |
115 csv_reader = csv.reader(csv_file) | 91 csv_reader = csv.reader(csv_file) |
116 csv_header = csv_reader.next() | 92 csv_header = csv_reader.next() |
117 | 93 |
118 if 'url' not in csv_header: | 94 if 'url' not in csv_header: |
119 raise Exception('Unusable results_file.') | 95 raise Exception('Unusable results_file.') |
120 | 96 |
121 url_index = csv_header.index('url') | 97 url_index = csv_header.index('url') |
122 | 98 |
123 for csv_row in csv_reader: | 99 for csv_row in csv_reader: |
124 if csv_row[url_index] in page_set_dict: | 100 if csv_row[url_index] in page_set_dict: |
125 self.AddPage(page_set_dict[csv_row[url_index]]) | 101 self.AddPage(page_set_dict[csv_row[url_index]]) |
126 else: | 102 else: |
127 raise Exception('Unusable results_file.') | 103 raise Exception('Unusable results_file.') |
128 | 104 |
129 return user_stories | 105 return user_stories |
130 | |
131 def WprFilePathForPage(self, page): | |
132 if not self.wpr_archive_info: | |
133 return None | |
134 return self.wpr_archive_info.WprFilePathForPage(page) | |
OLD | NEW |