update tizen version to tizen20231130
[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 BEGIN {
24   unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
25 }
26
27 use LWP::UserAgent;
28 use URI;
29 use File::Path;
30 use File::Basename;
31
32 use strict;
33
34 sub hide_passwd {
35     my $url = shift;
36     $url =~ s|://[^@]*@|://|;
37     return $url
38 }
39
40 die "USAGE: $0 DIR URLS..." unless $#ARGV >= 1;
41
42 my $dir = shift @ARGV;
43
44 my $ua = LWP::UserAgent->new(
45   agent => "openSUSE build script",
46   timeout => 42,
47   ssl_opts => {
48   verify_hostname => 1
49 });
50
51 for my $url (@ARGV) {
52   my $original = $url;
53   if ($url =~ /^zypp:\/\/([^\/]*)\/?/) {
54     use Build::Zypp;
55     my $repo = Build::Zypp::parserepo($1);
56     die "can't parse $1\n" unless $repo;
57     die "missing url in repo ".$repo->{'name'}."\n" unless exists $repo->{'baseurl'};
58     my $u = $repo->{'baseurl'};
59     $u .= '/' unless $u =~ /\/$/;
60     $url =~ s/^zypp:\/\/[^\/]*\/*//;
61     $url = URI->new($u.$url);
62     if ($url->scheme eq 'dir') {
63       my $dest = "$dir/".basename($url->path);
64       unlink($dest);    # just in case
65       system('cp', $url->path, $dest) && die("cp $url->path $dest failed\n");
66       last;
67     }
68   } else {
69    my $found = 0;
70    if ( defined $ENV{BUILD_ROOT} && -e $ENV{BUILD_ROOT} . "/.repo.config" ) {
71        open FILE, "<", $ENV{BUILD_ROOT} . "/.repo.config" or die $!;
72        while (<FILE>) {
73            next if ($_ !~ /^http[s]?:\/\/([^\/]*)\/?/);
74            chomp($_);
75            my $hidden = URI->new($_);
76            my $ui = $hidden->userinfo;
77            $hidden->userinfo(undef);
78            if ( $url =~ m/^$hidden/ ) {
79                $url = URI->new($url);
80                $url->userinfo($ui);
81                $found = 1;
82                last;
83            }
84        }
85        close FILE;
86    }
87    if ($found == 0 ) {
88        $url = URI->new($url);
89    }
90   }
91   $ua->env_proxy;
92   my $dest = "$dir/".basename($url->path).".tmp";
93   unlink($dest);        # just in case
94   my $retry = 3;
95   while ($retry--) {
96     my $res = $ua->mirror($url, $dest);
97     last if $res->is_success;
98     # if it's a redirect we probably got a bad mirror and should just retry
99         die "reqesting " . hide_passwd($original) . " failed: ".$res->status_line."\n" unless $retry;
100         warn "retrying " . hide_passwd($original) . "\n";
101   }
102   rename($dest, "$dir/".basename($url->path));
103 }
104
105 # vim:sw=2