Module xalt_util
[hide private]
[frames] | no frames]

Source Code for Module xalt_util

  1  #----------------------------------------------------------------------- 
  2  # XALT: A tool that tracks users jobs and environments on a cluster. 
  3  # Copyright (C) 2013-2014 University of Texas at Austin 
  4  # Copyright (C) 2013-2014 University of Tennessee 
  5  #  
  6  # This library is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser General Public License as 
  8  # published by the Free Software Foundation; either version 2.1 of  
  9  # the License, or (at your option) any later version.  
 10  # 
 11  # This library is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 14  # Lesser  General Public License for more details.  
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public 
 17  # License along with this library; if not, write to the Free 
 18  # Software Foundation, Inc., 59 Temple Place, Suite 330, 
 19  # Boston, MA 02111-1307 USA 
 20  #----------------------------------------------------------------------- 
 21  from __future__         import print_function 
 22  import logging 
 23  from   logging.handlers import SysLogHandler 
 24  from   fnmatch          import fnmatch 
 25  import os, re, sys, subprocess, getent 
 26   
 27  colonPairPat = re.compile(r"([^:]+):(.*)") 
28 -def config_logger():
29 """ 30 Configure the logging functions 31 """ 32 logger = logging.getLogger() 33 logger.setLevel(logging.INFO) 34 syslog = SysLogHandler(address='/dev/log', facility='local3') 35 formatter = logging.Formatter('XALT: %(name)s: %(levelname)s %(message)r') 36 syslog.setFormatter(formatter) 37 logger.addHandler(syslog) 38 return logger
39
40 -def extract_compiler(pstree):
41 """ 42 Take the output of pstree and find the compiler 43 @param pstree: the single line of processes back to init. 44 """ 45 result = "unknown" 46 ignoreT = { 47 'pstree' : True, 48 'ld' : True, 49 'collect2' : True, 50 } 51 52 if (pstree == "unknown"): 53 return result 54 55 a = pstree.split("---") 56 n = len(a) 57 58 for cmd in reversed(a): 59 if (not (cmd in ignoreT)): 60 result = cmd 61 break 62 63 return cmd
64
65 -def files_in_tree(path, pattern):
66 """ 67 Find a list of files in directory [[path]] that match [[pattern]] 68 @param path: directory. 69 @param pattern: file glob pattern. 70 """ 71 fileA = [] 72 wd = os.getcwd() 73 if (not os.path.isdir(path)): 74 return fileA 75 76 os.chdir(path) 77 path = os.getcwd() 78 os.chdir(wd) 79 80 for root, dirs, files in os.walk(path): 81 for name in files: 82 fn = os.path.join(root, name) 83 if (fnmatch(fn,pattern)): 84 fileA.append(fn) 85 return fileA
86
87 -def which(program):
88 """ 89 Find full path to [[program]] 90 @param program: program to find in path. 91 """ 92 93 def is_exe(fpath): 94 """ 95 Check to see if [[fpath]] exists and is executable 96 @param fpath: full path to executable 97 """ 98 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
99 100 if (not program): 101 return None 102 103 fpath, fname = os.path.split(program) 104 if (fpath): 105 if (is_exe(program)): 106 return os.path.realpath(program) 107 else: 108 for path in os.environ.get("PATH","").split(os.pathsep): 109 exe_file = os.path.join(path, program) 110 for candidate in ext_candidates(exe_file): 111 if (is_exe(candidate)): 112 return os.path.realpath(candidate) 113 114 return None 115
116 -def capture(cmd):
117 """ 118 Capture standard out for a command. 119 @param cmd: Command string or array. 120 """ 121 122 if (type(cmd) == type(' ')): 123 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, 124 stderr=subprocess.STDOUT, shell =True) 125 else: 126 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, 127 stderr=subprocess.STDOUT) 128 129 130 return p.communicate()[0]
131 132
133 -def carp(key, v):
134 """ 135 Check to make sure that the first part of v contains key. Raise exception if not. 136 137 @param key: key 138 @param v: value 139 """ 140 141 k = "unknown" 142 m = colonPairPat.match(v) 143 found = False 144 if (m): 145 k = m.group(1) 146 if (k == key): 147 found = True 148 if (not found): 149 print("Wanted: ",key, ", got: ",v) 150 raise Exception
151
152 -def remove_files(fileA):
153 """ 154 Remove this list of files. Do not raise exception if file doesn't exist. 155 @param fileA: array of file 156 """ 157 158 for f in fileA: 159 try: 160 os.remove(f) 161 except: 162 pass
163 164 numberPat = re.compile(r'[0-9][0-9]*')
165 -def obj_type(object_path):
166 """ 167 Determine what object type [[object_path]] is (a, o, so) 168 @param object_path: name of object. 169 """ 170 result = None 171 a = object_path.split('.') 172 for entry in reversed(a): 173 m = numberPat.search(entry) 174 if (m): 175 continue 176 else: 177 result = entry 178 break 179 return result
180 181 defaultPat = re.compile(r'default:?')
182 -def obj2module(object_path, reverseMapT):
183 """ 184 Find module for [[object_path]] in [[reverseMapT]] if it is exists, "NULL" otherwise. 185 @param object_path: full object path 186 @param reverseMapT: reverse map table. Paths to modules 187 """ 188 189 190 dirNm, fn = os.path.split(object_path) 191 moduleName = 'NULL' 192 pkg = reverseMapT.get(dirNm) 193 if (pkg): 194 flavor = pkg['flavor'][0] 195 flavor = defaultPat.sub('',flavor) 196 if (flavor): 197 moduleName = "'" + pkg['pkg'] + '(' + flavor + ")'" 198 else: 199 moduleName = "'" + pkg['pkg'] + "'" 200 return moduleName
201 202
203 -def dbConfigFn(dbname):
204 """ 205 Build config file name from dbname. 206 @param dbname: db name 207 """ 208 return dbname + "_db.conf"
209