bldsystemtools/commonbldutils/BuildExpiry.pl
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 # Copyright (c) 2003-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 # Script to navigate through the builds and check for the expiry time
       
    15 # 
       
    16 #
       
    17 
       
    18 use strict;
       
    19 use FindBin;		# for FindBin::Bin
       
    20 use Getopt::Long;
       
    21 use File::Path;
       
    22 
       
    23 # For Date calculations
       
    24 use lib "$FindBin::Bin/../tools/build/lib"; # For running in source
       
    25 use Date::Manip;
       
    26 
       
    27 # Set TimeZone because Date:Manip needs it set and then tell it to IGNORE the TimeZone
       
    28 &Date_Init("TZ=GMT","ConvTZ=IGNORE");
       
    29 
       
    30 # Process the commandline
       
    31 my ( $iPath, $iPreview, $iLogPath ) = ProcessCommandLine();
       
    32 
       
    33 # Open the output logfile if needed
       
    34 if ( $iLogPath && !$iPreview )
       
    35 {
       
    36   # Redirect both STDOUT and STDERR to the logfile
       
    37   open (STDOUT, ">>$iLogPath") or die ("ERROR: Unable to open file \"$iLogPath\" for writing: $!");
       
    38   open (STDERR, ">&STDOUT") or die ("ERROR: Unable to redirect STDERR to STDOUT: $!");
       
    39   
       
    40   # Turn off buffering for each file handle to preserve ordering
       
    41   select((select(STDOUT), $|=1)[0]);
       
    42   select((select(STDERR), $|=1)[0]);
       
    43 }
       
    44 
       
    45 # Get today's date
       
    46 my $timestamp = &UnixDate("today","%Y-%m-%d");
       
    47 
       
    48 # Open the published path and get an array of build directories there
       
    49 opendir (DIRS, $iPath);
       
    50 my @dirs = readdir(DIRS);
       
    51 closedir(DIRS);
       
    52 chomp @dirs;
       
    53 
       
    54 my @build_dirs;
       
    55 foreach my $dir (@dirs)
       
    56 {
       
    57    if (( -d "$iPath\\$dir" ) && ( $dir =~ /^\d{5}/ ))
       
    58    {
       
    59       # We have found a build directory
       
    60       push @build_dirs, "$iPath\\$dir";
       
    61    }
       
    62 }
       
    63 
       
    64 # Go through each build directory looking for expiry files
       
    65 foreach my $dir (@build_dirs)
       
    66 {
       
    67   &ProcessDir($dir);
       
    68 }
       
    69 
       
    70 sub ProcessDir
       
    71 {
       
    72   my ($dir) = @_;
       
    73 
       
    74   opendir(DIR, $dir);
       
    75   my @expirys = readdir(DIR);
       
    76   closedir(DIR);
       
    77   chomp @expirys;
       
    78 
       
    79   foreach my $entry (@expirys)
       
    80   {
       
    81     next if (($entry eq ".") or ($entry eq ".."));
       
    82     &ProcessDir("$dir\\$entry") if (-d "$dir\\$entry");
       
    83     next if ((!-f "$dir\\$entry") or ($entry !~ /^(\d+)_(\d+)_(\d+).expiry$/i));
       
    84     
       
    85     # We have found an expiry file so extract the expiry date
       
    86     my ($year, $month, $day) = ($1, $2, $3);
       
    87 
       
    88     my $delta = &DateCalc("$year-$month-$day","today");
       
    89     if ( &Delta_Format($delta,'0',"%dt") > 0)
       
    90     {
       
    91       # This build directory has expired
       
    92       print "$timestamp: Removing directory \"$dir\" (Expired $year-$month-$day)\n";
       
    93       &RemoveDirectory($dir) if (!$iPreview);
       
    94       
       
    95       # Removal of this build directory is complete, so move onto the next one
       
    96       last;
       
    97     }
       
    98   }
       
    99 }
       
   100 
       
   101 sub RemoveDirectory()
       
   102 {
       
   103   my $dir = shift;
       
   104 
       
   105   my $temp = rmtree($dir);
       
   106   print "$temp file(s) removed.\n";
       
   107 
       
   108   return if ( !-d $dir );
       
   109 
       
   110   # Directory removal has failed
       
   111   warn "WARNING: Directory \"$dir\" could not be removed.\n";
       
   112 }
       
   113 
       
   114 
       
   115 # End of script
       
   116 
       
   117 sub ProcessCommandLine {
       
   118   my ($iHelp, $iPath, $iPreview,$iLogPath);
       
   119   GetOptions('h' => \$iHelp, 'p=s' => \$iPath, 'n' => \$iPreview,, 'l=s' => \$iLogPath);
       
   120 
       
   121   if ( ($iHelp) || (!defined $iPath) )
       
   122   {
       
   123     Usage();
       
   124   } 
       
   125   else 
       
   126   {
       
   127     return( $iPath, $iPreview, $iLogPath );
       
   128   };
       
   129 }
       
   130 
       
   131 # Usage
       
   132 #
       
   133 # Output Usage Information.
       
   134 #
       
   135 
       
   136 sub Usage {
       
   137   print <<USAGE_EOF;
       
   138 
       
   139   Usage: BuildExpiry.pl [options]
       
   140 
       
   141   options:
       
   142 
       
   143   -h  Help
       
   144   -p  %s Path to publish directory
       
   145   -n  Preview only
       
   146   -l  %s Path to log file
       
   147 
       
   148 USAGE_EOF
       
   149   exit 1;
       
   150 }
       
   151