2 # Copyright 2012 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 """Miscellaneous utility functions."""
24 # pkg_resources (part of setuptools) is needed when WPR is
25 # distributed as a package. (Resources may need to be extracted from
30 def resource_exists(resource_name
):
31 return pkg_resources
.resource_exists(__name__
, resource_name
)
33 def resource_string(resource_name
):
34 return pkg_resources
.resource_string(__name__
, resource_name
)
37 # Import of pkg_resources failed, so fall back to getting resources
38 # from the file system.
42 def _resource_path(resource_name
):
43 _replay_dir
= os
.path
.dirname(os
.path
.abspath(__file__
))
44 return os
.path
.join(_replay_dir
, resource_name
)
46 def resource_exists(resource_name
):
47 return os
.path
.exists(_resource_path(resource_name
))
49 def resource_string(resource_name
):
50 return open(_resource_path(resource_name
)).read()
53 class TimeoutException(Exception):
57 def WaitFor(condition
, timeout
):
58 """Waits for up to |timeout| secs for the function |condition| to return True.
60 Polling frequency is (elapsed_time / 10), with a min of .1s and max of 5s.
63 Result of |condition| function (if present).
65 min_poll_interval
= 0.1
69 def GetConditionString():
70 if condition
.__name
__ == '<lambda>':
72 return inspect
.getsource(condition
).strip()
75 return condition
.__name
__
77 start_time
= time
.time()
78 last_output_time
= start_time
84 elapsed_time
= now
- start_time
85 last_output_elapsed_time
= now
- last_output_time
86 if elapsed_time
> timeout
:
87 raise TimeoutException('Timed out while waiting %ds for %s.' %
88 (timeout
, GetConditionString()))
89 if last_output_elapsed_time
> output_interval
:
90 logging
.info('Continuing to wait %ds for %s. Elapsed: %ds.',
91 timeout
, GetConditionString(), elapsed_time
)
92 last_output_time
= time
.time()
93 poll_interval
= min(max(elapsed_time
/ 10., min_poll_interval
),
95 time
.sleep(poll_interval
)