common/tools/bitops.pl
changeset 1280 61388ab6ba2c
parent 1182 f15fa527df95
child 1281 c5308055d94d
--- a/common/tools/bitops.pl	Tue Oct 05 13:15:25 2010 +0100
+++ b/common/tools/bitops.pl	Tue Oct 05 13:32:23 2010 +0100
@@ -10,34 +10,50 @@
 
 my $help = 0;
 my $create = 0;
+my $file = '';
 my $id = '';
-my $file = '';
+my $append = 0;
+my $delete = '';
 my $verbose = 0;
 my $dryrun = 0;
 GetOptions((
 	'create!' => \$create,
 	'file=s' => \$file,
-	'id=s' => \$id,
+	'update=s' => \$id,
+	'append!' => \$append,
+	'remove=s' => \$delete,
 	'help!' => \$help,
 	'verbose!' => \$verbose,
 	'dryrun!' => \$dryrun,
 ));
 
-if ($help||!$file)
+if ($help||(($create||$id)&&!$file)||(!$create&&!$id&&!$delete))
 {
   print <<_EOH;
 bitops
 Executes operations on the BIT db
 
-Usage: bitops.pl [-c|-i ID] -f FILE
+Usage:
+  bitops.pl -c -f FILE
+  bitops.pl -u ID [-a] -f FILE
+  bitops.pl -r ID
 
 Options:
   -h, --help            Show this help message and exit
   -c, --create          Create entry for a new build then set the attributes to
                         the values provided in FILE. Returns the build id for
                         the newly created build.
-  -i ID, --id ID        Update entry for build number ID with the values
-                        provided in FILE.
+  -u ID, --update ID    Update entry for build number ID with the values
+                        provided in FILE. For the update operation only the
+                        entry in the BUILDS table will be actually updated, the
+                        entries in the other tables will be deleted, then the
+                        data supplied will be inserted (this behaviour can be
+                        changed with the -a option)
+  -a, --append          Do not delete entries in the tables different than
+                        BUILDS before inserting the data supplied. This option
+                        must be used in conjuntion with -u (see info for -u
+                        option for more information)
+  -r, --remove          Remove build ID (entries in all tables) from the db
   -f FILE, --file FILE  Use attributes in FILE to create/update the build info
                         See below for file format.
   -v, --verbose         Increase info level
@@ -47,24 +63,29 @@
 File format:
   File must be a list of attribute-value-pairs. One avp per line, on each line
   attribute and value are separated by a tab.
-  These are the supported attributes (in brackets the syntax of the value):
-  - build_id_string
-  - package (VALUE)
-  - platform (VALUE)
-  - started (VALUE)
-  - finished (VALUE)
-  - creator (VALUE)
-  - machine (VALUE)
-  - build_brag (VALUE)
-  - test_brag (VALUE)
-  - baseline_short (VALUE)
-  - sources_short (VALUE)
-  - envinfo (TOOL,VERSION)
-  - failure (CATEGORY,COUNT)
-  - report (NAME,URL,TYPE)
-  - content (NAME,URL,REVISION)
-  - baseline (TYPE,PATH)
-  - label (VALUE)
+  These are the supported attributes (in brackets the syntax of the value) with
+  the table they belong to:
+  - build_id_string - BUILDS
+  - package (VALUE) - BUILDS
+  - platform (VALUE) - BUILDS
+  - started (VALUE) - BUILDS
+  - finished (VALUE) - BUILDS
+  - creator (VALUE) - BUILDS
+  - machine (VALUE) - BUILDS
+  - build_brag (VALUE) - BUILDS
+  - test_brag (VALUE) - BUILDS
+  - baseline_short (VALUE) - BUILDS
+  - sources_short (VALUE) - BUILDS
+  - envinfo (TOOL,VERSION) - ENVINFO
+  - failure (CATEGORY,COUNT) - FAILURES
+  - report (NAME,URL,TYPE) - REPORTS
+  - content (NAME,URL,REVISION) - CONTENT
+  - baseline (TYPE,PATH) - BASELINES
+  - label (VALUE) - LABELS
+  - test (NAME,TYPE,TARGET,STARTED,FINISHED,CASES_TOTAL,CASES_PASSED,
+      CASES_FAILED,CASES_NOT_RUN,CASES_NOT_AVAILABLE,PASS_RATE) - TESTING
+  
+Note on the update operation:
 _EOH
 	exit(0);
 }
@@ -78,7 +99,7 @@
 my $content = [];
 my $baselines = [];
 my $labels = [];
