ATLAS Offline Software
Loading...
Searching...
No Matches
rfio.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
3import os,time
4import stat as statconsts
5
6import subprocess
7
8__doc__ = """Module with utilities for rfio files"""
9
10class RFIOError(IOError):
11 def __init__(self,*vargs):
12 super().__init__(*vargs)
13
14def _remove_prefix(filename,prefix):
15 if filename.startswith(prefix): filename = filename[len(prefix):]
16 return filename
17
18
19def 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
33def 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
48def 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
63def 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
76def 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
90def exists(filename):
91 status,output = rfstat(filename)
92 return status == 0
93
94
95def getsize(filename):
96 """Return size of file <filename> in bytes"""
97 return int(rfstat_item(filename,'Size'))
98
99
100def 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
141def _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
150def 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
183stat = lstat
184
185
186def 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
__init__(self, *vargs)
Definition rfio.py:11
__getitem__(self, idx)
Definition rfio.py:120
__setitem__(self, idx, val)
Definition rfio.py:127
access(filename, mode)
Definition rfio.py:186
rfdir(dirname)
Definition rfio.py:33
lstat(filename)
Definition rfio.py:150
rfstat_item(filename, item)
Definition rfio.py:63
getmtime(filename)
Definition rfio.py:100
_stat_time(time_string)
Definition rfio.py:141
listdir(dirname)
Definition rfio.py:76
_remove_prefix(filename, prefix)
Definition rfio.py:14
getsize(filename)
Definition rfio.py:95
exists(filename)
Definition rfio.py:90
rfstat_dict(filename)
Definition rfio.py:48
rfstat(filename)
Definition rfio.py:19