4 import stat
as statconsts
8 __doc__ =
"""Module with utilities for rfio files"""
12 IOError.__init__(self,*vargs)
15 if filename.startswith(prefix): filename = filename[len(prefix):]
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."""
23 from PyJobTransformsCore
import envutil
24 if not envutil.find_executable(statcmd):
25 raise RFIOError(
'%s not found in PATH' % statcmd )
27 status,output = subprocess.getstatusoutput( cmd )
30 return (status, output.split(os.linesep))
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"""
38 from PyJobTransformsCore
import envutil
39 if not envutil.find_executable(dircmd):
40 raise RFIOError(
'%s not found in PATH' % dircmd )
42 status,ls_la = subprocess.getstatusoutput( cmd )
45 return (status, ls_la.split(os.linesep))
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
55 colon = line.index(
':')
56 name = line[:colon].strip()
57 value = line[colon+1:].strip()
64 """Return the contents of <item> in the rfstat output"""
65 status,output =
rfstat(filename)
66 if status:
return None
68 if line.startswith( item ):
69 colon = line.index(
':')
70 return line[colon+1:].strip()
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)
84 if dd != os.curdir
and dd != os.pardir:
91 status,output =
rfstat(filename)
96 """Return size of file <filename> in bytes"""
101 return lstat(filename).st_mtime
117 self.
__items = [ i.upper()
for i
in self.__dict__
if i.startswith(
'st_') ]
122 if idx == getattr(statconsts,i):
123 return getattr(self,i.lower())
129 if idx == getattr(statconsts,i):
130 setattr(self,i.lower(),val)
136 vals.append( self[i] )
137 return str( tuple(vals) )
142 t = time.mktime(time.strptime(time_string))
143 if os.stat_float_times():
153 if output
is None:
raise RFIOError(
'file %s not found' % filename)
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)
168 st.st_uid =
int(value.split()[0])
170 st.st_gid =
int(value.split()[0])
171 elif name.startswith(
'Size'):
172 st.st_size =
int(value)
173 elif name ==
'Last access':
175 elif name ==
'Last modify':
177 elif name ==
'Last stat. mod.':
187 if mode == os.F_OK:
return exists(filename)
190 filemode = st.st_mode
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 )
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 )
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 )
220 return rOK
and wOK
and xOK