-my $testing_entry = {};
+my $testing = [];
 
 sub ConnectToDB()
 {
@@ -112,7 +133,7 @@
       my $attr = $1;
       my $value = $2;
       
-      if ($attr =~ /^(build_id_string|package|platform|started|finished|creator|machine|build_brag|test_brag|baseline_short|sources_short)$/i)
+      if ($attr =~ /^(build_id_string|package|platform|started|finished|creator|machine|build_brag|test_brag|baseline_short|sources_short|id)$/i)
       {
         $attr = lc($attr);
         print " found ($attr,$value) for table 'build'\n" if ($verbose);
@@ -235,6 +256,45 @@
         print " found ($attr,$value) for table 'labels'\n" if ($verbose);
         push(@{$labels}, {label=>$value});
       }
+      elsif ($attr =~ /^(test)$/i)
+      {
+        $attr = lc($attr);
+        if ($value =~ /([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)/)
+        {
+          my $name=$1;
+          my $type=$2;
+          my $target=$3;
+          my $started=$4;
+          my $finished=$5;
+          my $cases_total=$6;
+          $cases_total=0 if (!$cases_total);
+          my $cases_passed=$7;
+          $cases_passed=0 if (!$cases_passed);
+          my $cases_failed=$8;
+          $cases_failed=0 if (!$cases_failed);
+          my $cases_not_run=$9;
+          $cases_not_run=0 if (!$cases_not_run);
+          my $cases_not_available=$10;
+          $cases_not_available=0 if (!$cases_not_available);
+          my $pass_rate=$11;
+          $pass_rate="0.0" if (!$pass_rate);
+          
+          if (!$name)
+          {
+            print "ERROR: Name is empty: \"$value\"\n";
+            return 1;
+          }
+          print " found ($name,$type,$target,$started,$finished,$cases_total,$cases_passed,$cases_failed,$cases_not_run,$cases_not_available,$pass_rate) for table 'testing'\n" if ($verbose);
+          push(@{$testing}, {name=>$name,type=>$type,target=>$target,started=>$started,finished=>$finished,
+            cases_total=>$cases_total,cases_passed=>$cases_passed,cases_failed=>$cases_failed,cases_not_run=>$cases_not_run,
+            cases_not_available=>$cases_not_available,pass_rate=>$pass_rate});
+        }
+        else
+        {
+          print "ERROR: Could not understand value of envinfo: \"$value\"\n";
+          return 1;
+        }
+      }
       else
       {
         print "ERROR: Could not understand avp: \"$line\"\n";
@@ -254,8 +314,8 @@
 }
 
 # MAIN PROGRAM
-
-my $r=parse_file($file);
+my $r=0;
+$r=parse_file($file) if ($create||$id);
 if ($r)
 {
   print "Error while parsing file $file\n";
@@ -286,7 +346,7 @@
       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" if (!$dryrun);
+		  or print "WARNING: Couldn't execute insert into builds ($field_list) values (@fields): " . $db->errstr() . "\n" if (!$dryrun);
 		  
 		$newbuildid = $db->last_insert_id(undef, undef, undef, undef) if (!$dryrun);
   }
@@ -385,12 +445,44 @@
 		    or print "WARNING: Couldn't execute insert into revision ($label): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
+  
+  if (@{$testing})
+  {
+    print " prepare_cached 'insert into testing (build_id,name,type,target,started,finished,cases_total,cases_passed,cases_failed,cases_not_run,cases_not_available,pass_rate) values ($newbuildid,?,?,?,?,?,?,?,?,?,?,?)'\n" if ($verbose);
+    my $query = $db->prepare_cached("insert into testing (build_id,name,type,target,started,finished,cases_total,cases_passed,cases_failed,cases_not_run,cases_not_available,pass_rate) values ($newbuildid,?,?,?,?,?,?,?,?,?,?,?)")
+      or die("Couldn't prepare query insert into testing: " . $db->errstr()) if (!$dryrun);
+      
+    for my $entry (@{$testing})
+    {
+      my $name = $entry->{name};
+      my $type = $entry->{type};
+      my $target = $entry->{target};
+      my $started = $entry->{started};
+      my $finished = $entry->{finished};
+      my $cases_total = $entry->{cases_total};
+      my $cases_passed = $entry->{cases_passed};
+      my $cases_failed = $entry->{cases_failed};
+      my $cases_not_run = $entry->{cases_not_run};
+      my $cases_not_available = $entry->{cases_not_available};
+      my $pass_rate = $entry->{pass_rate};
+    
+      print " execute '$name,$type,$target,$started,$finished,$cases_total,$cases_passed,$cases_failed,$cases_not_run,$cases_not_available,$pass_rate'\n" if ($verbose);  
+      $query->execute($name,$type,$target,$started,$finished,$cases_total,$cases_passed,$cases_failed,$cases_not_run,$cases_not_available,$pass_rate)
+		    or print "WARNING: Couldn't execute insert into testing ($name,$type,$target,$started,$finished,$cases_total,$cases_passed,$cases_failed,$cases_not_run,$cases_not_available,$pass_rate): " . $db->errstr() . "\n" if (!$dryrun);
+    }
+  }
   print "new build id: $newbuildid\n";
 }
-else
+elsif ($id)
 {
   print "Updating build $id\n";
   
+  if (defined $builds_entry->{id} && $id != $builds_entry->{id})
+  {
+    print "ERROR: The build id specified in the input file is different than the one on the command line. Not updating build.\n";
+    exit(1);
+  }
+  
   if (keys %{$builds_entry})
   {
     my $field_list = '';
@@ -412,13 +504,16 @@
   }
   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()) if (!$dryrun);
+	if (!$append)
+	{
+      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()) if (!$dryrun);
     
-    print " execute ''\n" if ($verbose);  
-    $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from envinfo: " . $db->errstr() . "\n" if (!$dryrun);
+      print " execute ''\n" if ($verbose);  
+      $delete_query->execute()
+	    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,?,?)")
@@ -436,14 +531,17 @@
   }
   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()) if (!$dryrun);
