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

Classes

class  AccessType
class  Tee

Functions

 retry_function_time (func, args, retryException, retryMaxTime=defaultRetryMaxTime, retryStartTime=defaultRetryMaxTime)
 retry_file_access (func, args, retryException=OSError, retryMaxTime=defaultRetryMaxTime, retryStartTime=defaultRetryMaxTime)
 get_access_type (filename)
 exists (filename)
 getsize (filename)
 getmtime (filename)
 listdir (filename)
 stat (filename)
 lstat (filename)
 access (filename, mode)
 remove (filename)
 exists_suffix (filename, suffixRE)
 exists_suffix_number (filename)
 mode_string (filename)
 mode_to_string (mode)

Variables

str __doc__ = """A set of file utilities that can be used for several file systems (local files, rfio, castor)"""
list __all__ = [ ]
float defaultRetryMaxTime = 1.0
float defaultRetryStartTime = 0.1
 IO_LOCAL = AccessType('local' , r'(.*)' , r'\1' , os , os.path)
 IO_RFIO = AccessType('rfio' , r'^rfio:' , r'rfio:' , rfio, rfio)
 IO_CASTOR = AccessType('castor', r'^(?:rfio:)?/castor/', 'rfio:/castor/', rfio, rfio)
 IO_XROOTD = AccessType('xrootd', r'^root:' , r'root:' , dummyaccess, dummyaccess )
 IO_LFN = AccessType('lfn' , r'^LFN:' , r'LFN:' , dummyaccess, dummyaccess )
tuple _accessTypes = ( IO_LFN, IO_XROOTD, IO_CASTOR, IO_RFIO, IO_LOCAL )

Function Documentation

◆ access()

python.fileutil.access ( filename,
mode )

Definition at line 145 of file fileutil.py.

145def access(filename,mode):
146 at = get_access_type(filename)
147 return retry_file_access( at.baseModule.access, (filename,mode) )
148
149

◆ exists()

python.fileutil.exists ( filename)

Definition at line 115 of file fileutil.py.

115def exists(filename):
116 at = get_access_type(filename)
117 return retry_file_access( at.pathModule.exists, filename )
118
119
bool exists(const std::string &filename)
does a file exist

◆ exists_suffix()

python.fileutil.exists_suffix ( filename,
suffixRE )
Test if a file exists in the same directory as <filename>, with the
same name, but with an additional suffix given as a regular expression
in <suffixRE>. It returns a list of all matching suffices, or an empty
list if no matching filename+suffix was found.

Definition at line 160 of file fileutil.py.

160def exists_suffix(filename,suffixRE):
161 """Test if a file exists in the same directory as <filename>, with the
162 same name, but with an additional suffix given as a regular expression
163 in <suffixRE>. It returns a list of all matching suffices, or an empty
164 list if no matching filename+suffix was found."""
165 sufs = []
166 dirname = os.path.dirname(filename) or os.curdir
167 # if directory does not exist, then file will never exist (and prevent crash later on)
168 if not os.path.isdir(dirname): return []
169 filename = os.path.basename(filename)
170 pat = re.compile( '^%s(%s)' % (filename,suffixRE) )
171 for f in listdir(dirname):
172 if pat.search( f ):
173 sufs.append( pat.sub( r'\1', f ) )
174
175 return sufs
176
177

◆ exists_suffix_number()

python.fileutil.exists_suffix_number ( filename)
Test if a file exists in the same directory as <filename>, with the same name,
but a non-negative integer added at the end of the name. It returns the filename
with the highest number added, or None if no such file exists.

Definition at line 178 of file fileutil.py.

178def exists_suffix_number(filename):
179 """Test if a file exists in the same directory as <filename>, with the same name,
180 but a non-negative integer added at the end of the name. It returns the filename
181 with the highest number added, or None if no such file exists."""
182 sufs = exists_suffix(filename, r'[0-9]+')
183 maxnum = -1
184 found = None
185 for suf in sufs:
186 num = int(suf)
187 if num > maxnum:
188 maxnum = num
189 found = filename + suf
190
191 return found
192
193

