releasing/cbrtools/perl/CleanEnv.pm
author jjkang
Wed, 30 Jun 2010 11:35:58 +0800
changeset 607 378360dbbdba
parent 602 3145852acc89
permissions -rw-r--r--
Merge missed changeset 11 (raptor v2.14 and helium 10.0)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
#!perl
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
# Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
# This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
# which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
# Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
# Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
package CleanEnv;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
use strict;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
# Globals.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
my $reallyClean = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
my $force = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
my $verbose = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
# Public.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
sub CleanEnv {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
  my $iniData = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
  $reallyClean = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
  $force = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
  $verbose = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
  my $envDb = EnvDb->Open($iniData, $verbose);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
  (my $overallStatus, undef, my $dirtyComps, my $unaccountedItems, my $duplicates) = $envDb->CheckEnv(1, $reallyClean, 1);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
  if ($overallStatus == EnvDb::STATUS_CLEAN) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
    print "Environment already clean\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
    return 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
    my $cleaned = 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
    if (scalar(@$unaccountedItems) > 0) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
      if (not $force or $verbose) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
        foreach my $line (@$unaccountedItems) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
          print "$line has unknown origin\n"; 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
        }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
      if (Query("\nDelete above file(s)? [y/n] ")) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
        foreach my $file (@$unaccountedItems) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
          if ($verbose) { print "Deleting $file...\n"; }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
          unlink $file or print "Warning: Couldn't delete $file: $!\n" if (-f $file);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
          RemoveDirIfEmpty($file) if (-d $file);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
          (my $dir) = Utils::SplitFileName($file);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
          RemoveDirIfEmpty($dir) if (-d $dir);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
        }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
      else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
        $cleaned = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
    if (scalar(@$dirtyComps) > 0) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
      print "\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
      if (not $force or $verbose) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
        foreach my $comp (@$dirtyComps) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
          print "$comp->{comp} $comp->{ver} is dirty\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
        }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
      if (Query("\nRe-install the above component(s)? [y/n] ")) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
        foreach my $comp (@$dirtyComps) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
          $envDb->RefreshComponent($comp->{comp});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
        }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
      else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
        $cleaned = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
    if (scalar(@$duplicates) > 0) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
       print "\nThe following components are claiming the ownership of the same file:\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
       # Compile a hash of conflicting components indexed by file
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
       my %duplicateFiles;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
       foreach my $dup (@$duplicates) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
         # Each list item contains the filename, plus only two conflicting components.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
         my $file = shift @$dup;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
         $duplicateFiles{$file} = [] if !exists $duplicateFiles{$file};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
         foreach my $comp (@$dup) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
           my $found = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
           foreach my $existingComp (@{$duplicateFiles{$file}}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
             if ($existingComp eq $comp) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
               $found = 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
               last;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
             }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
           }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
           push @{$duplicateFiles{$file}}, $comp if !$found;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
         }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
       }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
       foreach my $file (keys(%duplicateFiles)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
         print join(", ", sort(@{$duplicateFiles{$file}})).": $file\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
       }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
       print "\nCleanEnv cannot resolve these duplicates.  To fix this, please remove one or\nmore of the conflicting components\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
    return $cleaned;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
# Private.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
sub RemoveDirIfEmpty {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
  my $dir = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
  if (DirEmpty($dir)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
    rmdir $dir or print "Warning: Couldn't delete \"$dir\": $!\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
    $dir =~ s/\\$//; # Remove trailing backslash.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
    (my $parentDir) = Utils::SplitFileName($dir);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
    RemoveDirIfEmpty($parentDir);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
sub DirEmpty {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   131
  my $dir = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   132
  return (scalar @{Utils::ReadDir($dir)} == 0);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   133
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   134
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   135
sub Query {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   136
  my $question = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   137
  return 1 if $force;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   138
  print $question;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   139
  my $response = lc <STDIN>;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   140
  chomp $response;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   141
  return ($response eq 'y')?1:0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   142
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   143
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   144
1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   145
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   146
__END__
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   147
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   148
=head1 NAME
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   149
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   150
CleanEnv.pm - Provides an interface for cleaning environments.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   151
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   152
=head1 INTERFACE
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   153
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   154
=head2 CleanEnv
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   155
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   156
Expects to be passed an C<IniData> reference, a flag indicating if a 'really clean' should be done, a flag indiacting of no user interaction (force) should be done, and a verbosity level. Cleans the environment accordingly. Returns true if the environment was cleaned (i.e. the user replied 'y' to all questions), false otherwise.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   157
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   158
=head1 KNOWN BUGS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   159
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   160
None.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   161
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   162
=head1 COPYRIGHT
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   163
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   164
 Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   165
 All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   166
 This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   167
 under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   168
 which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   169
 at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   170
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   171
 Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   172
 Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   173
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   174
 Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   175
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   176
 Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   177
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   178
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   179
=cut