ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/osudb
Revision: 1.2
Committed: Wed Jun 6 12:31:55 2012 UTC (12 years, 11 months ago) by ahart
Branch: MAIN
CVS Tags: V00-00-01
Changes since 1.1: +1 -1 lines
Log Message:
Fixed a stupid typo.

File Contents

# Content
1 #!/usr/bin/env perl
2
3 use strict;
4 use Mysql;
5 use Getopt::Long;
6
7 sub getDataset;
8 sub dbUpdate;
9 sub addSlashes;
10
11 our $db;
12
13 my %opt;
14 Getopt::Long::Configure ("bundling");
15 GetOptions (\%opt, "comment|c=s", "format|f=s", "pyConfig|p=s", "crabCfg|b=s", "jsonFile|j=s", "remoteLocation|r=s", "lumiSummary|s=s", "location|l=s", "help|h");
16 my $argc = @ARGV;
17
18 printHelp () if $opt{"help"};
19 printHelp () if $argc != 2;
20 printHelp () if $ARGV[0] != "create" && $ARGV[0] != "update" && $ARGV[0] != "finish" && $ARGV[0] != "deprecate";
21 my $id = -1;
22 $id = getDataset ($ARGV[1]) if $ARGV[0] ne "create";
23 $ARGV[0] = "create" if $id < 0;
24 my $status = "present";
25 $status = "" if $ARGV[0] eq "update";
26 $status = "deprecated" if $ARGV[0] eq "deprecate";
27 $opt{"format"} = "BEAN" if $ARGV[0] eq "create" && !$opt{"format"};
28 dbUpdate ($id, $ARGV[1], $ENV{"USER"}, $opt{"format"}, $opt{"location"}, $opt{"remoteLocation"}, $status, $opt{"comment"}, $opt{"pyConfig"}, $opt{"crabCfg"}, $opt{"jsonFile"}, $opt{"lumiSummary"});
29
30 sub
31 addSlashes
32 {
33 my $string = shift;
34
35 $string =~ s/\\/\\\\/g;
36 $string =~ s/'/\\'/g;
37 $string =~ s/"/\\"/g;
38 $string =~ s/\\0/\\\\0/g;
39
40 return $string;
41 }
42
43 sub
44 getDataset
45 {
46 my $dataset = shift;
47
48 my $results;
49 $db = Mysql->connect ("cmshead.mps.ohio-state.edu", "ntuple", "osuT3User");
50 my $query = "select id,user,creationTime from ntuple where dataset='$dataset' order by lastUpdateTime";
51 $db->selectdb ("ntuple");
52 $results = $db->query ($query);
53 if ($results->numrows () == 1)
54 {
55 my @row = $results->fetchrow ();
56 return $row[0];
57 }
58 if ($results->numrows () == 0)
59 {
60 print "Database entry does not exist. Create it? (Y/n): ";
61 my $response = <STDIN>;
62 $response =~ s/\n//g;
63 $response = "y" if !$response;
64 exit if substr (lc ($response), 0, 1) ne 'y';
65 return -1;
66 }
67 my %map;
68 print "Found multiple database entries matching\n";
69 print "\"$dataset\":\n";
70 print "(0) new\n";
71 for (my $i = 1; $i <= $results->numrows (); $i++)
72 {
73 my @row = $results->fetchrow ();
74 $map{"$i"} = $row[0];
75 print "($i) created by $row[1] on $row[2]\n";
76 }
77 print "\nWhich entry would you like to update? (Select 0 to create a new entry): ";
78 my $response = <STDIN>;
79 $response =~ s/[ \t\n]//g;
80 return -1 if $response == 0;
81 if (!(exists $map{$response}))
82 {
83 print "Your selection \"$response\" was not a valid option! Quitting.\n";
84 exit;
85 }
86
87 return $map{$response};
88 }
89
90 sub
91 dbUpdate
92 {
93 my $id = shift;
94 my $dataset = shift;
95 my $user = shift;
96 my $format = shift;
97 my $location = shift;
98 my $remoteLocation = shift;
99 my $status = shift;
100 my $comment = shift;
101 my $psetName = shift;
102 my $crabCfgName = shift;
103 my $jsonFileName = shift;
104 my $lumiSummaryName = shift;
105
106 my $pset;
107 my $crabCfg;
108 my $jsonFile;
109 my $lumiSummary;
110 my $size = 0;
111 my $nFiles = 0;
112 if ($psetName)
113 {
114 if (!(-e $psetName))
115 {
116 print "$psetName does not exist!\n";
117 exit;
118 }
119 open (PY_CONFIG, "<$psetName");
120 my @pset = <PY_CONFIG>;
121 close (PY_CONFIG);
122 $pset = join ("", @pset);
123 }
124 if ($crabCfgName)
125 {
126 if (!(-e $crabCfgName))
127 {
128 print "$crabCfgName does not exist!\n";
129 exit;
130 }
131 open (CRAB_CONFIG, "<$crabCfgName");
132 my @crabCfg = <CRAB_CONFIG>;
133 close (CRAB_CONFIG);
134 $crabCfg = join ("", @crabCfg);
135 }
136 if ($jsonFileName)
137 {
138 if (!(-e $jsonFileName))
139 {
140 print "$jsonFileName does not exist!\n";
141 exit;
142 }
143 open (JSON_FILE, "<$jsonFileName");
144 my @jsonFile = <JSON_FILE>;
145 close (JSON_FILE);
146 $jsonFile = join ("", @jsonFile);
147 }
148 if ($lumiSummaryName)
149 {
150 if (!(-e $lumiSummaryName))
151 {
152 print "$lumiSummaryName does not exist!\n";
153 exit;
154 }
155 open (LUMI_SUMMARY, "<$lumiSummaryName");
156 my @lumiSummary = <LUMI_SUMMARY>;
157 close (LUMI_SUMMARY);
158 $lumiSummary = join ("", @lumiSummary);
159 }
160 if ($location)
161 {
162 if (!(-e $location))
163 {
164 print "$location does not exist!\n";
165 exit;
166 }
167 $nFiles = `ls $location | wc -l`;
168 $size = `du -s $location`;
169 $size =~ s/([^ ]*) .*/$1/;
170 $size /= 1024 * 1024;
171 $size = sprintf "%.2f", $size;
172 }
173
174 $dataset = addSlashes ($dataset);
175 $user = addSlashes ($user);
176 $format = addSlashes ($format);
177 $pset = addSlashes ($pset);
178 $crabCfg = addSlashes ($crabCfg);
179 $jsonFile = addSlashes ($jsonFile);
180 $lumiSummary = addSlashes ($lumiSummary);
181 $location = addSlashes ($location);
182 $remoteLocation = addSlashes ($remoteLocation);
183 $status = addSlashes ($status);
184 $comment = addSlashes ($comment);
185
186 my $results;
187 my $query;
188 if ($id < 0)
189 {
190 $db = Mysql->connect ("cmshead.mps.ohio-state.edu", "ntuple", "osuT3User");
191 $query = "select max(id) from ntuple";
192 $db->selectdb ("ntuple");
193 $results = $db->query ($query);
194 my @row = $results->fetchrow ();
195 my $id = 1;
196 $id = $row[0] + 1 if $results->numrows ();
197
198 $query = "insert into ntuple (id, dataset, creationTime, lastUpdateTime, user, format, location, remoteLocation, nFiles, sizeInGB, status, comment, pset, crabCfg, jsonFile, lumiSummary) values ($id, '$dataset', now(), now(), '$user', '$format', '$location', '$remoteLocation', $nFiles, $size, '$status', '$comment', '$pset', '$crabCfg', '$jsonFile', '$lumiSummary')";
199 }
200 if ($id > 0)
201 {
202 my $fields;
203 my $values;
204
205 $values .= ", dataset='$dataset'" if $dataset;
206 $values .= ", lastUpdateTime=now()";
207 $values .= ", format='$format'" if $format;
208 $values .= ", location='$location'" if $location;
209 $values .= ", remoteLocation='$remoteLocation'" if $remoteLocation;
210 $values .= ", nFiles=$nFiles" if $location;
211 $values .= ", sizeInGB=$size" if $location;
212 $values .= ", status='$status'" if $status;
213 $values .= ", comment='$comment'" if $comment;
214 $values .= ", pset='$pset'" if $pset;
215 $values .= ", crabCfg='$crabCfg'" if $crabCfg;
216 $values .= ", jsonFile='$jsonFile'" if $jsonFile;
217 $values .= ", lumiSummary='$lumiSummary'" if $lumiSummary;
218
219 $values =~ s/^, //;
220 $query = "update ntuple set $values where id=$id";
221 }
222 $results = $db->query ($query);
223
224 return $results;
225 }
226
227 sub
228 printHelp
229 {
230 my $exeName = $0;
231 $exeName =~ s/^.*\/([^\/]*)$/$1/;
232
233 print "Usage: $exeName [OPTION]... COMMAND DATASET\n";
234 print "Manipulates entries in the OSU Tier 3 ntuple database.\n";
235 print "\n";
236 print "Mandatory arguments to long options are mandatory for short options too.\n";
237 printf "%-29s%s\n", " -b, --crabCfg FILE", "CRAB config file used to submit the ntuple jobs";
238 printf "%-29s%s\n", " -c, --comment COMMENT", "comment for this database entry";
239 printf "%-29s%s\n", " -f, --format FORMAT", "ntuple format (default: BEAN)";
240 printf "%-29s%s\n", " -j, --jsonFile FILE", "JSON file used to ntuplize the dataset";
241 printf "%-29s%s\n", " -h, --help", "print this help message";
242 printf "%-29s%s\n", " -l, --location DIRECTORY", "location on the Tier 3";
243 printf "%-29s%s\n", " -p, --pyConfig FILE", "python config used to ntuplize the dataset";
244 printf "%-29s%s\n", " -s, --lumiSummary FILE", "lumiSummary.json returned by CRAB";
245 print "\n";
246 print "COMMAND may be one of the following:\n";
247 printf "%-29s%s\n", " create", "creates the entry (assumes the dataset is present)";
248 printf "%-29s%s\n", " update", "updates the entry";
249 printf "%-29s%s\n", " finish", "finalizes the database entry";
250 printf "%-29s%s\n", " deprecate", "marks the dataset for deletion";
251
252 exit;
253 }