4 import stat
as statconsts
6 from future
import standard_library
7 standard_library.install_aliases()
10 __doc__ =
"""Module with utilities for rfio files"""
14 IOError.__init__(self,*vargs)
17 if filename.startswith(prefix): filename = filename[len(prefix):]
22 """Return tuple (status,output) of rfstat shell command. Output is a list of strings
23 (one entry per line). Raises RFIOError if rfstat command is not found."""
25 from PyJobTransformsCore
import envutil
26 if not envutil.find_executable(statcmd):
27 raise RFIOError(
'%s not found in PATH' % statcmd )
29 status,output = subprocess.getstatusoutput( cmd )
32 return (status, output.split(os.linesep))
36 """Return tuple (status,output) of rfdir shell command. Output in a list of strings (one entry per line).
37 The format is the same as the unix shell command \'ls -la\'
38 Raises RFIOError if rfdir command is not found"""
40 from PyJobTransformsCore
import envutil
41 if not envutil.find_executable(dircmd):
42 raise RFIOError(
'%s not found in PATH' % dircmd )
44 status,ls_la = subprocess.getstatusoutput( cmd )
47 return (status, ls_la.split(os.linesep))
51 """Return dictionary with name:value pairs of the rfstat output. Returns None
52 if rfstat returns an error code (i.e. file does not exist)."""
53 status,output =
rfstat(filename)
54 if status:
return None
57 colon = line.index(
':')
58 name = line[:colon].strip()
59 value = line[colon+1:].strip()
66 """Return the contents of <item> in the rfstat output"""
67 status,output =
rfstat(filename)
68 if status:
return None
70 if line.startswith( item ):
71 colon = line.index(
':')
72 return line[colon+1:].strip()
79 """Return the contents of directory <dirname> in a python list a-la os.listdir(),
80 i.e. only the filenames, and not including the current dir (.) and parent dir (..)"""
81 status,ls_la =
rfdir(dirname)
82 if status:
raise RFIOError(
'Directory %s not found' % dirname)
86 if dd != os.curdir
and dd != os.pardir:
93 status,output =
rfstat(filename)
98 """Return size of file <filename> in bytes"""
103 return lstat(filename).st_mtime
119 self.
__items = [ i.upper()
for i
in self.__dict__
if i.startswith(
'st_') ]
124 if idx == getattr(statconsts,i):
125 return getattr(self,i.lower())
131 if idx == getattr(statconsts,i):
132 setattr(self,i.lower(),val)
138 vals.append( self[i] )
139 return str( tuple(vals) )
144 t = time.mktime(time.strptime(time_string))
145 if os.stat_float_times():
155 if output
is None:
raise RFIOError(
'file %s not found' % filename)
159 st.st_dev = eval(
'0x' + value)
160 elif name.startswith(
'Inode'):
161 st.st_ino =
int(value)
162 elif name.find(
'blocks') != -1:
163 st.st_blocks =
int(value)
164 elif name ==
'Protection':
165 octal = value.split()[-1][1:-1]
166 st.st_mode = eval(
'0' + octal)
167 elif name ==
'Hard Links':
168 st.st_nlink =
int(value)
170 st.st_uid =
int(value.split()[0])
172 st.st_gid =
int(value.split()[0])
173 elif name.startswith(
'Size'):
174 st.st_size =
int(value)
175 elif name ==
'Last access':
177 elif name ==
'Last modify':
179 elif name ==
'Last stat. mod.':
189 if mode == os.F_OK:
return exists(filename)
192 filemode = st.st_mode
196 rOK = ( filemode & statconsts.S_IROTH )
or \
197 ( filemode & statconsts.S_IRGRP
and os.getgid() == gid )
or \
198 ( filemode & statconsts.S_IRUSR
and os.getuid() == uid )
or \
199 ( filemode & statconsts.S_ISGID
and os.getegid() == gid )
or \
200 ( filemode & statconsts.S_ISUID
and os.geteuid() == uid )
205 wOK = ( filemode & statconsts.S_IWOTH )
or \
206 ( filemode & statconsts.S_IWGRP
and os.getgid() == gid )
or \
207 ( filemode & statconsts.S_IWUSR
and os.getuid() == uid )
or \
208 ( filemode & statconsts.S_ISGID
and os.getegid() == gid )
or \
209 ( filemode & statconsts.S_ISUID
and os.geteuid() == uid )
214 xOK = ( filemode & statconsts.S_IXOTH )
or \
215 ( filemode & statconsts.S_IXGRP
and os.getgid() == gid )
or \
216 ( filemode & statconsts.S_IXUSR
and os.getuid() == uid )
or \
217 ( filemode & statconsts.S_ISGID
and os.getegid() == gid )
or \
218 ( filemode & statconsts.S_ISUID
and os.geteuid() == uid )
222 return rOK
and wOK
and xOK