ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osudb
Revision: 1.22
Committed: Wed Feb 6 00:35:57 2013 UTC (12 years, 3 months ago) by ahart
Branch: MAIN
Changes since 1.21: +6 -13 lines
Log Message:
Allow arbitrary ntuple release names and add an option for naming the config file.

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