/
lfc_dir_scan_threading.py
executable file
·99 lines (77 loc) · 2.64 KB
/
lfc_dir_scan_threading.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /usr/bin/env python
import os, sys, time, stat
import lfc
#import lfcthr as lfc
import threading
_n_threads = 10
_path_to_dir = '/grid/belle'
class slave(threading.Thread):
def __init__ (self, slave_id, path_to_dir):
super(slave, self).__init__()
self.slave_id_ = slave_id
self.path_to_dir_ = path_to_dir
def run(self):
print "slave number: ", self.slave_id_
self.scan_dir(self.path_to_dir_)
def print_error(self, msg):
err_num = lfc.cvar.serrno
err_string = lfc.sstrerror(err_num)
print >> sys.stderr, msg, "Error " + str(err_num) + " (" + err_string + ")"
return err_num
def print_GUID(self, path_to_file):
stat = lfc.lfc_filestatg()
res = lfc.lfc_statg(path_to_file, "", stat)
if res == 0:
guid = stat.guid
print path_to_file + ": " + guid
return 0
else:
return self.print_error("There was an error while looking for " + path_to_file)
def scan_dir(self, root_dir):
dir = lfc.lfc_opendirg(root_dir, "")
if (dir == None) or (dir == 0):
return self.print_error("Error while looking for " + root_dir)
while True:
read_pt = lfc.lfc_readdirxr(dir,"")
if (read_pt == None) or (read_pt == 0):
break
entry, list = read_pt
p = root_dir + '/' + entry.d_name
if stat.S_ISDIR(entry.filemode):
self.scan_dir(p)
else:
self.print_GUID(p)
try:
n_replica = len(list)
for i in range(n_replica):
print " ==> replica (%d/%d) %s" % (i+1, n_replica, list[i].sfn)
except TypeError, x:
print " ==> None"
lfc.lfc_closedir(dir)
return
def print_config(msg):
print >> sys.stderr, msg,
for i in range(5):
sys.stderr.write('.')
sys.stderr.flush()
time.sleep(1)
print >> sys.stderr
if __name__ == '__main__':
# os.environ['LFC_HOST'] = 'kek2-lfc.cc.kek.jp'
# Threaded library initialisation
#lfc.init()
#
# Start up of threads
if len(sys.argv) == 2:
n_threads = int(sys.argv[1])
path_to_dir = _path_to_dir
elif len(sys.argv) == 3:
n_threads = int(sys.argv[1])
path_to_dir = str(sys.argv[2])
else:
n_threads = _n_threads
path_to_dir = _path_to_dir
print_config('n_threads=' + str(n_threads) + ' path_to_dir=' + path_to_dir)
for i in xrange(n_threads):
slv = slave(i, path_to_dir)
slv.start()