1
2
3
4
5
6
7
8
9
10
11
12
13
14 import srb
15 import types
16 import socket
17 import atexit
18 from srbErrors import errmsgs
19
20
21
22
23
24
25
26 connections = []
33 atexit.register(disconnect_all)
34
35
41
43 return "<<SrbException: (%s) %s>>" % (self.message, self.info)
45 return "<<SrbException: (%s) %s>>" % (self.message, self.info)
46
47
48
50 conn = None
51 fd = -1
52 name = ""
53 collection = ""
54
55
56 position = 0
57
65
66
73
74 - def _create(self, size, resource, type, path):
81
84
86 return False
87
89
90 return None
91
94
95 - def read(self, size=-1):
96 if self.fd < 0:
97 raise TypeError('File is not open.')
98 if size > -1:
99 data = srb.obj_read(self.conn.id, self.fd, size)
100 self.position += size
101 else:
102 data = ""
103 while 1:
104 buffer = srb.obj_read(self.conn.id, self.fd, 1024)
105 self.position += 1024
106 if buffer == "": break
107 data += buffer
108 return data
109
111 if self.fd < 0:
112 raise TypeError('File is not open.')
113 resp = srb.obj_close(self.conn.id, self.fd)
114 if resp < 0:
115 raise SrbException(resp)
116 else:
117 self.fd = -1
118
120 if self.fd < 0:
121 raise TypeError('File is not open.')
122
123
124
125
126
127
128
129
130 resp = srb.obj_write(self.conn.id, self.fd, value, len(value))
131 if resp < 0:
132 raise SrbException(resp)
133 else:
134 return resp
135
136 - def seek(self, offset, whence=0):
137 if self.fd < 0:
138 raise TypeError('File is not open.')
139 resp = srb.obj_seek(self.conn.id, self.fd, offset, whence)
140 if resp < 0:
141 raise SrbException(resp)
142 else:
143 self.position = whence + offset
144
149
158
169
180
181
183 id = -1
184 collection = ""
185 resource = ""
186
187 host = ""
188 port = ""
189 domain = ""
190 auth = ""
191 user = ""
192 passwd = ""
193 dn = ""
194
195 - def __init__(self, host, port, domain, auth="ENCRYPT1", user="", passwd="", dn=""):
196
197
198 try:
199 info = socket.gethostbyname(host)
200 self.host = host
201 except socket.gaierror, e:
202 raise TypeError("Unknown host: %s" % host)
203
204 if type(port) == types.IntType:
205 self.port = str(port)
206 elif type(port) == types.StringType:
207 if port.isdigit():
208 self.port = port
209 else:
210 raise TypeError("Port must be numeric")
211 else:
212 raise TypeError("Port must be integer or numeric string")
213
214 self.domain = domain
215
216 if auth == "PASSWD_AUTH":
217
218 self.auth = "ENCRYPT1"
219 elif auth in ["ENCRYPT1", "GSI_AUTH"]:
220 self.auth = auth
221 elif not auth and user and passwd:
222 self.auth = "ENCRYPT1"
223 elif not auth and dn:
224 self.auth = "GSI_AUTH"
225 else:
226 print user, passwd
227 raise TypeError("Unknown authentication type: %s" % auth)
228
229 assert self.host
230 assert self.port
231 assert self.domain
232 if self.auth == "ENCRYPT1":
233 assert user
234 assert passwd
235 elif self.auth == "GSI_AUTH":
236 assert dn
237
238 sid = srb.connect(self.host, self.port, self.domain, self.auth, user, passwd, dn)
239 if sid < 0:
240 raise SrbException(sid)
241 else:
242 self.id = sid
243 connections.append(self)
244 self.open_collection("/home/%s.%s" % (user, domain))
245
248
249 - def open(self, name, flag='w'):
250
251
252 if flag == 'r':
253 iflag = 0
254 else:
255 iflag = 2
256 f = SrbFile(self, name)
257 f._connect(iflag)
258 return f
259
260 - def create(self, name, size=-1, type="generic", resource="", path=""):
268
274
278
285
292
294 n = srb.get_subcolls(self.id, 0, self.collection)
295 if n < 0:
296 raise SrbException(n)
297 else:
298 self.subcolls = n
299 return n
300
309
318
320 if not self.collection:
321 raise TypeError("Must open a collection first")
322 elif idx >= self.subcolls:
323 raise IndexError("Subcollection index out of range")
324 return srb.get_subcoll_name(self.id, idx)
325
326
338
348
349
351 for path, dirs, files in self.walk():
352 for file in files:
353 f = self.open(file)
354 f.close()
355 f.delete()
356