clean_package_dirs.pl
changeset 18 27ea4714a2aa
child 30 45b248d07f63
equal deleted inserted replaced
17:d75feffe620f 18:27ea4714a2aa
       
     1 # Copyright (c) 2009 Symbian Foundation Ltd
       
     2 # This component and the accompanying materials are made available
       
     3 # under the terms of the License "Eclipse Public License v1.0"
       
     4 # which accompanies this distribution, and is available
       
     5 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     6 #
       
     7 # Initial Contributors:
       
     8 # Symbian Foundation Ltd - initial contribution.
       
     9 #
       
    10 # Contributors:
       
    11 #
       
    12 # Description:
       
    13 # This is a helper script which cleans up some space on the machine by
       
    14 # removing old package-build-related directories 
       
    15 
       
    16 use strict;
       
    17 
       
    18 use Getopt::Long;
       
    19 use File::Path;
       
    20 
       
    21 my $sJOB_BASE_DIR = "D:\\fbf_job";
       
    22 my $nJOB_MAX_DIR_AGE_SECS = 259200; # 259200=3 days
       
    23 my $bJOB_SAVE_LAST_DIR = 1;
       
    24 
       
    25 my $sPROJECT_BASE_DIR = "D:\\fbf_project";
       
    26 my $nPROJECT_MAX_DIR_AGE_SECS = 259200; # 259200=3 days
       
    27 my $bPROJECT_SAVE_LAST_DIR = 1;
       
    28 
       
    29 my $nNow = time();
       
    30 
       
    31 print "#### Cleaning old job dirs. ####\n";
       
    32 opendir(DIR, "$sJOB_BASE_DIR");
       
    33 my @asDirs = readdir(DIR);
       
    34 close(DIR);
       
    35 
       
    36 for my $sDir ( @asDirs )
       
    37 {
       
    38 	next if ( $sDir eq "." or $sDir eq ".." );
       
    39 	next if ( ! -d "$sJOB_BASE_DIR\\$sDir" );
       
    40 	
       
    41 	#print "--- $sDir\n";
       
    42 	
       
    43 	if ( $sDir =~ /^([^.]+)\.(\d+)/ )
       
    44 	{
       
    45 		my $sBaseName = $1;
       
    46 		my $sBuildNumber = $2;
       
    47 		
       
    48 		my $nTs = (stat("$sJOB_BASE_DIR\\$sDir"))[9]; # modified time
       
    49 			
       
    50 		my $bLastDir = 0;
       
    51 		if ( $bJOB_SAVE_LAST_DIR )
       
    52 		{
       
    53 			my @asSimilarDirs = grep(/^$sBaseName(\.|$)/, @asDirs);
       
    54 			$bLastDir = 1;
       
    55 			for my $sSimilarDir ( @asSimilarDirs )
       
    56 			{
       
    57 				my $nSimDirTs = (stat("$sJOB_BASE_DIR\\$sSimilarDir"))[9];
       
    58 				$bLastDir = 0 if ( $nSimDirTs > $nTs );
       
    59 			}
       
    60 			$bLastDir = 1 if ( ! scalar @asSimilarDirs );
       
    61 		}
       
    62 		
       
    63 		if ( $bJOB_SAVE_LAST_DIR && $bLastDir )
       
    64 		{
       
    65 			print "Skipping $sDir as last dir in the series\n";
       
    66 		}
       
    67 		elsif ( $nNow - $nTs > $nJOB_MAX_DIR_AGE_SECS )
       
    68 		{
       
    69 			print "Removing $sDir...\n";
       
    70 			print "rmdir /S $sJOB_BASE_DIR\\$sDir\n";
       
    71 			system("rmdir /S /Q $sJOB_BASE_DIR\\$sDir");
       
    72 		}
       
    73 		else
       
    74 		{
       
    75 			print "Keeping $sDir\n";
       
    76 		}
       
    77 	}
       
    78 	else
       
    79 	{
       
    80 		print "$sDir doesn't match\n";
       
    81 	}
       
    82 }
       
    83 
       
    84 print "#### Cleaning old project dirs. ####\n";
       
    85 opendir(DIR, "$sPROJECT_BASE_DIR");
       
    86 @asDirs = readdir(DIR);
       
    87 close(DIR);
       
    88 
       
    89 for my $sDir ( @asDirs )
       
    90 {
       
    91 	next if ( $sDir eq "." or $sDir eq ".." );
       
    92 	next if ( ! -d "$sPROJECT_BASE_DIR\\$sDir" );
       
    93 	
       
    94 	if ( $sDir =~ /^([^.]+)\.(\d+)/ or $sDir =~ /^([^.]+)$/ )
       
    95 	{
       
    96 		my $sBaseName = "";
       
    97 		my $sBuildNumber = 0;
       
    98 		if ( $sDir =~ /^([^.]+)\.(\d+)/ )
       
    99 		{
       
   100 			$sBaseName = $1;
       
   101 			$sBuildNumber = $2;
       
   102 		}
       
   103 		elsif ( $sDir =~ /^([^.]+)$/ )
       
   104 		{
       
   105 			$sBaseName = $1;
       
   106 		}
       
   107 		
       
   108 		my $nTs = (stat("$sPROJECT_BASE_DIR\\$sDir"))[9]; # modified time
       
   109 		
       
   110 		my $bLastDir = 0;
       
   111 		if ( $bPROJECT_SAVE_LAST_DIR )
       
   112 		{
       
   113 			my @asSimilarDirs = grep(/^$sBaseName(\.|$)/, @asDirs);
       
   114 			$bLastDir = 1;
       
   115 			for my $sSimilarDir ( @asSimilarDirs )
       
   116 			{
       
   117 				my $nSimDirTs = (stat("$sPROJECT_BASE_DIR\\$sSimilarDir"))[9];
       
   118 				$bLastDir = 0 if ( $nSimDirTs > $nTs );
       
   119 			}
       
   120 			$bLastDir = 1 if ( ! scalar @asSimilarDirs );
       
   121 		}
       
   122 		
       
   123 		if ( $bPROJECT_SAVE_LAST_DIR && $bLastDir )
       
   124 		{
       
   125 			print "Skipping $sDir as last dir in the series\n";
       
   126 		}
       
   127 		elsif ( $nNow - $nTs > $nPROJECT_MAX_DIR_AGE_SECS )
       
   128 		{
       
   129 			print "Removing $sDir...\n";
       
   130 			print "rmdir /S $sPROJECT_BASE_DIR\\$sDir\n";
       
   131 			system("rmdir /S /Q $sPROJECT_BASE_DIR\\$sDir");
       
   132 		}
       
   133 		else
       
   134 		{
       
   135 			print "Keeping $sDir\n";
       
   136 		}
       
   137 	}
       
   138 	else
       
   139 	{
       
   140 		print "$sDir doesn't match\n";
       
   141 	}
       
   142 }