Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: tools/telemetry/telemetry/page/page_set.py

Issue 612963002: Introduce UserStory & UserStorySet classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up staticmethods related to PageCreation Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 10 from telemetry.page import page_set_archive_info
11 from telemetry.user_story import user_story_set
11 from telemetry.util import cloud_storage 12 from telemetry.util import cloud_storage
12 13
13 PUBLIC_BUCKET = cloud_storage.PUBLIC_BUCKET 14 PUBLIC_BUCKET = cloud_storage.PUBLIC_BUCKET
14 PARTNER_BUCKET = cloud_storage.PARTNER_BUCKET 15 PARTNER_BUCKET = cloud_storage.PARTNER_BUCKET
15 INTERNAL_BUCKET = cloud_storage.INTERNAL_BUCKET 16 INTERNAL_BUCKET = cloud_storage.INTERNAL_BUCKET
16 17
17 18
18 class PageSetError(Exception): 19 class PageSetError(Exception):
19 pass 20 pass
20 21
21 22
22 class PageSet(object): 23 class PageSet(user_story_set.UserStorySet):
23 def __init__(self, file_path=None, archive_data_file='', 24 def __init__(self, file_path=None, archive_data_file='',
24 credentials_path=None, user_agent_type=None, 25 credentials_path=None, user_agent_type=None,
25 make_javascript_deterministic=True, startup_url='', 26 make_javascript_deterministic=True, startup_url='',
26 serving_dirs=None, bucket=None): 27 serving_dirs=None, bucket=None):
28 super(PageSet, self).__init__()
27 # The default value of file_path is location of the file that define this 29 # The default value of file_path is location of the file that define this
28 # page set instance's class. 30 # page set instance's class.
29 if file_path is None: 31 if file_path is None:
30 file_path = inspect.getfile(self.__class__) 32 file_path = inspect.getfile(self.__class__)
31 # Turn pyc file into py files if we can 33 # Turn pyc file into py files if we can
32 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]):
33 file_path = file_path[:-1] 35 file_path = file_path[:-1]
34 36
35 self.file_path = file_path 37 self.file_path = file_path
36 # These attributes can be set dynamically by the page set. 38 # These attributes can be set dynamically by the page set.
37 self.archive_data_file = archive_data_file 39 self.archive_data_file = archive_data_file
38 self.credentials_path = credentials_path 40 self.credentials_path = credentials_path
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 43 self._wpr_archive_info = None
42 self.startup_url = startup_url 44 self.startup_url = startup_url
43 self.pages = [] 45 self.user_stories = []
44 self.serving_dirs = set() 46 self.serving_dirs = set()
45 serving_dirs = [] if serving_dirs is None else serving_dirs 47 serving_dirs = [] if serving_dirs is None else serving_dirs
46 # Makes sure that page_set's serving_dirs are absolute paths 48 # Makes sure that page_set's serving_dirs are absolute paths
47 for sd in serving_dirs: 49 for sd in serving_dirs:
48 if os.path.isabs(sd): 50 if os.path.isabs(sd):
49 self.serving_dirs.add(os.path.realpath(sd)) 51 self.serving_dirs.add(os.path.realpath(sd))
50 else: 52 else:
51 self.serving_dirs.add(os.path.realpath(os.path.join(self.base_dir, sd))) 53 self.serving_dirs.add(os.path.realpath(os.path.join(self.base_dir, sd)))
52 if self._IsValidPrivacyBucket(bucket): 54 if self._IsValidPrivacyBucket(bucket):
53 self._bucket = bucket 55 self._bucket = bucket
54 else: 56 else:
55 raise ValueError("Pageset privacy bucket %s is invalid" % bucket) 57 raise ValueError("Pageset privacy bucket %s is invalid" % bucket)
56 58
57 @classmethod 59 @property
58 def Name(cls): 60 def pages(self):
59 return cls.__module__.split('.')[-1] 61 return self.user_stories
60 62
61 @classmethod 63 def AddUserStory(self, user_story):
62 def Description(cls): 64 assert isinstance(user_story, page_module.Page)
63 if cls.__doc__: 65 assert user_story.page_set is self
64 return cls.__doc__.splitlines()[0] 66 super(PageSet, self).AddUserStory(user_story)
65 else:
66 return ''
67 67
68 def AddPage(self, page): 68 def AddPage(self, page):
69 assert page.page_set is self 69 self.AddUserStory(page)
70 self.pages.append(page)
71 70
72 def AddPageWithDefaultRunNavigate(self, page_url): 71 def AddPageWithDefaultRunNavigate(self, page_url):
73 """ Add a simple page with url equals to page_url that contains only default 72 """ Add a simple page with url equals to page_url that contains only default
74 RunNavigateSteps. 73 RunNavigateSteps.
75 """ 74 """
76 self.AddPage(page_module.Page( 75 self.AddUserStory(page_module.Page(
77 page_url, self, self.base_dir)) 76 page_url, self, self.base_dir))
78 77
79 @staticmethod 78 @staticmethod
80 def _IsValidPrivacyBucket(bucket_name): 79 def _IsValidPrivacyBucket(bucket_name):
81 if not bucket_name: 80 if not bucket_name:
82 return True 81 return True
83 if (bucket_name in [PUBLIC_BUCKET, PARTNER_BUCKET, INTERNAL_BUCKET]): 82 if (bucket_name in [PUBLIC_BUCKET, PARTNER_BUCKET, INTERNAL_BUCKET]):
84 return True 83 return True
85 return False 84 return False
86 85
(...skipping 15 matching lines...) Expand all
102 101
103 @property 102 @property
104 def bucket(self): 103 def bucket(self):
105 return self._bucket 104 return self._bucket
106 105
107 @wpr_archive_info.setter 106 @wpr_archive_info.setter
108 def wpr_archive_info(self, value): # pylint: disable=E0202 107 def wpr_archive_info(self, value): # pylint: disable=E0202
109 self._wpr_archive_info = value 108 self._wpr_archive_info = value
110 109
111 def ContainsOnlyFileURLs(self): 110 def ContainsOnlyFileURLs(self):
112 for page in self.pages: 111 for page in self.user_stories:
113 if not page.is_file: 112 if not page.is_file:
114 return False 113 return False
115 return True 114 return True
116 115
117 def ReorderPageSet(self, results_file): 116 def ReorderPageSet(self, results_file):
118 """Reorders this page set based on the results of a past run.""" 117 """Reorders this page set based on the results of a past run."""
119 page_set_dict = {} 118 page_set_dict = {}
120 for page in self.pages: 119 for page in self.user_stories:
121 page_set_dict[page.url] = page 120 page_set_dict[page.url] = page
122 121
123 pages = [] 122 user_stories = []
124 with open(results_file, 'rb') as csv_file: 123 with open(results_file, 'rb') as csv_file:
125 csv_reader = csv.reader(csv_file) 124 csv_reader = csv.reader(csv_file)
126 csv_header = csv_reader.next() 125 csv_header = csv_reader.next()
127 126
128 if 'url' not in csv_header: 127 if 'url' not in csv_header:
129 raise Exception('Unusable results_file.') 128 raise Exception('Unusable results_file.')
130 129
131 url_index = csv_header.index('url') 130 url_index = csv_header.index('url')
132 131
133 for csv_row in csv_reader: 132 for csv_row in csv_reader:
134 if csv_row[url_index] in page_set_dict: 133 if csv_row[url_index] in page_set_dict:
135 self.AddPage(page_set_dict[csv_row[url_index]]) 134 self.AddPage(page_set_dict[csv_row[url_index]])
136 else: 135 else:
137 raise Exception('Unusable results_file.') 136 raise Exception('Unusable results_file.')
138 137
139 return pages 138 return user_stories
140 139
141 def WprFilePathForPage(self, page): 140 def WprFilePathForPage(self, page):
142 if not self.wpr_archive_info: 141 if not self.wpr_archive_info:
143 return None 142 return None
144 return self.wpr_archive_info.WprFilePathForPage(page) 143 return self.wpr_archive_info.WprFilePathForPage(page)
145
146 def __iter__(self):
147 return self.pages.__iter__()
148
149 def __len__(self):
150 return len(self.pages)
151
152 def __getitem__(self, key):
153 return self.pages[key]
154
155 def __setitem__(self, key, value):
156 self.pages[key] = value
OLDNEW
« no previous file with comments | « tools/telemetry/telemetry/page/__init__.py ('k') | tools/telemetry/telemetry/user_story/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698