◆ get_access_type()

python.fileutil.get_access_type ( filename)

Definition at line 109 of file fileutil.py.

109def get_access_type(filename):
110 for at in _accessTypes:
111 if at.matches( filename ): return at
112 return IO_LOCAL
113
114

◆ getmtime()

python.fileutil.getmtime ( filename)

Definition at line 125 of file fileutil.py.

125def getmtime(filename):
126 at = get_access_type(filename)
127 return retry_file_access( at.pathModule.getmtime, filename )
128
129

◆ getsize()

python.fileutil.getsize ( filename)

Definition at line 120 of file fileutil.py.

120def getsize(filename):
121 at = get_access_type(filename)
122 return retry_file_access( at.pathModule.getsize, filename )
123
124

◆ listdir()

python.fileutil.listdir ( filename)

Definition at line 130 of file fileutil.py.

130def listdir(filename):
131 at = get_access_type(filename)
132 return retry_file_access( at.baseModule.listdir, filename )
133
134

◆ lstat()

python.fileutil.lstat ( filename)

Definition at line 140 of file fileutil.py.

140def lstat(filename):
141 at = get_access_type(filename)
142 return retry_file_access( at.baseModule.lstat, filename )
143
144

◆ mode_string()

python.fileutil.mode_string ( filename)

Definition at line 194 of file fileutil.py.

194def mode_string(filename):
195 return mode_to_string( lstat(filename).st_mode )
196
197

◆ mode_to_string()

python.fileutil.mode_to_string ( mode)
Return the unix like string corresponding to the file access mode (rwxd etc)

Definition at line 198 of file fileutil.py.

198def mode_to_string(mode):
199 """Return the unix like string corresponding to the file access mode (rwxd etc)"""
200 modeList = ['-']*10
201 # first character (dir/symlink)
202 if statconsts.S_ISDIR(mode):
203 modeList[0] = 'd'
204 elif statconsts.S_ISLNK(mode):
205 modeList[0] = 'l'
206 # user modes
207 if mode & statconsts.S_IRUSR: modeList[1] = 'r'
208 if mode & statconsts.S_IWUSR: modeList[2] = 'w'
209 if mode & statconsts.S_ISUID: modeList[3] = 's'
210 elif mode & statconsts.S_IXUSR: modeList[3] = 'x'
211 # group modes
212 if mode & statconsts.S_IRGRP: modeList[4] = 'r'
213 if mode & statconsts.S_IWGRP: modeList[5] = 'w'
214 if mode & statconsts.S_ISGID: modeList[6] = 's'
215 elif mode & statconsts.S_IXGRP: modeList[6] = 'x'
216 # other modes
217 if mode & statconsts.S_IROTH: modeList[7] = 'r'
218 if mode & statconsts.S_IWOTH: modeList[8] = 'w'
219 if mode & statconsts.S_IXOTH: modeList[9] = 'x'
220
221 return ''.join(modeList)
222

◆ remove()

python.fileutil.remove ( filename)
Remove file <filename> if it exists. Only supported for local files.

Definition at line 150 of file fileutil.py.

150def remove(filename):
151 """Remove file <filename> if it exists. Only supported for local files."""
152 at = get_access_type(filename)
153 if at == IO_LOCAL:
154 if exists(filename): retry_file_access( os.remove, filename )
155 else:
156 print ("WARNING: file %s file %s can not be removed" %
157 (at.name, filename))
158
159

◆ retry_file_access()

python.fileutil.retry_file_access ( func,
args,
retryException = OSError,
retryMaxTime = defaultRetryMaxTime,
retryStartTime = defaultRetryMaxTime )

Definition at line 47 of file fileutil.py.

49 retryStartTime = defaultRetryMaxTime ):
50
51 return retry_function_time( func, args, retryException, retryMaxTime, retryStartTime )
52

