1
2 """
3 Utils module for StarCluster
4 """
5
6 import re
7 import string
8 import urlparse
9 import time
10 from datetime import datetime
11 from starcluster.logger import log
12
14 """ Subclass of dict that allows read-only attribute-like access to
15 dictionary key/values"""
17 try:
18 return self.__getitem__(name)
19 except KeyError:
20 return super(AttributeDict, self).__getattribute__(name)
21
23 """Decorator for printing execution time (in mins) of a function"""
24 def wrapper(*arg, **kargs):
25 """Raw timing function """
26 time1 = time.time()
27 res = func(*arg, **kargs)
28 time2 = time.time()
29 log.info('%s took %0.3f mins' % (func.func_name, (time2-time1)/60.0))
30 return res
31 return wrapper
32
34 regex = re.compile('/dev/sd[a-z]')
35 try:
36 return len(dev) == 8 and regex.match(dev)
37 except TypeError,e:
38 return False
39
41 regex = re.compile('/dev/sd[a-z][1-9][0-9]?')
42 try:
43 return len(part) in [9,10] and regex.match(part)
44 except TypeError,e:
45 return False
46
48 """
49 Check if bucket_name is a valid S3 bucket name (as defined by the AWS docs)
50 """
51 length = len(bucket_name)
52 valid_length = length >= 3 and length <= 255
53 if not valid_length:
54 return False
55 numbers_or_letters = string.ascii_lowercase + string.digits
56 valid_chars = numbers_or_letters + '._-'
57 if not bucket_name[0] in numbers_or_letters:
58 return False
59 for c in bucket_name:
60 if c not in valid_chars:
61 return False
62 if validate_ip(bucket_name):
63 return False
64 return True
65
67 pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|"
68 pattern += r"[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25"
69 pattern += r"[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
70 if re.match(pattern, ip_address):
71 return True
72 else:
73 return False
74
76 """
77 Check if image_name is a valid AWS image name (as defined by the AWS docs)
78 """
79 length = len(image_name)
80 valid_length = length>=3 and length <=128
81 valid_chars = string.letters + string.digits + "().-/_"
82 if not valid_length:
83 return False
84 for c in image_name:
85 if c not in valid_chars:
86 return False
87 return True
88
90 """
91 Returns command to execute python script as a one-line python program
92
93 e.g.
94
95 import os
96 script = '''
97 import os
98 print os.path.exists('hi')
99 '''
100 os.system(make_one_liner(script))
101
102 Will print out:
103
104 <module 'os' from ...>
105 False
106 """
107 return 'python -c "%s"' % script.strip().replace('\n',';')
108
110 try:
111 parts = urlparse.urlparse(url)
112 scheme = parts[0]
113 netloc = parts[1]
114 if scheme and netloc:
115 return True
116 else:
117 return False
118 except:
119 return False
120
127
129
130 iso = iso.split('.')[0]
131 return datetime.strptime(iso, "%Y-%m-%dT%H:%M:%S")
132
134 iso = datetime.strftime(tup, "%Y-%m-%dT%H:%M:%S")
135 return iso
136
137 try:
138 import IPython.Shell
139 ipy_shell = IPython.Shell.IPShellEmbed(argv=[])
140 except ImportError,e:
142 log.error("Unable to load IPython.")
143 log.error("Please check that IPython is installed and working.")
144 log.error("If not, you can install it via: easy_install ipython")
145