ATLAS Offline Software
Loading...
Searching...
No Matches
python.envutil Namespace Reference

Functions

 has_wildcards (filename)
 find_file_split (filename, dirlist=[os.getcwd()], access=os.R_OK, depth=0)
 find_file (filename, dirlist=[os.getcwd()], access=os.R_OK, depth=0)
 find_files_split (filename, dirlist, access, depth)
 find_files (filename, dirlist, access, depth)
 find_file_env (filename, env_var_name, access=os.R_OK, sep=defaultPathSeps, depth=0)
 find_files_env (filename, env_var_name, access=os.R_OK, sep=defaultPathSeps, depth=0)
 find_libraries (lib)

Variables

str __doc__ = """Environment variables utilities"""
str LD_LIBRARY_PATH = 'LD_LIBRARY_PATH'
str filenameWildCards = r'\?|\*|\[.*\]'
 filenameWildCardsCompiled = re.compile(filenameWildCards)
str defaultPathSeps = os.pathsep + '|,'
 _libraryNameRE = re.compile(r'\.so(\.[0-9]+)*$')
 filelist = glob.glob(filename)
 print ("Checking files %s..." % fullfile)
 dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )
 print ("Trying %s..." % f)
list subdirlist = []
 fulldir = os.path.join(dir,d)

Function Documentation

◆ find_file()

python.envutil.find_file ( filename,
dirlist = [ os.getcwd() ],
access = os.R_OK,
depth = 0 )
Search for file <filename> with access rights <access> (see os.access()) in directory list <dirlist>,
Search into directory tree of each directory up to depth <depth>. The default directory list is
a list containing only the current working directory.
<depth> = 0 : only search in directories given in list.
<depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
<depth> < 0 : ascend upwards into the directory tree up to max -<depth> levels.

Definition at line 85 of file envutil.py.

88 depth = 0 ):
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."""
95 found = find_file_split( filename, dirlist, access, depth )
96 return found and os.path.join( found[0], found[1] )
97
98

◆ find_file_env()

python.envutil.find_file_env ( filename,
env_var_name,
access = os.R_OK,
sep = defaultPathSeps,
depth = 0 )
Search for file <filename> with access rights <access> (see os.access()) in directory list
given as a <sep> separated list of paths in environment variable <env_var_name>.
Search into directory tree of each directory up to depth <depth> (0=don't descend at all).

Definition at line 178 of file envutil.py.

178def find_file_env( filename, env_var_name, access = os.R_OK, sep = defaultPathSeps, depth = 0 ):
179 """Search for file <filename> with access rights <access> (see os.access()) in directory list
180 given as a <sep> separated list of paths in environment variable <env_var_name>.
181 Search into directory tree of each directory up to depth <depth> (0=don't descend at all)."""
182 env = os.environ.get( env_var_name )
183 envList = [ os.getcwd() ]
184 if not env:
185 return envList
186 envList.extend( re.split( sep, env ) )
187 return find_file( filename, envList, access, depth )
188
189

◆ find_file_split()

python.envutil.find_file_split ( filename,
dirlist = [ os.getcwd() ],
access = os.R_OK,
depth = 0 )
Search for file <filename> with access rights <access> (see os.access()) in directory list <dirlist>.
Search into directory tree of each directory in <dirlist> up to depth <depth>. The default directory
list is a list containing only the current working directory.
No wildcards are allowed in <filename>.
<depth> = 0 : only search in directories given in list.
<depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
<depth> < 0 : ascend upwards into the directory tree up to max -<depth> levels.
It returns 2-tuple (dir,file) where:
   file=<filename> if no wildcards, or the actual (local) match to <filename> if wildcarded.
   dir=the directory where <file> was found (from <dirlist>, or from a subdir if depth > 0)
If no file is found, it returns None.

Definition at line 30 of file envutil.py.

33 depth = 0 ):
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
46 for dir in dirlist:
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 )
50 # print ("Checking file %s..." % fullfile)
51 if fileutil.access( fullfile, access ):
52 return (dir,filename)
53
54 if depth == 0:
55 # not found at all
56 return None
57 elif depth > 0:
58 # not found at this level. Go one level down in directory structure
59 for dir in dirlist:
60 dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )
61 if not os.path.isdir(dir): continue
62 subdirlist = []
63 for d in fileutil.listdir(dir):
64 fulldir = os.path.join(dir,d)
65 if os.path.isdir(fulldir):
66 subdirlist.append( fulldir )
67 if subdirlist:
68 found = find_file_split( filename, subdirlist, access, depth - 1 )
69 if found: return found
70 elif depth < 0:
71 # not found at this level. Go one level up in directory structure
72 for dir in dirlist:
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)
76 if updir != dir:
77 found = find_file_split( filename, [ updir ], access, depth + 1 )
78 if found: return found
79
80
81 # not found at all
82 return None
83
84

