Moved common code to shared module.
authorSimon Howkins <simonh@symbian.org>
Tue, 10 Nov 2009 16:52:27 +0000
changeset 752 1f07674ec99f
parent 751 da0e0bc45450
child 753 5069de517698
Moved common code to shared module.
common/tools/brag/raptorToBRAG.pl
common/tools/brag/toBrag.pm
common/tools/brag/yarpToBRAG.pl
--- a/common/tools/brag/raptorToBRAG.pl	Thu Nov 12 11:56:20 2009 +0000
+++ b/common/tools/brag/raptorToBRAG.pl	Tue Nov 10 16:52:27 2009 +0000
@@ -18,10 +18,12 @@
 
 use FindBin;
 use lib "$FindBin::Bin/../lib";
+use lib "$FindBin::Bin";
 
 use Getopt::Long;
 use Text::CSV;
 
+use ToBrag;
 
 my $raptorSummary;
 my $help = 0;
@@ -39,24 +41,9 @@
 }
 
 # Start to build structure to be output as XML (same format as XML::Parser would create for us)
-my $xmlNewline = bless { Text => "\n" }, "Characters";
-my $buildStatus =
-[
-	bless
-	{
-		Kids =>
-		[
-			$xmlNewline,
-			bless
-			{
-				name => "Build",
-				Kids => [ $xmlNewline ]
-			}, "phase",
-		]
-	}, "buildStatus"
-];
-# Get a shortcut reference to the bit we will use a lot
-my $buildPhase = $buildStatus->[0]->{Kids}->[-1];
+my $buildStatus = ToBrag::createBuildStatus();
+# Obtain a phase object
+my $buildPhase = ToBrag::ensurePhase($buildStatus, "Build");
 
 # READ SUMMARY.CSV FILE
 open(CSV, $raptorSummary);
@@ -98,47 +85,15 @@
 	$failure->{subcategory} ||= 'uncategorized';
 	$failure->{severity} ||= 'unknown';
 	
-	# Look through the steps to see if we already have one to match this config
-	my $step;
-	foreach (@{$buildPhase->{Kids}})
-	{
-		next unless ref $_ eq "step";
-		if ($_->{name} eq $failure->{config})
-		{
-			$step = $_;
-			last;
-		}
-	}
-	unless ($step)
-	{
-		# First item found in this step - create step entry
-		$step = bless { name => $failure->{config}, Kids => [ $xmlNewline ] }, "step";
-		push @{$buildPhase->{Kids}}, $step, $xmlNewline;
-		# Also create empty <failures> tags with severities in a sensible order
-		foreach my $severity (qw{critical major minor})
-		{
-			my $failureSet = bless { level => $severity, Kids => [ $xmlNewline ] }, "failures";
-			push @{$step->{Kids}}, $failureSet, $xmlNewline;
-		}
-	}
+	# Obtain a matching step
+	my $step = ToBrag::ensureStep($buildPhase, $failure->{config});
+	# Also create empty <failures> tags with severities in a sensible order
+	ToBrag::ensureFailureSet($step, "critical");
+	ToBrag::ensureFailureSet($step, "major");
+	ToBrag::ensureFailureSet($step, "minor");
 	
-	# Look through the sets of failures in this step to see if we hve one which matches this severity
-	my $failureSet;
-	foreach (@{$step->{Kids}})
-	{
-		next unless ref $_ eq "failures";
-		if ($_->{level} eq $failure->{severity})
-		{
-			$failureSet = $_;
-			last;
-		}
-	}
-	unless ($failureSet)
-	{
-		# First item found at this severity - create failures entry
-		$failureSet = bless { level => $failure->{severity}, Kids => [ $xmlNewline ] }, "failures";
-		push @{$step->{Kids}}, $failureSet, $xmlNewline;
-	}
+	# Obtain a set of failures which matches this severity
+	my $failureSet = ToBrag::ensureFailureSet($step, $failure->{severity});
 
 	# Now create the failure itself, and add it to this failure set
 	my $failureItem = bless {
@@ -150,64 +105,15 @@
 		$failure->{component} =~ s{^(/sf/.*?/.*?)/.*$}{$1};
 		$failureItem->{package} = $failure->{component};
 	}
-	push @{$failureSet->{Kids}}, $failureItem, $xmlNewline;
+	push @{$failureSet->{Kids}}, $failureItem, $ToBrag::xmlNewline;
 }
 close(CSV);
 
 # Print XML
 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 print "<?xml-stylesheet type='text/xsl' href='brag.xsl'?>\n";
-printTree($buildStatus->[0]);
+ToBrag::printTree($buildStatus->[0]);
 print "\n";
 
 exit(0);
 
