This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid-bulk-del.py
198 lines (165 loc) · 5.08 KB
/
grid-bulk-del.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
import os
import re
import sys
import subprocess
from threading import Thread
from Queue import Queue
MAX_THREADS_DELETE = 5
def execute(name, arguments):
command = [name]
command += arguments
process = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
code = process.returncode
if (code == 0):
return stdout
else:
raise RuntimeError(stderr)
class CommandThread(Thread):
def __init__(self, command, arguments):
self.command = command
self.arguments = arguments
self.result = None
Thread.__init__(self)
def run(self):
try:
self.result = execute(self.command, self.arguments)
except RuntimeError:
print "Error executing %s %s" % (self.command, self.arguments)
os._exit(1)
class FileEraserThread(Thread):
def __init__(self, path):
self.path = path
self.result = None
Thread.__init__(self)
def run(self):
delete(self.path)
def delete(path):
lfn = ["lfn:" + path]
lcg_lg = CommandThread("lcg-lg", lfn)
lcg_lr = CommandThread("lcg-lr", lfn)
lcg_lg.start()
lcg_lr.start()
lcg_lg.join()
lcg_lr.join()
guid = lcg_lg.result.rstrip()
surls = re.split('\n', lcg_lr.result.rstrip())
lowlevel_delete(guid, surls, MAX_THREADS_DELETE)
print "\t%s: deleted" % lfn[0]
def exists(path):
exists = True
arguments = [path]
try:
output = execute("lfc-ls", arguments)
except RuntimeError:
exists = False
return exists
def islink(path):
islink = True
arguments = ["-l", path]
try:
long_list = execute("lfc-ls", arguments)
if not(long_list.startswith('l')):
islink = False
finally:
return islink
def isfile(path):
if islink(path):
return False
isfile = True
lfn = "lfn:" + path
arguments = ["-l", "-d", lfn]
try:
long_list = execute("lcg-ls", arguments)
if not(long_list.startswith('-')):
isfile = False
except RuntimeError:
isfile = False
return isfile
def isdir(path):
if islink(path):
return False
isdir = True
lfn = "lfn:" + path
arguments = ["-l", "-d", lfn]
try:
long_list= execute("lcg-ls", arguments)
if not(long_list.startswith('d')):
isdir = False
except RuntimeError:
isdir = False
return isdir
def lst_contents(path):
contents = list()
if isdir(path):
arguments = [path]
list_contents = execute("lfc-ls", arguments)
if list_contents: # if dir is not empty
nested_list = list_contents[:-1].split(os.linesep)
for item in nested_list:
subpath = os.path.join(path, item)
subcontents = lst_contents(subpath)
contents.extend(subcontents)
elif isfile(path):
contents.append(path)
return contents
def lowlevel_delete(guid, surls, concurrent_process):
finished = []
def producer(queue, guid, surls):
for surl in surls:
arguments = [guid, surl]
thread = CommandThread("lcg-uf", arguments)
thread.start()
queue.put(thread, True)
def consumer(queue, surls):
while len(finished) < surls:
thread = queue.get(True)
thread.join()
finished.append(thread.result)
queue = Queue(concurrent_process)
producer = Thread(target=producer, args=(queue, guid, surls))
consumer = Thread(target=consumer, args=(queue, len(surls)))
producer.start()
consumer.start()
producer.join()
consumer.join()
def delete_directory(path, process):
archives = lst_contents(path)
finished = list()
queue = Queue(process)
def producer(queue, files):
for archive in archives:
thread = FileEraserThread(archive)
thread.start()
queue.put(thread, True)
def consumer(queue, archives):
while len(finished) < archives:
thread = queue.get(True)
thread.join()
finished.append(thread.result)
producer = Thread(target=producer, args=(queue, archives))
consumer = Thread(target=consumer, args=(queue, len(archives)))
producer.start()
consumer.start()
producer.join()
consumer.join()
arguments = ["-r", path]
execute("lfc-rm", arguments)
def main(argv):
path = argv[0]
if exists(path):
if isfile(path):
delete(path)
elif isdir(path):
message = "Do you really want to delete " + path + " and ALL its contents? (yes/no): "
yesno = raw_input(message)
if (yesno == 'yes'):
delete_directory(path, 5)
else:
message = path + ": No such file or directory" + os.linesep
sys.stderr.write(message)
if __name__=="__main__":
main(sys.argv[1:])