3 import os, sys, re, glob
4 from PyJobTransformsCore
import fileutil
6 __doc__ =
"""Environment variables utilities"""
9 LD_LIBRARY_PATH =
'LD_LIBRARY_PATH'
12 filenameWildCards =
r'\?|\*|\[.*\]'
13 filenameWildCardsCompiled = re.compile(filenameWildCards)
16 defaultPathSeps = os.pathsep +
'|,'
19 _libraryNameRE = re.compile(
r'\.so(\.[0-9]+)*$')
23 """Return boolean indicating if the filename contains any unix shell filename wildcards"""
24 if filenameWildCardsCompiled.search(filename):
31 dirlist = [ os.getcwd() ],
34 """Search for file <filename> with access rights <access> (see os.access()) in directory list <dirlist>.
35 Search into directory tree of each directory in <dirlist> up to depth <depth>. The default directory
36 list is a list containing only the current working directory.
37 No wildcards are allowed in <filename>.
38 <depth> = 0 : only search in directories given in list.
39 <depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
40 <depth> < 0 : ascend upwards into the directory tree up to max -<depth> levels.
41 It returns 2-tuple (dir,file) where:
42 file=<filename> if no wildcards, or the actual (local) match to <filename> if wildcarded.
43 dir=the directory where <file> was found (from <dirlist>, or from a subdir if depth > 0)
44 If no file is found, it returns None."""
45 if not dirlist:
return None
47 dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )
48 if not os.path.isdir(dir):
continue
49 fullfile = os.path.join( dir, filename )
51 if fileutil.access( fullfile, access ):
60 dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )
61 if not os.path.isdir(dir):
continue
63 for d
in fileutil.listdir(dir):
64 fulldir = os.path.join(dir,d)
65 if os.path.isdir(fulldir):
66 subdirlist.append( fulldir )
69 if found:
return found
73 dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )
74 if not os.path.isdir(dir):
continue
75 updir = os.path.dirname(dir)
78 if found:
return found
86 dirlist = [ os.getcwd() ],
89 """Search for file <filename> with access rights <access> (see os.access()) in directory list <dirlist>,
90 Search into directory tree of each directory up to depth <depth>. The default directory list is
91 a list containing only the current working directory.
92 <depth> = 0 : only search in directories given in list.
93 <depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
94 <depth> < 0 : ascend upwards into the directory tree up to max -<depth> levels."""
96 return found
and os.path.join( found[0], found[1] )
102 """Find a file in directory <dir> or its higher level dirs."""
103 curdir = os.path.abspath( dir )
104 fullfile = os.path.join( curdir, filename )
105 if fileutil.access( fullfile, access ):
return fullfile
107 updir = os.path.dirname(curdir)
108 while updir != curdir:
110 fullfile = os.path.join( curdir, filename )
111 if fileutil.access( fullfile, access ):
return fullfile
112 updir = os.path.dirname(curdir)
119 """Search for all (regular) files that match <filename> with access rights <access> (see os.access())
120 in directory list <dirlist>.
121 Search is done into subdirectories each directory up to depth <depth>.
122 The default value for <dirlist> is the current working directory.
123 If the same file (without the directory name) is found in more than one places, only the first match is kept.
124 <filename> : can contain wildcards as used on the unix command line.
125 <depth> = 0 : only search in directories given in list.
126 <depth> < 0 : treated as = 0
127 <depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
128 It returns a list of 2-tuples (dir,file) where
129 file=<filename> if no wildcards, or the actual (local) match to <filename> if wildcarded.
130 dir=the directory where <file> was found (from <dirlist>, or from a subdir if depth > 0)
131 If none is found, an empty list is returned."""
134 if depth < 0: depth = 0
139 return [ singleFile ]
146 dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )
148 if not os.path.isdir(dir):
continue
152 filelist = glob.glob(filename)
155 if not os.path.isfile(f)
or not fileutil.access(f, access):
continue
156 if f
not in filenameList:
158 dirnameList.append(dir)
159 filenameList.append(f)
167 dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )
169 if not os.path.isdir(dir):
continue
171 for d
in fileutil.listdir(dir):
172 fulldir = os.path.join(dir,d)
173 if os.path.isdir(fulldir):
174 subdirlist.append( fulldir )
178 if not f[1]
in filenameList:
179 dirnameList.append(f[0])
180 filenameList.append(f[1])
181 return map(
lambda arg1,arg2 : (arg1,arg2) , dirnameList, filenameList )
185 """Search for all (regular) files that match <filename> with access rights <access> (see os.access()) in directory list <dirlist>.
186 Search is done into subdirectories each directory up to depth <depth>.
187 The default value for <dirlist> is the current working directory.
188 If the same file (without the directory name) is found in more than one places, only the first match is kept.
189 <filename> : can contain wildcards as used on the unix command line.
190 <depth> = 0 : only search in directories given in list.
191 <depth> < 0 : treated as = 0
192 <depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
193 It returns a list of filenames with full pathnames. If none is found, an empty list is returned."""
194 return list (map(
lambda arg : os.path.join( arg[0], arg[1] ),
find_files_split( filename, dirlist, access, depth ) ))
197 def find_file_env( filename, env_var_name, access = os.R_OK, sep = defaultPathSeps, depth = 0 ):
198 """Search for file <filename> with access rights <access> (see os.access()) in directory list
199 given as a <sep> separated list of paths in environment variable <env_var_name>.
200 Search into directory tree of each directory up to depth <depth> (0=don't descend at all)."""
201 env = os.environ.get( env_var_name )
202 envList = [ os.getcwd() ]
205 envList.extend( re.split( sep, env ) )
206 return find_file( filename, envList, access, depth )
209 def find_files_env( filename, env_var_name, access = os.R_OK, sep = defaultPathSeps, depth = 0 ):
210 """Search for all files that match <filename> with access rights <access> (see os.access()) in directory list
211 given as a <sep> (a regular expression) separated list of paths in environment variable <env_var_name>.
212 <filename> can contain wildcards as used on the unix command line.
213 Search into directory tree of each directory up to depth <depth> (0=don't descend at all)."""
214 env = os.environ.get( env_var_name )
215 envList = [ os.getcwd() ]
218 envList.extend( re.split( sep, env ) )
219 return find_files( filename, envList, access, depth )
223 """Search for libraries in LD_LIBRARY_PATH. Return list of full paths of libraries if found.
224 <lib> can contain wildcards, in which case all files matching the wildcard will be returned.
225 If the same file appears in several paths, the first one found will be taken."""
227 global _libraryNameRE
230 if _libraryNameRE.search(lib):
235 libsfull = [ l
for l
in libsfull
if _libraryNameRE.search(l) ]
238 libname = lib +
'.so*'
241 libsfull = [ l
for l
in libsfull
if _libraryNameRE.search(l) ]
244 if not libsfull
and not lib.startswith(
'lib'):
251 _pyext = [
'.so',
'.pyo',
'.pyc',
'.py' ]
254 """Search for python file (full path) corresponding to python module <mod> in sys.path
255 (PYTONPATH + some system paths). <mod> should not contain the filename
256 extension (.py etc), and no wildcards. Returns None is module is not found."""
258 filename = modname.replace(
'.',os.sep) +
'.*'
260 all = [ f
for f
in find_files_split( filename, sys.path )
if os.path.splitext(f[1])[1]
in _pyext ]
268 foundPrio = _pyext.index( os.path.splitext(found[1])[1] )
271 if d != found[0]:
break
272 prio = _pyext.index( os.path.splitext(f)[1] )
277 return os.path.join(found[0],found[1])
281 """Search for python module(s) in PYTHONPATH + some system paths.
282 Returns a list of full paths to python module files. If non are found, returns empty list.
283 <mod> can contain wildcards, in which case all files matching the wildcard will be returned.
284 If the same file appears in several paths, the first one found will be taken."""
285 b,e = os.path.splitext(mod)
288 filename = b.replace(
'.',os.sep) + e
298 filename = mod.replace(
'.',os.sep) +
'.*'
302 b,e = os.path.splitext(f)
303 if e
not in _pyext:
continue
304 modname = b.replace(os.sep,
'.')
305 if modname
not in modsFound:
306 modsFound.append( modname )
311 if f: found.append( f )