common/tools/bitops.pl
changeset 1175 b1b8e71b0c52
parent 1162 1127dea6b78b
child 1176 5fcaa694186a
--- a/common/tools/bitops.pl	Tue Jul 13 14:37:59 2010 +0100
+++ b/common/tools/bitops.pl	Tue Jul 13 16:47:02 2010 +0100
@@ -13,12 +13,14 @@
 my $id = '';
 my $file = '';
 my $verbose = 0;
+my $dryrun = 0;
 GetOptions((
 	'create!' => \$create,
 	'file=s' => \$file,
 	'id=s' => \$id,
 	'help!' => \$help,
-	'verbose!' => \$verbose
+	'verbose!' => \$verbose,
+	'dryrun!' => \$dryrun,
 ));
 
 if ($help||!$file)
@@ -38,6 +40,9 @@
                         provided in FILE.
   -f FILE, --file FILE  Use attributes in FILE to create/update the build info
                         See below for file format.
+  -v, --verbose         Increase info level
+  -d, --dryrun          Only show what would happen, do not actually interact
+                        with the DB
   
 File format:
   File must be a list of attribute-value-pairs. One avp per line, on each line
@@ -76,18 +81,20 @@
 sub ConnectToDB()
 {
 	$db = DBI->connect('DBI:mysql:bit:v800016:3306','fbf','mysql')
-		or die("Couldn't connect to database: " . DBI->errstr());
+		or die("Couldn't connect to database: " . DBI->errstr()) if (!$dryrun);
 }
 
 sub DisconnectFromDB()
 {
-	$db->disconnect();
+	$db->disconnect() if (!$dryrun);
 }
 
 sub parse_file
 {
   my ($file) = @_;
   
+  print "Reading $file...\n" if ($verbose);
+  
   open(FILE, $file) or die "Can't open file $file";
   
   while (<FILE>)
@@ -191,7 +198,7 @@
             return 1;
           }
           print " found ($name,$url,$revision) for table 'content'\n" if ($verbose);
-          push(@{$reports}, {name=>$name, url=>$url, revision=>$revision});
+          push(@{$content}, {name=>$name, url=>$url, revision=>$revision});
         }
         else
         {
@@ -231,6 +238,8 @@
   exit(1);
 }
 
+print "Executing SQL commands...\n" if ($verbose);
+
 ConnectToDB();
 my $newbuildid = -1;
 if ($create)
@@ -250,17 +259,17 @@
     $qm_list =~ s/,$//;
     
     my $query = $db->prepare("insert into builds ($field_list) values ($qm_list)")
-      or die("Couldn't prepare query insert into builds: " . $db->errstr());
+      or die("Couldn't prepare query insert into builds: " . $db->errstr()) if (!$dryrun);
 
     $query->execute(@fields)
-		  or print "WARNING: Couldn't execute insert into builds (@fields): " . $db->errstr() . "\n";
+		  or print "WARNING: Couldn't execute insert into builds (@fields): " . $db->errstr() . "\n" if (!$dryrun);
 		  
