OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 inspect | 5 import inspect |
6 import os | 6 import os |
7 | 7 |
8 from telemetry import decorators | |
8 from telemetry import user_story as user_story_module | 9 from telemetry import user_story as user_story_module |
10 from telemetry.util import cloud_storage | |
9 from telemetry.wpr import archive_info | 11 from telemetry.wpr import archive_info |
10 | 12 |
11 | 13 |
12 class UserStorySet(object): | 14 class UserStorySet(object): |
13 """A collection of user story. | 15 """A collection of user story. |
14 | 16 |
15 A typical usage of UserStorySet would be to subclass it and then calling | 17 A typical usage of UserStorySet would be to subclass it and then calling |
16 AddUserStory for each UserStory.. | 18 AddUserStory for each UserStory.. |
17 """ | 19 """ |
18 | 20 |
19 def __init__(self, archive_data_file='', cloud_storage_bucket=None, | 21 def __init__(self, archive_data_file='', cloud_storage_bucket=None, |
20 serving_dirs=None): | 22 serving_dirs=None): |
21 """Creates a new UserStorySet. | 23 """Creates a new UserStorySet. |
22 | 24 |
23 Args: | 25 Args: |
24 archive_data_file: The path to Web Page Replay's archive data, relative | 26 archive_data_file: The path to Web Page Replay's archive data, relative |
25 to self.base_dir. | 27 to self.base_dir. |
26 cloud_storage_bucket: The cloud storage bucket used to download | 28 cloud_storage_bucket: The cloud storage bucket used to download |
27 Web Page Replay's archive data. Valid values are: None, | 29 Web Page Replay's archive data. Valid values are: None, |
28 PUBLIC_BUCKET, PARTNER_BUCKET, or INTERNAL_BUCKET (defined | 30 PUBLIC_BUCKET, PARTNER_BUCKET, or INTERNAL_BUCKET (defined |
29 in telemetry.util.cloud_storage). | 31 in telemetry.util.cloud_storage). |
32 serving_dirs: A set of paths, relative to self.base_dir, to directories | |
33 containing hash files for non-wpr archive data stored in cloud | |
34 storage. | |
30 """ | 35 """ |
31 self.user_stories = [] | 36 self.user_stories = [] |
32 self._archive_data_file = archive_data_file | 37 self._archive_data_file = archive_data_file |
33 self._wpr_archive_info = None | 38 self._wpr_archive_info = None |
34 archive_info.AssertValidCloudStorageBucket(cloud_storage_bucket) | 39 archive_info.AssertValidCloudStorageBucket(cloud_storage_bucket) |
35 self._cloud_storage_bucket = cloud_storage_bucket | 40 self._cloud_storage_bucket = cloud_storage_bucket |
36 self._base_dir = os.path.dirname(inspect.getfile(self.__class__)) | 41 self._base_dir = os.path.dirname(inspect.getfile(self.__class__)) |
37 # Convert any relative serving_dirs to absolute paths. | 42 # Convert any relative serving_dirs to absolute paths. |
38 self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d)) | 43 self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d)) |
39 for d in serving_dirs or []) | 44 for d in serving_dirs or []) |
40 | 45 |
41 @property | 46 @property |
42 def base_dir(self): | 47 def base_dir(self): |
43 """The base directory to resolve archive_data_file. | 48 """The base directory to resolve archive_data_file. |
44 | 49 |
45 This defaults to the directory containing the UserStorySet instance's class. | 50 This defaults to the directory containing the UserStorySet instance's class. |
46 """ | 51 """ |
47 return self._base_dir | 52 return self._base_dir |
48 | 53 |
49 @property | 54 @property |
50 def serving_dirs(self): | 55 def serving_dirs(self): |
nednguyen
2015/02/03 18:03:55
Can you add unittest coverage for user_story_set.s
aiolos (Not reviewing)
2015/02/19 21:29:39
There already is in page_set_unittest. (I updated
| |
51 return self._serving_dirs | 56 all_serving_dirs = self._serving_dirs.copy() |
57 for user_story in self.user_stories: | |
58 if user_story.serving_dir: | |
59 all_serving_dirs.add(user_story.serving_dir) | |
60 return all_serving_dirs | |
52 | 61 |
53 @property | 62 @property |
54 def archive_data_file(self): | 63 def archive_data_file(self): |
55 return self._archive_data_file | 64 return self._archive_data_file |
56 | 65 |
57 @property | 66 @property |
58 def bucket(self): | 67 def bucket(self): |
59 return self._cloud_storage_bucket | 68 return self._cloud_storage_bucket |
60 | 69 |
61 @property | 70 @property |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 user_story: The UserStory to lookup. | 110 user_story: The UserStory to lookup. |
102 | 111 |
103 Returns: | 112 Returns: |
104 The WPR archive file path for the given UserStory, if found. | 113 The WPR archive file path for the given UserStory, if found. |
105 Otherwise, return None. | 114 Otherwise, return None. |
106 """ | 115 """ |
107 if not self.wpr_archive_info: | 116 if not self.wpr_archive_info: |
108 return None | 117 return None |
109 return self.wpr_archive_info.WprFilePathForUserStory(story) | 118 return self.wpr_archive_info.WprFilePathForUserStory(story) |
110 | 119 |
120 @decorators.Cache | |
121 def UpdateServingDirDataIfNeeded(self): | |
nednguyen
2015/02/03 01:37:37
Why do you think it's better to move this function
aiolos (Not reviewing)
2015/02/03 02:35:36
I definitely think we should move the function fro
nednguyen
2015/02/03 18:03:55
I am not sure I understand this argument, but it s
aiolos (Not reviewing)
2015/02/04 18:11:59
cloud_storage actually doesn't seem to have unitte
aiolos (Not reviewing)
2015/02/19 21:29:39
Done.
| |
122 # Scan every serving directory for .sha1 files | |
123 # and download them from Cloud Storage. Assume all data is public. | |
124 serving_dirs = self.serving_dirs | |
125 # Scan all serving dirs. | |
126 for serving_dir in serving_dirs: | |
127 if os.path.splitdrive(serving_dir)[1] == '/': | |
128 raise ValueError('Trying to serve root directory from HTTP server.') | |
129 for dirpath, _, filenames in os.walk(serving_dir): | |
130 for filename in filenames: | |
131 path, extension = os.path.splitext( | |
132 os.path.join(dirpath, filename)) | |
133 if extension != '.sha1': | |
134 continue | |
135 cloud_storage.GetIfChanged(path, self.bucket) | |
136 | |
111 def __iter__(self): | 137 def __iter__(self): |
112 return self.user_stories.__iter__() | 138 return self.user_stories.__iter__() |
113 | 139 |
114 def __len__(self): | 140 def __len__(self): |
115 return len(self.user_stories) | 141 return len(self.user_stories) |
116 | 142 |
117 def __getitem__(self, key): | 143 def __getitem__(self, key): |
118 return self.user_stories[key] | 144 return self.user_stories[key] |
119 | 145 |
120 def __setitem__(self, key, value): | 146 def __setitem__(self, key, value): |
121 self.user_stories[key] = value | 147 self.user_stories[key] = value |
OLD | NEW |