Merge branch 'master' into devel
[tools/build.git] / download
1 #!/usr/bin/perl -w
2
3 ################################################################
4 #
5 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 or 3 as
9 # published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program (see the file COPYING); if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 #
21 ################################################################
22
23 use Net::SSL ();
24 BEGIN {
25   $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0,
26   unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
27 }
28
29 use LWP::UserAgent;
30 use URI;
31 use File::Path;
32 use File::Basename;
33
34 use strict;
35
36 sub hide_passwd {
37     my $url = shift;
38     $url =~ s|://[^@]*@|://|;
39     return $url
40 }
41
42 die "USAGE: $0 DIR URLS..." unless $#ARGV >= 1;
43
44 my $dir = shift @ARGV;
45
46 my $ua = LWP::UserAgent->new(
47   agent => "openSUSE build script",
48   timeout => 42);
49
50 for my $url (@ARGV) {
51   my $original = $url;
52   if ($url =~ /^zypp:\/\/([^\/]*)\/?/) {
53     use Build::Zypp;
54     my $repo = Build::Zypp::parserepo($1);
55     die "can't parse $1\n" unless $repo;
56     die "missing url in repo ".$repo->{'name'}."\n" unless exists $repo->{'baseurl'};
57     my $u = $repo->{'baseurl'};
58     $u .= '/' unless $u =~ /\/$/;
59     $url =~ s/^zypp:\/\/[^\/]*\/*//;
60     $url = URI->new($u.$url);
61     if ($url->scheme eq 'dir') {
62       my $dest = "$dir/".basename($url->path);
63       unlink($dest);    # just in case
64       system('cp', $url->path, $dest) && die("cp $url->path $dest failed\n");
65       last;
66     }
67   } else {
68     my $found = 0;
69     if ( defined $ENV{BUILD_ROOT} && -e $ENV{BUILD_ROOT} . "/.repo.config" ) {
70         open FILE, "<", $ENV{BUILD_ROOT} . "/.repo.config" or die $!;
71         while (<FILE>) {
72             next if ($_ !~ /^http[s]?:\/\/([^\/]*)\/?/);
73             chomp($_);
74             my $hidden = URI->new($_);
75             my $ui = $hidden->userinfo;
76             $hidden->userinfo(undef);
77             if ( $url =~ m/^$hidden/ ) {
78                 $url = URI->new($url);
79                 $url->userinfo($ui);
80                 $found = 1;
81                 last;
82             }
83         }
84         close FILE;
85     }
86     if ($found == 0 ) {
87         $url = URI->new($url);
88     }
89   }
90   $ua->env_proxy  if $url->scheme ne 'https';
91   my $dest = "$dir/".basename($url->path);
92   unlink($dest);        # just in case
93   my $retry = 3;
94   while ($retry--) {
95     my $res = $ua->mirror($url, $dest);
96     last if $res->is_success;
97     # if it's a redirect we probably got a bad mirror and should just retry
98     die "reqesting " . hide_passwd($original) . " failed: ".$res->status_line."\n" unless $retry && $res->previous;
99     warn "retrying " . hide_passwd($original) . "\n";
100   }
101 }
102
103 # vim:sw=2