◆ retry_function_time()

python.fileutil.retry_function_time ( func,
args,
retryException,
retryMaxTime = defaultRetryMaxTime,
retryStartTime = defaultRetryMaxTime )
Call function several times if it throws a <retryException>.
It will wait an increasing amount of time in between tries.
First waiting time is <retryStartTime>, which is increased by
a factor of 2 for each retry. It will give up and raise the
original exception if it still fails after a total retry time of <retryMaxTime>.
<func>: function to be called
<args>: tuple with the function arguments, or the single function argument

Definition at line 14 of file fileutil.py.

16 retryStartTime = defaultRetryMaxTime ):
17 """Call function several times if it throws a <retryException>.
18 It will wait an increasing amount of time in between tries.
19 First waiting time is <retryStartTime>, which is increased by
20 a factor of 2 for each retry. It will give up and raise the
21 original exception if it still fails after a total retry time of <retryMaxTime>.
22 <func>: function to be called
23 <args>: tuple with the function arguments, or the single function argument"""
24 if type(args) is not tuple: args = (args,)
25 retryDelay = retryStartTime
26 if retryDelay <= 0: retryDelay = 0.1 # avoid infinite loop
27 OK = False
28 tStart = time.time()
29 while not OK:
30 try:
31 val = func( *args )
32 OK = True
33 except retryException:
34 OK = False
35 dt = time.time() - tStart
36 argsStr = ', '.join( [ '%r' % a for a in args ] )
37 if dt > retryMaxTime:
38 print ("%s(%s) Failed" % (func.__name__,argsStr))
39 raise
40 time.sleep(retryDelay)
41 retryDelay *= 2
42 print ("Retrying %s(%s)" % (func.__name__,argsStr))
43
44 return val
45
46

◆ stat()

python.fileutil.stat ( filename)

Definition at line 135 of file fileutil.py.

135def stat(filename):
136 at = get_access_type(filename)
137 return retry_file_access( at.baseModule.stat, filename )
138
139

Variable Documentation

◆ __all__

list python.fileutil.__all__ = [ ]
private

Definition at line 9 of file fileutil.py.

◆ __doc__

str python.fileutil.__doc__ = """A set of file utilities that can be used for several file systems (local files, rfio, castor)"""
private

Definition at line 7 of file fileutil.py.

◆ _accessTypes

tuple python.fileutil._accessTypes = ( IO_LFN, IO_XROOTD, IO_CASTOR, IO_RFIO, IO_LOCAL )
protected

Definition at line 76 of file fileutil.py.

◆ defaultRetryMaxTime

float python.fileutil.defaultRetryMaxTime = 1.0

Definition at line 11 of file fileutil.py.

◆ defaultRetryStartTime

float python.fileutil.defaultRetryStartTime = 0.1

Definition at line 12 of file fileutil.py.

◆ IO_CASTOR

python.fileutil.IO_CASTOR = AccessType('castor', r'^(?:rfio:)?/castor/', 'rfio:/castor/', rfio, rfio)

Definition at line 73 of file fileutil.py.

◆ IO_LFN

python.fileutil.IO_LFN = AccessType('lfn' , r'^LFN:' , r'LFN:' , dummyaccess, dummyaccess )

Definition at line 75 of file fileutil.py.

◆ IO_LOCAL

python.fileutil.IO_LOCAL = AccessType('local' , r'(.*)' , r'\1' , os , os.path)

Definition at line 71 of file fileutil.py.

◆ IO_RFIO

python.fileutil.IO_RFIO = AccessType('rfio' , r'^rfio:' , r'rfio:' , rfio, rfio)

Definition at line 72 of file fileutil.py.

◆ IO_XROOTD

python.fileutil.IO_XROOTD = AccessType('xrootd', r'^root:' , r'root:' , dummyaccess, dummyaccess )

Definition at line 74 of file fileutil.py.