releasing/cbrtools/perl/ImportRel
changeset 602 3145852acc89
equal deleted inserted replaced
600:6d08f4a05d93 602:3145852acc89
       
     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 RelTransfer::Import;
       
    25 use CommandController;
       
    26 
       
    27 
       
    28 #
       
    29 # Globals.
       
    30 #
       
    31 
       
    32 my $verbose = 0;
       
    33 my $iniData;
       
    34 my $commandController;
       
    35 my $force = 0;
       
    36 my $ftpResume = 0;
       
    37 my %releases; # Data structure changed to a double hash (from a single hash).
       
    38               # Top level keys are (case lowered) component names with has reference values.
       
    39               # Second level hash keys are versions, with a dummy value.
       
    40               # This arrangement makes it possible for a single component to have more than one
       
    41               # version, and to easily delete versions that have already been imported.
       
    42 my $passphraseFile;
       
    43 my $noPassphraseRetry;
       
    44 my %goodImports;
       
    45 my %failedImports;
       
    46 my %alreadyImported;
       
    47 
       
    48 #
       
    49 # Main.
       
    50 #
       
    51 
       
    52 ProcessCommandLine();
       
    53 CheckAlreadyImported();
       
    54 ImportRelease() if (keys %releases); # It may be that all components have already been imported
       
    55 PrintReport();
       
    56 
       
    57 #
       
    58 # Subs.
       
    59 #
       
    60 
       
    61 sub ProcessCommandLine {
       
    62   Getopt::Long::Configure ("bundling");
       
    63   my $help;
       
    64   GetOptions("h" => \$help, "v+" => \$verbose, "f" => \$force, "r" => \$ftpResume, "p=s" => \$passphraseFile, "noPassphraseRetry" => \$noPassphraseRetry);
       
    65 
       
    66   if ($help) {
       
    67     Usage(0);
       
    68   }
       
    69 
       
    70   if (scalar(@ARGV) == 1) {
       
    71     if (-f $ARGV[0]) {
       
    72       open IN, $ARGV[0] or die "Error: Couldn't open $ARGV[0] for reading: $!\n";
       
    73       while (my $line = <IN>) {
       
    74 	chomp $line;
       
    75 	$line =~ s/^\s*$//;
       
    76 	$line =~ s/#.*//;
       
    77 	if ($line eq '') {
       
    78 	  next; #Nothing left
       
    79 	}
       
    80 	my ($comp, $ver) = split(" ", $line);
       
    81 	if (defined $comp and defined $ver) {
       
    82 	  $releases{lc($comp)}->{$ver} = 1;
       
    83 	}
       
    84 	else {
       
    85 	  print "Error: Invalid file format in $ARGV[0]\n";
       
    86 	  Usage(1);
       
    87 	}
       
    88       }
       
    89       close IN;
       
    90     }
       
    91     else {
       
    92       print "Error: $ARGV[0] is not a file\n";
       
    93       Usage(1);
       
    94     }
       
    95   }	
       
    96   elsif (scalar(@ARGV) == 2) {
       
    97     $releases{lc($ARGV[0])}->{$ARGV[1]} = 1;
       
    98   }
       
    99   else {
       
   100     print "Error: Invalid arguments\n";
       
   101     Usage(1);
       
   102   }
       
   103   $iniData = IniData->New(undef,1);
       
   104   $commandController = CommandController->New($iniData, 'ImportRel');
       
   105   #if ftp resume option is used override value in reltools.ini
       
   106   if ($ftpResume) {
       
   107     $iniData->FtpServerSupportsResume(1);
       
   108   }
       
   109 }
       
   110 
       
   111 sub Usage {
       
   112   my $exitCode = shift;
       
   113 
       
   114   Utils::PrintDeathMessage($exitCode, "\nUsage: importrel [options] (<component> <version>) | (<component_ver_list_file>)
       
   115 
       
   116 options:
       
   117 
       
   118 -h  help
       
   119 -f  force import overwriting if necessary
       
   120 -r  use FTP reconnect and resume transfer mode
       
   121 -p <file> file containing passphrase
       
   122 -v  verbose output (-vv very verbose)
       
   123 --noPassphraseRetry  Will cause ImportRel to terminate if an incorrect passphrase is specified\n");
       
   124 }
       
   125 
       
   126 sub CheckAlreadyImported {
       
   127 #remove releases from attempted imports if they already exist locally
       
   128   unless ($force) {
       
   129     foreach my $comp (sort keys %releases) {
       
   130       foreach my $ver (keys %{$releases{$comp}}) {
       
   131 	my $loc = $iniData->PathData->LocalArchivePathForExistingComponent($comp, $ver);
       
   132 	if ($loc && -d $loc) { 
       
   133 	  push (@{$alreadyImported{$comp}}, $ver);          
       
   134 	  print "$comp $ver already exists in local archive\n" if ($verbose);
       
   135 	  delete $releases{$comp}->{$ver};
       
   136 	  unless (scalar(keys %{$releases{$comp}}) > 0) {
       
   137 	    delete $releases{$comp};
       
   138 	  }
       
   139 	}	
       
   140       }
       
   141     }
       
   142   }
       
   143 }
       
   144 
       
   145 sub ReadPassphraseFile {
       
   146   return undef unless $passphraseFile;
       
   147   open(PP, $passphraseFile) or die "Couldn't open passphrase file \"$passphraseFile\" because $!\n";
       
   148   my $passphrase = join ("\n", <PP>);
       
   149   close PP;
       
   150   return $passphrase;
       
   151 }
       
   152 
       
   153 sub ImportRelease {
       
   154   my $importer = RelTransfer::Import->New(ini_data => $iniData,
       
   155 					  force => $force,
       
   156 					  verbose => $verbose,
       
   157             passphrase => ReadPassphraseFile);
       
   158   #do the import
       
   159   foreach my $comp (sort keys %releases) {
       
   160     my $imported;
       
   161     foreach my $ver (keys %{$releases{$comp}}) {
       
   162       eval {
       
   163         $imported = $importer->TransferRelease($comp, $ver, $noPassphraseRetry);
       
   164       };
       
   165       if ($@) {
       
   166         print $@;
       
   167         if ($@ =~ /cannot\s+connect/i) {
       
   168           print "\nConnection to remote site dropped aborting import\n";
       
   169           last;
       
   170         }
       
   171         my $error = $@;
       
   172         chomp $error;
       
   173         $error =~ s/^error: ("?$comp $ver"? )?//i;
       
   174         $failedImports{$comp}->{$ver} = $error;
       
   175       }
       
   176       else {
       
   177         if ($imported) {
       
   178           push (@{$goodImports{$comp}}, $ver);
       
   179         } else {
       
   180           push (@{$alreadyImported{$comp}}, $ver);
       
   181         }
       
   182       }
       
   183     }
       
   184   }
       
   185 }
       
   186 
       
   187 sub PrintReport { 
       
   188   print "\n=========IMPORT SUMMARY==========\n";
       
   189 
       
   190   my $tableData = [["Component", "Version", "status"]];
       
   191 
       
   192   foreach my $comp (sort keys %goodImports) {
       
   193     foreach my $ver (@{$goodImports{$comp}}) {
       
   194       push (@$tableData, [$comp, $ver, 'successfully imported']);
       
   195     }
       
   196   }
       
   197     
       
   198   foreach my $comp (sort keys %alreadyImported) {
       
   199     foreach my $ver (@{$alreadyImported{$comp}}) {
       
   200       push (@$tableData, [$comp, $ver, 'has already been imported']);
       
   201     }
       
   202   }
       
   203   
       
   204   if (scalar @{$tableData} > 1) {
       
   205     $iniData->TableFormatter->PrintTable($tableData, 1);
       
   206   }
       
   207   
       
   208   if (scalar (keys %alreadyImported) > 0) {
       
   209     print "\nYou can specify the -f option to force the re-import of component releases\n";
       
   210   }
       
   211   
       
   212   #handle failed imports
       
   213   if (keys %failedImports) {
       
   214     print "\n=========FAILED IMPORTS==========\n";
       
   215     my $failureTableData = [["Component", "Version", "Failure reason"]];
       
   216     foreach my $comp (sort keys %failedImports) {
       
   217       foreach my $ver (sort keys %{$failedImports{$comp}}) {
       
   218         push (@$failureTableData, [$comp, $ver, $failedImports{$comp}->{$ver}]);
       
   219       }
       
   220     }
       
   221     $iniData->TableFormatter->PrintTable($failureTableData, 1);
       
   222     print "\nError: Unable to import component release successfully.  Component release might be corrupted.\n";
       
   223   }
       
   224   else
       
   225   {
       
   226     if (keys %goodImports) {
       
   227       print "\nAll releases imported successfully\n";
       
   228     } else {
       
   229       print "\nNothing to do!\n";
       
   230     }
       
   231   }
       
   232 }
       
   233 
       
   234   
       
   235 __END__
       
   236 
       
   237 =head1 NAME
       
   238 
       
   239 ImportRel - Imports a component release from a remote site.
       
   240 
       
   241 =head1 SYNOPSIS
       
   242 
       
   243   importrel [options] (<component> <version>) | (<component_ver_list_file>)
       
   244 
       
   245 options:
       
   246 
       
   247   -h  help
       
   248   -f  force import overwriting if necessary
       
   249   -r  use FTP reconnect and resume transfer mode
       
   250   -p <file> file containing passphrase
       
   251   -v  verbose output (-vv very verbose)
       
   252   --noPassphraseRetry  Will cause ImportRel to terminate if an incorrect passphrase is specified
       
   253 
       
   254 =head1 DESCRIPTION
       
   255 
       
   256 Attempts to import the component release specified on the command line from the projects remote site to the local archive.
       
   257 
       
   258 The name of file containing a list of components and versions maybe passed as an argument to the tool instead of a single component to import multiple releases.
       
   259 
       
   260 Using the C<-f> option will force releases to be imported even if they already exist in the local archive.
       
   261 
       
   262 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.
       
   263 
       
   264 It is recommended NOT to use the C<-p> option as it may be a security risk. You will be prompted if a passphrase is needed.
       
   265 
       
   266 =head1 KNOWN BUGS
       
   267 
       
   268 None
       
   269 
       
   270 =head1 COPYRIGHT
       
   271 
       
   272  Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
   273  All rights reserved.
       
   274  This component and the accompanying materials are made available
       
   275  under the terms of the License "Eclipse Public License v1.0"
       
   276  which accompanies this distribution, and is available
       
   277  at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
   278  
       
   279  Initial Contributors:
       
   280  Nokia Corporation - initial contribution.
       
   281  
       
   282  Contributors:
       
   283  
       
   284  Description:
       
   285  
       
   286 
       
   287 =cut