-		$newbuildid = $db->last_insert_id(undef, undef, undef, undef);
+		$newbuildid = $db->last_insert_id(undef, undef, undef, undef) if (!$dryrun);
   }
   if (@{$envinfo})
   {
     my $query = $db->prepare_cached("insert into envinfo (build_id,tool,version) values ($newbuildid,?,?)")
-      or die("Couldn't prepare query insert into envinfo: " . $db->errstr());
+      or die("Couldn't prepare query insert into envinfo: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$envinfo})
     {
@@ -268,14 +277,14 @@
       my $version = $entry->{version};
       
       $query->execute($tool, $version)
-		    or print "WARNING: Couldn't execute insert into envinfo ($tool,$version): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into envinfo ($tool,$version): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   if (@{$failures})
   {
     print " prepare_cached 'insert into failures (build_id,category,count) values ($newbuildid,?,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into failures (build_id,category,count) values ($newbuildid,?,?)")
-      or die("Couldn't prepare query insert into failures: " . $db->errstr());
+      or die("Couldn't prepare query insert into failures: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$failures})
     {
@@ -284,14 +293,14 @@
     
       print " execute '$category, $count'\n" if ($verbose);  
       $query->execute($category, $count)
-		    or print "WARNING: Couldn't execute insert into failures ($category,$count): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into failures ($category,$count): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   if (@{$reports})
   {
     print " prepare_cached 'insert into reports (build_id,name,url,type) values ($newbuildid,?,?,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into reports (build_id,name,url,type) values ($newbuildid,?,?,?)")
-      or die("Couldn't prepare query insert into reports: " . $db->errstr());
+      or die("Couldn't prepare query insert into reports: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$reports})
     {
@@ -301,14 +310,14 @@
     
       print " execute '$name, $url, $type'\n" if ($verbose);  
       $query->execute($name, $url, $type)
-		    or print "WARNING: Couldn't execute insert into reports ($name,$url,$type): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into reports ($name,$url,$type): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   if (@{$content})
   {
     print " prepare_cached 'insert into content (build_id,name,url,revision) values ($newbuildid,?,?,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into content (build_id,name,url,revision) values ($newbuildid,?,?,?)")
-      or die("Couldn't prepare query insert into content: " . $db->errstr());
+      or die("Couldn't prepare query insert into content: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$content})
     {
@@ -318,14 +327,14 @@
     
       print " execute '$name, $url, $revision'\n" if ($verbose);  
       $query->execute($name, $url, $revision)
-		    or print "WARNING: Couldn't execute insert into content ($name,$url,$revision): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into content ($name,$url,$revision): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   if (@{$labels})
   {
     print " prepare_cached 'insert into labels (build_id,label) values ($newbuildid,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into labels (build_id,label) values ($newbuildid,?)")
-      or die("Couldn't prepare query insert into labels: " . $db->errstr());
+      or die("Couldn't prepare query insert into labels: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$labels})
     {
@@ -333,7 +342,7 @@
     
       print " execute '$label'\n" if ($verbose);  
       $query->execute($label)
-		    or print "WARNING: Couldn't execute insert into revision ($label): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into revision ($label): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   print "new build id: $newbuildid\n";
@@ -355,49 +364,49 @@
     
     print " prepare 'update builds set $field_list where id=$id'\n" if ($verbose);
     my $query = $db->prepare("update builds set $field_list where id=$id")
-      or die("Couldn't prepare query update builds: " . $db->errstr());
+      or die("Couldn't prepare query update builds: " . $db->errstr()) if (!$dryrun);
 
     print " execute '@fields'\n" if ($verbose);
     $query->execute(@fields)
-		  or print "WARNING: Couldn't execute update builds (@fields): " . $db->errstr() . "\n";
+		  or print "WARNING: Couldn't execute update builds (@fields): " . $db->errstr() . "\n" if (!$dryrun);
   }
   if (@{$envinfo})
   {
     print " prepare 'delete from envinfo where build_id=$id'\n" if ($verbose);
     my $delete_query = $db->prepare("delete from envinfo where build_id=$id")
-      or die("Couldn't prepare query delete from envinfo: " . $db->errstr());
+      or die("Couldn't prepare query delete from envinfo: " . $db->errstr()) if (!$dryrun);
     
     print " execute ''\n" if ($verbose);  
     $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from envinfo: " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute delete from envinfo: " . $db->errstr() . "\n" if (!$dryrun);
     
     print " prepare 'insert into envinfo (build_id,tool,version) values ($id,?,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into envinfo (build_id,tool,version) values ($id,?,?)")
-      or die("Couldn't prepare query insert into envinfo: " . $db->errstr());
+      or die("Couldn't prepare query insert into envinfo: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$envinfo})
     {
       my $tool = $entry->{tool};
       my $version = $entry->{version};
     
-      print " execute '$tool,$version'\n" if ($verbose);  
+      print " execute '$tool,$version'\n" if ($verbose);
       $query->execute($tool, $version)
-		    or print "WARNING: Couldn't execute insert into envinfo ($tool,$version): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into envinfo ($tool,$version): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   if (@{$failures})
   {
     print " prepare 'delete from failures where build_id=$id'\n" if ($verbose);
     my $delete_query = $db->prepare("delete from failures where build_id=$id")
-      or die("Couldn't prepare query delete from failures: " . $db->errstr());
+      or die("Couldn't prepare query delete from failures: " . $db->errstr()) if (!$dryrun);
     
     print " execute ''\n" if ($verbose);  
     $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from failures: " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute delete from failures: " . $db->errstr() . "\n" if (!$dryrun);
     
     print " prepare 'insert into failures (build_id,category,count) values ($id,?,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into failures (build_id,category,count) values ($id,?,?)")
-      or die("Couldn't prepare query insert into failures: " . $db->errstr());
+      or die("Couldn't prepare query insert into failures: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$failures})
     {
@@ -406,14 +415,14 @@
     
       print " execute '$category,$count'\n" if ($verbose);  
       $query->execute($category, $count)
-		    or print "WARNING: Couldn't execute insert into failures ($category,$count): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into failures ($category,$count): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   if (@{$reports})
   {
     print " prepare 'delete from reports where build_id=$id'\n" if ($verbose);
     my $delete_query = $db->prepare("delete from reports where build_id=$id")
-      or die("Couldn't prepare query delete from reports: " . $db->errstr());
+      or die("Couldn't prepare query delete from reports: " . $db->errstr()) if (!$dryrun);
     
     print " execute ''\n" if ($verbose);  
     $delete_query->execute()
@@ -421,7 +430,7 @@
     
     print " prepare 'insert into reports (build_id,name,url,type) values ($id,?,?,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into reports (build_id,name,url,type) values ($id,?,?,?)")
-      or die("Couldn't prepare query insert into failures: " . $db->errstr());
+      or die("Couldn't prepare query insert into failures: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$reports})
     {
@@ -431,22 +440,22 @@
     
       print " execute '$name,$url,$type'\n" if ($verbose);  
       $query->execute($name, $url, $type)
-		    or print "WARNING: Couldn't execute insert into reports ($name,$url,$type): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into reports ($name,$url,$type): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   if (@{$content})
   {
     print " prepare 'delete from content where build_id=$id'\n" if ($verbose);
     my $delete_query = $db->prepare("delete from content where build_id=$id")
-      or die("Couldn't prepare query delete from content: " . $db->errstr());
+      or die("Couldn't prepare query delete from content: " . $db->errstr()) if (!$dryrun);
     
     print " execute ''\n" if ($verbose);  
     $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from content: " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute delete from content: " . $db->errstr() . "\n" if (!$dryrun);
     
     print " prepare 'insert into content (build_id,name,url,revision) values ($id,?,?,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into content (build_id,name,url,revision) values ($id,?,?,?)")
-      or die("Couldn't prepare query insert into content: " . $db->errstr());
+      or die("Couldn't prepare query insert into content: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$content})
     {
@@ -456,22 +465,22 @@
     
       print " execute '$name,$url,$revision'\n" if ($verbose);  
       $query->execute($name, $url, $revision)
-		    or print "WARNING: Couldn't execute insert into content ($name,$url,$revision): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into content ($name,$url,$revision): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
   if (@{$labels})
   {
     print " prepare 'delete from labels where build_id=$id'\n" if ($verbose);
     my $delete_query = $db->prepare("delete from labels where build_id=$id")
-      or die("Couldn't prepare query delete from labels: " . $db->errstr());
+      or die("Couldn't prepare query delete from labels: " . $db->errstr()) if (!$dryrun);
     
     print " execute ''\n" if ($verbose);  
     $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from labels: " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute delete from labels: " . $db->errstr() . "\n" if (!$dryrun);
     
     print " prepare 'insert into labels (build_id,label) values ($id,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into label (build_id,label) values ($id,?)")
-      or die("Couldn't prepare query insert into label: " . $db->errstr());
+      or die("Couldn't prepare query insert into label: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$labels})
     {
@@ -479,7 +488,7 @@
       
       print " execute '$label'\n" if ($verbose);  
       $query->execute($label)
-		    or print "WARNING: Couldn't execute insert into labels ($label): " . $db->errstr() . "\n";
+		    or print "WARNING: Couldn't execute insert into labels ($label): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
 }