ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osudb
Revision: 1.25
Committed: Fri May 10 00:21:12 2013 UTC (11 years, 11 months ago) by ahart
Branch: MAIN
CVS Tags: V02-01-00
Changes since 1.24: +44 -9 lines
Log Message:
Added an option for modifying the higher-order cross section field of the database, and modified the behavior of the update command such that if no options are given, the number of files and size on disk of the dataset are updated using the location in the database.

File Contents

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