ATLAS Offline Software
rfio.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
2 
3 import os,time
4 import stat as statconsts
5 
6 import subprocess
7 
8 __doc__ = """Module with utilities for rfio files"""
9 
10 class RFIOError(IOError):
11  def __init__(self,*vargs):
12  IOError.__init__(self,*vargs)
13 
14 def _remove_prefix(filename,prefix):
15  if filename.startswith(prefix): filename = filename[len(prefix):]
16  return filename
17 
18 
19 def rfstat(filename):
20  """Return tuple (status,output) of rfstat shell command. Output is a list of strings
21  (one entry per line). Raises RFIOError if rfstat command is not found."""
22  statcmd = 'rfstat'
23  from PyJobTransformsCore import envutil
24  if not envutil.find_executable(statcmd):
25  raise RFIOError( '%s not found in PATH' % statcmd )
26  cmd = '%s %s' % (statcmd,_remove_prefix(filename,'rfio:'))
27  status,output = subprocess.getstatusoutput( cmd )
28  status >>= 8
29 
30  return (status, output.split(os.linesep))
31 
32 
33 def rfdir(dirname):
34  """Return tuple (status,output) of rfdir shell command. Output in a list of strings (one entry per line).
35  The format is the same as the unix shell command \'ls -la\'
36  Raises RFIOError if rfdir command is not found"""
37  dircmd = 'rfdir'
38  from PyJobTransformsCore import envutil
39  if not envutil.find_executable(dircmd):
40  raise RFIOError( '%s not found in PATH' % dircmd )
41  cmd = '%s %s' % (dircmd,_remove_prefix(dirname,'rfio:'))
42  status,ls_la = subprocess.getstatusoutput( cmd )
43  status >>= 8
44 
45  return (status, ls_la.split(os.linesep))
46 
47 
48 def rfstat_dict(filename):
49  """Return dictionary with name:value pairs of the rfstat output. Returns None
50  if rfstat returns an error code (i.e. file does not exist)."""
51  status,output = rfstat(filename)
52  if status: return None
53  rfdict = { }
54  for line in output:
55  colon = line.index(':')
56  name = line[:colon].strip()
57  value = line[colon+1:].strip()
58  rfdict[name] = value
59 
60  return rfdict
61 
62 
63 def rfstat_item(filename,item):
64  """Return the contents of <item> in the rfstat output"""
65  status,output = rfstat(filename)
66  if status: return None
67  for line in output:
68  if line.startswith( item ):
69  colon = line.index(':')
70  return line[colon+1:].strip()
71 
72  # nothing found
73  return None
74 
75 
76 def listdir(dirname):
77  """Return the contents of directory <dirname> in a python list a-la os.listdir(),
78  i.e. only the filenames, and not including the current dir (.) and parent dir (..)"""
79  status,ls_la = rfdir(dirname)
80  if status: raise RFIOError('Directory %s not found' % dirname)
81  dir = [ ]
82  for d in ls_la:
83  dd = d.split()[-1]
84  if dd != os.curdir and dd != os.pardir:
85  dir.append( dd )
86 
87  return dir
88 
89 
90 def exists(filename):
91  status,output = rfstat(filename)
92  return status == 0
93 
94 
95 def getsize(filename):
96  """Return size of file <filename> in bytes"""
97  return int(rfstat_item(filename,'Size'))
98 
99 
100 def getmtime(filename):
101  return lstat(filename).st_mtime
102 
103 
105  def __init__(self):
106  self.st_mode = 0
107  self.st_ino = 0
108  self.st_dev = 0
109  self.st_nlink = 0
110  self.st_uid = 0
111  self.st_gid = 0
112  self.st_size = 0
113  self.st_atime = 0
114  self.st_mtime = 0
115  self.st_ctime = 0
116 
117  self.__items = [ i.upper() for i in self.__dict__ if i.startswith('st_') ]
118 
119 
120  def __getitem__(self,idx):
121  for i in self.__items:
122  if idx == getattr(statconsts,i):
123  return getattr(self,i.lower())
124  return None
125 
126 
127  def __setitem__(self,idx,val):
128  for i in self.__items:
129  if idx == getattr(statconsts,i):
130  setattr(self,i.lower(),val)
131 
132 
133  def __str__(self):
134  vals = []
135  for i in range(len(self.__items)):
136  vals.append( self[i] )
137  return str( tuple(vals) )
138 
139 
140 
141 def _stat_time(time_string):
142  t = time.mktime(time.strptime(time_string))
143  if os.stat_float_times():
144  return float(t)
145  else:
146  return int(t)
147 
148 
149 
150 def lstat(filename):
151  st = StatResult()
152  output = rfstat_dict(filename)
153  if output is None: raise RFIOError('file %s not found' % filename)
154  for name in output:
155  value = output[name]
156  if name == 'Device':
157  st.st_dev = eval('0x' + value)
158  elif name.startswith('Inode'):
159  st.st_ino = int(value)
160  elif name.find('blocks') != -1:
161  st.st_blocks = int(value)
162  elif name == 'Protection':
163  octal = value.split()[-1][1:-1]
164  st.st_mode = eval('0' + octal)
165  elif name == 'Hard Links':
166  st.st_nlink = int(value)
167  elif name == 'Uid':
168  st.st_uid = int(value.split()[0])
169  elif name == 'Gid':
170  st.st_gid = int(value.split()[0])
171  elif name.startswith('Size'):
172  st.st_size = int(value)
173  elif name == 'Last access':
174  st.st_atime = _stat_time(value)
175  elif name == 'Last modify':
176  st.st_mtime = _stat_time(value)
177  elif name == 'Last stat. mod.':
178  st.st_ctime = _stat_time(value)
179 
180  return st
181 
182 # never follow symbolic links, so stat == lstat
183 stat = lstat
184 
185 
186 def access(filename,mode):
187  if mode == os.F_OK: return exists(filename)
188 
189  st = stat(filename)
190  filemode = st.st_mode
191  uid = st.st_uid
192  gid = st.st_gid
193  if mode & os.R_OK:
194  rOK = ( filemode & statconsts.S_IROTH ) or \
195  ( filemode & statconsts.S_IRGRP and os.getgid() == gid ) or \
196  ( filemode & statconsts.S_IRUSR and os.getuid() == uid ) or \
197  ( filemode & statconsts.S_ISGID and os.getegid() == gid ) or \
198  ( filemode & statconsts.S_ISUID and os.geteuid() == uid )
199  else:
200  rOK = True
201 
202  if mode & os.W_OK:
203  wOK = ( filemode & statconsts.S_IWOTH ) or \
204  ( filemode & statconsts.S_IWGRP and os.getgid() == gid ) or \
205  ( filemode & statconsts.S_IWUSR and os.getuid() == uid ) or \
206  ( filemode & statconsts.S_ISGID and os.getegid() == gid ) or \
207  ( filemode & statconsts.S_ISUID and os.geteuid() == uid )
208  else:
209  wOK = True
210 
211  if mode & os.X_OK:
212  xOK = ( filemode & statconsts.S_IXOTH ) or \
213  ( filemode & statconsts.S_IXGRP and os.getgid() == gid ) or \
214  ( filemode & statconsts.S_IXUSR and os.getuid() == uid ) or \
215  ( filemode & statconsts.S_ISGID and os.getegid() == gid ) or \
216  ( filemode & statconsts.S_ISUID and os.geteuid() == uid )
217  else:
218  xOK = True
219 
220  return rOK and wOK and xOK
221 
222 
python.rfio.StatResult.__str__
def __str__(self)
Definition: rfio.py:133
python.rfio.getmtime
def getmtime(filename)
Definition: rfio.py:100
python.rfio.exists
def exists(filename)
Definition: rfio.py:90
python.rfio.StatResult
Definition: rfio.py:104
python.rfio.StatResult.__setitem__
def __setitem__(self, idx, val)
Definition: rfio.py:127
python.rfio.StatResult.__init__
def __init__(self)
Definition: rfio.py:105
python.rfio.StatResult.st_nlink
st_nlink
Definition: rfio.py:109
python.rfio._stat_time
def _stat_time(time_string)
Definition: rfio.py:141
python.rfio.rfstat
def rfstat(filename)
Definition: rfio.py:19
python.rfio.StatResult.st_atime
st_atime
Definition: rfio.py:113
python.rfio.rfstat_item
def rfstat_item(filename, item)
Definition: rfio.py:63
python.rfio.lstat
def lstat(filename)
Definition: rfio.py:150
python.rfio.rfdir
def rfdir(dirname)
Definition: rfio.py:33
python.rfio.listdir
def listdir(dirname)
Definition: rfio.py:76
python.rfio.StatResult.st_size
st_size
Definition: rfio.py:112
python.rfio.StatResult.st_ino
st_ino
Definition: rfio.py:107
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
python.rfio.getsize
def getsize(filename)
Definition: rfio.py:95
python.rfio.RFIOError.__init__
def __init__(self, *vargs)
Definition: rfio.py:11
python.rfio.stat
stat
Definition: rfio.py:183
python.rfio.StatResult.st_ctime
st_ctime
Definition: rfio.py:115
python.rfio.StatResult.st_gid
st_gid
Definition: rfio.py:111
python.rfio._remove_prefix
def _remove_prefix(filename, prefix)
Definition: rfio.py:14
python.rfio.StatResult.st_mtime
st_mtime
Definition: rfio.py:114
python.rfio.StatResult.st_mode
st_mode
Definition: rfio.py:106
python.rfio.rfstat_dict
def rfstat_dict(filename)
Definition: rfio.py:48
python.rfio.StatResult.st_dev
st_dev
Definition: rfio.py:108
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.rfio.access
def access(filename, mode)
Definition: rfio.py:186
python.rfio.StatResult.st_uid
st_uid
Definition: rfio.py:110
str
Definition: BTagTrackIpAccessor.cxx:11
python.rfio.StatResult.__getitem__
def __getitem__(self, idx)
Definition: rfio.py:120
python.rfio.StatResult.__items
__items
Definition: rfio.py:117
python.rfio.RFIOError
Definition: rfio.py:10
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65