ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osudb
Revision: 1.21
Committed: Sun Feb 3 00:14:20 2013 UTC (12 years, 3 months ago) by ahart
Branch: MAIN
Changes since 1.20: +8 -3 lines
Log Message:
Added a global tag option.

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.21 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", "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.21 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"}) 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.1
196 ahart 1.15 my $user = $userAndHost;
197     $user =~ s/@.*$//g;
198    
199 ahart 1.4 my $fileList;
200 ahart 1.1 my $pset;
201     my $crabCfg;
202     my $jsonFile;
203     my $lumiSummary;
204     my $size = 0;
205     my $nFiles = 0;
206 ahart 1.4 if ($fileListName)
207     {
208     if (!(-e $fileListName))
209     {
210     print "$fileListName does not exist!\n";
211     exit;
212     }
213     open (FILE_LIST, "<$fileListName");
214     my @fileList = <FILE_LIST>;
215     close (FILE_LIST);
216     $fileList = join ("", @fileList);
217     }
218 ahart 1.1 if ($psetName)
219     {
220     if (!(-e $psetName))
221     {
222     print "$psetName does not exist!\n";
223     exit;
224     }
225     open (PY_CONFIG, "<$psetName");
226     my @pset = <PY_CONFIG>;
227     close (PY_CONFIG);
228     $pset = join ("", @pset);
229     }
230     if ($crabCfgName)
231     {
232     if (!(-e $crabCfgName))
233     {
234     print "$crabCfgName does not exist!\n";
235     exit;
236     }
237     open (CRAB_CONFIG, "<$crabCfgName");
238     my @crabCfg = <CRAB_CONFIG>;
239     close (CRAB_CONFIG);
240     $crabCfg = join ("", @crabCfg);
241     }
242     if ($jsonFileName)
243     {
244     if (!(-e $jsonFileName))
245     {
246     print "$jsonFileName does not exist!\n";
247     exit;
248     }
249     open (JSON_FILE, "<$jsonFileName");
250     my @jsonFile = <JSON_FILE>;
251     close (JSON_FILE);
252     $jsonFile = join ("", @jsonFile);
253     }
254     if ($lumiSummaryName)
255     {
256     if (!(-e $lumiSummaryName))
257     {
258     print "$lumiSummaryName does not exist!\n";
259     exit;
260     }
261     open (LUMI_SUMMARY, "<$lumiSummaryName");
262     my @lumiSummary = <LUMI_SUMMARY>;
263     close (LUMI_SUMMARY);
264     $lumiSummary = join ("", @lumiSummary);
265     }
266 ahart 1.5 if ($release)
267     {
268     my $query = "select id from ntupleRelease where name='$release'";
269     $db->selectdb ("ntuple");
270     my $results = $db->query ($query);
271     if ($results->numrows () != 1)
272     {
273     print "Ntuple release \"$release\" not found!\n";
274     exit;
275     }
276     }
277 ahart 1.4 ($nFiles, $size) = sizeOfDataset ($dataset, $location, $fileList);
278 ahart 1.7 my $fullLocation = $location;
279     $fullLocation = "$ENV{'PWD'}/$location" if !($location =~ m/^\//);
280 ahart 1.1
281     $dataset = addSlashes ($dataset);
282     $user = addSlashes ($user);
283     $format = addSlashes ($format);
284     $pset = addSlashes ($pset);
285     $crabCfg = addSlashes ($crabCfg);
286     $jsonFile = addSlashes ($jsonFile);
287     $lumiSummary = addSlashes ($lumiSummary);
288 ahart 1.6 $fullLocation = addSlashes ($fullLocation);
289 ahart 1.17 $name = addSlashes ($name);
290 ahart 1.4 $fileList = addSlashes ($fileList);
291 ahart 1.1 $status = addSlashes ($status);
292     $comment = addSlashes ($comment);
293 ahart 1.5 $release = addSlashes ($release);
294 ahart 1.21 $globalTag = addSlashes ($globalTag);
295 ahart 1.1
296     my $results;
297     my $query;
298     if ($id < 0)
299     {
300 ahart 1.16 $query = "select id from ntuple where dataset='$dataset'";
301     $db->selectdb ("ntuple");
302     $results = $db->query ($query);
303     if ($results->numrows ())
304     {
305     print "This dataset already exists in the database. Create anyway? (y/N): ";
306     my $response = <STDIN>;
307     $response =~ s/\n//g;
308     $response = "n" if !$response;
309     exit if substr (lc ($response), 0, 1) ne 'y';
310     }
311    
312 ahart 1.1 $query = "select max(id) from ntuple";
313     $db->selectdb ("ntuple");
314     $results = $db->query ($query);
315     my @row = $results->fetchrow ();
316     my $id = 1;
317     $id = $row[0] + 1 if $results->numrows ();
318    
319 ahart 1.21 $query = "insert into ntuple (id, dataset, creationTime, lastUpdateTime, lastUpdateUser, user, format, location, fileList, nFiles, sizeInGB, status, comment, pset, crabCfg, jsonFile, lumiSummary, version, globalTag) values ($id, '$dataset', now(), now(), '$userAndHost', '$user', '$format', '$fullLocation', '$fileList', $nFiles, $size, '$status', '$comment', '$pset', '$crabCfg', '$jsonFile', '$lumiSummary', '$release', '$globalTag')";
320 ahart 1.1 }
321     if ($id > 0)
322     {
323     my $fields;
324     my $values;
325    
326 ahart 1.17 if ($name)
327     {
328     $query = "select id from ntuple where dataset='$name'";
329     $db->selectdb ("ntuple");
330     $results = $db->query ($query);
331     if ($results->numrows ())
332     {
333     print "This dataset already exists in the database. Create anyway? (y/N): ";
334     my $response = <STDIN>;
335     $response =~ s/\n//g;
336     $response = "n" if !$response;
337     exit if substr (lc ($response), 0, 1) ne 'y';
338     }
339     $values .= ", dataset='$name'";
340     }
341    
342 ahart 1.1 $values .= ", lastUpdateTime=now()";
343 ahart 1.9 $values .= ", lastUpdateUser='$user'";
344 ahart 1.1 $values .= ", format='$format'" if $format;
345 ahart 1.6 $values .= ", location='$fullLocation'" if $location;
346 ahart 1.4 $values .= ", fileList='$fileList'" if $fileList;
347     $values .= ", nFiles=$nFiles" if $nFiles;
348     $values .= ", sizeInGB=$size" if $size;
349 ahart 1.1 $values .= ", status='$status'" if $status;
350     $values .= ", comment='$comment'" if $comment;
351     $values .= ", pset='$pset'" if $pset;
352     $values .= ", crabCfg='$crabCfg'" if $crabCfg;
353     $values .= ", jsonFile='$jsonFile'" if $jsonFile;
354     $values .= ", lumiSummary='$lumiSummary'" if $lumiSummary;
355 ahart 1.5 $values .= ", version='$release'" if $release;
356 ahart 1.21 $values .= ", globalTag='$globalTag'" if $globalTag;
357 ahart 1.1
358     $values =~ s/^, //;
359     $query = "update ntuple set $values where id=$id";
360     }
361     $results = $db->query ($query);
362    
363     return $results;
364     }
365    
366     sub
367 ahart 1.8 dbUpdateOther
368     {
369     my $id = shift;
370     my $comment = shift;
371 ahart 1.9 my $user = shift;
372 ahart 1.8
373     $comment = addSlashes ($comment);
374    
375 ahart 1.9 my $query = "update other set lastUpdateTime=now(), lastUpdateUser='$user', comment='$comment' where id=$id";
376 ahart 1.8 $db->selectdb ("ntuple");
377     my $results = $db->query ($query);
378    
379     return $results;
380     }
381    
382     sub
383 ahart 1.4 dbDelete
384     {
385     my $id = shift;
386    
387     my $query = "delete from ntuple where id=$id";
388     $db->selectdb ("ntuple");
389     my $results = $db->query ($query);
390     }
391    
392     sub
393 ahart 1.8 dbDeleteOther
394     {
395     my $id = shift;
396    
397     my $query = "delete from other where id=$id";
398     $db->selectdb ("ntuple");
399     my $results = $db->query ($query);
400     }
401    
402     sub
403 ahart 1.1 printHelp
404     {
405 ahart 1.4 my $command = shift;
406    
407 ahart 1.1 my $exeName = $0;
408     $exeName =~ s/^.*\/([^\/]*)$/$1/;
409    
410 ahart 1.4 if ($command eq "create")
411     {
412     print "Usage: $exeName -l DIRECTORY [OPTION]... create NAME\n";
413     print "Creates an entry in the database for dataset NAME. The ntuples are assumed to\n";
414     print "be present on the Tier 3, with the status being set accordingly.\n";
415     print "\n";
416     print "Mandatory arguments to long options are mandatory for short options too.\n";
417     printf "%-29s%s\n", " -b, --crabCfg FILE", "CRAB config used to submit ntuple jobs";
418     printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
419     printf "%-29s%s\n", " -f, --format FORMAT", "ntuple format (default: BEAN)";
420 ahart 1.21 printf "%-29s%s\n", " -g, --globalTag TAG", "global tag";
421 ahart 1.4 printf "%-29s%s\n", " -j, --jsonFile FILE", "JSON file used for this dataset";
422     printf "%-29s%s\n", " -l, --location DIRECTORY", "directory containing the ntuples";
423     printf "%-29s%s\n", " -p, --pyConfig FILE", "Python config used to produce ntuples";
424 ahart 1.5 printf "%-29s%s\n", " -r, --release NAME", "ntuple release used to produce ntuples";
425 ahart 1.4 printf "%-29s%s\n", " -s, --lumiSummary FILE", "lumiSummary.json reported by CRAB";
426     }
427 ahart 1.8 elsif ($command eq "createOther")
428     {
429     print "Usage: $exeName [OPTION]... createOther DIRECTORIES_AND_FILES\n";
430     print "Creates an entry in the database for non-ntuple data.\n";
431     print "\n";
432     print "Mandatory arguments to long options are mandatory for short options too.\n";
433     printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
434     }
435 ahart 1.4 elsif ($command eq "update")
436     {
437     print "Usage: $exeName [OPTION]... update NAME\n";
438     print "Updates an existing database entry for dataset NAME.\n";
439     print "\n";
440     print "Mandatory arguments to long options are mandatory for short options too.\n";
441     printf "%-29s%s\n", " -b, --crabCfg FILE", "CRAB config used to submit ntuple jobs";
442     printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
443     printf "%-29s%s\n", " -f, --format FORMAT", "ntuple format";
444 ahart 1.21 printf "%-29s%s\n", " -g, --globalTag TAG", "global tag";
445 ahart 1.4 printf "%-29s%s\n", " -j, --jsonFile FILE", "JSON file used for this dataset";
446     printf "%-29s%s\n", " -l, --location DIRECTORY", "directory containing the ntuples";
447 ahart 1.17 printf "%-29s%s\n", " -n, --name NAME", "rename the entry";
448 ahart 1.8 printf "%-29s%s\n", " -o, --other", "update an entry in the non-ntuple database";
449 ahart 1.4 printf "%-29s%s\n", " -p, --pyConfig FILE", "Python config used to produce ntuples";
450 ahart 1.5 printf "%-29s%s\n", " -r, --release NAME", "ntuple release used to produce ntuples";
451 ahart 1.4 printf "%-29s%s\n", " -s, --lumiSummary FILE", "lumiSummary.json reported by CRAB";
452     }
453     elsif ($command eq "finish")
454     {
455     print "Usage: $exeName -l DIRECTORY finish NAME\n";
456     print "Finalizes the database entry for dataset NAME, changing its status to\n";
457     print "\"present\". This is intended to be the final step in command-line based ntuple\n";
458     print "production.\n";
459     print "\n";
460     print "Mandatory arguments to long options are mandatory for short options too.\n";
461     printf "%-29s%s\n", " -l, --location DIRECTORY", "directory containing the ntuples";
462     }
463     elsif ($command eq "deleteEntry")
464     {
465 ahart 1.8 print "Usage: $exeName [OPTION]... deleteEntry NAME\n";
466 ahart 1.4 print "Deletes the database entry for dataset NAME. This is intended primarily for\n";
467     print "mistaken database entries. If you wish to actually delete a set of ntuples,\n";
468     print "please use the \"deprecate\" command instead.\n";
469 ahart 1.8 print "\n";
470     printf "%-29s%s\n", " -o, --other", "update an entry in the non-ntuple database";
471 ahart 1.4 }
472     elsif ($command eq "deprecate")
473     {
474     print "Usage: $exeName deprecate NAME\n";
475     print "Marks the dataset NAME for deletion. WARNING: The dataset will be deleted from\n";
476     print "the Tier 3 within one week of this action. If you wish to simply delete a\n";
477     print "mistaken database entry, use the \"deleteEntry\" command instead.\n";
478     }
479     elsif ($command eq "uploadRelease")
480     {
481 ahart 1.13 print "Usage: $exeName -e FILE -p FILE -r DIRECTORY [OPTION]... uploadRelease NAME\n";
482 ahart 1.4 print "Copies an ntuple release to the appropriate area on the Tier 3, and creates a\n";
483     print "database entry for it, with NAME being the name of the release. An ntuple\n";
484     print "release is a CMSSW release with all the necessary packages added for creating\n";
485     print "ntuples, along with a default Python config file.\n";
486     print "\n";
487     print "Mandatory arguments to long options are mandatory for short options too.\n";
488     printf "%-29s%s\n", " -c, --comment COMMENT", "comment for the database entry";
489 ahart 1.13 printf "%-29s%s\n", " -e, --recipe FILE", "file containing the recipe for the release";
490 ahart 1.4 printf "%-29s%s\n", " -f, --format FORMAT", "ntuple format (default: BEAN)";
491     printf "%-29s%s\n", " -p, --pyConfig FILE", "default Python config for the release";
492     printf "%-29s%s\n", " -r, --release DIRECTORY", "CMSSW release containing ntuple packages";
493     }
494 ahart 1.11 elsif ($command eq "downloadRelease")
495     {
496     print "Usage: $exeName [OPTION]... downloadRelease NAME\n";
497     print "Copies an ntuple release to the current directory on the Tier 3. Optionally\n";
498     print "copies the corresponding Python configuration file registered in the database.\n";
499     print "\n";
500     print "Mandatory arguments to long options are mandatory for short options too.\n";
501     printf "%-29s%s\n", " -p, --pyConfig FILE", "Python configuration file name";
502     }
503 ahart 1.14 elsif ($command eq "uploadConfig")
504     {
505 ahart 1.19 print "Usage: $exeName -p FILE -r RELEASE [OPTION]... uploadConfig NAME\n";
506 ahart 1.14 print "Creates an entry in the database for a customized Python configuration 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     printf "%-29s%s\n", " -p, --pyConfig FILE", "Python config file";
511     printf "%-29s%s\n", " -r, --release NAME", "ntuple release with which to use this config";
512     }
513 ahart 1.4 else
514     {
515     print "Usage: $exeName [OPTION]... COMMAND NAME\n";
516     print "Manipulates entry in the OSU Tier 3 ntuple database given by NAME.\n";
517     print "\n";
518     print "Mandatory arguments to long options are mandatory for short options too.\n";
519     printf "%-29s%s\n", " -h, --help", "print help. If COMMAND is present, print help";
520     printf "%-29s%s\n", " ", "specific to COMMAND.";
521     print "\n";
522     print "COMMAND may be one of the following:\n";
523     printf "%-29s%s\n", " create", "creates the entry";
524 ahart 1.11 printf "%-29s%s\n", " createOther", "creates an entry for non-ntuple data";
525 ahart 1.4 printf "%-29s%s\n", " update", "updates the entry";
526     printf "%-29s%s\n", " finish", "finalizes the database entry";
527     printf "%-29s%s\n", " deleteEntry", "removes the database entry";
528     printf "%-29s%s\n", " deprecate", "marks the dataset for deletion";
529     printf "%-29s%s\n", " uploadRelease", "upload an ntuple release";
530 ahart 1.11 printf "%-29s%s\n", " downloadRelease", "download an ntuple release";
531 ahart 1.14 printf "%-29s%s\n", " uploadConfig", "upload an ntuple config";
532 ahart 1.4 }
533 ahart 1.1
534     exit;
535     }
536 ahart 1.4
537     sub
538     sizeOfDataset
539     {
540     my $dataset = shift;
541     my $location = shift;
542     my $fileList = shift;
543    
544     my $size = 0.0;
545     my $nFiles = 0;
546 ahart 1.6 if ($location && !$fileList)
547 ahart 1.4 {
548     if (!(-e $location))
549     {
550     print "$location does not exist!\n";
551     exit;
552     }
553     $nFiles = `ls $location | wc -l`;
554     $size = `du -s $location`;
555     $size =~ s/([^ ]*) .*/$1/;
556     $size /= 1024 * 1024;
557     $size = sprintf "%.2f", $size;
558     }
559     elsif ($fileList)
560     {
561     foreach my $file (split (/\n/, $fileList))
562     {
563     $nFiles++;
564     my $fileSize = `du -s $file`;
565     $fileSize =~ s/([^ ]*) .*/$1/;
566     $size += $fileSize;
567     }
568     $size /= 1024 * 1024;
569     $size = sprintf "%.2f", $size;
570     }
571    
572     return ($nFiles, $size);
573     }
574    
575     sub
576     uploadRelease
577     {
578     my $format = shift;
579     my $cmsswRelease = shift;
580     my $release = shift;
581     my $psetName = shift;
582     my $name = shift;
583     my $user = shift;
584     my $comment = shift;
585 ahart 1.13 my $recipeName = shift;
586 ahart 1.4
587     if (!(-e $psetName))
588     {
589     print "$psetName does not exist!\n";
590     exit;
591     }
592 ahart 1.13 if (!(-e $recipeName))
593     {
594     print "$recipeName does not exist!\n";
595     exit;
596     }
597 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";
598 ahart 1.4 open (PY_CONFIG, "<$psetName");
599     my @pset = <PY_CONFIG>;
600     close (PY_CONFIG);
601     my $pset = join ("", @pset);
602 ahart 1.13 open (RECIPE, "<$recipeName");
603     my @recipe = <RECIPE>;
604     close (RECIPE);
605     my $recipe = join ("\n", @recipe);
606 ahart 1.4
607     my $query = "select max(id) from ntupleRelease";
608     $db->selectdb ("ntuple");
609     my $results = $db->query ($query);
610     my @row = $results->fetchrow ();
611     my $id = 1;
612     $id = $row[0] + 1 if $results->numrows ();
613    
614     $name = addSlashes ($name);
615     $pset = addSlashes ($pset);
616     $user = addSlashes ($user);
617     $format = addSlashes ($format);
618     $cmsswRelease = addSlashes ($cmsswRelease);
619     $comment = addSlashes ($comment);
620 ahart 1.13 $recipe = addSlashes ($recipe);
621 ahart 1.4
622 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')";
623 ahart 1.4 $db->selectdb ("ntuple");
624     my $results = $db->query ($query);
625     }
626 ahart 1.8
627     sub
628 ahart 1.11 downloadRelease
629     {
630     my $release = shift;
631     my $pyConfigName = shift;
632    
633     my $query = "select id,pset from ntupleRelease where name='$release'";
634     $db->selectdb ("ntuple");
635     my $results = $db->query ($query);
636     if ($results->numrows () != 1)
637     {
638     print "Ntuple release \"$release\" not found!\n";
639     exit;
640     }
641     if ($pyConfigName)
642     {
643     my @row = $results->fetchrow ();
644     open (PY_CONFIG, ">$pyConfigName");
645     print PY_CONFIG $row[1];
646     close (PY_CONFIG);
647     }
648 ahart 1.20 `wget "http://cmshead.mps.ohio-state.edu/~osucms/releases/$release.tar.gz"`;
649 ahart 1.11 }
650    
651     sub
652 ahart 1.8 dbOtherCreate
653     {
654 ahart 1.15 my $userAndHost = shift;
655 ahart 1.8 my $listOfFiles = shift;
656     my $comment = shift;
657    
658 ahart 1.15 my $user = $userAndHost;
659     $user =~ s/@.*$//g;
660    
661 ahart 1.8 my $size = 0.0;
662     my $nFiles = 0.0;
663     for (my $i = 0; $i < @$listOfFiles; $i++)
664     {
665     if (!(-e $$listOfFiles[$i]))
666     {
667     print "$$listOfFiles[$i] does not exist!\n";
668     exit;
669     }
670     $nFiles += `ls -R $$listOfFiles[$i] | grep -v ':\$' | grep -v '^\$' | wc -l`;
671     my $fileSize = `du -s $$listOfFiles[$i]`;
672     $fileSize =~ s/([^ ]*) .*/$1/;
673     $fileSize /= 1024 * 1024;
674     $size += $fileSize;
675     $$listOfFiles[$i] = "$ENV{'PWD'}/$$listOfFiles[$i]" if !($$listOfFiles[$i] =~ m/^\//);
676     }
677     $size = sprintf "%.2f", $size;
678     my $location = join ("<br />", @$listOfFiles);
679    
680     my $query = "select max(id) from other";
681     $db->selectdb ("ntuple");
682     my $results = $db->query ($query);
683     my @row = $results->fetchrow ();
684     my $id = 1;
685     $id = $row[0] + 1 if $results->numrows ();
686    
687     $user = addSlashes ($user);
688     $location = addSlashes ($location);
689     $comment = addSlashes ($comment);
690    
691 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')";
692 ahart 1.14 $db->selectdb ("ntuple");
693     $results = $db->query ($query);
694    
695     print "ID $id\n";
696     }
697    
698     sub
699     uploadConfig
700     {
701     my $name = shift;
702     my $user = shift;
703     my $psetName = shift;
704     my $release = shift;
705     my $comment = shift;
706    
707     if (!(-e $psetName))
708     {
709     print "$psetName does not exist!\n";
710     exit;
711     }
712     if ($release)
713     {
714     my $query = "select id from ntupleRelease where name='$release'";
715     $db->selectdb ("ntuple");
716     my $results = $db->query ($query);
717     if ($results->numrows () != 1)
718     {
719     print "Ntuple release \"$release\" not found!\n";
720     exit;
721     }
722     }
723     open (PSET, "<$psetName");
724     my @pset = <PSET>;
725     close (PSET);
726     my $pset = join ("", @pset);
727    
728     my $query = "select max(id) from ntupleConfig";
729 ahart 1.8 $db->selectdb ("ntuple");
730     my $results = $db->query ($query);
731 ahart 1.14 my @row = $results->fetchrow ();
732     my $id = 1;
733     $id = $row[0] + 1 if $results->numrows ();
734 ahart 1.8
735 ahart 1.14 $name = addSlashes ($name);
736     $user = addSlashes ($user);
737     $pset = addSlashes ($pset);
738     $release = addSlashes ($release);
739     $comment = addSlashes ($comment);
740    
741     my $query = "insert into ntupleConfig (id, name, pset, user, pending, ntupleRelease, comment) values ($id, '$name', '$pset', '$user', 1, '$release', '$comment')";
742     $db->selectdb ("ntuple");
743     $results = $db->query ($query);
744 ahart 1.8 }
745 ahart 1.16
746     sub
747     locationExists
748     {
749 ahart 1.18 my $id = shift;
750 ahart 1.16 my $location = shift;
751    
752     $location =~ s/^(.*)\/+$/$1/;
753 ahart 1.18 my $query = "select id from ntuple where location like '$location%' and id!=$id";
754 ahart 1.16 $db->selectdb ("ntuple");
755     my $results = $db->query ($query);
756     return 1 if $results->numrows ();
757    
758     if ($location =~ m/^\/store\/.*$/)
759     {
760     $location =~ s/^\/store\/(.*)$/\/data\/se\/store\/$1/;
761     }
762     elsif ($location =~ m/^\/data\/se\/store\/.*$/)
763     {
764     $location =~ s/^\/data\/se\/store\/(.*)$/\/store\/$1/;
765     }
766    
767 ahart 1.18 my $query = "select id from ntuple where location like '$location%' and id!=$id";
768 ahart 1.16 $db->selectdb ("ntuple");
769     my $results = $db->query ($query);
770     return 1 if $results->numrows ();
771    
772     return 0;
773     }