1lfc_python(3) Python Reference
2lfc_python(3)
3
4
5
6[1mNAME[0m
7 lfc ‐ Python interface to the LFC
8 lfcthr ‐ Thread enabled version of Python interface to the
9LFC
10
11[1mSYNOPSIS[0m
12 [1mimport lfc[0m
13
14 [1mimport lfcthr[0m
15 [1mlfcthr.init()[0m
16
17
18[1mDESCRIPTION[0m
19 The lfc module permits you to access the LFC client
20interface from
21 python programs. The lfc module is a swig wrapping of the
22standard C
23 interface. For detailed descriptions of each function see
24the individ‐
25 ual man page of each function.
26
27 The lfcthr module is a version of the lfc module sup‐
28porting multi‐
29 threaded Python clients. Its usage is similar to the us‐
30age of the lfc
31 module except the obligatory initialisation call lfc‐
32thr.init() in the
33 main program before threads are started.
34
35 There follows a series of examples of how to use selected
36functions and
37 how to retrieve the information returned by them: Examples
38are finding
39 the GUID of an existing entry, listing the replicas of a
40given GUID and
41 setting and retrieving the comment associated with an en‐
42try.
43
44[1mEXAMPLE[0m
45 #!/usr/bin/python
46
47 import sys
48 import lfc
49
50 """
51 # stat an existing entry in the LFC and print the GUID
52 """
53
54 name = "/grid/dteam/my.test"
55
56 stat = lfc.lfc_filestatg()
57 res = lfc.lfc_statg(name,"",stat)
58
59 if res == 0:
60 guid = stat.guid
61 print "The GUID for " + name + " is " + guid
62 else:
63 err_num = lfc.cvar.serrno
64 err_string = lfc.sstrerror(err_num)
65 print "There was an error while looking for " + name +
66": Error " + str(err_num) + " (" + err_string +
67")"
68 sys.exit(1)
69
70[1mEXAMPLE[0m
71 #!/usr/bin/python
72
73 import lfc
74
75 """
76 # list the replicas of a given entry, starting from the
77GUID
78 """
79
80 guid = "6a3164e0‐a4d7‐4abe‐9f76‐e3b8882735d1"
81
82 listp = lfc.lfc_list()
83 flag = lfc.CNS_LIST_BEGIN
84
85 print "Listing replicas for GUID " + guid
86
87 num_replicas=0
88
89 while(1):
90 res = lfc.lfc_listreplica("",guid,flag,listp)
91 flag = lfc.CNS_LIST_CONTINUE
92
93 if res == None:
94 break
95 else:
96 rep_name = res.sfn
97 print "Replica: " + rep_name
98 num_replicas = num_replicas + 1
99
100 lfc.lfc_listreplica("",guid,lfc.CNS_LIST_END,listp)
101 print "Found " + str(num_replicas) + " replica(s)"
102
103[1mEXAMPLE[0m
104 #!/usr/bin/python
105
106 import sys
107 import lfc
108 import re
109
110 """
111 # setting and retrieving a comment on a file
112 """
113
114 file = "/grid/dteam/my.test"
115
116 comment = "MyComment"
117 res = lfc.lfc_setcomment(file,comment)
118
119 if res != 0:
120 err_num = lfc.cvar.serrno
121 err_string = lfc.sstrerror(err_num)
122 print "Problem while setting comment for " + file + ":
123Error " + str(err_num) + " (" + err_string + ")"
124 sys.exit(1)
125
126 buffer=""
127 for i in range(0,lfc.CA_MAXCOMMENTLEN+1):
128 buffer=buffer + " "
129
130 res = lfc.lfc_getcomment(file,buffer)
131
132 if res != 0:
133 err_num = lfc.cvar.serrno
134 err_string = lfc.sstrerror(err_num)
135 print "Problem while reading the comment for " + file +
136": Error " + str(err_num) + " (" + err_string +
137")"
138 sys.exit(1)
139
140 r = re.compile("(.*?) ", re.DOTALL)
141 comment = r.findall(buffer)[0]
142
143 print "Read back comment " + comment
144
145
146[1mEXAMPLE[0m
147 #!/usr/bin/python
148
149 """
150 # Using the lfc_readdirxr method
151 """
152
153 import sys
154 import lfc
155
156 name = "/grid/dteam/my.test"
157
158 dir = lfc.lfc_opendirg(name,"")
159 if (dir == None) or (dir == 0):
160 err_num = lfc.cvar.serrno
161 err_string = lfc.sstrerror(err_num)
162 print "Error while looking for " + name + ": Error
163" + str(err_num) + " (" + err_string + ")"
164 sys.exit(1)
165
166 while 1:
167 read_pt = lfc.lfc_readdirxr(dir,"")
168 if (read_pt == None) or (read_pt == 0):
169 break
170 entry, list = read_pt
171 print entry.d_name
172 try:
173 for i in range(len(list)):
174 print " ==> %s" % list[i].sfn
175 except TypeError, x:
176 print " ==> None"
177
178 lfc.lfc_closedir(dir)
179
180
181[1mEXAMPLE[0m
182 #!/usr/bin/python
183
184 import lfc
185
186 """
187 # Using the lfc_getlinks method
188 """
189
190 result, list = lfc.lfc_getlinks("/grid/dteam/antotests/ex‐
191tratests/dir2/f105", "")
192 print result
193 print len(list)
194 if (result == 0):
195 for i in list:
196 print i.path
197
198
199[1mEXAMPLE[0m
200 #!/usr/bin/python
201
202 import lfc
203
204 """
205 # Using the lfc_getreplica method
206 """
207
208 result, list = lfc.lfc_getreplica("/grid/dteam/an‐
209totests/extratests/dir2/f105", "", "")
210 print result
211 print len(list)
212 if (result == 0):
213 for i in list:
214 print i.host
215 print i.sfn
216
217
218[1mEXAMPLE[0m
219 #!/usr/bin/python
220
221 import lfc
222
223 """
224 # Using the lfc_getacl and lfc_setacl methods to add a us‐
225er ACL
226 """
227
228 nentries, acls_list = lfc.lfc_geta‐
229cl("/grid/dteam/tests_sophie3", lfc.CA_MAXACLENTRIES)
230
231 print nentries
232 print len(acls_list)
233
234 for i in acls_list:
235 print i.a_type
236 print i.a_id
237 print i.a_perm
238
239 # When adding a first ACL for a given user, you also need
240to add the mask
241 # When adding the second user ACL, it is not necessary
242anymore
243
244 acl_user = lfc.lfc_acl()
245 acl_mask = lfc.lfc_acl()
246
247 acl_user.a_type=2 # 2 corresponds to CNS_ACL_USER
248 acl_user.a_id=18701 # user id
249 acl_user.a_perm=5
250
251 acl_mask.a_type=5 # 5 corresponds to CNS_ACL_MASK
252 acl_mask.a_id=0 # no user id specified
253 acl_mask.a_perm=5
254
255 acls_list.append(acl_user)
256 acls_list.append(acl_mask)
257
258 res = lfc.lfc_setacl("/grid/dteam/tests_sophie3",
259acls_list)
260
261 if res == 0:
262 print "OK"
263 else:
264 err_num = lfc.cvar.serrno
265 err_string = lfc.sstrerror(err_num)
266 print "There was an error : Error " + str(err_num)
267+ " (" + err_string + ")"
268 sys.exit(1)
269
270
271[1mEXAMPLE[0m
272 #!/usr/bin/python
273
274 import lfc
275
276 """
277 # Using the lfc_getacl and lfc_setacl methods to remove a
278user ACL
279 """
280
281 nentries, acls_list = lfc.lfc_geta‐
282cl("/grid/dteam/tests_sophie3", lfc.CA_MAXACLENTRIES)
283
284 # Note : you cannot remove the owner ACL (i.e. for
285CNS_ACL_USER_OBJ type) if ACLs
286 # ====== for other users exist. Ff all the other user ACLs
287are deleted, the owner
288 # ====== ACL is automatically removed.
289
290 for i in acls_list:
291 print i.a_type
292 print i.a_id
293 print i.a_perm
294
295 del acls_list[1] # delete a given user ACL from the
296list of ACLs
297
298 res = lfc.lfc_setacl("/grid/dteam/tests_sophie3",
299acls_list)
300
301 if res == 0:
302 print "OK"
303 else:
304 err_num = lfc.cvar.serrno
305 err_string = lfc.sstrerror(err_num)
306 print "There was an error : Error " + str(err_num)
307+ " (" + err_string + ")"
308 sys.exit(1)
309
310
311[1mEXAMPLE[0m
312 #!/usr/bin/env python
313 import lfcthr
314 import os
315 from threading import Thread
316
317 class slave(Thread):
318
319 def __init__ (self):
320 Thread.__init__(self)
321
322 def run(self):
323 ....
324 result = lfcthr.lfc_getreplica("", guid, "")
325 ....
326 return
327
328 if __name__ == ’__main__’:
329
330 os.environ[’LFC_HOST’] = ’my_lfc.cern.ch’
331
332 # Threaded library initialisation
333 lfcthr.init()
334
335 ....
336 # Start up of threads
337 for i in xrange(totalNumberOfSlaves):
338 slv = slave(i)
339 slv.start()
340
341 ....
342
343
344
345[1mEXAMPLE[0m
346 #!/usr/bin/python
347
348 import lfc
349
350 """
351 # Using the lfc_getusrmap method
352 """
353
354 result, list = lfc.lfc_getusrmap()
355 print result
356 print len(list)
357 if (result == 0):
358 for i in list:
359 print i.userid + " " + i.username
360
361
362[1mEXAMPLE[0m
363 #!/usr/bin/python
364
365 import lfc
366
367 """
368 # Using the lfc_getgrpmap method
369 """
370
371 result, list = lfc.lfc_getgrpmap()
372 print result
373 print len(list)
374 if (result == 0):
375 for i in list:
376 print i.gid + " " + i.groupname
377
378
379[1mKNOWN BUGS[0m
380 The current interface to the [1mlfc_getcom‐
381ment(3)[22m, [1mlfc_getcwd(3)[22m,
382 [1mlfc_readlink(3)[22m, [1mlfc_seterrbuf(3) [22mrequires
383the passing of str object
384 which is modified to contain the result (in a similar
385way to the C
386 functions, which accept a buffer). However this breaks
387the immutabil‐
388 ity of python str. This will be changed in the future.
389
390[1mSEE ALSO[0m
391 [1mLFC C interface man pages[0m
392
393
394
395LFC $Date$
396lfc_python(3)