+    if (!$append)
+	{
+      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()) if (!$dryrun);
     
-    print " execute ''\n" if ($verbose);  
-    $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from failures: " . $db->errstr() . "\n" if (!$dryrun);
-    
+      print " execute ''\n" if ($verbose);  
+      $delete_query->execute()
+	    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()) if (!$dryrun);
@@ -460,17 +558,20 @@
   }
   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()) if (!$dryrun);
+    if (!$append)
+	{
+      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()) if (!$dryrun);
     
-    print " execute ''\n" if ($verbose);  
-    $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from reports: " . $db->errstr() . "\n";
+      print " execute ''\n" if ($verbose);  
+      $delete_query->execute()
+	    or print "WARNING: Couldn't execute delete from reports: " . $db->errstr() . "\n";
+    }
     
     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()) if (!$dryrun);
+      or die("Couldn't prepare query insert into reports: " . $db->errstr()) if (!$dryrun);
       
     for my $entry (@{$reports})
     {
@@ -485,13 +586,16 @@
   }
   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()) if (!$dryrun);
+    if (!$append)
+	{
+	  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()) if (!$dryrun);
     
-    print " execute ''\n" if ($verbose);  
-    $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from content: " . $db->errstr() . "\n" if (!$dryrun);
+      print " execute ''\n" if ($verbose);  
+      $delete_query->execute()
+	    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,?,?,?)")
@@ -510,13 +614,16 @@
   }
   if (@{$baselines})
   {
-    print " prepare 'delete from baselines where build_id=$id'\n" if ($verbose);
-    my $delete_query = $db->prepare("delete from baselines where build_id=$id")
-      or die("Couldn't prepare query delete from baselines: " . $db->errstr()) if (!$dryrun);
+    if (!$append)
+	{
+	  print " prepare 'delete from baselines where build_id=$id'\n" if ($verbose);
+      my $delete_query = $db->prepare("delete from baselines where build_id=$id")
+        or die("Couldn't prepare query delete from baselines: " . $db->errstr()) if (!$dryrun);
     
-    print " execute ''\n" if ($verbose);  
-    $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from baselines: " . $db->errstr() . "\n" if (!$dryrun);
+      print " execute ''\n" if ($verbose);  
+      $delete_query->execute()
+	    or print "WARNING: Couldn't execute delete from baselines: " . $db->errstr() . "\n" if (!$dryrun);
+    }
     
     print " prepare 'insert into baselines (build_id,type,path) values ($id,?,?)'\n" if ($verbose);
     my $query = $db->prepare_cached("insert into baselines (build_id,type,path) values ($id,?,?)")
@@ -534,17 +641,21 @@
   }
   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()) if (!$dryrun);
+    if (!$append)
+	{
+	  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()) if (!$dryrun);
     
-    print " execute ''\n" if ($verbose);  
-    $delete_query->execute()
-		    or print "WARNING: Couldn't execute delete from labels: " . $db->errstr() . "\n" if (!$dryrun);
+      print " execute ''\n" if ($verbose);  
+      $delete_query->execute()
+	    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()) if (!$dryrun);
+    
       
     for my $entry (@{$labels})
     {
@@ -555,6 +666,102 @@
 		    or print "WARNING: Couldn't execute insert into labels ($label): " . $db->errstr() . "\n" if (!$dryrun);
     }
   }
