1 |
#!/usr/bin/env perl
|
2 |
|
3 |
use strict;
|
4 |
use Mysql;
|
5 |
|
6 |
sub getWorkingDir;
|
7 |
sub dbUpdate;
|
8 |
sub dbStatus;
|
9 |
sub dbReport;
|
10 |
sub dbInsert;
|
11 |
sub addSlashes;
|
12 |
sub getOption;
|
13 |
sub deleteEntry;
|
14 |
|
15 |
our $db = Mysql->connect ("cmshead.mps.ohio-state.edu", "ntuple", "osuT3User") or die "Failed to connect to Tier 3, stopped";
|
16 |
|
17 |
my $arg = join (" ", @ARGV);
|
18 |
if ($arg =~ m/-create/)
|
19 |
{
|
20 |
my $crabCfgName = $arg;
|
21 |
$crabCfgName =~ s/.*-cfg *([^ \t\n]*).*/$1/;
|
22 |
open (CRAB_CFG, "<$crabCfgName");
|
23 |
my @crabCfg = <CRAB_CFG>;
|
24 |
close (CRAB_CFG);
|
25 |
my $dataset = getOption (\@crabCfg, "datasetpath");
|
26 |
my $psetName = getOption (\@crabCfg, "pset");
|
27 |
my $jsonFileName = getOption (\@crabCfg, "lumi_mask");
|
28 |
my $workingDir = getOption (\@crabCfg, "ui_working_dir");
|
29 |
|
30 |
print "Ntuple format (BEAN): ";
|
31 |
my $format = <STDIN>;
|
32 |
$format =~ s/\n//g;
|
33 |
$format = "BEAN" if !$format;
|
34 |
print "Comment for the ntuple database: ";
|
35 |
my $comment = <STDIN>;
|
36 |
$comment =~ s/\n//g;
|
37 |
|
38 |
system ("crab $arg");
|
39 |
|
40 |
my $status = "created";
|
41 |
$status = "submitted" if $arg =~ m/-submit/;
|
42 |
my $results = dbInsert ($dataset, "$ENV{'USER'}\@$ENV{'HOSTNAME'}", $format, "", 0, 0, $status, $comment, $psetName, $crabCfgName, $jsonFileName, $workingDir);
|
43 |
}
|
44 |
elsif ($arg =~ m/-submit/)
|
45 |
{
|
46 |
my $workingDir = getWorkingDir (\@ARGV);
|
47 |
my $id = -1;
|
48 |
if (-e "$workingDir/log/.osuID")
|
49 |
{
|
50 |
open (ID_FILE, "<$workingDir/log/.osuID");
|
51 |
$id = <ID_FILE>;
|
52 |
close (ID_FILE);
|
53 |
$id =~ s/(.*)\n/$1/;
|
54 |
}
|
55 |
|
56 |
system ("crab $arg");
|
57 |
|
58 |
my $results = dbUpdate ($id, "submitted", "$ENV{'USER'}\@$ENV{'HOSTNAME'}") if $id > -1;
|
59 |
}
|
60 |
elsif ($arg =~ m/-report/)
|
61 |
{
|
62 |
my $workingDir = getWorkingDir (\@ARGV);
|
63 |
my $id = -1;
|
64 |
if (-e "$workingDir/log/.osuID")
|
65 |
{
|
66 |
open (ID_FILE, "<$workingDir/log/.osuID");
|
67 |
$id = <ID_FILE>;
|
68 |
close (ID_FILE);
|
69 |
$id =~ s/(.*)\n/$1/;
|
70 |
}
|
71 |
my $lumiSummaryName = $workingDir . "/res/lumiSummary.json";
|
72 |
|
73 |
system ("crab $arg");
|
74 |
|
75 |
my $results = dbReport ($id, "complete", $lumiSummaryName, "$ENV{'USER'}\@$ENV{'HOSTNAME'}") if $id > -1;
|
76 |
}
|
77 |
elsif ($arg =~ m/-status/)
|
78 |
{
|
79 |
my $workingDir = getWorkingDir (\@ARGV);
|
80 |
my $id = -1;
|
81 |
if (-e "$workingDir/log/.osuID")
|
82 |
{
|
83 |
open (ID_FILE, "<$workingDir/log/.osuID");
|
84 |
$id = <ID_FILE>;
|
85 |
close (ID_FILE);
|
86 |
$id =~ s/(.*)\n/$1/;
|
87 |
}
|
88 |
my $currentStatusName = "$workingDir/log/.currentStatus";
|
89 |
unlink ($currentStatusName) if -e $currentStatusName;
|
90 |
system ("crab $arg | tee $currentStatusName");
|
91 |
my $results = dbStatus ($id, $currentStatusName, "$ENV{'USER'}\@$ENV{'HOSTNAME'}") if $id > -1;
|
92 |
unlink ($currentStatusName);
|
93 |
}
|
94 |
elsif ($arg =~ m/-kill/)
|
95 |
{
|
96 |
my $workingDir = getWorkingDir (\@ARGV);
|
97 |
my $id = -1;
|
98 |
if (-e "$workingDir/log/.osuID")
|
99 |
{
|
100 |
open (ID_FILE, "<$workingDir/log/.osuID");
|
101 |
$id = <ID_FILE>;
|
102 |
close (ID_FILE);
|
103 |
$id =~ s/(.*)\n/$1/;
|
104 |
}
|
105 |
|
106 |
print "Are you cancelling this task? (y/N): ";
|
107 |
my $response = <STDIN>;
|
108 |
$response =~ s/\n//g;
|
109 |
deleteEntry ($id) if lc ($response) eq "y";
|
110 |
system ("crab $arg");
|
111 |
}
|
112 |
else
|
113 |
{
|
114 |
system ("crab $arg");
|
115 |
}
|
116 |
|
117 |
sub
|
118 |
getWorkingDir
|
119 |
{
|
120 |
my $argv = shift;
|
121 |
|
122 |
my $workingDir;
|
123 |
my $nextToLast = 0;
|
124 |
foreach my $argument (@$argv)
|
125 |
{
|
126 |
if ($nextToLast)
|
127 |
{
|
128 |
$workingDir = $argument;
|
129 |
last;
|
130 |
}
|
131 |
$nextToLast = 1 if ($argument eq "-c" || $argument eq "-continue");
|
132 |
}
|
133 |
|
134 |
return $workingDir;
|
135 |
}
|
136 |
|
137 |
sub
|
138 |
getOption
|
139 |
{
|
140 |
my $file = shift;
|
141 |
my $option = shift;
|
142 |
|
143 |
my $optionValue;
|
144 |
foreach my $line (@$file)
|
145 |
{
|
146 |
next if (!($line =~ m/$option *= *[^ \t\n]*/));
|
147 |
$optionValue = $line;
|
148 |
$optionValue =~ s/.*$option *= *([^ \t\n]*).*\n/$1/;
|
149 |
}
|
150 |
|
151 |
return $optionValue;
|
152 |
}
|
153 |
|
154 |
sub
|
155 |
addSlashes
|
156 |
{
|
157 |
my $string = shift;
|
158 |
|
159 |
$string =~ s/\\/\\\\/g;
|
160 |
$string =~ s/'/\\'/g;
|
161 |
$string =~ s/"/\\"/g;
|
162 |
$string =~ s/\\0/\\\\0/g;
|
163 |
|
164 |
return $string;
|
165 |
}
|
166 |
|
167 |
sub
|
168 |
dbInsert
|
169 |
{
|
170 |
my $dataset = shift;
|
171 |
my $userAndHost = shift;
|
172 |
my $format = shift;
|
173 |
my $location = shift;
|
174 |
my $nFiles = shift;
|
175 |
my $sizeInGB = shift;
|
176 |
my $status = shift;
|
177 |
my $comment = shift;
|
178 |
my $psetName = shift;
|
179 |
my $crabCfgName = shift;
|
180 |
my $jsonFileName = shift;
|
181 |
my $workingDir = shift;
|
182 |
|
183 |
my $user = $userAndHost;
|
184 |
$user =~ s/@.*$//;
|
185 |
|
186 |
open (PY_CONFIG, "<$psetName");
|
187 |
my @pset = <PY_CONFIG>;
|
188 |
close (PY_CONFIG);
|
189 |
my $pset = join ("", @pset);
|
190 |
open (CRAB_CONFIG, "<$crabCfgName");
|
191 |
my @crabCfg = <CRAB_CONFIG>;
|
192 |
close (CRAB_CONFIG);
|
193 |
my $crabCfg = join ("", @crabCfg);
|
194 |
open (JSON_FILE, "<$jsonFileName");
|
195 |
my @jsonFile = <JSON_FILE>;
|
196 |
close (JSON_FILE);
|
197 |
my $jsonFile = join ("", @jsonFile);
|
198 |
|
199 |
$dataset = addSlashes ($dataset);
|
200 |
$user = addSlashes ($user);
|
201 |
$format = addSlashes ($format);
|
202 |
$pset = addSlashes ($pset);
|
203 |
$crabCfg = addSlashes ($crabCfg);
|
204 |
$jsonFile = addSlashes ($jsonFile);
|
205 |
$location = addSlashes ($location);
|
206 |
$status = addSlashes ($status);
|
207 |
$comment = addSlashes ($comment);
|
208 |
|
209 |
my $results;
|
210 |
my $query = "select max(id) from ntuple";
|
211 |
$db->selectdb ("ntuple");
|
212 |
$results = $db->query ($query);
|
213 |
my @row = $results->fetchrow ();
|
214 |
my $id = 1;
|
215 |
$id = $row[0] + 1 if $results->numrows ();
|
216 |
|
217 |
open (ID_FILE, ">$workingDir/log/.osuID");
|
218 |
print ID_FILE "$id\n";
|
219 |
close (ID_FILE);
|
220 |
|
221 |
$query = "insert into ntuple (id, dataset, creationTime, lastUpdateTime, lastUpdateUser, user, format, location, nFiles, sizeInGB, status, comment, pset, crabCfg, jsonFile) values ($id, '$dataset', now(), now(), '$userAndHost', '$user', '$format', '$location', $nFiles, $sizeInGB, '$status', '$comment', '$pset', '$crabCfg', '$jsonFile')";
|
222 |
$results = $db->query ($query);
|
223 |
|
224 |
return $results;
|
225 |
}
|
226 |
|
227 |
sub
|
228 |
dbUpdate
|
229 |
{
|
230 |
my $id = shift;
|
231 |
my $status = shift;
|
232 |
my $user = shift;
|
233 |
|
234 |
$status = addSlashes ($status);
|
235 |
$user = addSlashes ($user);
|
236 |
|
237 |
my $results;
|
238 |
my $query = "update ntuple set lastUpdateTime=now(), lastUpdateUser='$user', status='$status' where id=$id";
|
239 |
$db->selectdb ("ntuple");
|
240 |
$results = $db->query ($query);
|
241 |
|
242 |
return $results;
|
243 |
}
|
244 |
|
245 |
sub
|
246 |
dbStatus
|
247 |
{
|
248 |
my $id = shift;
|
249 |
my $currentStatusName = shift;
|
250 |
my $user = shift;
|
251 |
|
252 |
open (CURRENT_STATUS, "<$currentStatusName");
|
253 |
my @currentStatus = <CURRENT_STATUS>;
|
254 |
close (CURRENT_STATUS);
|
255 |
my $currentStatus = join ("", @currentStatus);
|
256 |
|
257 |
$currentStatus = addSlashes ($currentStatus);
|
258 |
$user = addSlashes ($user);
|
259 |
|
260 |
my $results;
|
261 |
my $query = "update ntuple set lastUpdateTime=now(), lastUpdateUser='$user', currentStatus='$currentStatus' where id=$id";
|
262 |
$db->selectdb ("ntuple");
|
263 |
$results = $db->query ($query);
|
264 |
|
265 |
return $results;
|
266 |
}
|
267 |
|
268 |
sub
|
269 |
dbReport
|
270 |
{
|
271 |
my $id = shift;
|
272 |
my $status = shift;
|
273 |
my $lumiSummaryName = shift;
|
274 |
my $user = shift;
|
275 |
|
276 |
open (LUMI_SUMMARY, "<$lumiSummaryName");
|
277 |
my @lumiSummary = <LUMI_SUMMARY>;
|
278 |
close (LUMI_SUMMARY);
|
279 |
my $lumiSummary = join ("", @lumiSummary);
|
280 |
|
281 |
$lumiSummary = addSlashes ($lumiSummary);
|
282 |
$status = addSlashes ($status);
|
283 |
$user = addSlashes ($user);
|
284 |
|
285 |
my $results;
|
286 |
my $query = "update ntuple set lastUpdateTime=now(), lastUpdateUser='$user', status='$status', lumiSummary='$lumiSummary' where id=$id";
|
287 |
$db->selectdb ("ntuple");
|
288 |
$results = $db->query ($query);
|
289 |
|
290 |
return $results;
|
291 |
}
|
292 |
|
293 |
sub
|
294 |
deleteEntry
|
295 |
{
|
296 |
my $id = shift;
|
297 |
|
298 |
my $results;
|
299 |
my $query = "delete from ntuple where id=$id";
|
300 |
$db->selectdb ("ntuple");
|
301 |
$results = $db->query ($query);
|
302 |
|
303 |
return $results;
|
304 |
}
|