releasing/cbrtools/perl/ImportEnv
changeset 607 378360dbbdba
parent 602 3145852acc89
equal deleted inserted replaced
591:22486c9c7b15 607:378360dbbdba
       
     1 #!perl
       
     2 # Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 # 
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 # 
       
    12 # Contributors:
       
    13 # 
       
    14 # Description:
       
    15 # 
       
    16 #
       
    17 
       
    18 use strict;
       
    19 use FindBin;
       
    20 use lib "$FindBin::Bin";
       
    21 use Getopt::Long;
       
    22 use File::Path;
       
    23 use IniData;
       
    24 use RelData;
       
    25 use RelTransfer::Import;
       
    26 use CommandController;
       
    27 
       
    28 
       
    29 #
       
    30 # Globals.
       
    31 #
       
    32 
       
    33 my $verbose = 0;
       
    34 my $ftpResume = 0;
       
    35 my $iniData;
       
    36 my $commandController;
       
    37 my $envcomp;
       
    38 my $envver;
       
    39 my $passphraseFile;
       
    40 my $force;
       
    41 my $noPassphraseRetry;
       
    42 my %goodImports;
       
    43 my %failedImports;
       
    44 my %alreadyImported;
       
    45 
       
    46 #
       
    47 # Main.
       
    48 #
       
    49 
       
    50 ProcessCommandLine();
       
    51 ImportEnvironment();
       
    52 PrintReport();
       
    53 
       
    54 #
       
    55 # Subs.
       
    56 #
       
    57 
       
    58 sub ProcessCommandLine {
       
    59   Getopt::Long::Configure ("bundling");
       
    60   my $help;
       
    61   GetOptions("h" => \$help, "v+" => \$verbose, "r" => \$ftpResume, "p=s" => \$passphraseFile, "f" => \$force, "noPassphraseRetry" => \$noPassphraseRetry);
       
    62 
       
    63   if ($help) {
       
    64     Usage(0);
       
    65   }
       
    66 
       
    67   $envcomp = lc($ARGV[0]);
       
    68   $envver = $ARGV[1];
       
    69 
       
    70   unless (defined $envcomp and defined $envver and $#ARGV = -1) {
       
    71     print "Error: Invalid arguments\n";
       
    72     Usage(1);
       
    73   }
       
    74   $iniData = IniData->New(undef,1);
       
    75   $commandController = CommandController->New($iniData, 'ImportEnv');
       
    76   #if ftp resume option is used override value in reltools.ini
       
    77   if ($ftpResume) {
       
    78     $iniData->FtpServerSupportsResume(1);
       
    79   }
       
    80 }
       
    81 
       
    82 sub Usage {
       
    83   my $exitCode = shift;
       
    84 
       
    85   Utils::PrintDeathMessage($exitCode, "\nUsage: importenv [options] <component> <external_version>
       
    86 
       
    87 options:
       
    88 
       
    89 -h  help
       
    90 -r  use FTP reconnect and resume transfer mode
       
    91 -v  verbose output (-vv very verbose)
       
    92 -p <file>  file containing passphrase
       
    93 -f  force the re-import of component releases
       
    94 --noPassphraseRetry  Will cause ImportEnv to terminate if an incorrect passphrase is specified
       
    95 
       
    96 N.B. You will be prompted for your passphrase unless the -p option is specified.
       
    97 Use of the -p option is NOT recommended though, as storing your passphrase in a file is considered a security risk.\n");
       
    98 }
       
    99 
       
   100 sub ReadPassphraseFile {
       
   101   return undef unless $passphraseFile;
       
   102   open(PP, $passphraseFile) or die "Couldn't open passphrase file \"$passphraseFile\" because $!\n";
       
   103   my $passphrase = join ("\n", <PP>);
       
   104   close PP;
       
   105   return $passphrase;
       
   106 }
       
   107 
       
   108 sub ImportEnvironment {
       
   109   my $importer = RelTransfer::Import->New(ini_data => $iniData, force => $force,
       
   110 					  verbose => $verbose, passphrase => ReadPassphraseFile);
       
   111 
       
   112   #import the release to get the environment information
       
   113   eval {
       
   114     $importer->TransferRelease($envcomp, $envver, $noPassphraseRetry);
       
   115   };
       
   116   if ($@) {
       
   117     print $@;
       
   118     die "Aborting import of $envcomp $envver environment\n";
       
   119   }
       
   120 
       
   121   #read the environment information and transfer releases
       
   122   print "Reading $envcomp $envver environment...\n" if ($verbose);
       
   123   my $relData = RelData->Open($iniData, $envcomp, $envver, $verbose);
       
   124   my %env = %{$relData->Environment()};
       
   125 
       
   126   delete $env{$envcomp};
       
   127 
       
   128   #do the import checking for errors
       
   129 
       
   130   foreach my $comp (sort keys %env) {
       
   131     my $imported;
       
   132     my $ver = $env{$comp};
       
   133     eval {
       
   134       $imported = $importer->TransferRelease($comp, $ver, $noPassphraseRetry);
       
   135     };
       
   136     if ($@) {
       
   137       print $@;
       
   138       if ($@ =~ /cannot\s+connect/i) {
       
   139         print "\nAborting import of $envcomp $envver environment\n";
       
   140         last;
       
   141       }
       
   142       my $error = $@;
       
   143       chomp $error;
       
   144       $error =~ s/^error: ("?$comp $ver"? )?//i;
       
   145       $failedImports{$comp}->{$ver} = $error;
       
   146 
       
   147       print "Aborting import of $envcomp $envver environment\n";
       
   148       last;
       
   149     }
       
   150     else {
       
   151       if ($imported) {
       
   152         push (@{$goodImports{$comp}}, $ver);
       
   153       } else {
       
   154         push (@{$alreadyImported{$comp}}, $ver);
       
   155       }
       
   156     }
       
   157   }
       
   158 }
       
   159 
       
   160 sub PrintReport {
       
   161   print "\n=========IMPORT SUMMARY==========\n";
       
   162 
       
   163   my $tableData = [["Component", "Version", "status"]];
       
   164 
       
   165   foreach my $comp (sort keys %goodImports) {
       
   166     foreach my $ver (@{$goodImports{$comp}}) {
       
   167       push (@$tableData, [$comp, $ver, 'successfully imported']);
       
   168     }
       
   169   }
       
   170     
       
   171   foreach my $comp (sort keys %alreadyImported) {
       
   172     foreach my $ver (@{$alreadyImported{$comp}}) {
       
   173       push (@$tableData, [$comp, $ver, 'has already been imported']);
       
   174     }
       
   175   }
       
   176   
       
   177   $iniData->TableFormatter->PrintTable($tableData, 1);
       
   178 
       
   179   if (scalar (keys %alreadyImported) > 0) {
       
   180     print "\nYou can specify the -f option to force the re-import of component releases\n";
       
   181   }
       
   182  
       
   183   if (keys %failedImports) {
       
   184     print "\n=========FAILED IMPORTS==========\n";
       
   185     print "\nImport Failure Summary\n\n";
       
   186     my $failureTableData = [["Component", "Version", "Failure reason"]];
       
   187     foreach my $comp (sort keys %failedImports) {
       
   188       foreach my $ver (sort keys %{$failedImports{$comp}}) {
       
   189         push (@$failureTableData, [$comp, $ver, $failedImports{$comp}->{$ver}]);
       
   190       }
       
   191     }
       
   192     $iniData->TableFormatter->PrintTable($failureTableData, 1);
       
   193     print "\nError: Unable to import environment successfully. Environment might be corrupted.\n";
       
   194   }
       
   195   else
       
   196   {
       
   197     if (keys %goodImports) {
       
   198       print "\nEnvironment $envcomp $envver successfully imported\n";
       
   199     } else {
       
   200       print "\nNothing to do!\n";
       
   201     }
       
   202   }
       
   203 }
       
   204 
       
   205 __END__
       
   206 
       
   207 =head1 NAME
       
   208 
       
   209 ImportEnv - Imports the environment from which a component release was made.
       
   210 
       
   211 =head1 SYNOPSIS
       
   212 
       
   213   importenv [options] <component> <version>
       
   214 
       
   215 options:
       
   216 
       
   217   -h  help
       
   218   -r  use FTP reconnect and resume transfer mode
       
   219   -v  verbose output (-vv very verbose)
       
   220   -p <file>  file containing passphrase
       
   221   -f  force the re-import of component releases
       
   222   --noPassphraseRetry  Will cause ImportEnv to terminate if an incorrect passphrase is specified
       
   223 
       
   224 =head1 DESCRIPTION
       
   225 
       
   226 When a release is made, a description of the environment it was made from is stored with it.
       
   227 C<ImportEnv> takes a component name and version, reads the environment data for this
       
   228 component and imports the necessary releases from a remote site if they do not already
       
   229 exist on the local archive. If the environment data is not available from the local archive
       
   230 an attempt is made to import this component first.
       
   231 
       
   232 If the C<-r> option is used and the FTP connection is dropped during the download of a release, the tools will automatically reconnect to the FTP site and resume the download. This feature may not be supported by some FTP servers.
       
   233 
       
   234 It is recommended NOT to use the -p option; you will be prompted for your
       
   235 passphrase. Having a file containing your passphrase is be a security risk.
       
   236 
       
   237 =head1 KNOWN BUGS
       
   238 
       
   239 None
       
   240 
       
   241 =head1 COPYRIGHT
       
   242 
       
   243  Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
   244  All rights reserved.
       
   245  This component and the accompanying materials are made available
       
   246  under the terms of the License "Eclipse Public License v1.0"
       
   247  which accompanies this distribution, and is available
       
   248  at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
   249  
       
   250  Initial Contributors:
       
   251  Nokia Corporation - initial contribution.
       
   252  
       
   253  Contributors:
       
   254  
       
   255  Description:
       
   256  
       
   257 
       
   258 =cut