Repository navigation
Expand file tree
/
Copy pathutil.py
More file actions
116 lines (101 loc) · 3.63 KB
/
Copy pathutil.py
File metadata and controls
116 lines (101 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright 2013 GridCentric Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import types
import time
import os
import urlparse
import urllib
from . logger import log
from . config import default_config
def assert_raises(exception_type, command, *args, **kwargs):
try:
command(*args, **kwargs)
assert False and 'Expected exception of type %s' % exception_type
except Exception, e:
if not isinstance(e, exception_type):
log.exception('Expected exception of type %s, got %s instead.' %
(exception_type, type(e)))
raise
log.debug('Got expected exception %s', e)
return e
class Notifier(object):
'''Adds pre_X(o) and post_X(o, r) to methods decorated with notify.'''
def __init__(self):
self.__pre = {}
self.__post = {}
def __getattr__(self, name):
if name.startswith('pre_'):
event = self.__pre
elif name.startswith('post_'):
event = self.__post
else:
raise AttributeError(name)
method_name = name.split('_', 1)[1]
try:
method = getattr(self, method_name)
except AttributeError:
raise AttributeError(name)
if type(method) != types.MethodType:
raise AttributeError(name)
def watch(callback):
self.__watch(event, method_name, callback)
return watch
def __watch(self, event, method_name, callback):
callbacks = event.get(method_name, [])
callbacks.append(callback)
event[method_name] = callbacks
def __fire(self, event, method_name, *args):
callbacks = event.get(method_name, [])
for callback in callbacks:
callback(*args)
@staticmethod
def notify(fn):
def wrapped(*args, **kwargs):
self = args[0]
self.__fire(self.__pre, fn.__name__, self)
r = fn(*args, **kwargs)
self.__fire(self.__post, fn.__name__, self, r)
return r
return wrapped
def list_filter(l, exclude=None, include=None):
if exclude == None:
exclude = []
if include != None:
l = l + include
return [e for e in l if e not in exclude]
def wait_for(message, condition, interval=1):
duration = int(default_config.ops_timeout)
log.info('Waiting %ss for %s', duration, message)
start = time.time()
while True:
if condition():
return
remaining = start + duration - time.time()
if remaining <= 0:
raise Exception('Timeout: waited %ss for %s' % (duration, message))
time.sleep(min(interval, remaining))
def wait_for_ping(addrs):
assert len(addrs) > 0
ip = addrs[0]
wait_for('ping %s to respond' % ip,
lambda: os.system('ping %s -c 1 -W 1 > /dev/null 2>&1' % ip) == 0)
def fix_url_for_yum(url):
# Yum's URL parser cannot deal with commas and such.
s = urlparse.urlsplit(url)
parts = [s[0], s[1]]
for i in range(2, len(s)):
elem = urllib.quote(s[i])
parts.append(elem)
return urlparse.urlunsplit(parts)