+  if (@{$testing})
+  {
+    if (!$append)
+	{
+	  print " prepare 'delete from testing where build_id=$id'\n" if ($verbose);
+      my $delete_query = $db->prepare("delete from testing where build_id=$id")
+        or die("Couldn't prepare query delete from testing: " . $db->errstr()) if (!$dryrun);
+    
+      print " execute ''\n" if ($verbose);  
+      $delete_query->execute()
+	    or print "WARNING: Couldn't execute delete from testing: " . $db->errstr() . "\n";
+    }
+    
+    print " prepare 'insert into testing (build_id,name,type,target,started,finished,cases_total,cases_passed,cases_failed,cases_not_run,cases_not_available,pass_rate) values ($id,?,?,?,?,?,?,?,?,?,?,?)'\n" if ($verbose);
+    my $query = $db->prepare_cached("insert into testing (build_id,name,type,target,started,finished,cases_total,cases_passed,cases_failed,cases_not_run,cases_not_available,pass_rate) values ($id,?,?,?,?,?,?,?,?,?,?,?)")
+      or die("Couldn't prepare query insert into testing: " . $db->errstr()) if (!$dryrun);
+      
+    for my $entry (@{$testing})
+    {
+	  my $name = $entry->{name};
+      my $type = $entry->{type};
+      my $target = $entry->{target};
+      my $started = $entry->{started};
+      my $finished = $entry->{finished};
+      my $cases_total = $entry->{cases_total};
+      my $cases_passed = $entry->{cases_passed};
+      my $cases_failed = $entry->{cases_failed};
+      my $cases_not_run = $entry->{cases_not_run};
+      my $cases_not_available = $entry->{cases_not_available};
+      my $pass_rate = $entry->{pass_rate};
+    
+      print " execute '$name,$type,$target,$started,$finished,$cases_total,$cases_passed,$cases_failed,$cases_not_run,$cases_not_available,$pass_rate'\n" if ($verbose);
+      $query->execute($name,$type,$target,$started,$finished,$cases_total,$cases_passed,$cases_failed,$cases_not_run,$cases_not_available,$pass_rate)
+		    or print "WARNING: Couldn't execute insert into testing ($name,$type,$target,$started,$finished,$cases_total,$cases_passed,$cases_failed,$cases_not_run,$cases_not_available,$pass_rate): " . $db->errstr() . "\n" if (!$dryrun);
+    }
+  }
+}
+elsif ($delete)
+{
+  print "Removing build $delete\n";
+  
+  print " prepare 'delete from builds where id=$delete'\n" if ($verbose);
+  my $delete_query = $db->prepare("delete from builds where id=$delete")
+    or die("Couldn't prepare query delete from builds: " . $db->errstr()) if (!$dryrun);
+  print " execute ''\n" if ($verbose);  
+  $delete_query->execute()
+    or print "WARNING: Couldn't execute delete from builds: " . $db->errstr() . "\n" if (!$dryrun);
+  
+  print " prepare 'delete from envinfo where build_id=$delete'\n" if ($verbose);
+  $delete_query = $db->prepare("delete from envinfo where build_id=$delete")
+    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" if (!$dryrun);
+  
+  print " prepare 'delete from failures where build_id=$delete'\n" if ($verbose);
+  $delete_query = $db->prepare("delete from failures where build_id=$delete")
+    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" if (!$dryrun);
+    
+  print " prepare 'delete from reports where build_id=$delete'\n" if ($verbose);
+  $delete_query = $db->prepare("delete from reports where build_id=$delete")
+    or die("Couldn't prepare query delete from reports: " . $db->errstr()) if (!$dryrun);
+  print " execute ''\n" if ($verbose);  
+  $delete_query->execute()
+    or print "WARNING: Couldn't execute delete from reports: " . $db->errstr() . "\n" if (!$dryrun);
+
+  print " prepare 'delete from content where build_id=$delete'\n" if ($verbose);
+  $delete_query = $db->prepare("delete from content where build_id=$delete")
+    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" if (!$dryrun);
+    
+  print " prepare 'delete from baselines where build_id=$delete'\n" if ($verbose);
+  $delete_query = $db->prepare("delete from baselines where build_id=$delete")
+    or die("Couldn't prepare query delete from baselines: " . $db->errstr()) if (!$dryrun);
+  print " execute ''\n" if ($verbose);  
+  $delete_query->execute()
+    or print "WARNING: Couldn't execute delete from baselines: " . $db->errstr() . "\n" if (!$dryrun);
+
+  print " prepare 'delete from labels where build_id=$delete'\n" if ($verbose);
+  $delete_query = $db->prepare("delete from labels where build_id=$delete")
+    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" if (!$dryrun);
+    
+  print " prepare 'delete from testing where build_id=$delete'\n" if ($verbose);
+  $delete_query = $db->prepare("delete from testing where build_id=$delete")
+    or die("Couldn't prepare query delete from testing: " . $db->errstr()) if (!$dryrun);
+  print " execute ''\n" if ($verbose);  
+  $delete_query->execute()
+    or print "WARNING: Couldn't execute delete from testing: " . $db->errstr() . "\n" if (!$dryrun);
 }
 DisconnectFromDB();