◆ find_files()

python.envutil.find_files ( filename,
dirlist,
access,
depth )
Search for all (regular) files that match <filename> with access rights <access> (see os.access()) in directory list <dirlist>.
Search is done into subdirectories each directory up to depth <depth>.
The default value for <dirlist> is the current working directory.
If the same file (without the directory name) is found in more than one places, only the first match is kept.
<filename> : can contain wildcards as used on the unix command line.
<depth> = 0 : only search in directories given in list.
<depth> < 0 : treated as = 0
<depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
It returns a list of filenames with full pathnames. If none is found, an empty list is returned.

Definition at line 165 of file envutil.py.

165def find_files( filename, dirlist, access, depth ):
166 """Search for all (regular) files that match <filename> with access rights <access> (see os.access()) in directory list <dirlist>.
167 Search is done into subdirectories each directory up to depth <depth>.
168 The default value for <dirlist> is the current working directory.
169 If the same file (without the directory name) is found in more than one places, only the first match is kept.
170 <filename> : can contain wildcards as used on the unix command line.
171 <depth> = 0 : only search in directories given in list.
172 <depth> < 0 : treated as = 0
173 <depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
174 It returns a list of filenames with full pathnames. If none is found, an empty list is returned."""
175 return list (map( lambda arg : os.path.join( arg[0], arg[1] ), find_files_split( filename, dirlist, access, depth ) ))
176
177
STL class.

◆ find_files_env()

python.envutil.find_files_env ( filename,
env_var_name,
access = os.R_OK,
sep = defaultPathSeps,
depth = 0 )
Search for all files that match <filename> with access rights <access> (see os.access()) in directory list
given as a <sep> (a regular expression) separated list of paths in environment variable <env_var_name>.
<filename> can contain wildcards as used on the unix command line.
Search into directory tree of each directory up to depth <depth> (0=don't descend at all).

Definition at line 190 of file envutil.py.

190def find_files_env( filename, env_var_name, access = os.R_OK, sep = defaultPathSeps, depth = 0 ):
191 """Search for all files that match <filename> with access rights <access> (see os.access()) in directory list
192 given as a <sep> (a regular expression) separated list of paths in environment variable <env_var_name>.
193 <filename> can contain wildcards as used on the unix command line.
194 Search into directory tree of each directory up to depth <depth> (0=don't descend at all)."""
195 env = os.environ.get( env_var_name )
196 envList = [ os.getcwd() ]
197 if not env:
198 return envList
199 envList.extend( re.split( sep, env ) )
200 return find_files( filename, envList, access, depth )
201
202

◆ find_files_split()

python.envutil.find_files_split ( filename,
dirlist,
access,
depth )
Search for all (regular) files that match <filename> with access rights <access> (see os.access())
in directory list <dirlist>.
Search is done into subdirectories each directory up to depth <depth>.
The default value for <dirlist> is the current working directory.
If the same file (without the directory name) is found in more than one places, only the first match is kept.
<filename> : can contain wildcards as used on the unix command line.
<depth> = 0 : only search in directories given in list.
<depth> < 0 : treated as = 0
<depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
It returns a list of 2-tuples (dir,file) where
   file=<filename> if no wildcards, or the actual (local) match to <filename> if wildcarded.
   dir=the directory where <file> was found (from <dirlist>, or from a subdir if depth > 0)
If none is found, an empty list is returned.

Definition at line 99 of file envutil.py.

99def find_files_split( filename, dirlist, access, depth ):
100 """Search for all (regular) files that match <filename> with access rights <access> (see os.access())
101 in directory list <dirlist>.
102 Search is done into subdirectories each directory up to depth <depth>.
103 The default value for <dirlist> is the current working directory.
104 If the same file (without the directory name) is found in more than one places, only the first match is kept.
105 <filename> : can contain wildcards as used on the unix command line.
106 <depth> = 0 : only search in directories given in list.
107 <depth> < 0 : treated as = 0
108 <depth> > 0 : descend deeper into the directory tree up to max <depth> levels.
109 It returns a list of 2-tuples (dir,file) where
110 file=<filename> if no wildcards, or the actual (local) match to <filename> if wildcarded.
111 dir=the directory where <file> was found (from <dirlist>, or from a subdir if depth > 0)
112 If none is found, an empty list is returned."""
113# if dirlist is None:
114# return []
115 if depth < 0: depth = 0
116 # to speed up search, do a single file search if the filename does not contain wildcards
117 if not has_wildcards(filename):
118 singleFile = find_file_split( filename, dirlist, access, depth )
119 if singleFile:
120 return [ singleFile ]
121 else:
122 return []
123 # filename has wildcards. Find all (first) files that match.
124 filenameList = [] # the list of files to return
125 dirnameList = [] # keep track of files already found
126 for dir in dirlist:
127 dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )
128 # protect against non-existing entries
129 if not os.path.isdir(dir): continue
130 olddir = os.getcwd()
131 os.chdir(dir)

