Package iceprod :: Package core :: Module functions
[hide private]
[frames] | no frames]

Source Code for Module iceprod.core.functions

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  """ 
  5    Module containing common functions 
  6   
  7    copyright (c) 2009 the icecube collaboration 
  8   
  9    @version: $Revision: $ 
 10    @date: $Date: $ 
 11    @author: Juan Carlos Diaz Velez <juancarlos@icecube.wisc.edu> 
 12   
 13  """ 
 14   
 15  import sys,os,re 
 16  import logging 
 17  import os.path 
 18  import socket 
 19   
 20  logger = logging.getLogger('functions') 
 21   
 22   
23 -def removedirs(path):
24 os.system('rm -rf %s' %path)
25 26
27 -def find(pathlist,pattern,filetype='file'):
28 """ 29 Return a list of files that match a given pattern 30 @param pathlist: base directory to start search from 31 @param pattern: pattern to match 32 """ 33 34 if filetype not in ['dir','path','file']: 35 logger.error('find: invalid filetype %s'%filetype) 36 matches = list() 37 regex = re.compile(r'%s'%pattern) 38 39 for root in os.path.expandvars(pathlist).split(os.pathsep): 40 if not root: continue 41 for path, dirs, files in os.walk(root): 42 if filetype == 'file': 43 for f in files: 44 if regex.match(f): 45 matches.append(os.path.join(path,f)) 46 elif filetype.startswith('dir'): 47 for d in dirs: 48 if regex.match(d): 49 matches.append(os.path.join(path,f)) 50 elif filetype.startswith('path'): 51 if regex.search(path): 52 matches.append(os.path.abspath(path)) 53 return matches
54 55
56 -def gethostname():
57 return socket.gethostbyaddr(socket.gethostbyname(socket.gethostname()))[0]
58
59 -def gethostid():
60 """ 61 Get MAC of firt network interface. 62 Todo: Mac OS (Windows???) 63 """ 64 eth0 = 'NULL' 65 try: 66 ifconfig = os.popen('/sbin/ifconfig eth0') 67 regex = re.compile(r'HWaddr\s+([0-9A-Z]{2}:){5}[0-9A-Z]{2}') 68 eth = regex.search(ifconfig.read()) 69 eth0 = eth.group(0).split()[1] 70 ifconfig.close() 71 except Exception,e: 72 logger.error(e) 73 return eth0
74
75 -def findjava(searchpath='/usr/java:/usr/local/java'):
76 platform = os.uname()[4].replace('i686','i386').replace('x86_64','amd64') 77 javaregex = re.compile(r'jre/lib/%s/server'%platform) 78 java = None 79 for path in find(searchpath,'libjvm.so'): 80 if javaregex.search(path): 81 javahome = path.split('/jre/lib')[0] 82 if not java or os.path.basename(javahome) > os.path.basename(java): 83 java = javahome 84 85 if not java: return java 86 87 ldpath = os.path.join(java,'jre/lib/%s/server'%platform) 88 ldpath += ":"+os.path.join(java,'jre/lib/%s'%platform) 89 return java,ldpath
90 91
92 -def md5sum(filename,buffersize=4096):
93 """ 94 Return md5 digest of file 95 """ 96 filed = open(filename) 97 try: 98 import hashlib 99 except ImportError: 100 import md5 101 digest = md5.new() 102 else: 103 digest = hashlib.md5() 104 105 buffer = filed.read(buffersize) 106 while buffer: 107 digest.update(buffer) 108 buffer = filed.read(buffersize) 109 filed.close() 110 return digest.hexdigest()
111
112 -def get_choice(choices,prompt=''):
113 print prompt 114 for i in range(len(choices)): 115 print "(%d): %s" % (i,choices[i]) 116 sys.stdout.write('choice: ') 117 line = sys.stdin.readline().strip() 118 try: 119 item = int(line) 120 print 'You selected (%d): %s.' % (item,choices[item]) 121 sys.stdout.write('is this correct? (Y/N): ') 122 if sys.stdin.readline().strip().upper() == 'Y': 123 return choices[item] 124 else: 125 return get_choice(choices,prompt) 126 except Exception,e: 127 print 'Invalid choice: %s' % str(e) 128 return get_choice(choices,prompt)
129
130 -def myputenv(name,value):
131 """ 132 function to set environment variables 133 """ 134 if value == None: print name,value 135 else: os.environ[name]=value
136
137 -def copy(src,dest):
138 """ 139 function to set environment variables 140 """ 141 return not os.system('cp %s %s' % (src,dest))
142 143
144 -def wget(url,dest='./',cache=False):
145 """ 146 wrapper for downloading from multiple protocols 147 """ 148 149 dest = os.path.expandvars(dest) 150 url = os.path.expandvars(url) 151 if os.path.isdir(dest.replace('file:','')): 152 dest = os.path.join(dest, os.path.basename(url)) 153 if not dest.startswith("file:"): 154 dest = "file:" + dest 155 156 if cache and os.path.exists(dest.replace('file:','')): 157 logger.info('file "%s" exists. skipping download ' % dest) 158 return 0 159 160 if url.startswith("http://") or url.startswith("ftp://"): 161 dest = dest.replace("file:","") 162 cmd = 'wget -nv --tries=4 --output-document=%s %s'%(dest,url) 163 elif url.startswith("lfn:"): 164 cmd = "lcg-cp --vo icecube -v %s %s" % (url,dest) 165 elif url.startswith("file:"): 166 src = url.replace("file:","") 167 dest = dest.replace("file:","") 168 cmd = 'cp %s %s'% (src,dest) 169 elif url.startswith("gsiftp://"): 170 cmd = 'globus-url-copy -cd -r -nodcau %s %s'% (url,dest) 171 else: 172 raise Exception, "unsupported protocol %s" % url 173 print cmd 174 return os.system(cmd)
175 176
177 -def isurl(url):
178 179 """ 180 Determine if this is a supported protocol 181 """ 182 prefixes = ('http:','ftp:','gsiftp:','srm:','lfn:') 183 bools = map( lambda x: url.startswith(x) , prefixes) 184 return reduce(lambda a,b: a or b, bools)
185
186 -def getmemusage():
187 # Get memory usage info 188 stats = { 189 'VmLib:' : 0.0, 190 'VmData:': 0.0, 191 'VmExe:' : 0.0, 192 'VmRSS:' : 0.0, 193 'VmSize:': 0.0, 194 'VmLck:' : 0.0, 195 'VmStk:' : 0.0, 196 } 197 if os.uname()[0] == 'Linux': 198 usage = open("/proc/self/status") 199 for line in usage.readlines(): 200 if line.startswith('Vm'): 201 name,value,unit = line.split() 202 value = float(value.strip()) 203 if unit == "MB": 204 stats[name] = value*1024 205 else: 206 stats[name] = value 207 else: 208 self.logger.warn("getmemusage: Not a Linux machine") 209 return stats
210 211
212 -def hoerandel_fluxsum(emin,dslope):
213 """ 214 function to caculate CORSIKA fluxsum 215 FLUXSUM is the integral in energy of the primary cosmic ray between 216 the minimum and the maximum set energy. 217 218 The cosmic ray energy spectrum is from Hoerandel polygonato model [Astrop. 219 Phys. Vol 19, Issue 2, Pages 193-312 (2003)]. The maximum energy is assumed 220 to be much higher than the minimum energy (the maximum energy actually is not explicitly 221 used in this calculation). Note : DSLOPE = 0 for unweighted CORSIKA sample and = -1 for weighted 222 CORSIKA sample. 223 """ 224 integral = 0.; 225 nmax = 26; 226 norm = [ 227 0.0873, 0.0571, 0.00208, 0.000474,0.000895, 228 0.0106, 0.00235, 0.0157, 0.000328, 229 0.0046, 0.000754, 0.00801, 0.00115, 230 0.00796, 0.00027, 0.00229, 0.000294, 231 0.000836, 0.000536, 0.00147, 0.000304, 232 0.00113, 0.000631, 0.00136, 0.00135, 0.0204 ]; 233 234 gamma = [ 235 2.71, 2.64, 2.54, 2.75, 2.95, 2.66, 2.72, 2.68, 2.69, 2.64, 236 2.66, 2.64, 2.66, 2.75, 2.69, 2.55, 2.68, 2.64, 2.65, 2.7, 237 2.64, 2.61, 2.63, 2.67, 2.46, 2.59 ]; 238 239 crs = [ 240 1.00797, 4.0026, 6.939, 9.0122, 10.811, 12.0112, 14.0067, 241 15.9994, 18.9984, 20.183, 22.9898, 24.312, 26.9815, 28.086, 242 30.984, 32.064, 35.453, 39.948, 39.102, 40.08, 44.956, 47.9, 243 50.942, 51.996, 54.938, 55.847]; 244 245 for i in range(nmax): 246 prwght = round(crs[i]); 247 gamma[i] += dslope; 248 integral += norm[i] * pow( (emin*prwght), (1-gamma[i]) ) / (gamma[i]-1); 249 250 return integral;
251 252
253 -def sendMail(cfg,subject,msg):
254 255 smtpuser = "%s@%s" % (os.getlogin(),os.uname()[1].split(".")[0]) 256 proto = 'sendmail' 257 if cfg.has_option('monitoring','smtphost'): 258 try: 259 import smtplib 260 except ImportError, e: 261 logger.error(e) 262 else: 263 proto = 'smtp' 264 265 if proto == 'stmp': 266 smtphost = cfg.get('monitoring','smtphost') 267 smtpuser = cfg.get('monitoring','smtpuser') 268 269 from_addr = "From: " + smtpuser 270 to_addrs = cfg.get('monitoring','smtpnotify').split(',') 271 subject = "Subject: %s\n" % subject 272 273 server = smtplib.SMTP(smtphost) 274 server.sendmail(from_addr, to_addrs, subject + msg) 275 server.quit() 276 277 else: 278 sendmail_location = "/usr/sbin/sendmail" # sendmail location 279 p = os.popen("%s -t" % sendmail_location, "w") 280 p.write("From: %s\n" % smtpuser) 281 p.write("To: %s\n" % smtpuser) 282 p.write("Subject: %s\n" % subject) 283 p.write("\n") 284 p.write(msg) 285 status = p.close() 286 if status != 0: 287 raise Exception, "Sendmail exited with status %u" % status
288