blob: 631f75b55450ae8cb35dbc1513b5d13c6e179bf9 [file] [log] [blame]
Chandan Kumar5e619872017-09-07 22:23:55 +05301# Copyright 2011, VMware, Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15#
16# Borrowed from nova code base, more utilities will be added/borrowed as and
17# when needed.
18
19"""Utilities and helper functions."""
20
Chandan Kumar667d3d32017-09-22 12:24:06 +053021import threading
22import time
zheng.yong74e760a2019-05-22 14:16:14 +080023try:
24 import urlparse
25except ImportError:
26 from urllib import parse as urlparse
Chandan Kumar667d3d32017-09-22 12:24:06 +053027
Brian Haley33ef4602018-04-26 14:37:49 -040028import eventlet
29
zheng.yong74e760a2019-05-22 14:16:14 +080030SCHEMA_PORT_MAPPING = {
31 "http": 80,
32 "https": 443,
33}
34
Chandan Kumar5e619872017-09-07 22:23:55 +053035
36class classproperty(object):
37 def __init__(self, f):
38 self.func = f
39
40 def __get__(self, obj, owner):
Chandan Kumar667d3d32017-09-22 12:24:06 +053041 return self.func(owner)
42
Chandan Kumar5e619872017-09-07 22:23:55 +053043
44class WaitTimeout(Exception):
45 """Default exception coming from wait_until_true() function."""
46
47
48class LockWithTimer(object):
49 def __init__(self, threshold):
50 self._threshold = threshold
51 self.timestamp = 0
52 self._lock = threading.Lock()
53
54 def acquire(self):
55 return self._lock.acquire(False)
56
57 def release(self):
58 return self._lock.release()
59
60 def time_to_wait(self):
61 return self.timestamp - time.time() + self._threshold
62
Chandan Kumar667d3d32017-09-22 12:24:06 +053063
Chandan Kumar5e619872017-09-07 22:23:55 +053064def wait_until_true(predicate, timeout=60, sleep=1, exception=None):
Brian Haleyae328b92018-10-09 19:51:54 -040065 """Wait until callable predicate is evaluated as True
66
Chandan Kumar5e619872017-09-07 22:23:55 +053067 :param predicate: Callable deciding whether waiting should continue.
68 Best practice is to instantiate predicate with functools.partial()
69 :param timeout: Timeout in seconds how long should function wait.
70 :param sleep: Polling interval for results in seconds.
71 :param exception: Exception instance to raise on timeout. If None is passed
72 (default) then WaitTimeout exception is raised.
73 """
74 try:
75 with eventlet.Timeout(timeout):
76 while not predicate():
77 eventlet.sleep(sleep)
78 except eventlet.Timeout:
79 if exception is not None:
Brian Haley8aaa73f2018-10-09 19:55:44 -040080 # pylint: disable=raising-bad-type
Chandan Kumar5e619872017-09-07 22:23:55 +053081 raise exception
82 raise WaitTimeout("Timed out after %d seconds" % timeout)
Brian Haleyba800452017-12-14 10:30:48 -050083
84
Federico Ressi0e04f8f2018-10-24 12:19:05 +020085def override_class(overriden_class, overrider_class):
86 """Override class definition with a MixIn class
87
88 If overriden_class is not a subclass of overrider_class then it creates
89 a new class that has as bases overrider_class and overriden_class.
90 """
91
92 if not issubclass(overriden_class, overrider_class):
93 name = overriden_class.__name__
94 bases = (overrider_class, overriden_class)
95 overriden_class = type(name, bases, {})
96 return overriden_class
zheng.yong74e760a2019-05-22 14:16:14 +080097
98
99def normalize_url(url):
100 """Normalize url without port with schema default port
101
102 """
103 parse_result = urlparse.urlparse(url)
104 (scheme, netloc, url, params, query, fragment) = parse_result
105 port = parse_result.port
106 if scheme in SCHEMA_PORT_MAPPING and not port:
107 netloc = netloc + ":" + str(SCHEMA_PORT_MAPPING[scheme])
108 return urlparse.urlunparse((scheme, netloc, url, params, query, fragment))