-sub printTree
-{
-	my $tree = shift or die;
-	die unless ref $tree;
-
-	my $tagName = ref $tree;
-	$tagName =~ s{^main::}{};
-	if ($tagName eq "Characters")
-	{
-		print $tree->{Text};
-		return;
-	}
-	
-	print "<$tagName";
-
-	foreach my $attr (
-		sort {
-			my $order = "name level start stop href";
-			my $ixA = index $order, $a;
-			my $ixB = index $order, $b;
-			die "$a $b" if $ixA + $ixB == -2;
-			$ixA - $ixB;
-		}
-		grep {
-			! ref $tree->{$_}
-		}
-		keys %$tree)
-	{
-		print " $attr=\"$tree->{$attr}\"";
-	}
-
-	my $children = $tree->{Kids} || [];
-	if (scalar @$children)
-	{
-		print ">";
-		foreach my $child (@$children)
-		{
-			printTree($child);
-		}
-		print "</$tagName";
-	}
-	else
-	{
-		print "/"
-	}
-
-	print ">";
-}
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/tools/brag/toBrag.pm	Tue Nov 10 16:52:27 2009 +0000
@@ -0,0 +1,140 @@
+#!perl -w
+#
+# Copyright (c) 2009 Symbian Foundation Ltd
+# This component and the accompanying materials are made available
+# under the terms of the License "Eclipse Public License v1.0"
+# which accompanies this distribution, and is available
+# at the URL "http://www.eclipse.org/legal/epl-v10.html".
+#
+# Initial Contributors:
+# Symbian Foundation Ltd - initial contribution.
+#
+# Contributors:
+#
+# Description:
+# Functionality common to BRAG file generation
+
+use strict;
+
+package ToBrag;
+
+# A useful constant
+our $xmlNewline = bless { Text => "\n" }, "Characters";
+
+sub createBuildStatus
+{
+	return [
+		bless
+		{
+			Kids =>
+			[ $ToBrag::xmlNewline ]
+		}, "buildStatus"
+	];
+}
+
+sub ensurePhase
+{
+	my $buildStatus = shift;
+	my $phaseName = shift;
+
+	my ($phase) = grep { ref $_ eq "phase" && $_->{name} eq $phaseName } @{$buildStatus->[-1]->{Kids}};
+	unless ($phase)
+	{
+		$phase = bless
+		{
+			name => $phaseName,
+			Kids => [ $ToBrag::xmlNewline ]
+		}, "phase";
+		push @{$buildStatus->[-1]->{Kids}}, $phase, $ToBrag::xmlNewline;
+	}
+	return $phase;
+}
+
+sub ensureStep
+{
+	my $phase = shift;
+	my $stepName = shift;
+
+	my ($step) = grep { ref $_ eq "step" && $_->{name} eq $stepName } @{$phase->{Kids}};
+	unless ($step)
+	{
+		$step = bless
+		{
+			name => $stepName,
+			Kids => [ $ToBrag::xmlNewline ]
+		}, "step";
+		push @{$phase->{Kids}}, $step, $ToBrag::xmlNewline;
+	}
+	return $step;
+}
+
+sub ensureFailureSet
+{
+	my $step = shift;
+	my $level = shift;
+
+	my ($failureSet) = grep { ref $_ eq "failures" && $_->{level} eq $level } @{$step->{Kids}};
+	unless ($failureSet)
+	{
+		$failureSet = bless
+		{
+			level => $level,
+			Kids => [ $ToBrag::xmlNewline ]
+		}, "failures";
+		push @{$step->{Kids}}, $failureSet, $ToBrag::xmlNewline;
+	}
+	return $failureSet;
+}
+
+# Prints out the XML tree to STDOUT
+sub printTree
+{
+	my $tree = shift or die;
+	die unless ref $tree;
+
+	my $tagName = ref $tree;
+	$tagName =~ s{^main::}{};
+	if ($tagName eq "Characters")
+	{
+		print $tree->{Text};
+		return;
+	}
+	
+	print "<$tagName";
+
+	foreach my $attr (
+		sort {
+			my $order = "name level start stop href";
+			my $ixA = index $order, $a;
+			my $ixB = index $order, $b;
+			die "$a $b" if $ixA + $ixB == -2;
+			$ixA - $ixB;
+		}
+		grep {
+			! ref $tree->{$_}
+		}
+		keys %$tree)
+	{
+		print " $attr=\"$tree->{$attr}\"";
+	}
+
+	my $children = $tree->{Kids} || [];
+	if (scalar @$children)
+	{
+		print ">";
+		foreach my $child (@$children)
+		{
+			printTree($child);
+		}
+		print "</$tagName";
+	}
+	else
+	{
+		print "/"
+	}
+
+	print ">";
+}
+
+1;
+
--- a/common/tools/brag/yarpToBRAG.pl	Thu Nov 12 11:56:20 2009 +0000
+++ b/common/tools/brag/yarpToBRAG.pl	Tue Nov 10 16:52:27 2009 +0000
@@ -18,10 +18,13 @@
 
 use FindBin;
 use lib "$FindBin::Bin/../lib";
+use lib "$FindBin::Bin";
 
 use Getopt::Long;
 use Text::CSV;
 
