bldsystemtools/commonbldutils/PreBldChecks.pl
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 # Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 # This script was originally just a test harness for PreBldChecks.pm
       
    15 # Subsequently it has been included in the MCL build
       
    16 # 
       
    17 #
       
    18 
       
    19 use strict;
       
    20 use Getopt::Long;
       
    21 use File::Copy;
       
    22 use Net::SMTP;
       
    23 use Sys::Hostname;
       
    24 use FindBin;
       
    25 use lib "$FindBin::Bin";
       
    26 use PreBldChecks;
       
    27 
       
    28 my $gSMTPServer = 'smtp.nokia.com';
       
    29 my $gNotificationSender = Sys::Hostname::hostname();
       
    30 if (defined $ENV{'USERNAME'}) { $gNotificationSender .= ".$ENV{'USERNAME'}"; }
       
    31 my $gXML2EnvVar = 'PreBldChecksXML2';
       
    32 
       
    33 # Notes on License Files used by CodeWarrior (CW) and/or ARM
       
    34 # The official IS-supported "RVCT2.0.1 to RVCT2.1" upgrade mechanism installs C:\APPS\ARM\LICENSE.DAT.
       
    35 # but leaves ARMLMD_LICENSE_FILE pointing to License Server(s)
       
    36 # The immediate fix was to delete the ARMLMD_LICENSE_FILE environment variable and edit
       
    37 # LM_LICENSE_FILE to read C:\apps\Metrowerks\OEM2.8\license.dat;C:\apps\arm\license.dat.
       
    38 
       
    39 # Note on CodeWarrior (CW) Version 3.0 (January 2005)
       
    40 # NOKIA now own CodeWarrior. Thus we have a further Environment Variable, named NOKIA_LICENSE_FILE
       
    41 
       
    42 # Note on CodeWarrior (CW) Version 3.1 (November 2006)
       
    43 # A licence is no longer required for the command line utilities, only for the IDE
       
    44 
       
    45 # Capture the name of this script for Help display etc.
       
    46 $0 =~ m/([^\\]+?)$/;
       
    47 my $gThisFile = $1;
       
    48 
       
    49 # Process the commandline
       
    50 my ($gXMLfile1,$gLogFile,$gNotificationAddress) = ProcessCommandLine();
       
    51 # If XML file specified, get environment variables from that file into %$gXMLEnvRef1.
       
    52 # Otherwise assume that all the required variables are already in the predefined hash %ENV
       
    53 if (defined $gXMLfile1)
       
    54 {
       
    55     my $gXMLEnvRef1;    # Reference to hash containing environment data read from the XML file
       
    56     $gXMLEnvRef1 = PreBldChecks::XMLEnvironment($gXMLfile1);
       
    57     PreBldChecks::MergeEnvironment($gXMLEnvRef1);
       
    58 }
       
    59 
       
    60 # Now refer to the build-specific XML file for further environment variables
       
    61 # In an MCL build, this file has probably not been sync'ed yet. So get it from Perforce into a temporary directory
       
    62 my $gP4Location = $ENV{CurrentCodeline};
       
    63 $gP4Location =~ s#/$##;      # Remove trailing slash, if any
       
    64 if ((uc $ENV{'Platform'}) eq 'SF')
       
    65 {
       
    66   $gP4Location = "$gP4Location/os/deviceplatformrelease/symbianosbld/cedarutils/$ENV{BuildBaseName}.xml"; #For Symbian MCL SF and TB91SF
       
    67 }
       
    68 else 
       
    69 {
       
    70   $gP4Location = "$gP4Location/$ENV{Platform}/generic/utils/$ENV{BuildBaseName}.xml";  #For Symbian OS v9.5 and earlier
       
    71 }
       
    72 
       
    73 # The following use of an environment variable to override $gP4Location is provided to make it 
       
    74 # possible to test a product-specific XML file from a development branch. This functionality
       
    75 # can be used in a manual test build or in stand-alone tests.
       
    76 # NB: Remember that you are specifying a location in Perforce, not a Windows directory!
       
    77 if (defined $ENV{$gXML2EnvVar})
       
    78 {
       
    79     $gP4Location = $ENV{$gXML2EnvVar};
       
    80 }
       
    81 
       
    82 my $gXMLfile2 = "$ENV{'TEMP'}\\$ENV{BuildBaseName}.xml";
       
    83 
       
    84 my $gCmd = "P4 print -q -o $gXMLfile2 $gP4Location 2>&1";
       
    85 my $gResponse = `$gCmd`;    # It seems that response is empty when P4 succeeds. On error, it will contain details.
       
    86 
       
    87 if (-e $gXMLfile2)
       
    88 {
       
    89     my $gXMLEnvRef2;
       
    90     $gXMLEnvRef2 = PreBldChecks::XMLEnvironment($gXMLfile2);
       
    91     chmod (0666, $gXMLfile2);   # Make file R/W
       
    92     unlink ($gXMLfile2);
       
    93     PreBldChecks::MergeEnvironment($gXMLEnvRef2);
       
    94 }
       
    95 else
       
    96 {
       
    97     $gP4Location = undef;   # See reporting text below
       
    98 }
       
    99 
       
   100 # Having acquired all available environment variables, set up text for eventual message.
       
   101 my $gBuildNumTxt = (defined $ENV{'BuildNumber'})? " - Build: $ENV{'BuildNumber'}": '';
       
   102 
       
   103 # Run checks.
       
   104 my($ErrorsRef,$WarningsRef) = PreBldChecks::AllChecks(\%ENV);
       
   105 # Log checks and/or print to screen
       
   106 my $errorCount = scalar @$ErrorsRef;
       
   107 my $warningCount = scalar @$WarningsRef;
       
   108 
       
   109 open BUILDENVOUTPUT, ">$gLogFile";
       
   110 
       
   111 print "\n\n";
       
   112 PrintLine('Results from pre-build checks:');
       
   113 PrintLine('Computer: ' . Sys::Hostname::hostname() . $gBuildNumTxt);
       
   114 PrintLine('XML Files used:');
       
   115 if (defined $gXMLfile1) { PrintLine("  $gXMLfile1"); }
       
   116 if (defined $gP4Location) { PrintLine("  $gP4Location"); }  # Report Perforce location ($gXMLfile2 points to temporary file only
       
   117 
       
   118 my @gEMailMsg = ();
       
   119 my $gErrMsg = '';
       
   120 
       
   121 if ($errorCount)
       
   122 {
       
   123     $gErrMsg = 'Errors must be fixed before restarting build!';
       
   124     PrintLine("$errorCount Error(s):");
       
   125     push @gEMailMsg, "$errorCount Error(s):\n";
       
   126     for my $text (@$ErrorsRef)
       
   127         {
       
   128         PrintLine("  $text");
       
   129         push @gEMailMsg, "\t$text\n";
       
   130         }
       
   131 }
       
   132 else
       
   133 {
       
   134     PrintLine('No error.');
       
   135 }
       
   136 
       
   137 if ($warningCount)
       
   138 {
       
   139     PrintLine("$warningCount Warning(s):");
       
   140     push @gEMailMsg, "$warningCount Warning(s):\n";
       
   141     for my $text (@$WarningsRef)
       
   142     {
       
   143         PrintLine("  $text");
       
   144         push @gEMailMsg, "\t$text\n";
       
   145     }
       
   146 }
       
   147 else
       
   148 {
       
   149     PrintLine('No warning.');
       
   150 }
       
   151 
       
   152 if($gNotificationAddress and scalar @gEMailMsg)
       
   153 {
       
   154     my $iHostName = Sys::Hostname::hostname;     # Depends on "use Sys::Hostname;"
       
   155     my $iEmailSubject = "ERROR: PreBldCheck Errors/Warnings!$gBuildNumTxt";
       
   156     my $iMsgIntro = "PreBldCheck Reports:$gBuildNumTxt";     # Message introduction (becomes first line of email body)
       
   157     $iMsgIntro .= "\nComputer $iHostName - Log file: $gLogFile";
       
   158     unshift @gEMailMsg, "$iMsgIntro:\n\n";
       
   159     push @gEMailMsg, "\n$gErrMsg\n---------------------------------------------\n";
       
   160     unless (SendEmail($iEmailSubject,@gEMailMsg)) { ++$errorCount; }
       
   161 }
       
   162 
       
   163 if ($errorCount)
       
   164 {
       
   165     print "\n";
       
   166     # IMPORTANT: In the following text, "ERROR:" is a keyword for ScanLog to identify
       
   167     PrintLine("ERROR: $gErrMsg\n");
       
   168 }
       
   169 
       
   170 close BUILDENVOUTPUT;
       
   171 print "\n";
       
   172 
       
   173 exit($errorCount);      # End of main script
       
   174 
       
   175 # PrintLine
       
   176 #
       
   177 # Inputs
       
   178 # Text to be written. (No CRLF required)
       
   179 #
       
   180 # Outputs
       
   181 # Text to screen and to log file.
       
   182 #
       
   183 # Description
       
   184 # This subroutine takes a line of text, adds CRLF and writes it to both screen and to the logfile.
       
   185 
       
   186 sub PrintLine
       
   187 {
       
   188     my $iLine = shift;
       
   189     print "$iLine\n";
       
   190     print BUILDENVOUTPUT "$iLine\n";
       
   191 }
       
   192 
       
   193 # SendEmail
       
   194 #
       
   195 # Input: Subject, Message (array of lines)
       
   196 #
       
   197 # Returns: TRUE on success
       
   198 #
       
   199 sub SendEmail
       
   200 {
       
   201     my ($iSubject, @iBody) = @_;
       
   202 
       
   203     my (@iMessage);
       
   204     my $iRetVal = 0;
       
   205 
       
   206     push @iMessage,"From: $gNotificationSender\n";
       
   207     push @iMessage,"To: $gNotificationAddress\n";
       
   208     push @iMessage,"Subject: $iSubject\n";
       
   209     push @iMessage,"\n";
       
   210     push @iMessage,@iBody;
       
   211 
       
   212     # Create an SMTP Client object that connects to the Symbian SMTP server
       
   213     # Client tells the server what the mail domain is.
       
   214     # Debug - just enables debugging information.
       
   215     my $iSMTP = Net::SMTP->new($gSMTPServer, Hello => $ENV{'COMPUTERNAME'}, Debug   => 0);
       
   216 
       
   217     if($iSMTP)
       
   218     {
       
   219         $iSMTP->mail();
       
   220         $iSMTP->to($gNotificationAddress);
       
   221         $iRetVal = $iSMTP->data(@iMessage);
       
   222         $iSMTP->quit;
       
   223     }
       
   224     unless ($iRetVal)
       
   225     {   # Report email failure to log and to screen (NB: "WARNING:" is a keyword for ScanLog to identify)
       
   226         PrintLine ("WARNING: Failed to send email notification!\nSubject: $iSubject\nMessage:\n");
       
   227         print join ('',@iBody), "\n";   # Send email body to screen only. (Will be logged by BuildServer/Client)
       
   228     }
       
   229     return $iRetVal;
       
   230 }
       
   231 
       
   232 # ProcessCommandLine
       
   233 #
       
   234 # Inputs
       
   235 #
       
   236 # Returns
       
   237 # $iXMLfile filename of XML file from, which environment data are to be read.
       
   238 # $iLogFile filename to write log to.
       
   239 #
       
   240 # Description
       
   241 # This function processes the commandline
       
   242 
       
   243 sub ProcessCommandLine {
       
   244     my ($iHelp, $iXMLfile,$iLogfile,$iEMailAddress);
       
   245     GetOptions('h' => \$iHelp, 'x=s' => \$iXMLfile, 'l=s' => \$iLogfile, 'e=s' => \$iEMailAddress);
       
   246 
       
   247     if ($iHelp) { Usage(); }
       
   248 
       
   249     if (!defined $iLogfile) { Usage("Logfile not specified!"); }
       
   250 
       
   251     &backupFile($iLogfile) if (-e $iLogfile);
       
   252   
       
   253     return($iXMLfile, $iLogfile, $iEMailAddress);
       
   254 }
       
   255 
       
   256 # backupFile
       
   257 #
       
   258 # Inputs
       
   259 # $iFile - filename to backup
       
   260 #
       
   261 # Outputs
       
   262 #
       
   263 # Description
       
   264 # This function renames an existing file with the .bak extension
       
   265 sub backupFile
       
   266 {
       
   267 	my ($iFile) = shift;
       
   268 	my ($iBak) = $iFile;
       
   269         $iBak =~ s/(\.\w*?$)/\.bak$1/;  # Convert, e.g., "PreBldChecks.log" to "PreBldChecks.bak.log"
       
   270 	if ((-e $iFile) and ($iFile ne 'NUL'))
       
   271 	{   # (NB: "WARNING:" is a keyword for ScanLog to identify)
       
   272 	    print "WARNING: $iFile already exists!\n\t Creating backup of original with new name of $iBak\n";
       
   273 	    move($iFile,$iBak) or die "WARNING: Could not backup $iFile to $iBak because of: $!\n";	  
       
   274 	}
       
   275 }
       
   276 
       
   277 # Usage
       
   278 #
       
   279 # Output Usage Information and exit whole script.
       
   280 #
       
   281 
       
   282 sub Usage {
       
   283     my $iMsg = shift;
       
   284     
       
   285     if (defined $iMsg) { print "\nERROR: $iMsg\n"; }
       
   286     
       
   287     print <<USAGE_EOF;
       
   288     
       
   289     $gThisFile:
       
   290         Carries out various checks on the build environment
       
   291         including disk free space, the state of licensing
       
   292         for CodeWarrion and for ARM RVCT.
       
   293         Writes errors and warnings to the specified log file.
       
   294         Emails notification of errors (but not warnings) to
       
   295         the specified email address.
       
   296         On exit, ERRORLEVEL is set to the Error Count.
       
   297 
       
   298     Usage: $gThisFile parameters [options]
       
   299 
       
   300     Parameters:
       
   301     -l  logfile (output)
       
   302 
       
   303     Options:
       
   304     -x  XML file (input)
       
   305     -e  EMail address for warnings (internet format)
       
   306     -h  Help
       
   307 
       
   308     If no XML file is specified, script will assume that all
       
   309     necessary variables are already in the environment (\%ENV).
       
   310     In any case, script will attempt to locate the relevant
       
   311     build-specific XML file in Perforce and will read any
       
   312     environment variables found in it.
       
   313     
       
   314     Strictly for test purposes, the calculated location of the
       
   315     secondary (product-specific) XML file may over-ridden by
       
   316     setting the Environment Variable $gXML2EnvVar
       
   317     
       
   318 USAGE_EOF
       
   319 
       
   320 	exit 1;
       
   321 }
       
   322