ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osudb
Revision: 1.27
Committed: Thu Jun 20 00:49:02 2013 UTC (11 years, 10 months ago) by ahart
Branch: MAIN
CVS Tags: V02-03-02, V02-03-01, V02-03-00
Changes since 1.26: +4 -6 lines
Log Message:
Modified the help messages for the "-x" and "-y" options to better reflect their intended usages.

File Contents

# Content
1 #!/usr/bin/env perl
2
3 use strict;
4 use Mysql;
5 use Getopt::Long;
6 use File::Copy;
7 use Term::ANSIColor;
8
9 sub getDataset;
10 sub dbUpdate;
11 sub dbUpdateOther;
12 sub dbDelete;
13 sub dbDeleteOther;
14 sub addSlashes;
15 sub uploadRelease;
16 sub downloadRelease;
17 sub datasetExists;
18 sub uploadConfig;
19 sub locationExists;
20 sub getLocation;
21
22 our $db = Mysql->connect ("cmshead.mps.ohio-state.edu", "ntuple", "osuT3User") or die "Failed to connect to Tier 3, stopped";
23
24 my %opt;
25 Getopt::Long::Configure ("bundling");
26 GetOptions (\%opt, "comment|c=s", "format|f=s", "pyConfig|p=s", "crabCfg|b=s", "jsonFile|j=s", "lumiSummary|s=s", "location|l=s", "name|n=s", "fileList|t=s", "release|r=s", "other|o", "recipe|e=s", "globalTag|g=s", "configName|i=s", "xSection|x=s", "higherOrderXSec|y=s", "help|h");
27 my $argc = @ARGV;
28
29 printHelp ($ARGV[0]) if $opt{"help"};
30 printHelp () if $argc != 2 && $ARGV[0] ne "createOther";
31 printHelp () if $ARGV[0] ne "create" && $ARGV[0] ne "createOther" && $ARGV[0] ne "update" && $ARGV[0] ne "finish" && $ARGV[0] ne "deprecate" && $ARGV[0] ne "uploadRelease" && $ARGV[0] ne "downloadRelease" && $ARGV[0] ne "deleteEntry" && $ARGV[0] ne "uploadConfig";
32 if (($ARGV[0] eq "create" || $ARGV[0] eq "finish") && !$opt{"location"})
33 {
34 print "The directory containing the ntuples must be given!\n";
35 exit;
36 }
37 if ($ARGV[0] eq "uploadRelease" && (!$opt{"pyConfig"} || !$opt{"release"} || !$opt{"recipe"}))
38 {
39 print "The Python config, CMSSW release directory, and recipe file must be given!\n";
40 exit;
41 }
42 if ($ARGV[0] eq "uploadConfig" && (!$opt{"pyConfig"} || !$opt{"release"}))
43 {
44 print "The Python config and ntuple release must be given!\n";
45 exit;
46 }
47 if ($ARGV[0] eq "uploadRelease")
48 {
49 my $parentDir = "./$opt{'release'}";
50 $parentDir =~ s/CMSSW_[^\/]*//g;
51 $parentDir =~ s/\/\/*/\//g;
52 my $cmsswRelease = $opt{'release'};
53 $cmsswRelease =~ s/^.*CMSSW_([^\/]*).*$/CMSSW_$1/;
54 `tar -C $parentDir -czf $ARGV[1].tar.gz $opt{"release"}`;
55 $opt{"format"} = "BEAN" if !$opt{"format"};
56 uploadRelease ($opt{"format"}, $cmsswRelease, "$ARGV[1].tar.gz", $opt{"pyConfig"}, $ARGV[1], "$ENV{'USER'}", $opt{"comment"}, $opt{"recipe"});
57 }
58 elsif ($ARGV[0] eq "downloadRelease")
59 {
60 downloadRelease ($ARGV[1], $opt{"pyConfig"});
61 }
62 elsif ($ARGV[0] eq "uploadConfig")
63 {
64 uploadConfig ($ARGV[1], $ENV{"USER"}, $opt{"pyConfig"}, $opt{"release"}, $opt{"comment"});
65 }
66 elsif ($ARGV[0] eq "createOther")
67 {
68 my @listOfFiles = @ARGV;
69 @listOfFiles = reverse (@listOfFiles);
70 pop (@listOfFiles);
71 @listOfFiles = reverse (@listOfFiles);
72 dbOtherCreate ("$ENV{'USER'}\@$ENV{'HOSTNAME'}", \@listOfFiles, $opt{"comment"});
73 }
74 else
75 {
76 my $id = -1;
77 my $fullDataset;
78 ($id, $fullDataset) = getDataset ($ARGV[0], $ARGV[1]) if $ARGV[0] ne "create" && ($ARGV[0] ne "update" || !$opt{"other"}) && ($ARGV[0] ne "deleteEntry" || !$opt{"other"});
79 $id = $ARGV[1] if ($ARGV[0] eq "update" && $opt{"other"}) || ($ARGV[0] eq "deleteEntry" && $opt{"other"});
80 $fullDataset = $ARGV[1] if $id < 0;
81 $ARGV[0] = "create" if $id < 0;
82 my $status = "present";
83 $status = "" if $ARGV[0] eq "update";
84 $status = "deprecated" if $ARGV[0] eq "deprecate";
85 $opt{"format"} = "BEAN" if $ARGV[0] eq "create" && !$opt{"format"};
86 if ($opt{"location"} && locationExists ($id, $opt{"location"}))
87 {
88 print "This location is already registered in the database!\n";
89 exit;
90 }
91 dbUpdate ($id, $fullDataset, "$ENV{'USER'}\@$ENV{'HOSTNAME'}", $opt{"format"}, $opt{"location"}, $opt{"name"}, $opt{"fileList"}, $status, $opt{"comment"}, $opt{"pyConfig"}, $opt{"crabCfg"}, $opt{"jsonFile"}, $opt{"lumiSummary"}, $opt{"release"}, $opt{"globalTag"}, $opt{"configName"}, $opt{"xSection"}, $opt{"higherOrderXSec"}) if $ARGV[0] ne "deleteEntry" && !$opt{"other"};
92 dbUpdateOther ($id, $opt{"comment"}, "$ENV{'USER'}\@$ENV{'HOSTNAME'}") if $ARGV[0] ne "deleteEntry" && $opt{"other"};
93 dbDelete ($id) if $ARGV[0] eq "deleteEntry" && !$opt{"other"};
94 dbDeleteOther ($id) if $ARGV[0] eq "deleteEntry" && $opt{"other"};
95 }
96
97 sub
98 addSlashes
99 {
100 my $string = shift;
101
102 $string =~ s/\\/\\\\/g;
103 $string =~ s/'/\\'/g;
104 $string =~ s/"/\\"/g;
105 $string =~ s/\\0/\\\\0/g;
106
107 return $string;
108 }
109
110 sub
111 getDataset
112 {
113 my $command = shift;
114 my $dataset = shift;
115
116 my $results;
117 my $queryDataset = $dataset;
118 $queryDataset =~ s/\*/%/g;
119 $queryDataset =~ s/(.*)/%$1%/g;
120 my $query = "select id,dataset,user,creationTime,status from ntuple where dataset like '$queryDataset' order by creationTime";
121 $db->selectdb ("ntuple");
122 $results = $db->query ($query);
123 if ($results->numrows () == 1)
124 {
125 my @row = $results->fetchrow ();
126 return ($row[0], $row[1]);
127 }
128 if ($results->numrows () == 0)
129 {
130 if ($command ne "deleteEntry" && $command ne "deprecate")
131 {
132 print "Database entry does not exist. Create it? (Y/n): ";
133 my $response = <STDIN>;
134 $response =~ s/\n//g;
135 $response = "y" if !$response;
136 exit if substr (lc ($response), 0, 1) ne 'y';
137 return (-1, "");
138 }
139 else
140 {
141 print "Database entry does not exist.\n";
142 exit;
143 }
144 }
145 my %id;
146 my %fullDataset;
147 print "Found multiple database entries matching\n";
148 print "\"$dataset\":\n";
149 print "( 0) new\n" if $command ne "deleteEntry" && $command ne "deprecate";
150 for (my $i = 1; $i <= $results->numrows (); $i++)
151 {
152 my @row = $results->fetchrow ();
153 $row[3] =~ s/([^ ]*) [^ ]*/$1/g;
154 $id{"$i"} = $row[0];
155 $fullDataset{"$i"} = $row[1];
156 printf "(%2d) $row[1]\n", $i;
157 print " (";
158 print color "green" if $row[4] eq "present";
159 print color "bold yellow" if $row[4] eq "submitted";
160 print color "bold red" if $row[4] eq "created" or $row[4] eq "cancelled" or $row[4] eq "deprecated";
161 print $row[4];
162 print color "reset";
163 print ") created by $row[2] on $row[3]\n";
164 }
165 print "\nWhich entry would you like to modify?";
166 if ($command ne "deleteEntry" && $command ne "deprecate")
167 {
168 print " (Select 0 to create a new entry): "
169 }
170 else
171 {
172 print ": ";
173 }
174 my $response = <STDIN>;
175 $response =~ s/[ \t\n]//g;
176 return (-1, "") if $response == 0 && $command ne "deleteEntry" && $command ne "deprecate";
177 if (!(exists $id{$response}))
178 {
179 print "Your selection \"$response\" was not a valid option! Quitting.\n";
180 exit;
181 }
182
183 return ($id{$response}, $fullDataset{$response});
184 }
185
186 sub
187 dbUpdate
188 {
189 my $id = shift;
190 my $dataset = shift;
191 my $userAndHost = shift;
192 my $format = shift;
193 my $location = shift;
194 my $name = shift;
195 my $fileListName = shift;
196 my $status = shift;
197 my $comment = shift;
198 my $psetName = shift;
199 my $crabCfgName = shift;
200 my $jsonFileName = shift;
201 my $lumiSummaryName = shift;
202 my $release = shift;
203 my $globalTag = shift;
204 my $configName = shift;
205 my $xSection = shift;
206 my $ySection = shift;
207
208 $location = getLocation ($id) if $id > 0
209 && !$crabCfgName
210 && !$comment
211 && !$format
212 && !$globalTag
213 && !$configName
214 && !$jsonFileName
215 && !$location
216 && !$name
217 && !$psetName
218 && !$release
219 && !$lumiSummaryName
220 && !$xSection
221 && !$ySection;
222
223 my $user = $userAndHost;
224 $user =~ s/@.*$//g;
225
226 my $fileList;
227 my $pset;
228 my $crabCfg;
229 my $jsonFile;
230 my $lumiSummary;
231 my $size = 0;
232 my $nFiles = 0;
233 if ($fileListName)
234 {
235 if (!(-e $fileListName))
236 {
237 print "$fileListName does not exist!\n";
238 exit;
239 }
240 open (FILE_LIST, "<$fileListName");
241 my @fileList = <FILE_LIST>;
242 close (FILE_LIST);
243 $fileList = join ("", @fileList);
244 }
245 if ($psetName)
246 {
247 if (!(-e $psetName))
248 {
249 print "$psetName does not exist!\n";
250 exit;
251 }
252 open (PY_CONFIG, "<$psetName");
253 my @pset = <PY_CONFIG>;
254 close (PY_CONFIG);
255 $pset = join ("", @pset);
256 }
257 if ($crabCfgName)
258 {
259 if (!(-e $crabCfgName))
260 {
261 print "$crabCfgName does not exist!\n";
262 exit;
263 }
264 open (CRAB_CONFIG, "<$crabCfgName");
265 my @crabCfg = <CRAB_CONFIG>;
266 close (CRAB_CONFIG);
267 $crabCfg = join ("", @crabCfg);
268 }
269 if ($jsonFileName)
270 {
271 if (!(-e $jsonFileName))
272 {
273 print "$jsonFileName does not exist!\n";
274 exit;
275 }
276 open (JSON_FILE, "<$jsonFileName");
277 my @jsonFile = <JSON_FILE>;
278 close (JSON_FILE);
279 $jsonFile = join ("", @jsonFile);
280 }
281 if ($lumiSummaryName)
282 {
283 if (!(-e $lumiSummaryName))
284 {
285 print "$lumiSummaryName does not exist!\n";
286 exit;
287 }
288 open (LUMI_SUMMARY, "<$lumiSummaryName");
289 my @lumiSummary = <LUMI_SUMMARY>;
290 close (LUMI_SUMMARY);
291 $lumiSummary = join ("", @lumiSummary);
292 }
293 $release .= ".$configName" if $configName;
294 ($nFiles, $size) = sizeOfDataset ($location, $fileList);
295 my $fullLocation = $location;
296 $fullLocation = "$ENV{'PWD'}/$location" if !($location =~ m/^\//);
297
298 $dataset = addSlashes ($dataset);
299 $user = addSlashes ($user);
300 $format = addSlashes ($format);
301 $pset = addSlashes ($pset);
302 $crabCfg = addSlashes ($crabCfg);
303 $jsonFile = addSlashes ($jsonFile);
304 $lumiSummary = addSlashes ($lumiSummary);
305 $fullLocation = addSlashes ($fullLocation);
306 $name = addSlashes ($name);
307 $fileList = addSlashes ($fileList);
308 $status = addSlashes ($status);
309 $comment = addSlashes ($comment);
310 $release = addSlashes ($release);
311 $globalTag = addSlashes ($globalTag);
312
313 my $results;
314 my $query;
315 if ($id < 0)
316 {
317 $query = "select id from ntuple where dataset='$dataset'";
318 $db->selectdb ("ntuple");
319 $results = $db->query ($query);
320 if ($results->numrows ())
321 {
322 print "This dataset already exists in the database. Create anyway? (y/N): ";
323 my $response = <STDIN>;
324 $response =~ s/\n//g;
325 $response = "n" if !$response;
326 exit if substr (lc ($response), 0, 1) ne 'y';
327 }
328
329 $query = "select max(id) from ntuple";
330 $db->selectdb ("ntuple");
331 $results = $db->query ($query);
332 my @row = $results->fetchrow ();
333 my $id = 1;
334 $id = $row[0] + 1 if $results->numrows ();
335
336 my $columns = "(id, dataset, creationTime, lastUpdateTime, lastUpdateUser, user, format, location, fileList, nFiles, sizeInGB, status, comment, pset, crabCfg, jsonFile, lumiSummary, version, globalTag";
337 $columns .= ", crossSectionInPicobarn" if $xSection;
338 $columns .= ", higherOrderCrossSectionInPicobarn" if $ySection;
339 $columns .= ")";
340 my $values = "($id, '$dataset', now(), now(), '$userAndHost', '$user', '$format', '$fullLocation', '$fileList', $nFiles, $size, '$status', '$comment', '$pset', '$crabCfg', '$jsonFile', '$lumiSummary', '$release', '$globalTag'";
341 $values .= ", $xSection" if $xSection;
342 $values .= ", $ySection" if $ySection;
343 $values .= ")";
344 $query = "insert into ntuple $columns values $values";
345 }
346 if ($id > 0)
347 {
348 my $fields;
349 my $values;
350
351 if ($name)
352 {
353 $query = "select id from ntuple where dataset='$name'";
354 $db->selectdb ("ntuple");
355 $results = $db->query ($query);
356 if ($results->numrows ())
357 {
358 print "This dataset already exists in the database. Use this name anyway? (y/N): ";
359 my $response = <STDIN>;
360 $response =~ s/\n//g;
361 $response = "n" if !$response;
362 exit if substr (lc ($response), 0, 1) ne 'y';
363 }
364 $values .= ", dataset='$name'";
365 }
366
367 $values .= ", lastUpdateTime=now()";
368 $values .= ", lastUpdateUser='$user'";
369 $values .= ", format='$format'" if $format;
370 $values .= ", location='$fullLocation'" if $location;
371 $values .= ", fileList='$fileList'" if $fileList;
372 $values .= ", nFiles=$nFiles" if $nFiles;
373 $values .= ", sizeInGB=$size" if $size;
374 $values .= ", status='$status'" if $status;
375 $values .= ", comment='$comment'" if $comment;
376 $values .= ", pset='$pset'" if $pset;
377 $values .= ", crabCfg='$crabCfg'" if $crabCfg;
378 $values .= ", jsonFile='$jsonFile'" if $jsonFile;
379 $values .= ", lumiSummary='$lumiSummary'" if $lumiSummary;
380 $values .= ", version='$release'" if $release;
381 $values .= ", globalTag='$globalTag'" if $globalTag;
382 $values .= ", crossSectionInPicobarn=$xSection" if $xSection;
383 $values .= ", higherOrderCrossSectionInPicobarn=$ySection" if $ySection;
384
385 $values =~ s/^, //;
386 $query = "update ntuple set $values where id=$id";
387 }
388 $results = $db->query ($query);
389
390 return $results;
391 }
392
393 sub
394 dbUpdateOther
395 {
396 my $id = shift;
397 my $comment = shift;
398 my $user = shift;
399
400 $comment = addSlashes ($comment);
401
402 my $query = "update other set lastUpdateTime=now(), lastUpdateUser='$user', comment='$comment' where id=$id";
403 $db->selectdb ("ntuple");
404 my $results = $db->query ($query);
405
406 return $results;
407 }
408
409 sub
410 dbDelete
411 {
412 my $id = shift;
413
414 my $query = "delete from ntuple where id=$id";
415 $db->selectdb ("ntuple");
416 my $results = $db->query ($query);
417 }
418
419 sub
420 dbDeleteOther
421 {
422 my $id = shift;
423
424 my $query = "delete from other where id=$id";
425 $db->selectdb ("ntuple");
426 my $results = $db->query ($query);
427 }
428
429 sub
430 printHelp
431 {
432 my $command = shift;
433
434 my $exeName = $0;
435 $exeName =~ s/^.*\/([^\/]*)$/$1/;
436
437 if ($command eq "create")
438 {
439 print "Usage: $exeName -l DIRECTORY [OPTION]... create NAME\n";
440 print "Creates an entry in the database for dataset NAME. The ntuples are assumed to\n";
441 print "be present on the Tier 3, with the status being set accordingly.\n";
442 print "\n";
443 print "Mandatory arguments to long options are mandatory for short options too.\n";
444 printf "%-29s%s\n", " -b, --crabCfg FILE", "CRAB config used to submit ntuple jobs";
445 printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
446 printf "%-29s%s\n", " -f, --format FORMAT", "ntuple format (default: BEAN)";
447 printf "%-29s%s\n", " -g, --globalTag TAG", "global tag";
448 printf "%-29s%s\n", " -i, --configName NAME", "name of config used to produce ntuples";
449 printf "%-29s%s\n", " -j, --jsonFile FILE", "JSON file used for this dataset";
450 printf "%-29s%s\n", " -l, --location DIRECTORY", "directory containing the ntuples";
451 printf "%-29s%s\n", " -p, --pyConfig FILE", "Python config used to produce ntuples";
452 printf "%-29s%s\n", " -r, --release NAME", "ntuple release used to produce ntuples";
453 printf "%-29s%s\n", " -s, --lumiSummary FILE", "lumiSummary.json reported by CRAB";
454 printf "%-29s%s\n", " -x, --xSection XSEC", "cross section in picobarns from PREP";
455 printf "%-29s%s\n", " -y, --higherOrderXSec XSEC", "higher-order cross section in picobarns";
456 }
457 elsif ($command eq "createOther")
458 {
459 print "Usage: $exeName [OPTION]... createOther DIRECTORIES_AND_FILES\n";
460 print "Creates an entry in the database for non-ntuple data.\n";
461 print "\n";
462 print "Mandatory arguments to long options are mandatory for short options too.\n";
463 printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
464 }
465 elsif ($command eq "update")
466 {
467 print "Usage: $exeName [OPTION]... update NAME\n";
468 print "Updates an existing database entry for dataset NAME. If no options are given,\n";
469 print "updates the number of files and the size on disk of the dataset using the\n";
470 print "location in the database.\n";
471 print "\n";
472 print "Mandatory arguments to long options are mandatory for short options too.\n";
473 printf "%-29s%s\n", " -b, --crabCfg FILE", "CRAB config used to submit ntuple jobs";
474 printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
475 printf "%-29s%s\n", " -f, --format FORMAT", "ntuple format";
476 printf "%-29s%s\n", " -g, --globalTag TAG", "global tag";
477 printf "%-29s%s\n", " -i, --configName NAME", "name of config used to produce ntuples";
478 printf "%-29s%s\n", " -j, --jsonFile FILE", "JSON file used for this dataset";
479 printf "%-29s%s\n", " -l, --location DIRECTORY", "directory containing the ntuples";
480 printf "%-29s%s\n", " -n, --name NAME", "rename the entry";
481 printf "%-29s%s\n", " -o, --other", "update an entry in the non-ntuple database";
482 printf "%-29s%s\n", " -p, --pyConfig FILE", "Python config used to produce ntuples";
483 printf "%-29s%s\n", " -r, --release NAME", "ntuple release used to produce ntuples";
484 printf "%-29s%s\n", " -s, --lumiSummary FILE", "lumiSummary.json reported by CRAB";
485 printf "%-29s%s\n", " -x, --xSection XSEC", "cross section in picobarns from PREP";
486 printf "%-29s%s\n", " -y, --higherOrderXSec XSEC", "higher-order cross section in picobarns";
487 }
488 elsif ($command eq "finish")
489 {
490 print "Usage: $exeName -l DIRECTORY finish NAME\n";
491 print "Finalizes the database entry for dataset NAME, changing its status to\n";
492 print "\"present\". This is intended to be the final step in command-line based ntuple\n";
493 print "production.\n";
494 print "\n";
495 print "Mandatory arguments to long options are mandatory for short options too.\n";
496 printf "%-29s%s\n", " -l, --location DIRECTORY", "directory containing the ntuples";
497 }
498 elsif ($command eq "deleteEntry")
499 {
500 print "Usage: $exeName [OPTION]... deleteEntry NAME\n";
501 print "Deletes the database entry for dataset NAME. This is intended primarily for\n";
502 print "mistaken database entries. If you wish to actually delete a set of ntuples,\n";
503 print "please use the \"deprecate\" command instead.\n";
504 print "\n";
505 printf "%-29s%s\n", " -o, --other", "update an entry in the non-ntuple database";
506 }
507 elsif ($command eq "deprecate")
508 {
509 print "Usage: $exeName deprecate NAME\n";
510 print "Marks the dataset NAME for deletion. WARNING: The dataset will be deleted from\n";
511 print "the Tier 3 within one week of this action. If you wish to simply delete a\n";
512 print "mistaken database entry, use the \"deleteEntry\" command instead.\n";
513 }
514 elsif ($command eq "uploadRelease")
515 {
516 print "Usage: $exeName -e FILE -p FILE -r DIRECTORY [OPTION]... uploadRelease NAME\n";
517 print "Copies an ntuple release to the appropriate area on the Tier 3, and creates a\n";
518 print "database entry for it, with NAME being the name of the release. An ntuple\n";
519 print "release is a CMSSW release with all the necessary packages added for creating\n";
520 print "ntuples, along with a default Python config file.\n";
521 print "\n";
522 print "Mandatory arguments to long options are mandatory for short options too.\n";
523 printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
524 printf "%-29s%s\n", " -e, --recipe FILE", "file containing the recipe for the release";
525 printf "%-29s%s\n", " -f, --format FORMAT", "ntuple format (default: BEAN)";
526 printf "%-29s%s\n", " -p, --pyConfig FILE", "default Python config for the release";
527 printf "%-29s%s\n", " -r, --release DIRECTORY", "CMSSW release containing ntuple packages";
528 }
529 elsif ($command eq "downloadRelease")
530 {
531 print "Usage: $exeName [OPTION]... downloadRelease NAME\n";
532 print "Copies an ntuple release to the current directory on the Tier 3. Optionally\n";
533 print "copies the corresponding Python configuration file registered in the database.\n";
534 print "\n";
535 print "Mandatory arguments to long options are mandatory for short options too.\n";
536 printf "%-29s%s\n", " -p, --pyConfig FILE", "Python configuration file name";
537 }
538 elsif ($command eq "uploadConfig")
539 {
540 print "Usage: $exeName -p FILE -r RELEASE [OPTION]... uploadConfig NAME\n";
541 print "Creates an entry in the database for a customized Python configuration file.\n";
542 print "\n";
543 print "Mandatory arguments to long options are mandatory for short options too.\n";
544 printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
545 printf "%-29s%s\n", " -p, --pyConfig FILE", "Python config file";
546 printf "%-29s%s\n", " -r, --release NAME", "ntuple release with which to use this config";
547 }
548 else
549 {
550 print "Usage: $exeName [OPTION]... COMMAND NAME\n";
551 print "Manipulates entry in the OSU Tier 3 ntuple database given by NAME.\n";
552 print "\n";
553 print "Mandatory arguments to long options are mandatory for short options too.\n";
554 printf "%-29s%s\n", " -h, --help", "print help. If COMMAND is present, print help";
555 printf "%-29s%s\n", " ", "specific to COMMAND.";
556 print "\n";
557 print "COMMAND may be one of the following:\n";
558 printf "%-29s%s\n", " create", "creates the entry";
559 printf "%-29s%s\n", " createOther", "creates an entry for non-ntuple data";
560 printf "%-29s%s\n", " update", "updates the entry";
561 printf "%-29s%s\n", " finish", "finalizes the database entry";
562 printf "%-29s%s\n", " deleteEntry", "removes the database entry";
563 printf "%-29s%s\n", " deprecate", "marks the dataset for deletion";
564 printf "%-29s%s\n", " uploadRelease", "upload an ntuple release";
565 printf "%-29s%s\n", " downloadRelease", "download an ntuple release";
566 printf "%-29s%s\n", " uploadConfig", "upload an ntuple config";
567 }
568
569 exit;
570 }
571
572 sub
573 sizeOfDataset
574 {
575 my $location = shift;
576 my $fileList = shift;
577
578 my $size = 0.0;
579 my $nFiles = 0;
580 if ($location && !$fileList)
581 {
582 if (!(-e $location))
583 {
584 print "$location does not exist!\n";
585 exit;
586 }
587 $nFiles = `ls $location | wc -l`;
588 $size = `du -s $location`;
589 $size =~ s/([^ ]*) .*/$1/;
590 $size /= 1024 * 1024;
591 $size = sprintf "%.2f", $size;
592 }
593 elsif ($fileList)
594 {
595 foreach my $file (split (/\n/, $fileList))
596 {
597 $nFiles++;
598 my $fileSize = `du -s $file`;
599 $fileSize =~ s/([^ ]*) .*/$1/;
600 $size += $fileSize;
601 }
602 $size /= 1024 * 1024;
603 $size = sprintf "%.2f", $size;
604 }
605
606 return ($nFiles, $size);
607 }
608
609 sub
610 uploadRelease
611 {
612 my $format = shift;
613 my $cmsswRelease = shift;
614 my $release = shift;
615 my $psetName = shift;
616 my $name = shift;
617 my $user = shift;
618 my $comment = shift;
619 my $recipeName = shift;
620
621 if (!(-e $psetName))
622 {
623 print "$psetName does not exist!\n";
624 exit;
625 }
626 if (!(-e $recipeName))
627 {
628 print "$recipeName does not exist!\n";
629 exit;
630 }
631 move ($release, "/home/osucms/public_html/releases/$name.tar.gz") or die "Ntuple releases may only be uploaded on the Tier 3, stopped";
632 open (PY_CONFIG, "<$psetName");
633 my @pset = <PY_CONFIG>;
634 close (PY_CONFIG);
635 my $pset = join ("", @pset);
636 open (RECIPE, "<$recipeName");
637 my @recipe = <RECIPE>;
638 close (RECIPE);
639 my $recipe = join ("", @recipe);
640
641 my $query = "select max(id) from ntupleRelease";
642 $db->selectdb ("ntuple");
643 my $results = $db->query ($query);
644 my @row = $results->fetchrow ();
645 my $id = 1;
646 $id = $row[0] + 1 if $results->numrows ();
647
648 $name = addSlashes ($name);
649 $pset = addSlashes ($pset);
650 $user = addSlashes ($user);
651 $format = addSlashes ($format);
652 $cmsswRelease = addSlashes ($cmsswRelease);
653 $comment = addSlashes ($comment);
654 $recipe = addSlashes ($recipe);
655
656 my $query = "insert into ntupleRelease (id, name, pset, user, pending, format, cmsswRelease, comment, recipe) values ($id, '$name', '$pset', '$user', 1, '$format', '$cmsswRelease', '$comment', '$recipe')";
657 $db->selectdb ("ntuple");
658 my $results = $db->query ($query);
659 }
660
661 sub
662 downloadRelease
663 {
664 my $release = shift;
665 my $pyConfigName = shift;
666
667 my $query = "select id,pset from ntupleRelease where name='$release'";
668 $db->selectdb ("ntuple");
669 my $results = $db->query ($query);
670 if ($results->numrows () != 1)
671 {
672 print "Ntuple release \"$release\" not found!\n";
673 exit;
674 }
675 if ($pyConfigName)
676 {
677 my @row = $results->fetchrow ();
678 open (PY_CONFIG, ">$pyConfigName");
679 print PY_CONFIG $row[1];
680 close (PY_CONFIG);
681 }
682 `wget "http://cmshead.mps.ohio-state.edu/~osucms/releases/$release.tar.gz"`;
683 }
684
685 sub
686 dbOtherCreate
687 {
688 my $userAndHost = shift;
689 my $listOfFiles = shift;
690 my $comment = shift;
691
692 my $user = $userAndHost;
693 $user =~ s/@.*$//g;
694
695 my $size = 0.0;
696 my $nFiles = 0.0;
697 for (my $i = 0; $i < @$listOfFiles; $i++)
698 {
699 if (!(-e $$listOfFiles[$i]))
700 {
701 print "$$listOfFiles[$i] does not exist!\n";
702 exit;
703 }
704 $nFiles += `ls -R $$listOfFiles[$i] | grep -v ':\$' | grep -v '^\$' | wc -l`;
705 my $fileSize = `du -s $$listOfFiles[$i]`;
706 $fileSize =~ s/([^ ]*) .*/$1/;
707 $fileSize /= 1024 * 1024;
708 $size += $fileSize;
709 $$listOfFiles[$i] = "$ENV{'PWD'}/$$listOfFiles[$i]" if !($$listOfFiles[$i] =~ m/^\//);
710 }
711 $size = sprintf "%.2f", $size;
712 my $location = join ("<br />", @$listOfFiles);
713
714 my $query = "select max(id) from other";
715 $db->selectdb ("ntuple");
716 my $results = $db->query ($query);
717 my @row = $results->fetchrow ();
718 my $id = 1;
719 $id = $row[0] + 1 if $results->numrows ();
720
721 $user = addSlashes ($user);
722 $location = addSlashes ($location);
723 $comment = addSlashes ($comment);
724
725 $query = "insert into other (id, creationTime, lastUpdateTime, lastUpdateUser, user, location, nFiles, sizeInGB, comment) values ($id, now(), now(), '$userAndHost', '$user', '$location', $nFiles, $size, '$comment')";
726 $db->selectdb ("ntuple");
727 $results = $db->query ($query);
728
729 print "ID $id\n";
730 }
731
732 sub
733 uploadConfig
734 {
735 my $name = shift;
736 my $user = shift;
737 my $psetName = shift;
738 my $release = shift;
739 my $comment = shift;
740
741 if (!(-e $psetName))
742 {
743 print "$psetName does not exist!\n";
744 exit;
745 }
746 if ($release)
747 {
748 my $query = "select id from ntupleRelease where name='$release'";
749 $db->selectdb ("ntuple");
750 my $results = $db->query ($query);
751 if ($results->numrows () != 1)
752 {
753 print "Ntuple release \"$release\" not found!\n";
754 exit;
755 }
756 }
757 open (PSET, "<$psetName");
758 my @pset = <PSET>;
759 close (PSET);
760 my $pset = join ("", @pset);
761
762 my $query = "select max(id) from ntupleConfig";
763 $db->selectdb ("ntuple");
764 my $results = $db->query ($query);
765 my @row = $results->fetchrow ();
766 my $id = 1;
767 $id = $row[0] + 1 if $results->numrows ();
768
769 $name = addSlashes ($name);
770 $user = addSlashes ($user);
771 $pset = addSlashes ($pset);
772 $release = addSlashes ($release);
773 $comment = addSlashes ($comment);
774
775 my $query = "insert into ntupleConfig (id, name, pset, user, pending, ntupleRelease, comment) values ($id, '$name', '$pset', '$user', 1, '$release', '$comment')";
776 $db->selectdb ("ntuple");
777 $results = $db->query ($query);
778 }
779
780 sub
781 locationExists
782 {
783 my $id = shift;
784 my $location = shift;
785
786 $location =~ s/^(.*)\/+$/$1/;
787 my $query = "select id from ntuple where location like '$location%' and id!=$id";
788 $db->selectdb ("ntuple");
789 my $results = $db->query ($query);
790 return 1 if $results->numrows ();
791
792 if ($location =~ m/^\/store\/.*$/)
793 {
794 $location =~ s/^\/store\/(.*)$/\/data\/se\/store\/$1/;
795 }
796 elsif ($location =~ m/^\/data\/se\/store\/.*$/)
797 {
798 $location =~ s/^\/data\/se\/store\/(.*)$/\/store\/$1/;
799 }
800
801 my $query = "select id from ntuple where location like '$location%' and id!=$id";
802 $db->selectdb ("ntuple");
803 my $results = $db->query ($query);
804 return 1 if $results->numrows ();
805
806 return 0;
807 }
808
809 sub
810 getLocation
811 {
812 my $id = shift;
813
814 my $query = "select location from ntuple where id=$id";
815 $db->selectdb ("ntuple");
816 my $results = $db->query ($query);
817 my @row = $results->fetchrow ();
818
819 return $row[0];
820 }