1 |
#!/usr/bin/env python
|
2 |
#---------------------------------------------------------------------------------------------------
|
3 |
# Simple interface to command line DBS to prepare my crabTask input files.
|
4 |
#---------------------------------------------------------------------------------------------------
|
5 |
import os,sys,types,string,getopt
|
6 |
|
7 |
# Define string to explain usage of the script
|
8 |
usage = "Usage: input.py --dataset=<name>\n"
|
9 |
usage += " --option=[ lfn, xml ]\n"
|
10 |
usage += " --help\n"
|
11 |
|
12 |
def printLine(option,nEvents,block,lfn,iJob):
|
13 |
if option == 'xml':
|
14 |
print ' <Job MaxEvents="%d'%nEvents + '" InputFiles="' + lfn \
|
15 |
+ '" SkipEvents="0" JobID="%d'%iJob + '" > </Job>'
|
16 |
else:
|
17 |
print "%s %s %d"%(block,lfn,nEvents)
|
18 |
|
19 |
|
20 |
# Define the valid options which can be specified and check out the command line
|
21 |
valid = ['db=','dataset=','option=','help']
|
22 |
try:
|
23 |
opts, args = getopt.getopt(sys.argv[1:], "", valid)
|
24 |
except getopt.GetoptError, ex:
|
25 |
print usage
|
26 |
print str(ex)
|
27 |
sys.exit(1)
|
28 |
|
29 |
# --------------------------------------------------------------------------------------------------
|
30 |
# Get all parameters for the production
|
31 |
# --------------------------------------------------------------------------------------------------
|
32 |
# Set defaults for each option
|
33 |
db = None
|
34 |
dataset = None
|
35 |
option = 'lfn'
|
36 |
|
37 |
# Read new values from the command line
|
38 |
for opt, arg in opts:
|
39 |
if opt == "--help":
|
40 |
print usage
|
41 |
sys.exit(0)
|
42 |
if opt == "--db":
|
43 |
db = arg
|
44 |
if opt == "--dataset":
|
45 |
dataset = arg
|
46 |
if opt == "--option":
|
47 |
option = arg
|
48 |
|
49 |
# Deal with obvious problems
|
50 |
if dataset == None:
|
51 |
cmd = "--dataset= required parameter not provided."
|
52 |
raise RuntimeError, cmd
|
53 |
|
54 |
#---------------------------------------------------------------------------------------------------
|
55 |
# main
|
56 |
#---------------------------------------------------------------------------------------------------
|
57 |
if not db:
|
58 |
# find relevant blocks
|
59 |
cmd = "dbs search --query=\"find block where dataset=" + dataset + "\""
|
60 |
cmd += "| grep \# | sort"
|
61 |
blocks = []
|
62 |
iJob = 1
|
63 |
if option == 'xml':
|
64 |
print '<arguments>'
|
65 |
for line in os.popen(cmd).readlines():
|
66 |
line = line[:-1]
|
67 |
blocks.append(line)
|
68 |
for block in blocks:
|
69 |
cmd = "dbs search --query=\"find file,file.numevents where block=" + block + "\""
|
70 |
cmd += "| grep store | sort"
|
71 |
for line in os.popen(cmd).readlines():
|
72 |
line = line[:-1]
|
73 |
f = line.split()
|
74 |
lfn = f[0]
|
75 |
nEvents = int(f[1])
|
76 |
f = lfn.split("/")
|
77 |
file = f[-1]
|
78 |
if nEvents != 0:
|
79 |
printLine(option,nEvents,block,lfn,iJob)
|
80 |
iJob = iJob + 1
|
81 |
if option == 'xml':
|
82 |
print '</arguments>'
|
83 |
|
84 |
if db:
|
85 |
cmd = 'cat ' + db
|
86 |
|
87 |
iJob = 1
|
88 |
if option == 'xml':
|
89 |
print '<arguments>'
|
90 |
for line in os.popen(cmd).readlines():
|
91 |
line = line[:-1]
|
92 |
|
93 |
f = line.split()
|
94 |
block = f[0]
|
95 |
lfn = f[1]
|
96 |
nEvents = int(f[2])
|
97 |
|
98 |
f = lfn.split("/")
|
99 |
file = f[-1]
|
100 |
|
101 |
if nEvents != 0:
|
102 |
printLine(option,nEvents,block,lfn,iJob)
|
103 |
iJob = iJob + 1
|
104 |
|
105 |
if option == 'xml':
|
106 |
print '</arguments>'
|