diff options
author | Norbert Preining <preining@logic.at> | 2009-12-16 07:06:01 +0000 |
---|---|---|
committer | Norbert Preining <preining@logic.at> | 2009-12-16 07:06:01 +0000 |
commit | 7a3ba3138290c49da244552d62e88a08b2d14e78 (patch) | |
tree | ed48216d1a7539d48f0eb44ca9f2bd08fbd0f51c /Master/tlpkg/TeXLive/TLDownload.pm | |
parent | 1501e3ba218f0698b94682df440b76ff94d64606 (diff) |
implement --persistent-downloads using LWP
git-svn-id: svn://tug.org/texlive/trunk@16424 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/tlpkg/TeXLive/TLDownload.pm')
-rw-r--r-- | Master/tlpkg/TeXLive/TLDownload.pm | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/Master/tlpkg/TeXLive/TLDownload.pm b/Master/tlpkg/TeXLive/TLDownload.pm new file mode 100644 index 00000000000..55ea323d0f3 --- /dev/null +++ b/Master/tlpkg/TeXLive/TLDownload.pm @@ -0,0 +1,131 @@ +# $Id$ +# TeXLive::TLDownload.pm - module for abstracting the download modes +# Copyright 2009 Norbert Preining +# +# This file is licensed under the GNU General Public License version 2 +# or any later version. +# + + +package TeXLive::TLDownload; + +use TeXLive::TLUtils; +use File::Temp qw/tempfile/; + +my $svnrev = '$Revision$'; +my $_modulerevision; +if ($svnrev =~ m/: ([0-9]+) /) { + $_modulerevision = $1; +} else { + $_modulerevision = "unknown"; +} +sub module_revision { + return $_modulerevision; +} + + +# since Net::HTTP and Net::FTP are shipped by the same packages +# we only test for Net::HTTP, if that fails, let us know ;-) +our $net_lib_avail = 0; +eval { require LWP; }; +if ($@) { + debug("LWP is not available, falling back to wget mode.\n"); + $net_lib_avail = 0; +} else { + require LWP::UserAgent; + require HTTP::Status; + $net_lib_avail = 1; + debug("LWP available!\n"); +} + + +sub new +{ + my $class = shift; + my $self = {}; + my $ua = LWP::UserAgent->new( + agent => "texlive/lwp", + keep_alive => 3, + env_proxy => 1, + ); + $self->{'ua'} = $ua; + bless $self, $class; + return $self; +} + + + +sub get_file +{ + my ($self, $url, $out, $size) = @_; + # + my $realout = $out; + my ($outfh, $outfn); + if ($out eq "|") { + ($outfh, $outfn) = tempfile(); + $realout = $outfn; + } + my $response = $self->{'ua'}->get($url, ':content_file' => $realout); + if ($response->is_success) { + if ($out ne "|") { + return 1; + } else { + # seek to beginning of file + seek $outfh, 0, 0; + return $outfh; + } + } else { + tlwarn("TLDownload::get_file: response error:\n"); + tlwarn(" " . $response->status_line . "\n"); + return; + } +} + + + +1; +__END__ + + +=head1 NAME + +C<TeXLive::TLDownload> -- TeX Live Download abstraction module + +=head1 SYNOPSIS + + use TeXLive::TLDownload; + + $TeXLive::TLDownload::net_lib_avail + my $dl = TeXLive::TLDownload->new(); + $dl->get_file($relpath, $output [, $expected_size ]); + +=head1 DESCRIPTION + +The C<TeXLive::TLDownload> is a simple wrapper around the LWP modules +that allows for persistent connections and different protocols. +At loading time it checks for the existence of the LWP module(s), +and sets C<$TeXLive::TLDownload::net_lib_avail> accordingly. + +=head2 Using proxies + +Please see C<LWP::UserAgent> for details, in a nut shell one can +specify proxies by setting C<I<protocol>_proxy> variable. + +=head1 SEE ALSO + +LWP + +=head1 AUTHORS AND COPYRIGHT + +This script and its documentation were written for the TeX Live +distribution (L<http://tug.org/texlive>) and both are licensed under the +GNU General Public License Version 2 or later. + +=cut + +### Local Variables: +### perl-indent-level: 2 +### tab-width: 2 +### indent-tabs-mode: nil +### End: +# vim:set tabstop=2 expandtab: # |