ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osudb
Revision: 1.16
Committed: Sat Jan 19 06:56:01 2013 UTC (12 years, 3 months ago) by ahart
Branch: MAIN
Changes since 1.15: +46 -0 lines
Log Message:
Do not register duplicate locations and warn of duplicate datasets.

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