summaryrefslogtreecommitdiff
path: root/Master
diff options
context:
space:
mode:
authorNorbert Preining <preining@logic.at>2009-12-16 07:31:07 +0000
committerNorbert Preining <preining@logic.at>2009-12-16 07:31:07 +0000
commit6d17760795e78ffc8a12f1408ba935b41a8a462f (patch)
tree66413e1eee57d5225fb369d47dd659841f3d23da /Master
parent7a3ba3138290c49da244552d62e88a08b2d14e78 (diff)
delete dev version, and keep a history of old TLDownload
git-svn-id: svn://tug.org/texlive/trunk@16425 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master')
-rw-r--r--Master/tlpkg/dev/TLDownload.pm.complicated_way (renamed from Master/tlpkg/dev/TLDownload.pm)156
1 files changed, 113 insertions, 43 deletions
diff --git a/Master/tlpkg/dev/TLDownload.pm b/Master/tlpkg/dev/TLDownload.pm.complicated_way
index 3bd5a24b61c..409f707af96 100644
--- a/Master/tlpkg/dev/TLDownload.pm
+++ b/Master/tlpkg/dev/TLDownload.pm.complicated_way
@@ -1,4 +1,4 @@
-# $Id$
+# $Id: TLDownload.pm -1 $
# TeXLive::TLDownload.pm - module for abstracting the download modes
# Copyright 2009 Norbert Preining
#
@@ -6,20 +6,27 @@
# or any later version.
#
# TODO:
-# - retry: currently we do not retry if one get did not succeed, but
-# return immediately 0. TLUtils::download_file will then try wget mode.
-# we should have retrials here, too
+# - retrials for opening the connections are now implemented (see
+# $RETRIES and $SLEEP_BETWEEN_RETRIES)
+# retrials for getting files are not implemented (and I am not sure
+# if it makes sense)
# - re-opening of a dropped connection, detection of dropped connection?
+# in ftp mode we check that we can send the pwd command before getting
+# a file. If this does not succeed we try to reopen the connection
+# In http mode I don't know.
# - timeout for ftp: default is AFAIR 120sec, that is probably not enough
# for a normal user to select all options and go press/enter "i"nstall
# either extend timeout or care for reopening of closed connections
+# See above, for ftp that should be fixed by automatic reopening.
+#
package TeXLive::TLDownload;
use TeXLive::TLUtils;
+use File::Temp qw/tempfile/;
-my $svnrev = '$Revision$';
+my $svnrev = '$Revision: -1 $';
my $_modulerevision;
if ($svnrev =~ m/: ([0-9]+) /) {
$_modulerevision = $1;
@@ -30,9 +37,16 @@ sub module_revision {
return $_modulerevision;
}
+
+my $CONNECT_RETRIES = 5;
+my $SLEEP_BETWEEN_CONNECT_RETRIES = 2; # (in seconds)
+my $GET_RETRIES = 5;
+my $SLEEP_BETWEEN_GET_RETRIES = 10; # (in seconds)
+
+
# since Net::HTTP and Net::FTP are shipped by the same packages
# we only test for Net::HTTP, if that fails, let us know ;-)
-my $net_lib_avail = 0;
+our $net_lib_avail = 0;
eval { require Net::HTTP; };
if ($@) {
debug("Net::HTTP is not available, falling back to wget mode.\n");
@@ -79,30 +93,37 @@ sub start_connection
if (!defined($self->server)) {
die "TLDownload::start_connection: No server defined, cannot start connection.";
}
- if ($self->mode eq "http") {
- my $handle = Net::HTTP->new(Host => $self->server, KeepAlive => 1);
- if (!$handle) {
- tlwarn("TLDownload::start_connection: Could not connect to server " . $self->server . "\n");
- return 0;
+ if (($self->mode eq "http") || ($self->mode eq "ftp")) {
+ my $handle;
+ for my $tries (1..$CONNECT_RETRIES) {
+ if ($self->mode eq "http") {
+ $handle = Net::HTTP->new(Host => $self->server, KeepAlive => 1);
+ } else {
+ $handle = Net::FTP->new($self->server, Debug => 0);
+ }
+ if ($handle) {
+ debug("TLDownload::start_connection: succeeded.\n");
+ last; # of $tries
+ }
+ sleep($SLEEP_BETWEEN_CONNECT_RETRIES);
}
- # now we got a handle, save it simply into $self for later reuse
- $self->{'handle'} = $handle;
- return 1;
- }
- if ($self->mode eq "ftp") {
- my $handle = Net::FTP->new($self->server, Debug => 0);
+ # if still not have a handle give up
if (!$handle) {
- tlwarn("TLDownload::start_connection: Could not connect to server " . $self->server . "\n");
- return 0;
- }
- if (!$handle->login("anonymous",'-texlive@')) {
- tlwarn("TLDownload::start_connection: Cannot login to server: " . $handle->message . "\n");
+ tlwarn("TLDownload::start_connection: Could not connect to server " . $self->server . " ($CONNECT_RETRIES trials), giving up.\n");
return 0;
}
- if (!$handle->binary) {
- tlwarn("TLDownload::start_connection: Cannot set transfer mode to binary: " . $handle->message . "\n");
- return 0;
+ if ($self->mode eq "ftp") {
+ # special ftp stuff
+ if (!$handle->login("anonymous",'-texlive@')) {
+ tlwarn("TLDownload::start_connection: Cannot login to server: " . $handle->message . "\n");
+ return 0;
+ }
+ if (!$handle->binary) {
+ tlwarn("TLDownload::start_connection: Cannot set transfer mode to binary: " . $handle->message . "\n");
+ return 0;
+ }
}
+ # again for both http and ftp
# now we got a handle, save it simply into $self for later reuse
$self->{'handle'} = $handle;
return 1;
@@ -162,20 +183,48 @@ sub get_file
return 0;
}
#
+ # for both http and ftp we want to support the destination "|" which
+ # should return a file handle.
+ my $realout = $out;
+ if ($out eq "|") {
+ my $outfh = tempfile();
+ $realout = $outfh;
+ }
+ #
# ftp mode
#
if ($self->mode eq "ftp") {
+ # test if handle is alive:
+ if (defined($self->{'handle'})) {
+ if (!$handle->pwd) {
+ # connection droped, time-out or similar ...
+ # delete the handle so that the following code restarts the
+ # connection
+ delete($self->{'handle'});
+ }
+ }
if (!defined($self->{'handle'})) {
if (!$self->start_connection) {
- # start_connection already warns ...
- return 0;
+ # warnings are already issued by start_connection
+ # which also does retrials
+ return;
}
}
# now we should have a $handle
my $handle = $self->{'handle'};
- if (!$handle->get($path, $out)) {
- tlwarn("TLDownload::get_file: Cannot get $path: " . $handle->message . "\n");
- return 0;
+ my $got_it = 0;
+ for my $tries (1..$GET_RETRIES) {
+ if ($handle->get($path, $realout)) {
+ $got_it = 1;
+ last;
+ }
+ sleep($SLEEP_BETWEEN_GET_RETRIES);
+ }
+ if (!$got_it) {
+ tlwarn("TLDownload::get_file: Could not get $path:\n");
+ tlwarn($handle->message . "\n");
+ tlwarn("($GET_RETRIES trials), giving up.\n");
+ return;
}
return 1;
}
@@ -191,14 +240,23 @@ sub get_file
}
# now we should have a $handle
my $handle = $self->{'handle'};
- if (! $handle->write_request(GET => $path, 'User-Agent' => 'texlive (net::http)')) {
- tlwarn("TLDownload::get_file: Cannot send GET request.\n");
- return 0;
+ my $got_it = 0;
+ for my $tries (1..$GET_RETRIES) {
+ if ($handle->write_request(GET => $path, 'User-Agent' => 'texlive (net::http)')) {
+ $got_it = 1;
+ last;
+ }
+ sleep($SLEEP_BETWEEN_GET_RETRIES);
+ }
+ if (!$got_it) {
+ tlwarn("TLDownload::get_file: Could not send GET request.\n");
+ tlwarn("($GET_RETRIES trials), giving up.\n");
+ return;
}
my ($code, $msg, %h) = $handle->read_response_headers;
if (HTTP::Status::is_error($code)) {
- tlwarn("Problem downloading from server: $msg\n");
- return 0;
+ tlwarn("Problem downloading from server ($code): $msg\n");
+ return;
}
my $buffer = "";
my $bytes_read = 0;
@@ -217,16 +275,28 @@ sub get_file
if (defined($size) && ($bytes_read != $size)) {
tlwarn("TLDownload::get_file: didn't get $size bytes, expect breakage\n");
}
- if (!open (OUT, ">$out")) {
- tlwarn("TLDownload::get_file: cannot open $out for writing: $!\n");
- return 0;
+ my $outhandle;
+ if ($out ne "|") {
+ if (!open (OUT, ">$out")) {
+ tlwarn("TLDownload::get_file: cannot open $out for writing: $!\n");
+ return 0;
+ }
+ $outhandle = \*OUT;
+ } else {
+ $outhandle = $realout;
}
- binmode(OUT);
- print OUT $buffer;
- if (!close(OUT)) {
- tlwarn("TLDownload::get_file: strange, cannot close file handle ...\n");
+ binmode($outhandle);
+ print $outhandle $buffer;
+ if ($out ne "|") {
+ if (!close(OUT)) {
+ tlwarn("TLDownload::get_file: strange, cannot close file handle ...\n");
+ }
+ return 1;
+ } else {
+ # seek to beginning of file
+ seek $outhandle, 0, 0;
+ return $outhandle;
}
- return 1;
}
#
# we are still here, strange