envinfo/envinfo.pl
changeset 275 f914b0494f03
parent 274 becbd829b411
child 276 ad9846216af2
equal deleted inserted replaced
274:becbd829b411 275:f914b0494f03
     9 #
     9 #
    10 # Contributors:
    10 # Contributors:
    11 # Dario Sestito <darios@symbian.org>
    11 # Dario Sestito <darios@symbian.org>
    12 #
    12 #
    13 # Description:
    13 # Description:
    14 # Dumps environment info such as tools version to cmdline or optionally to a Diamonds file
    14 # Dumps environment info such as tools version to cmdline and/or to a file
    15 
    15 
    16 use strict;
    16 use strict;
    17 
    17 
    18 use Getopt::Long;
    18 use Getopt::Long;
    19 
    19 
    20 my $output = "\\output\\logs\\diamonds_envinfo.xml";
    20 my $report;
    21 my $diamonds = 0;
    21 my $output = "\\output\\logs\\envinfo.txt";
       
    22 my $compare;
       
    23 my $baseline = "\\build_info\\logs\\envinfo.txt";
    22 my $help = 0;
    24 my $help = 0;
    23 GetOptions((
    25 GetOptions((
    24 	'diamonds!' => \$diamonds,
    26 	'report:s' => \$report,
    25 	'out=s' => \$output,
    27 	'compare:s' => \$compare,
    26 	'help!' => \$help
    28 	'help!' => \$help
    27 ));
    29 ));
    28 
    30 
       
    31 $output = $report if ($report);
       
    32 $baseline = $compare if ($compare);
       
    33 
    29 if ($help)
    34 if ($help)
    30 {
    35 {
    31 	print "Dumps environment info such as tools version to cmdline or optionally to a Diamonds file\n";
    36 	print "Dumps environment info such as tools version to cmdline and/or to a file\n";
    32 	print "Usage: perl envinfo.pl [-d [-o XMLFILE]]\n";
    37 	print "Usage: perl envinfo.pl [OPTIONS]\n";
    33 	print "\n";
    38 	print "where OPTIONS are:\n";
    34 	print "-d,--diamonds\tcreate Diamonds file with environment info\n";
    39 	print "-r,--report [FILE]\tCreate report file (default \\output\\logs\\envinfo.txt)\n";
    35 	print "-o,--out XMLFILE Diamonds file to write to (default \\output\\logs\\diamonds_envinfo.xml)\n";
    40 	print "-c,--compare [LOCATION]\tCompare environment with info at LOCATION (default \\output\\logs\\envinfo.txt)\n";
    36 	exit(0);
    41 	exit(0);
    37 }
    42 }
       
    43 
       
    44 my $baseline_environment_info = {};
       
    45 if (defined $compare)
       
    46 {
       
    47 	my $target = '';
       
    48 	my $tmp_file = '';
       
    49 	# understand where we should get the info from
       
    50 	$target = $baseline if (-f $baseline);
       
    51 	$target = "$baseline\\envinfo.txt" if (!$target && -f "$baseline\\envinfo.txt");
       
    52 	$target = "$baseline\\build_info\\logs\\envinfo.txt" if (!$target && -f "$baseline\\build_info\\logs\\envinfo.txt");
       
    53 	$target = "$baseline\\build_BOM.zip" if (!$target && -f "$baseline\\build_BOM.zip");
       
    54 	if (!$target)
       
    55 	{
       
    56 		warn "WARNING: Can't find envinfo.txt from location '$baseline'\n";
       
    57 	}
       
    58 	elsif ($target =~ /\.zip$/)
       
    59 	{
       
    60 		print "Extracting envinfo.txt from $target\n";
       
    61 		my $cmd = "7z e -y $target build_info\logs\envinfo.txt";
       
    62 		my $output = `$cmd 2>&1`;
       
    63 		if ($output =~ /is not recognized as an internal or external command/)
       
    64 		{
       
    65 			$target = '';
       
    66 			warn "WARNING: You need to have 7z in the PATH if you want to do comparison against a compressed baseline\n";
       
    67 		}
       
    68 		elsif ($output =~ /No files to process/)
       
    69 		{
       
    70 			$target = '';
       
    71 			warn "WARNING: The compressed baseline doesn't seem to contain an envinfo.txt file\n";
       
    72 		}
       
    73 		else
       
    74 		{
       
    75 			my $tmp_file = "tmp$$.txt";
       
    76 			system("ren envinfo.txt $tmp_file"); 
       
    77 			$target = $tmp_file;
       
    78 		}
       
    79 	}
       
    80 	
       
    81 	if (!$target)
       
    82 	{
       
    83 		warn "WARNING: Will not do comparison\n";
       
    84 		$compare = undef; 
       
    85 	}
       
    86 	else
       
    87 	{
       
    88 		print "Will compare environment info to $target\n";
       
    89 		
       
    90 		if (open(BASEINFO, $target))
       
    91 		{
       
    92 			for my $line (<BASEINFO>)
       
    93 			{
       
    94 				if ($line =~ /([^\t]*)\t([^\t]*)/)
       
    95 				{
       
    96 					my $name = $1;
       
    97 					my $version = $2;
       
    98 					chomp $name;
       
    99 					chomp $version;
       
   100 					$baseline_environment_info->{$name}=$version;
       
   101 				}
       
   102 			}
       
   103 			close(BASEINFO);
       
   104 			unlink $tmp_file if ($tmp_file);
       
   105 		}
       
   106 		else
       
   107 		{
       
   108 			warn "WARNING: Could not open file $target for reading. Will not do comparison\n";
       
   109 			$compare = undef;
       
   110 		}
       
   111 	}
       
   112 	
       
   113 }
       
   114 
    38 
   115 
    39 my @environment_info = ();
   116 my @environment_info = ();
    40 
   117 
    41 # Machine name
   118 # Machine name
    42 push @environment_info, {name=>'Machine', version=>$ENV{'COMPUTERNAME'}};
   119 push @environment_info, {name=>'Machine', version=>$ENV{'COMPUTERNAME'}};
   157 my $java_ver = 'N.A.';
   234 my $java_ver = 'N.A.';
   158 my $java_out = `java -version 2>&1`;
   235 my $java_out = `java -version 2>&1`;
   159 $java_ver = $1 if ($java_out =~ /^java version (.*)/m);
   236 $java_ver = $1 if ($java_out =~ /^java version (.*)/m);
   160 push @environment_info, {name=>'java', version=>$java_ver};
   237 push @environment_info, {name=>'java', version=>$java_ver};
   161 
   238 
       
   239 # change tabs to spaces
   162 for my $tool_info (@environment_info)
   240 for my $tool_info (@environment_info)
   163 {
   241 {
   164 	print $tool_info->{name} . ": " . $tool_info->{version} . "\n";
   242 	$tool_info->{name} =~ s/\t/ /g;
   165 }
   243 	$tool_info->{version} =~ s/\t/ /g;
   166 
   244 }
   167 
   245 
   168 # write diamonds file
   246 print "\nTools breakdown\n";
   169 if ($diamonds)
   247 
   170 {
   248 my $cmp_notpresent = 0;
   171 	@environment_info = reverse(@environment_info);
   249 my $cmp_diffver = 0;
       
   250 for my $tool_info (@environment_info)
       
   251 {
       
   252 	print " " . $tool_info->{name} . ": " . $tool_info->{version};
   172 	
   253 	
   173 	my $xml_content = <<_EOX;
   254 	if (defined $compare &&
   174 <?xml version=\"1.0\" encoding=\"UTF-8\"?>
   255 		$tool_info->{name} ne 'Machine' &&
   175 <diamonds-build>
   256 		$tool_info->{name} ne 'OS Name')
   176  <schema>10</schema>
   257 	{
   177   <tools>        
   258 		print "\t";
   178 _HERE_TOOLS_LINES_
   259 		if (defined $baseline_environment_info->{$tool_info->{name}})
   179   </tools>
   260 		{
   180 </diamonds-build>
   261 			my $baselineversion = $baseline_environment_info->{$tool_info->{name}};
   181 _EOX
   262 			if ($tool_info->{version} eq 'N.A.' && $baselineversion ne 'N.A.')
   182 	
   263 			{
   183 	my $tools_lines = '';
   264 				print "[ERROR: tool not present]";
   184 	for my $tool_info (@environment_info)
   265 				$cmp_notpresent++;
   185 	{
   266 			}
   186 		$tools_lines .= "   <tool><name>$tool_info->{name}</name><version>$tool_info->{version}</version></tool>\n";
   267 			elsif ($tool_info->{version} eq $baselineversion || $baselineversion eq 'N.A.')
   187 	}
   268 			{
   188 	
   269 				print "[OK]";
   189 	$xml_content =~ s/_HERE_TOOLS_LINES_/$tools_lines/;
   270 			}
   190 	
   271 			elsif ($tool_info->{version} cmp $baselineversion < 0)
       
   272 			{
       
   273 				print "[WARNING: less recent than baseline]";
       
   274 				$cmp_diffver++;
       
   275 			}
       
   276 			elsif ($tool_info->{version} cmp $baselineversion > 0)
       
   277 			{
       
   278 				print "[WARNING: more recent than baseline]";
       
   279 				$cmp_diffver++;
       
   280 			}
       
   281 		}
       
   282 	}
       
   283 	print "\n";
       
   284 }
       
   285 
       
   286 print "\n";
       
   287 
       
   288 if (defined $compare)
       
   289 {
       
   290 	print "Summary of comparison to baseline:\n";
       
   291 	if ($cmp_notpresent || $cmp_diffver)
       
   292 	{
       
   293 		print " Tools not present or not found in the expected location: $cmp_notpresent\n";
       
   294 		print " Tools at different version: $cmp_diffver\n";
       
   295 	}
       
   296 	else
       
   297 	{
       
   298 		print " All tools seem to match the baseline :-)\n";
       
   299 	}
       
   300 	print "\n";
       
   301 }
       
   302 
       
   303 # write report file
       
   304 if (defined $report)
       
   305 {
   191 	if (open(ENVINFO, ">$output"))
   306 	if (open(ENVINFO, ">$output"))
   192 	{
   307 	{
   193 		print ENVINFO $xml_content;
   308 		for my $tool_info (@environment_info)
       
   309 		{
       
   310 			print ENVINFO "$tool_info->{name}\t$tool_info->{version}\n";
       
   311 		}
   194 		close(ENVINFO);
   312 		close(ENVINFO);
   195 		print "Wrote Diamonds file: $output\n";
   313 		print "Wrote report file: $output\n";
   196 	}
   314 	}
   197 	else
   315 	else
   198 	{
   316 	{
   199 		warn "Could not write to file: $output\n";
   317 		warn "WARNING: Could not write to file: $output\n";
   200 	}
   318 	}
   201 }
   319 }