bldsystemtools/commonbldutils/clean.pl
changeset 0 83f4b4db085c
child 2 99082257a271
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 use strict;
       
     2 
       
     3 use Getopt::Long;
       
     4 use File::Path;
       
     5 use File::Spec::Functions;
       
     6 
       
     7 my $gRealTimeBuildErrors = 0;
       
     8 sub RealTimeBuildError($)
       
     9 {
       
    10 	$gRealTimeBuildErrors++;
       
    11 	print STDERR "ERROR: RealTimeBuild: ", @_, "\n";
       
    12 	return "RealTimeBuild error";
       
    13 }
       
    14 
       
    15 # Process the commandline
       
    16 my ($iDataSource, $iSrc, $iMRPSrc, $platform, $iDummy, $iVerbose) = ProcessCommandLine();
       
    17 
       
    18 my ($iDirList)= ProcessList($iDataSource, $iSrc, $iMRPSrc, $platform, $iVerbose);
       
    19 my @delete;
       
    20 
       
    21 search_dir($iSrc, $iDirList, \@delete, $iVerbose);
       
    22 
       
    23 foreach my $leftover (keys %$iDirList)
       
    24 {
       
    25   print "REMARK: LEFTOVER: $leftover ($$iDirList{$leftover})\n";
       
    26 }
       
    27 
       
    28 if ($gRealTimeBuildErrors && !$iDummy)
       
    29 {
       
    30   print STDERR "\nWARNING: Files will NOT be deleted, because of earlier real time build errors\n\n";
       
    31   $iDummy = 1;
       
    32 }
       
    33 
       
    34 foreach my $delete (@delete)
       
    35 {
       
    36   if ($iDummy)
       
    37   {
       
    38     print "REMARK: $delete is not referenced by any MRP\n";
       
    39   } else {
       
    40     # Delete the files or directories
       
    41     # make sure it is not read only
       
    42     #Convert back to \ for dos command
       
    43     $delete =~ s#\/#\\#g;
       
    44     system("attrib -r /s /d \"$delete\"");
       
    45     my $deletenum = rmtree($delete);
       
    46     if ($deletenum == 0)
       
    47     {
       
    48       RealTimeBuildError("failed to deleted $delete");
       
    49     } elsif (-d "$delete") {
       
    50       RealTimeBuildError("failed to deleted directory $delete"); #Because it still exists
       
    51     } elsif ($deletenum > 1) {
       
    52       print "REMARK: deleted $deletenum files in directory $delete as they are not referenced by any MRP\n";
       
    53     } else {
       
    54       print "REMARK: deleted $delete as it is not referenced by any MRP\n";
       
    55     }
       
    56   }
       
    57 }
       
    58 
       
    59 sub search_dir
       
    60 { 
       
    61   my ($dir, $iDirList, $delete, $iVerbose) = @_; 
       
    62   my @flist;
       
    63   
       
    64   print "Processing $dir\n" if ($iVerbose);
       
    65   if (opendir(DIRH,"$dir"))
       
    66   {
       
    67     @flist=readdir(DIRH); 
       
    68     closedir DIRH; 
       
    69     ENTRY: foreach my $entry (@flist)
       
    70     { 
       
    71       # ignore . and .. : 
       
    72       next if ($entry eq "." || $entry eq "..");
       
    73       my $partial_match;
       
    74       # Check entry again $iDirList for matches
       
    75       foreach my $sourceline (keys %$iDirList)
       
    76       {
       
    77         if ($sourceline =~ m#^$dir/$entry$#i)
       
    78         {
       
    79           # Exact match delete entry in %$iDirList
       
    80           # Check to see if something has already partial matched
       
    81           print "REMARK: $dir/$entry is probably covered more than once\n" if ($partial_match);
       
    82           if ($iVerbose)
       
    83           {
       
    84             if (-d "$dir/$entry")
       
    85             {
       
    86               print "Keeping directory $dir/$entry ($$iDirList{$sourceline})\n";
       
    87             } else {
       
    88               print "Keeping file $dir/$entry ($$iDirList{$sourceline})\n";
       
    89             }
       
    90           }
       
    91           delete $$iDirList{$sourceline};
       
    92           # No more processing required
       
    93           next ENTRY;
       
    94         }
       
    95         # Check to see if there is reference to inside this directory
       
    96         if ($sourceline =~ m#^$dir/$entry/#i)
       
    97         {
       
    98           # something reference this as a directory need more processing
       
    99           $partial_match = 1 if (-d "$dir/$entry");
       
   100         }
       
   101       }
       
   102       if ($partial_match)
       
   103       {
       
   104         search_dir("$dir/$entry", $iDirList, $delete, $iVerbose) if (-d "$dir/$entry");
       
   105         next ENTRY;
       
   106       }
       
   107       # No match place on deletion list
       
   108       push @$delete, "$dir/$entry";
       
   109       print "Marking $dir/$entry for delete\n" if ($iVerbose);
       
   110     }
       
   111   }else{ 
       
   112     RealTimeBuildError("can not read directory $dir"); 
       
   113   } 
       
   114 }
       
   115 
       
   116 # ProcessList
       
   117 #
       
   118 # Inputs
       
   119 # $iDataSource - ref to array of files to process
       
   120 # $iSrc - real location of source files
       
   121 # $iMRPSrc - where the mrp thinks they are
       
   122 #
       
   123 # Outputs
       
   124 #
       
   125 # Description
       
   126 # This function processes mrp files
       
   127 sub ProcessList
       
   128 {
       
   129   my ($iDataSource, $iSrc, $iMRPSrc, $platform, $iVerbose) = @_;
       
   130   
       
   131   my %Sources;
       
   132   my @ComponentList;
       
   133   my %mrpHash;
       
   134   
       
   135   # Need the dir swap
       
   136   $iDataSource =~ s/^$iMRPSrc/$iSrc/;
       
   137   # Read the options.txt
       
   138   open OPTIONS, $iDataSource or die RealTimeBuildError("Cannot open $iDataSource $!");
       
   139   while(<OPTIONS>)
       
   140   {
       
   141     if (/^GT\+Techview baseline mrp location:\s*(\S+)\s*$/i)
       
   142     {
       
   143       $mrpHash{lc $1} = $1;
       
   144       next;
       
   145     }
       
   146     if (/^GT only baseline mrp location:\s*(\S+)\s*$/i)
       
   147     {
       
   148       $mrpHash{lc $1} = $1;
       
   149       next;
       
   150     }
       
   151     if (/^Strong crypto mrp location:\s*(\S+)\s*$/i)
       
   152     {
       
   153       $mrpHash{lc $1} = $1;
       
   154       next;
       
   155     }
       
   156     if (/^Techview component list:\s*(\S+)\s*$/i)
       
   157     {
       
   158       push @ComponentList, $1;
       
   159       next;
       
   160     }
       
   161     if (/^GT component list:\s*(\S+)\s*$/i)
       
   162     {
       
   163       push @ComponentList, $1;
       
   164       next;
       
   165     }
       
   166   }
       
   167   close OPTIONS;
       
   168   for (my $i = 0; $i < scalar(@ComponentList); $i++)
       
   169   {
       
   170     # Fix path
       
   171     $ComponentList[$i] =~ s#\\#\/#g;
       
   172     # Need the dir swap
       
   173     $ComponentList[$i] =~ s/^$iMRPSrc/$iSrc/;
       
   174     open IN, $ComponentList[$i] or die RealTimeBuildError("Cannot open ".$ComponentList[$i]." $!");
       
   175     while(<IN>)
       
   176     {
       
   177       my ($mrp) = /^\s*\S+\s+(\S+)\s*$/;
       
   178       $mrpHash{lc $mrp} = $mrp;
       
   179     }
       
   180     close IN;
       
   181   }
       
   182 
       
   183   my @mrpList = sort values %mrpHash;
       
   184   for (my $i = 0; $i < scalar(@mrpList); $i++)
       
   185   {
       
   186     # Fix path
       
   187     $mrpList[$i] =~ s#\\#\/#g;
       
   188     # Need the dir swap
       
   189     $mrpList[$i] =~ s/^$iMRPSrc/$iSrc/i;
       
   190     # Fix the CustKit / Devkit and techviewexamplesdk mrp locations
       
   191     $mrpList[$i] =~ s#^/product/CustKit#$iSrc/os/unref/orphan/cedprd/CustKit#i;
       
   192     $mrpList[$i] =~ s#^/product/DevKit#$iSrc/os/unref/orphan/cedprd/DevKit#i;
       
   193     $mrpList[$i] =~ s#^/product/techviewexamplesdk#$iSrc/os/unref/orphan/cedprd/techviewexamplesdk#i;
       
   194     $Sources{"$iSrc/os/unref/orphan/cedprd/SuppKit"} = "clean.pl";
       
   195     $Sources{"$iSrc/os/unref/orphan/cedprd/tools"} = "clean.pl";
       
   196     $Sources{"$iSrc/os/buildtools/toolsandutils/productionbldtools"} = "clean.pl";
       
   197     
       
   198     if (open MRP, $mrpList[$i])
       
   199     {
       
   200       my $mrpfile = $mrpList[$i];
       
   201       my $mrpfile_in_source = 0;
       
   202       
       
   203       while(<MRP>)
       
   204       {
       
   205         my $dir;
       
   206         if (/^\s*source\s+(\S.*\S)\s*$/i)		# must allow for spaces in names
       
   207         {
       
   208           my $origdir = $1;
       
   209           $dir = $origdir;
       
   210          
       
   211           #Find any relative paths and add them to the end of the mrp location to create a full path
       
   212           if (($dir =~ /\.\\/)||($dir =~ /\.\.\\/)||($dir !~ /\\/))
       
   213           {
       
   214 		  $dir =~ s#\.\.#\.#g;		# .. becomes .	
       
   215 		  $dir =~ s#^\.#\\\.#g;		# add an extra \ incase one is not present at start of path, canonpath wil cleanup multiple \
       
   216 	  
       
   217 		  $dir = "\\".$dir if ($dir !~ /\\/);	#add \ to start of path if source line only specifies a file
       
   218 		  
       
   219 		  # Fix paths to /
       
   220 		  $dir =~ s#\\#\/#g;
       
   221 		  
       
   222 		  $dir =~ s/$iMRPSrc/$iSrc/i if ($dir =~ /^$iMRPSrc/);
       
   223 		  
       
   224 		  $dir = $iSrc.$dir if ($dir !~ /^$iMRPSrc/);
       
   225 		  
       
   226 		  $dir = canonpath($dir);
       
   227           }
       
   228 	  
       
   229 	  # Fix paths to /
       
   230 	  $dir =~ s#\\#\/#g;
       
   231 	  
       
   232           # Remove any / from the end of the sourceline just in case the directory was ended in one
       
   233           $dir =~ s#\/$##;
       
   234           # Need the dir swap
       
   235           $dir =~ s/^$iMRPSrc/$iSrc/i;
       
   236           # Fix the CustKit / Devkit and techviewexamplesdk mrp locations
       
   237           $dir =~ s#^/product/CustKit#$iSrc/os/unref/orphan/cedprd/CustKit#i;
       
   238           $dir =~ s#^/product/DevKit#$iSrc/os/unref/orphan/cedprd/DevKit#i;
       
   239           $dir =~ s#^/product/techviewexamplesdk#$iSrc/os/unref/orphan/cedprd/techviewexamplesdk#i;
       
   240           
       
   241           if ($mrpfile =~ /^$dir$/i || $mrpfile =~ /^$dir\//i) {
       
   242             # mrpfile covered by source statements
       
   243             $mrpfile_in_source = 1;
       
   244           }
       
   245 
       
   246           # ignore location of release notes
       
   247           next if ($dir =~ /^\/component_defs/i);
       
   248           
       
   249           if (!-e $dir) {
       
   250             # CBR tools consider missing source as a fatal error
       
   251             RealTimeBuildError("$dir does not exist (listed as source in $mrpfile)");
       
   252           } elsif (!defined $Sources{$dir}) {
       
   253             $Sources{$dir} = $mrpfile;
       
   254           } else {
       
   255             print "REMARK: $origdir in $mrpfile is already defined in $Sources{$dir}\n";
       
   256           }
       
   257         }
       
   258       }
       
   259       close MRP;
       
   260       print "REMARK: $mrpList[$i] does not include itself as source\n" if (!$mrpfile_in_source);
       
   261     } else {
       
   262       RealTimeBuildError("Cannot open ".$mrpList[$i]." $!");
       
   263     }
       
   264   }
       
   265   return \%Sources;
       
   266 }
       
   267 
       
   268 # ProcessCommandLine
       
   269 #
       
   270 # Inputs
       
   271 #
       
   272 # Outputs
       
   273 # @iDataSource array of multiple (txt file(s) to process)
       
   274 # $iSrc - real location of files
       
   275 # $iMRPSrc - where the mrp thinks they are
       
   276 # @iDummy - do not delete anything
       
   277 #
       
   278 # Description
       
   279 # This function processes the commandline
       
   280 
       
   281 sub ProcessCommandLine {
       
   282   my ($iHelp, $iDataSource, $iSrc, $iMRPSrc, $platform, $iDummy, $iVerbose);
       
   283   GetOptions('h' => \$iHelp, 'o=s' =>\$iDataSource, 's=s' =>\$iSrc, 'm=s' =>\$iMRPSrc, 'p=s' =>\$platform,'n' => \$iDummy, 'v' => \$iVerbose);
       
   284 
       
   285   if (($iHelp) || (!defined $iSrc) || (!defined $iMRPSrc) || (!defined $platform))
       
   286   {
       
   287     Usage();
       
   288   }
       
   289 
       
   290   die RealTimeBuildError("Source directory $iSrc must be an absolute path with no drive letter") if ($iSrc !~ m#^[\\\/]#);
       
   291   die RealTimeBuildError("Source directory $iMRPSrc must be an absolute path with no drive letter") if ($iMRPSrc !~ m#^[\\\/]#);
       
   292   # Fix the paths
       
   293   $iSrc =~ s#\\#\/#g;
       
   294   $iMRPSrc =~ s#\\#\/#g;
       
   295   $iDataSource =~ s#\\#\/#g;
       
   296   if (! -d "$iSrc")
       
   297   {
       
   298     die RealTimeBuildError("$iSrc is not a directory") ;
       
   299   }
       
   300 
       
   301   # Need the dir swap
       
   302   $iDataSource =~ s/^$iMRPSrc/$iSrc/i;
       
   303   if (! -e "$iDataSource")
       
   304   {
       
   305     die RealTimeBuildError("Cannot open $iDataSource");
       
   306   }
       
   307 
       
   308   return($iDataSource, $iSrc, $iMRPSrc, $platform, $iDummy, $iVerbose);
       
   309 }
       
   310 
       
   311 # Usage
       
   312 #
       
   313 # Output Usage Information.
       
   314 #
       
   315 
       
   316 sub Usage {
       
   317   print <<USAGE_EOF;
       
   318 
       
   319   Usage: clean.pl [options]
       
   320 
       
   321   options:
       
   322 
       
   323   -h  help
       
   324   -o  options.txt to process
       
   325   -s  Source directory to process
       
   326   -m  MRP source directory
       
   327   -p  platform of product (beech or cedar)
       
   328   -n  Not do anything (dummy run)
       
   329   -v  Verbose
       
   330   
       
   331   Note:
       
   332   Due to CustKit using the clean-src directory (%clean-src%) for source
       
   333   The files need to be deleted from the %clean-src% directory
       
   334   This tool substitutes directory specified by -m with the directory
       
   335   specified by -s in all locations
       
   336   This means that options.txt and the component lists and mrp's must be
       
   337   referenced in the directory
       
   338   specified by -m
       
   339 
       
   340 USAGE_EOF
       
   341   exit 1;
       
   342 }