602
|
1 |
# Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
# All rights reserved.
|
|
3 |
# This component and the accompanying materials are made available
|
|
4 |
# under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
# which accompanies this distribution, and is available
|
|
6 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
#
|
|
8 |
# Initial Contributors:
|
|
9 |
# Nokia Corporation - initial contribution.
|
|
10 |
#
|
|
11 |
# Contributors:
|
|
12 |
#
|
|
13 |
# Description:
|
|
14 |
#
|
|
15 |
#
|
|
16 |
# Description:
|
|
17 |
# RelTransfer::Import.pm
|
|
18 |
#
|
|
19 |
|
|
20 |
package RelTransfer::Import;
|
|
21 |
|
|
22 |
use strict;
|
|
23 |
use Utils;
|
|
24 |
use File::Copy;
|
|
25 |
use File::Basename;
|
|
26 |
use File::Path;
|
|
27 |
use Cwd;
|
|
28 |
use RelTransfer;
|
|
29 |
use vars qw(@ISA);
|
|
30 |
@ISA=("RelTransfer");
|
|
31 |
|
|
32 |
#
|
|
33 |
# Constructor
|
|
34 |
#
|
|
35 |
|
|
36 |
sub Initialize {
|
|
37 |
my $self = shift;
|
|
38 |
|
|
39 |
$self->SUPER::Initialize();
|
|
40 |
|
|
41 |
#set the passphrase for decryption
|
|
42 |
$self->SetPgpPassPhrase();
|
|
43 |
}
|
|
44 |
|
|
45 |
#
|
|
46 |
# Public methods
|
|
47 |
#
|
|
48 |
|
|
49 |
sub TransferRelease {
|
|
50 |
my $self = shift;
|
|
51 |
my $comp = shift;
|
|
52 |
my $ver = shift;
|
|
53 |
my $noPassphraseRetry = shift;
|
|
54 |
|
|
55 |
print "\nImporting $comp $ver...\n" if ($self->{verbose});
|
|
56 |
|
|
57 |
#check to see if ok to import
|
|
58 |
if ($self->ReleaseExistsInLocalArchive($comp, $ver) and not $self->{force}) {
|
|
59 |
my $localReleaseDir = $self->PathData->LocalArchivePathForExistingComponent($comp, $ver);
|
|
60 |
my $reldatafile = File::Spec->catfile( $localReleaseDir,"reldata");
|
|
61 |
if (-f $reldatafile) {
|
|
62 |
print "$comp $ver already exists in local archive\n" if ($self->{verbose});
|
|
63 |
return 0;
|
|
64 |
}
|
|
65 |
print "$comp $ver is corrupted - attempting to remove and re-import.\n";
|
|
66 |
my $origDir = cwd();
|
|
67 |
|
|
68 |
chdir(dirname($localReleaseDir)); #If you try to rmtree a UNC path the cwd must also be a UNC path
|
|
69 |
rmtree ($localReleaseDir) or die "$localReleaseDir can't be deleted\n";
|
|
70 |
chdir($origDir);
|
|
71 |
}
|
|
72 |
unless ($self->ReleaseExistsOnRemoteSite($comp, $ver)) {
|
|
73 |
die "Error: $comp $ver does not exist on remote site\n";
|
|
74 |
}
|
|
75 |
|
|
76 |
my $excludeRelease = 0;
|
|
77 |
|
|
78 |
#Get remote release file, unzip, decrypt and move to local archive
|
|
79 |
# We pass around the remote archive path because with the project-based
|
|
80 |
# PathData scheme, the remote location might affect which local location
|
|
81 |
# to put it in.
|
|
82 |
my $remoteDir = $self->PathData->RemoteArchivePathForExistingComponent($comp, $ver, $self->{remoteSite});
|
|
83 |
eval {
|
|
84 |
$self->GetZippedReleaseFile($comp, $ver, $remoteDir);
|
|
85 |
select STDOUT;$|=1;
|
|
86 |
$self->UnzipReleaseFile($comp, $ver);
|
|
87 |
select STDOUT;$|=1;
|
|
88 |
$self->DecryptReleaseFiles($comp, $ver, $noPassphraseRetry);
|
|
89 |
|
|
90 |
# DEF104279 - If the users key can decrypt the exclude.txt then the user is not able to recieve this release.
|
|
91 |
opendir(DIR, Utils::TempDir()) or die "Error: cannot open Utils::TempDir()\n";
|
|
92 |
$excludeRelease = 1 if (grep /exclude.txt$/, readdir DIR);
|
|
93 |
closedir(DIR);
|
|
94 |
|
|
95 |
$self->MoveDecryptedFilesToArchive($comp, $ver, $remoteDir) if (!$excludeRelease);
|
|
96 |
};
|
|
97 |
if ($@) {
|
|
98 |
my $error = $@;
|
|
99 |
$self->CleanupTempDir();
|
|
100 |
my $localReleaseDir = $self->PathData->LocalArchivePathForImportingComponent($comp, $ver, $remoteDir);
|
|
101 |
if (-d $localReleaseDir) {
|
|
102 |
my $origDir = cwd();
|
|
103 |
|
|
104 |
chdir(dirname($localReleaseDir)); #If you try to rmtree a UNC path the cwd must also be a UNC path
|
|
105 |
rmtree ($localReleaseDir) or die "$localReleaseDir can't be deleted\n";
|
|
106 |
chdir($origDir);
|
|
107 |
}
|
|
108 |
die $error;
|
|
109 |
}
|
|
110 |
$self->CleanupTempDir();
|
|
111 |
|
|
112 |
print "$comp $ver successfully imported from remote site.\n" if ($self->{verbose} && !$excludeRelease);
|
|
113 |
return 1;
|
|
114 |
}
|
|
115 |
|
|
116 |
#
|
|
117 |
# Private methods
|
|
118 |
#
|
|
119 |
|
|
120 |
sub GetZippedReleaseFile {
|
|
121 |
my $self = shift;
|
|
122 |
my $comp = shift;
|
|
123 |
my $ver = shift;
|
|
124 |
my $remoteDir = shift;
|
|
125 |
|
|
126 |
my $localFile = Utils::TempDir()."/$comp$ver.zip";
|
|
127 |
my $remoteFile = "$remoteDir/$comp$ver.zip";
|
|
128 |
|
|
129 |
$self->{remoteSite}->GetFile($remoteFile, $localFile);
|
|
130 |
}
|
|
131 |
|
|
132 |
sub UnzipReleaseFile {
|
|
133 |
my $self = shift;
|
|
134 |
my $comp = shift;
|
|
135 |
my $ver = shift;
|
|
136 |
|
|
137 |
if ($self->{verbose}) {
|
|
138 |
print "Unzipping $comp$ver.zip ...\n";
|
|
139 |
}
|
|
140 |
my $tempDir = Utils::TempDir();
|
|
141 |
$tempDir =~ s/[\/\\]$//;
|
|
142 |
$tempDir .= "/";
|
|
143 |
my $zipName = File::Spec->catfile("$tempDir","$comp$ver.zip");
|
|
144 |
|
|
145 |
my $zip = Archive::Zip->new($zipName);
|
|
146 |
foreach my $member ($zip->members()) {
|
|
147 |
my $filename=$member->fileName();
|
|
148 |
eval {Utils::ExtractFile($tempDir, $filename,$member, 0, 1, $self->{verbose})}; # 0 is being passed in because we are not validating 1 = Overwrite.
|
|
149 |
die "$@\n" if ($@);
|
|
150 |
}
|
|
151 |
unlink $zipName;
|
|
152 |
}
|
|
153 |
|
|
154 |
sub DecryptReleaseFiles {
|
|
155 |
my $self = shift;
|
|
156 |
my $comp = shift;
|
|
157 |
my $ver = shift;
|
|
158 |
my $noPassphraseRetry = shift;
|
|
159 |
|
|
160 |
#build up list of pgp encrypted files in TEMP_DIR
|
|
161 |
my $tempDir = Utils::TempDir();
|
|
162 |
opendir(TEMPDIR, $tempDir);
|
|
163 |
my @encryptedFiles = grep {/\.pgp$/} readdir TEMPDIR;
|
|
164 |
closedir(TEMPDIR);
|
|
165 |
|
|
166 |
my $noFilesDecrypted = 1;
|
|
167 |
TRYAGAIN:
|
|
168 |
foreach my $encryptedFile (@encryptedFiles) {
|
|
169 |
my ($decryptedFile) = ($encryptedFile =~ /(.+).pgp$/);
|
|
170 |
|
|
171 |
#set the passphrase for decryption
|
|
172 |
$self->SetPgpPassPhrase();
|
|
173 |
# returns if it's already set
|
|
174 |
|
|
175 |
if ($self->{verbose}) {
|
|
176 |
print "Decrypting $encryptedFile ... \n";
|
|
177 |
}
|
|
178 |
eval {
|
|
179 |
$self->{crypt}->Decrypt("$tempDir/$encryptedFile", "$tempDir/$decryptedFile", $self->{pgpPassPhrase});
|
|
180 |
};
|
|
181 |
if ($@) {
|
|
182 |
if ($@ =~ /BAD_PASSPHRASE/i) {
|
|
183 |
$@ =~ s/BAD_PASSPHRASE//;
|
|
184 |
print "Incorrect PGP passphrase\n";
|
|
185 |
|
|
186 |
if ($noPassphraseRetry) {
|
|
187 |
die "\n";
|
|
188 |
}
|
|
189 |
|
|
190 |
$self->{pgpPassPhrase} = undef;
|
|
191 |
redo TRYAGAIN;
|
|
192 |
}
|
|
193 |
elsif ($@ =~ /NO_SECKEY/i) {
|
|
194 |
# Do nothing - it's perfectly possible that we don't have access to certain release files, particularly
|
|
195 |
# since the addition of 'exclude' keyword to ExportData.
|
|
196 |
}
|
|
197 |
else {
|
|
198 |
die $@;
|
|
199 |
}
|
|
200 |
}
|
|
201 |
else {
|
|
202 |
$noFilesDecrypted = 0;
|
|
203 |
}
|
|
204 |
unlink "$tempDir/$encryptedFile";
|
|
205 |
}
|
|
206 |
|
|
207 |
if ($noFilesDecrypted) {
|
|
208 |
die "Error: Unable to decrypt any part of $comp $ver (see FAQ for more detail)\n";
|
|
209 |
}
|
|
210 |
}
|
|
211 |
|
|
212 |
sub MoveDecryptedFilesToArchive {
|
|
213 |
my $self = shift;
|
|
214 |
my $comp = shift;
|
|
215 |
my $ver = shift;
|
|
216 |
my $remotedir = shift;
|
|
217 |
|
|
218 |
if ($self->{verbose}) {
|
|
219 |
print "Moving release files to local archive ... \n";
|
|
220 |
}
|
|
221 |
my $tempDir = Utils::TempDir();
|
|
222 |
opendir(DIR, $tempDir) or die "Error: cannot open $tempDir\n";
|
|
223 |
my @releaseFiles = grep {$_ ne '.' and $_ ne '..'} readdir DIR;
|
|
224 |
closedir(DIR);
|
|
225 |
if (grep(lc($_) eq "reldata", @releaseFiles)) {
|
|
226 |
# Move the 'reldata' entry to the end
|
|
227 |
@releaseFiles = grep(lc($_) ne "reldata", @releaseFiles);
|
|
228 |
push @releaseFiles, "reldata";
|
|
229 |
}
|
|
230 |
unless (@releaseFiles) {
|
|
231 |
die; # If we've got this far, there should have been some files decyrpted.
|
|
232 |
}
|
|
233 |
|
|
234 |
#create release directory if doesnot exist
|
|
235 |
my $localReleaseDir = $self->PathData->LocalArchivePathForImportingComponent($comp, $ver, $remotedir);
|
|
236 |
unless (-e $localReleaseDir) {
|
|
237 |
Utils::MakeDir($localReleaseDir);
|
|
238 |
}
|
|
239 |
else {
|
|
240 |
#clean the local release directory if it already exists
|
|
241 |
opendir(DIR, $localReleaseDir) or die "Error: cannot open $localReleaseDir\n";
|
|
242 |
my @allFiles = grep {$_ ne '.' and $_ ne '..'} map {"$localReleaseDir/$_"} readdir DIR;
|
|
243 |
closedir(DIR);
|
|
244 |
unlink @allFiles;
|
|
245 |
}
|
|
246 |
foreach my $releaseFile (@releaseFiles) {
|
|
247 |
move("$tempDir/$releaseFile", "$localReleaseDir/$releaseFile") or die "Error: Unable to move $tempDir/$releaseFile to $localReleaseDir/$releaseFile: $!";
|
|
248 |
|
|
249 |
Utils::SetFileReadOnly("$localReleaseDir/$releaseFile");
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
sub SetPgpPassPhrase {
|
|
254 |
my $self = shift;
|
|
255 |
|
|
256 |
return if ($self->{pgpPassPhrase});
|
|
257 |
print "PGP passphrase: \n";
|
|
258 |
$self->{pgpPassPhrase} = Utils::QueryPassword();
|
|
259 |
}
|
|
260 |
|
|
261 |
1;
|
|
262 |
|
|
263 |
__END__
|
|
264 |
|
|
265 |
=head1 NAME
|
|
266 |
|
|
267 |
RelTransfer::Import.pm - Import releases from the remote site
|
|
268 |
|
|
269 |
=head1 SYNOPSIS
|
|
270 |
|
|
271 |
use RelTransfer::Import;
|
|
272 |
|
|
273 |
$importer = RelTransfer::Import->New(ini_data => $iniData,
|
|
274 |
force => 1,
|
|
275 |
verbose => 1);
|
|
276 |
|
|
277 |
$importer->TransferRelease('componentname', 'componentversion');
|
|
278 |
|
|
279 |
=head1 DESCRIPTION
|
|
280 |
|
|
281 |
Implements the abstract TransferRelease method from the C<RelTransfer> base class module which transfers a release from the remote site to the local archive.
|
|
282 |
|
|
283 |
=head1 INTERFACE
|
|
284 |
|
|
285 |
=head2 New
|
|
286 |
|
|
287 |
Passed an argument list in the form of hash key value pairs. The supported arguments are...
|
|
288 |
|
|
289 |
ini_data => $iniData_object
|
|
290 |
force => $force_integer
|
|
291 |
verbose => $verbosity_integer
|
|
292 |
|
|
293 |
Returns a reference to a C<RelTransfer::Import> object.
|
|
294 |
|
|
295 |
=head2 TransferRelease
|
|
296 |
|
|
297 |
Passed a component name and version number. Performs the following steps:
|
|
298 |
|
|
299 |
=over 4
|
|
300 |
|
|
301 |
=item *
|
|
302 |
|
|
303 |
Check to see if the release can or needs to be imported. If the release already exists on the local archive or does not exist on the remote site then do not attempt to import
|
|
304 |
|
|
305 |
=item *
|
|
306 |
|
|
307 |
Get the release zip from the remote site
|
|
308 |
|
|
309 |
=item *
|
|
310 |
|
|
311 |
Unzip the release zip
|
|
312 |
|
|
313 |
=item *
|
|
314 |
|
|
315 |
Decrypt the release files (ie the reldata, source and binary zips)
|
|
316 |
|
|
317 |
=item *
|
|
318 |
|
|
319 |
Move the release files to the local archive
|
|
320 |
|
|
321 |
=back
|
|
322 |
|
|
323 |
=head1 KNOWN BUGS
|
|
324 |
|
|
325 |
None
|
|
326 |
|
|
327 |
=head1 COPYRIGHT
|
|
328 |
|
|
329 |
Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
330 |
All rights reserved.
|
|
331 |
This component and the accompanying materials are made available
|
|
332 |
under the terms of the License "Eclipse Public License v1.0"
|
|
333 |
which accompanies this distribution, and is available
|
|
334 |
at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
335 |
|
|
336 |
Initial Contributors:
|
|
337 |
Nokia Corporation - initial contribution.
|
|
338 |
|
|
339 |
Contributors:
|
|
340 |
|
|
341 |
Description:
|
|
342 |
|
|
343 |
|
|
344 |
=cut
|