deprecated/buildtools/buildsystemtools/removebuild.pl
changeset 655 3f65fd25dfd4
equal deleted inserted replaced
649:02d78c9f018e 655:3f65fd25dfd4
       
     1 #!/usr/bin/perl
       
     2 
       
     3 # Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description:
       
    16 # removebuild.pl - prepares a drive for a clean licensee build by removing
       
    17 # all buildable files from the drive.
       
    18 # 
       
    19 #
       
    20 
       
    21 my ($file) = readOpts(@ARGV);
       
    22 
       
    23 exit remove($file);
       
    24 
       
    25 sub readOpts(@)
       
    26 	{
       
    27 	my (@args) = @_;
       
    28 
       
    29 	my $path = undef;
       
    30 
       
    31 	foreach my $arg (@args)
       
    32 		{
       
    33 		if ($arg =~ /^-/)
       
    34 			{
       
    35 			if ((lc($arg) eq "--help")
       
    36 			  ||(lc($arg) eq "-h")
       
    37 			   )
       
    38 			   	{
       
    39 				showHelp();
       
    40 				exit 0;
       
    41 				}
       
    42 			else
       
    43 				{
       
    44 				print STDERR "Option '$arg' not recognised.\n\n";
       
    45 				print STDERR "Try 'removebuild --help' for help.\n";
       
    46 				exit 1;
       
    47 				}
       
    48 			}
       
    49 		else
       
    50 			{
       
    51 			if (defined($path))
       
    52 				{
       
    53 				print STDERR "Removebuild accepts only one argument.\n\n";
       
    54 				print STDERR "Try 'removebuild --help' for help.\n";
       
    55 				exit 1;
       
    56 				}
       
    57 			else
       
    58 				{
       
    59 				$path = $arg;
       
    60 				}
       
    61 			}
       
    62 		}
       
    63 	
       
    64 	if (!defined($path))
       
    65 		{
       
    66 		print STDERR "Removebuild must be given a list of files to remove.\n\n";
       
    67 		print STDERR "Try 'removebuild --help' for help.\n";
       
    68 		exit 1;
       
    69 		}
       
    70 	
       
    71 	return ($path);
       
    72 	}
       
    73 
       
    74 sub remove($)
       
    75 	{
       
    76 	my ($file) = @_;
       
    77 
       
    78 	open(FILE, $file);
       
    79 
       
    80 	my $dir = undef;
       
    81 	my $failed = 0;
       
    82 	my $worked = 0;
       
    83 	my $hasentries = 0;
       
    84 
       
    85 	foreach my $line (<FILE>)
       
    86 		{
       
    87 		chomp($line);
       
    88 		$hasentries = 1;
       
    89 
       
    90 		if ($line =~ /^\*/)
       
    91 			{
       
    92 			if ($line =~ /^\*DIR:/)
       
    93 				{
       
    94 				$dir = $line;
       
    95 				$dir =~ s/^\*DIR:\s*//;
       
    96 
       
    97 				$dir =~ s/[\/\\]*$//; # Remove trailing \/
       
    98 				}
       
    99 			else
       
   100 				{
       
   101 				close(FILE);
       
   102 				die "'$file' is not a valid input.\n('$line' not recognised)\n";
       
   103 				}
       
   104 			}
       
   105 		else
       
   106 			{
       
   107 			if (defined($dir))
       
   108 				{
       
   109 				$line =~ s/^[\/\\]*//; # Remove preceding \/
       
   110 
       
   111 				# Attempt to delete '$dir\$line'
       
   112 
       
   113 				$line = $dir."\\".$line;
       
   114 
       
   115 				if (-e $line)
       
   116 					{
       
   117 					if (-d $line)
       
   118 						{
       
   119 						$failed = 1;
       
   120 						print STDERR "ERROR: Could not remove file '$line' because $line is a directory\n";
       
   121 						}
       
   122 					else
       
   123 						{
       
   124 						if (!unlink($line))
       
   125 							{
       
   126 							$failed = 1;
       
   127 							print STDERR "ERROR: Could not remove file '$line'. Make sure it is not write protected.\n";
       
   128 							}
       
   129 						else
       
   130 							{
       
   131 							$worked = 1;
       
   132 							
       
   133 							# Remove parent dirs if now empty
       
   134 							my $empty = 1;
       
   135 							while (($line =~ /[\/\\]/) && $empty)
       
   136 								{
       
   137 								$line =~ s/[\/\\][^\/\\]*$//; # Go to parent dir
       
   138 								if (!rmdir($line))
       
   139 									{
       
   140 									# If it fails, the dir can't be empty
       
   141 									$empty = 0;
       
   142 									}
       
   143 								}
       
   144 							}
       
   145 						}
       
   146 					}
       
   147 				}
       
   148 			else
       
   149 				{
       
   150 				close(FILE);
       
   151 				die "'$file' is not a valid input.\n(DIR must be set before '$line')\n";
       
   152 				}
       
   153 			}
       
   154 		}
       
   155 	
       
   156 	close(FILE);
       
   157 
       
   158 	if ($hasentries && (!$worked))
       
   159 		{
       
   160 		print STDERR "WARNING: No files listed in '$file' were found. Is the current directory correct?\n";
       
   161 		}
       
   162 
       
   163 	return $failed;
       
   164 	}
       
   165 
       
   166 sub showHelp()
       
   167 	{
       
   168 	print "removebuild [options] Filename\n";
       
   169 	print " - prepares a drive for a 'build from clean' by removing\n";
       
   170 	print "   all buildable files.\n\n";
       
   171 	print "  Filename - The file listing the buildable files to be removed\n\n";
       
   172 	print "Options:\n";
       
   173 	print "  --help or -h - Display this message\n\n";
       
   174 	}
       
   175