-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsrm-hammer.py
More file actions
executable file
·147 lines (120 loc) · 4.25 KB
/
srm-hammer.py
File metadata and controls
executable file
·147 lines (120 loc) · 4.25 KB
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
#!/usr/bin/python
#
# Don't use this brittle script for anything
#
import subprocess
import sys
import os
import shlex
import time
import random
from optparse import OptionParser
class SRMFile:
def __init__(self,filepath,destdir):
self.filepath = filepath.rstrip()
self.filename = os.path.basename(self.filepath).rstrip()
self.process = ""
self.isstarted = False
self.command = ""
self.start_time = 0.0
self.destdir = destdir
def filename(self):
return self.filename
def getprocess(self):
return self.process
def get_command(self):
return self.command
def get_start_time(self):
return self.start_time
def start(self):
#self.process =subprocess.Popen(["sleep",str(random.randint(1,10))])
self.command = "%s %s %s" % ("lcg-cp", self.filepath, "%s/%s" % (self.destdir,self.filename) )
print self.command
self.start_time = time.time()
self.process = subprocess.Popen(["lcg-cp", self.filepath, "%s/%s" % (self.destdir,self.filename) ])
self.isstarted = True
def kill(self):
try:
self.process.kill()
print "Killed: %s" % self.command
except (OSError):
print "Failed to kill %s" % self.command
def isstarted(self):
return self.is_started
def isdone(self):
if self.process:
return self.process.poll()
else:
return None
def printstatus(waiting, running, finished):
print "%s %s %s" % (waiting, running, finished)
def main():
try:
threads = 1
filelist = ""
destdir = ""
timeout = 60.0
parser = OptionParser(usage="%prog -f FILELIST -d DESTDIR -t NUMTHREADS -o TIMEOUT",version = "%prog 0.1")
parser.add_option("-f", "--file-list", dest="filelist",
help="list of files to transfer", metavar="FILELIST")
parser.add_option("-t","--threads",dest="threads",
help="the number of transfers to do in parallel", metavar="NUMTHREADS")
parser.add_option("-d", "--dest-dir", dest="destdir",
help="destination directory", metavar="DESTDIR")
parser.add_option("-o", "--timeout", dest="timeout",
help="transfer timeout", metavar="TIMEOUT")
(options, args) = parser.parse_args()
if not options.filelist:
parser.print_help()
parser.error("You must specify a file list")
if not options.threads:
options.threads = 1
if not options.destdir:
destdir ="."
else:
destdir = options.destdir
if options.timeout:
timeout = float(options.timeout)
if not os.path.exists(options.filelist):
parser.print_help()
parser.error("The file: " + options.filelist + " does not exist.")
filelist = options.filelist
threads = int(options.threads)
except:
raise
files = []
waiting = []
running = []
finished = []
print "Running %s threads" % threads
with open(filelist) as f:
files = f.readlines()
for file in files:
waiting.append(SRMFile(file, destdir))
while len(waiting) or len(running):
for transfer in running:
if 0 == transfer.isdone():
finished.append(transfer)
if (time.time() - transfer.get_start_time()) > timeout:
transfer.kill()
finished.append(transfer)
for transfer in finished:
if running.count(transfer):
running.remove(transfer)
printstatus(len(waiting), len(running), len(finished))
while (len(running) < threads) and (len(waiting) > 0):
current = waiting.pop()
current.start()
running.append(current)
printstatus(len(waiting), len(running), len(finished))
time.sleep(1);
for transfer in finished:
stdoutdata = ""
stderrdata = ""
(stdoutdata, stderrdata) = transfer.getprocess().communicate()
if stdoutdata or stderrdata:
print transfer.command()
print "Output this:"
print stdoutdata
print stderrdata
main()