ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osudb
Revision: 1.23
Committed: Thu Feb 7 09:04:47 2013 UTC (12 years, 2 months ago) by ahart
Branch: MAIN
CVS Tags: V00-00-06
Changes since 1.22: +7 -3 lines
Log Message:
Added an option for setting the cross section of a sample.

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