Package starcluster :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module starcluster.utils

  1  #!/usr/bin/env python 
  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   
13 -class AttributeDict(dict):
14 """ Subclass of dict that allows read-only attribute-like access to 15 dictionary key/values"""
16 - def __getattr__(self, name):
17 try: 18 return self.__getitem__(name) 19 except KeyError: 20 return super(AttributeDict, self).__getattribute__(name)
21 31 return wrapper 32
33 -def is_valid_device(dev):
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
40 -def is_valid_partition(part):
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
47 -def is_valid_bucket_name(bucket_name):
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
66 -def validate_ip(ip_address):
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
75 -def is_valid_image_name(image_name):
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
89 -def make_one_liner(script):
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
109 -def is_url(url):
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
121 -def is_iso_time(iso):
122 try: 123 iso_to_datetime_tuple(iso) 124 return True 125 except ValueError,e: 126 return False
127
128 -def iso_to_datetime_tuple(iso):
129 #remove timezone 130 iso = iso.split('.')[0] 131 return datetime.strptime(iso, "%Y-%m-%dT%H:%M:%S")
132
133 -def datetime_tuple_to_iso(tup):
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:
141 - def ipy_shell():
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