+use ToBrag;
+
 if (!@ARGV)
 {
 	warn "Generate an XML summary of the Raptor build from a Yarp CSV file\n";
@@ -30,20 +33,9 @@
 }
 
 # Start to build structure to be output as XML (same format as XML::Parser would create for us)
-my $xmlNewline = bless { Text => "\n" }, "Characters";
-my $buildPhase = bless { name => "Build", Kids => [ $xmlNewline ] }, "phase";
-my $buildStatus =
-[
-	bless
-	{
-		Kids =>
-		[
-			$xmlNewline,
-			$buildPhase,
-			$xmlNewline,
-		]
-	}, "buildStatus"
-];
+my $buildStatus = ToBrag::createBuildStatus();
+# Obtain a phase object
+my $buildPhase = ToBrag::ensurePhase($buildStatus, "Build");
 
 @ARGV = map { glob $_ } @ARGV;
 
@@ -114,48 +106,16 @@
 #		die $failure->{name} unless exists $errorIdToDetail{$failure->{name}};
 		my $message = $errorIdToDetail{$failure->{name}}->{message} || "Unknown failure tag '$failure->{name}' ($failure->{source} -> $failure->{target})";
 		$failure->{severity} = $errorIdToDetail{$failure->{name}}->{severity} || "unknown";
-
-		# Look through the steps to see if we already have one to match this platform
-		my $step;
-		foreach (@{$buildPhase->{Kids}})
-		{
-			next unless ref $_ eq "step";
-			if ($_->{name} eq $failure->{platform})
-			{
-				$step = $_;
-				last;
-			}
-		}
-		unless ($step)
-		{
-			# First item found for this platform - create step entry
-			$step = bless { name => $failure->{platform}, Kids => [ $xmlNewline ] }, "step";
-			push @{$buildPhase->{Kids}}, $step, $xmlNewline;
-			# Also create empty <failures> tags with severities in a sensible order
-			foreach my $severity (qw{critical major minor})
-			{
-				my $failureSet = bless { level => $severity, Kids => [ $xmlNewline ] }, "failures";
-				push @{$step->{Kids}}, $failureSet, $xmlNewline;
-			}
-		}
 		
-		# Look through the sets of failures in this step to see if we hve one which matches this severity
-		my $failureSet;
-		foreach (@{$step->{Kids}})
-		{
-			next unless ref $_ eq "failures";
-			if ($_->{level} eq $failure->{severity})
-			{
-				$failureSet = $_;
-				last;
-			}
-		}
-		unless ($failureSet)
-		{
-			# First item found at this severity - create failures entry
-			$failureSet = bless { level => $failure->{severity}, Kids => [ $xmlNewline ] }, "failures";
-			push @{$step->{Kids}}, $failureSet, $xmlNewline;
-		}
+		# Obtain a matching step
+		my $step = ToBrag::ensureStep($buildPhase, $failure->{platform});
+		# Also create empty <failures> tags with severities in a sensible order
+		ToBrag::ensureFailureSet($step, "critical");
+		ToBrag::ensureFailureSet($step, "major");
+		ToBrag::ensureFailureSet($step, "minor");
+		
+		# Obtain a set of failures which matches this severity
+		my $failureSet = ToBrag::ensureFailureSet($step, $failure->{severity});
 		
 		# Now create the failure itself, and add it to this failure set
 		my $failureItem = bless {
@@ -163,64 +123,15 @@
 			"package" => $failure->{package},
 			Kids => [ bless { Text => $message }, "Characters" ],
 		}, "failure";
-		push @{$failureSet->{Kids}}, $failureItem, $xmlNewline;
+		push @{$failureSet->{Kids}}, $failureItem, $ToBrag::xmlNewline;
 	}
 	close(CSV);
 }
 # Print XML
 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 print "<?xml-stylesheet type='text/xsl' href='brag.xsl'?>\n";
-printTree($buildStatus->[0]);
+ToBrag::printTree($buildStatus->[0]);
 print "\n";
 
 exit(0);
 
-sub printTree
-{
-	my $tree = shift or die;
-	die unless ref $tree;
-
-	my $tagName = ref $tree;
-	$tagName =~ s{^main::}{};
-	if ($tagName eq "Characters")
-	{
-		print $tree->{Text};
-		return;
-	}
-	
-	print "<$tagName";
-
-	foreach my $attr (
-		sort {
-			my $order = "name level start stop href";
-			my $ixA = index $order, $a;
-			my $ixB = index $order, $b;
-			die "$a $b" if $ixA + $ixB == -2;
-			$ixA - $ixB;
-		}
-		grep {
-			! ref $tree->{$_}
-		}
-		keys %$tree)
-	{
-		print " $attr=\"$tree->{$attr}\"";
-	}
-
-	my $children = $tree->{Kids} || [];
-	if (scalar @$children)
-	{
-		print ">";
-		foreach my $child (@$children)
-		{
-			printTree($child);
-		}
-		print "</$tagName";
-	}
-	else
-	{
-		print "/"
-	}
-
-	print ">";
-}
-