◆ find_libraries()

python.envutil.find_libraries ( lib)
Search for libraries in LD_LIBRARY_PATH. Return list of full paths of libraries if found.
<lib> can contain wildcards, in which case all files matching the wildcard will be returned.
If the same file appears in several paths, the first one found will be taken.

Definition at line 203 of file envutil.py.

203def find_libraries( lib ):
204 """Search for libraries in LD_LIBRARY_PATH. Return list of full paths of libraries if found.
205 <lib> can contain wildcards, in which case all files matching the wildcard will be returned.
206 If the same file appears in several paths, the first one found will be taken."""
207 # require extension .so (or .so with version numbers)
208 libsfull = []
209 libname = lib
210 if _libraryNameRE.search(lib): # fully specified ending
211 libsfull = find_files_env( libname, LD_LIBRARY_PATH )
212 else:
213 libsfull = find_files_env( libname, LD_LIBRARY_PATH )
214 # filter results for valid shared library ending (basically to get rid of *.cmtref)
215 libsfull = [ l for l in libsfull if _libraryNameRE.search(l) ]
216 if not libsfull:
217 # add generic ending
218 libname = lib + '.so*'
219 libsfull = find_files_env( libname, LD_LIBRARY_PATH )
220 # filter results for valid shared library ending (basically to get rid of *.cmtref)
221 libsfull = [ l for l in libsfull if _libraryNameRE.search(l) ]
222
223 # if still not found anything, try with prefix 'lib'
224 if not libsfull and not lib.startswith('lib'):
225 libsfull = find_libraries( 'lib' + lib )
226
227 return libsfull

◆ has_wildcards()

python.envutil.has_wildcards ( filename)
Return boolean indicating if the filename contains any unix shell filename wildcards

Definition at line 22 of file envutil.py.

22def has_wildcards(filename):
23 """Return boolean indicating if the filename contains any unix shell filename wildcards"""
24 if filenameWildCardsCompiled.search(filename):
25 return True
26 else:
27 return False
28
29

Variable Documentation

◆ __doc__

str python.envutil.__doc__ = """Environment variables utilities"""
private

Definition at line 6 of file envutil.py.

◆ _libraryNameRE

python.envutil._libraryNameRE = re.compile(r'\.so(\.[0-9]+)*$')
protected

Definition at line 19 of file envutil.py.

◆ defaultPathSeps

str python.envutil.defaultPathSeps = os.pathsep + '|,'

Definition at line 16 of file envutil.py.

◆ dir

python.envutil.dir = os.path.abspath( os.path.expandvars( os.path.expanduser(dir) ) )

print ("Trying %s..." % f)

print ("==> Adding %s to list from %s" % (f,dir))

print ("==> Already have %s in list" % (base))

Definition at line 148 of file envutil.py.

◆ filelist

python.envutil.filelist = glob.glob(filename)

print ("Checking files %s..." % fullfile)

Definition at line 133 of file envutil.py.

◆ filenameWildCards

str python.envutil.filenameWildCards = r'\?|\*|\[.*\]'

Definition at line 12 of file envutil.py.

◆ filenameWildCardsCompiled

python.envutil.filenameWildCardsCompiled = re.compile(filenameWildCards)

Definition at line 13 of file envutil.py.

◆ fulldir

python.envutil.fulldir = os.path.join(dir,d)

Definition at line 153 of file envutil.py.

◆ LD_LIBRARY_PATH

str python.envutil.LD_LIBRARY_PATH = 'LD_LIBRARY_PATH'

Definition at line 9 of file envutil.py.

◆ subdirlist

list python.envutil.subdirlist = []

Definition at line 151 of file envutil.py.