ATLAS Offline Software
Classes | Functions | Variables
python.fileutil Namespace Reference

Classes

class  AccessType
 
class  Tee
 

Functions

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

Variables

 __doc__
 
 __all__
 
 defaultRetryMaxTime
 
 defaultRetryStartTime
 
 IO_LOCAL
 
 IO_RFIO
 
 IO_CASTOR
 
 IO_XROOTD
 
 IO_LFN
 
 _accessTypes
 

Function Documentation

◆ access()

def python.fileutil.access (   filename,
  mode 
)

Definition at line 145 of file fileutil.py.

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

◆ exists()

def python.fileutil.exists (   filename)

Definition at line 115 of file fileutil.py.

115 def exists(filename):
116  at = get_access_type(filename)
117  return retry_file_access( at.pathModule.exists, filename )
118 
119 

◆ exists_suffix()

def 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.

160 def 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()

def 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.

178 def 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()

def python.fileutil.get_access_type (   filename)

Definition at line 109 of file fileutil.py.

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

◆ getmtime()

def python.fileutil.getmtime (   filename)

Definition at line 125 of file fileutil.py.

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

◆ getsize()

def python.fileutil.getsize (   filename)

Definition at line 120 of file fileutil.py.

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

◆ listdir()

def python.fileutil.listdir (   filename)

Definition at line 130 of file fileutil.py.

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

◆ lstat()

def python.fileutil.lstat (   filename)

Definition at line 140 of file fileutil.py.

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

◆ mode_string()

def python.fileutil.mode_string (   filename)

Definition at line 194 of file fileutil.py.

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

◆ mode_to_string()

def 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.

198 def 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()

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

Definition at line 150 of file fileutil.py.

150 def 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()

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

Definition at line 47 of file fileutil.py.

47 def retry_file_access( func, args, retryException = OSError,
48  retryMaxTime = defaultRetryMaxTime,
49  retryStartTime = defaultRetryMaxTime ):
50 
51  return retry_function_time( func, args, retryException, retryMaxTime, retryStartTime )
52 

◆ retry_function_time()

def 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.

14 def retry_function_time( func, args, retryException,
15  retryMaxTime = defaultRetryMaxTime,
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()

def python.fileutil.stat (   filename)

Definition at line 135 of file fileutil.py.

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

Variable Documentation

◆ __all__

python.fileutil.__all__
private

Definition at line 9 of file fileutil.py.

◆ __doc__

python.fileutil.__doc__
private

Definition at line 7 of file fileutil.py.

◆ _accessTypes

python.fileutil._accessTypes
private

Definition at line 76 of file fileutil.py.

◆ defaultRetryMaxTime

python.fileutil.defaultRetryMaxTime

Definition at line 11 of file fileutil.py.

◆ defaultRetryStartTime

python.fileutil.defaultRetryStartTime

Definition at line 12 of file fileutil.py.

◆ IO_CASTOR

python.fileutil.IO_CASTOR

Definition at line 73 of file fileutil.py.

◆ IO_LFN

python.fileutil.IO_LFN

Definition at line 75 of file fileutil.py.

◆ IO_LOCAL

python.fileutil.IO_LOCAL

Definition at line 71 of file fileutil.py.

◆ IO_RFIO

python.fileutil.IO_RFIO

Definition at line 72 of file fileutil.py.

◆ IO_XROOTD

python.fileutil.IO_XROOTD

Definition at line 74 of file fileutil.py.

python.fileutil.exists
def exists(filename)
Definition: fileutil.py:115
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.fileutil.getmtime
def getmtime(filename)
Definition: fileutil.py:125
python.fileutil.getsize
def getsize(filename)
Definition: fileutil.py:120
python.fileutil.lstat
def lstat(filename)
Definition: fileutil.py:140
python.fileutil.remove
def remove(filename)
Definition: fileutil.py:150
python.fileutil.exists_suffix
def exists_suffix(filename, suffixRE)
Definition: fileutil.py:160
python.fileutil.listdir
def listdir(filename)
Definition: fileutil.py:130
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.fileutil.exists_suffix_number
def exists_suffix_number(filename)
Definition: fileutil.py:178
python.fileutil.get_access_type
def get_access_type(filename)
Definition: fileutil.py:109
python.fileutil.retry_function_time
def retry_function_time(func, args, retryException, retryMaxTime=defaultRetryMaxTime, retryStartTime=defaultRetryMaxTime)
Definition: fileutil.py:14
python.fileutil.mode_to_string
def mode_to_string(mode)
Definition: fileutil.py:198
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
python.fileutil.access
def access(filename, mode)
Definition: fileutil.py:145
python.fileutil.stat
def stat(filename)
Definition: fileutil.py:135
python.fileutil.retry_file_access
def retry_file_access(func, args, retryException=OSError, retryMaxTime=defaultRetryMaxTime, retryStartTime=defaultRetryMaxTime)
Definition: fileutil.py:47
python.fileutil.mode_string
def mode_string(filename)
Definition: fileutil.py:194