download.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) 2006 OpenWrt.org
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. # See /LICENSE for more information.
  7. #
  8. use strict;
  9. use warnings;
  10. my $target = shift @ARGV;
  11. my $filename = shift @ARGV;
  12. my $md5sum = shift @ARGV;
  13. my @mirrors;
  14. my $ok;
  15. @ARGV > 0 or die "Syntax: $0 <target dir> <filename> <md5sum> <mirror> [<mirror> ...]\n";
  16. sub which($) {
  17. my $prog = shift;
  18. my $res = `which $prog`;
  19. $res or return undef;
  20. $res =~ /^no / and return undef;
  21. $res =~ /not found/ and return undef;
  22. return $res;
  23. }
  24. my $md5cmd = which("md5sum");
  25. $md5cmd or $md5cmd = which("md5");
  26. $md5cmd or die 'no md5 checksum program found, please install md5 or md5sum';
  27. chomp $md5cmd;
  28. sub download
  29. {
  30. my $mirror = shift;
  31. my $options = $ENV{WGET_OPTIONS};
  32. $options or $options = "";
  33. open WGET, "wget -t1 --timeout=20 $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
  34. open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
  35. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  36. my $buffer;
  37. while (read WGET, $buffer, 1048576) {
  38. print MD5SUM $buffer;
  39. print OUTPUT $buffer;
  40. }
  41. close MD5SUM;
  42. close WGET;
  43. close OUTPUT;
  44. if (($? >> 8) != 0 ) {
  45. print STDERR "Download failed.\n";
  46. cleanup();
  47. return;
  48. }
  49. my $sum = `cat "$target/$filename.md5sum"`;
  50. $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
  51. $sum = $1;
  52. if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
  53. print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
  54. cleanup();
  55. return;
  56. }
  57. unlink "$target/$filename";
  58. system("mv \"$target/$filename.dl\" \"$target/$filename\"");
  59. cleanup();
  60. }
  61. sub cleanup
  62. {
  63. unlink "$target/$filename.dl";
  64. unlink "$target/$filename.md5sum";
  65. }
  66. foreach my $mirror (@ARGV) {
  67. if ($mirror =~ /^\@SF\/(.+)$/) {
  68. my $sfpath = $1;
  69. open SF, "wget -t1 -q -O- 'http://prdownloads.sourceforge.net/$sfpath/$filename' |";
  70. while (<SF>) {
  71. /RADIO NAME=use_default VALUE=(\w+) OnClick="form\.submit\(\)">/ or
  72. /type="radio" name="use_default" value="(\w+)" onclick="form\.submit\(\)"\/>/ and do {
  73. push @mirrors, "http://$1.dl.sourceforge.net/sourceforge/$sfpath";
  74. };
  75. /<a href="\/.+\?use_mirror=(\w+)"><b>Download/ and do {
  76. push @mirrors, "http://$1.dl.sourceforge.net/sourceforge/$sfpath";
  77. };
  78. }
  79. push @mirrors, "http://dl.sourceforge.net/sourceforge/$sfpath";
  80. close SF;
  81. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  82. my $gnupath = $1;
  83. push @mirrors, "ftp://ftp.gnu.org/gnu/$gnupath";
  84. push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$gnupath";
  85. push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$gnupath";
  86. push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$gnupath";
  87. push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$gnupath";
  88. push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$gnupath";
  89. push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$gnupath";
  90. push @mirrors, "ftp://ftp.digex.net/pub/gnu/$gnupath";
  91. } else {
  92. push @mirrors, $mirror;
  93. }
  94. }
  95. #push @mirrors, 'http://mirror1.openwrt.org';
  96. push @mirrors, 'http://mirror2.openwrt.org/sources';
  97. push @mirrors, 'http://downloads.openwrt.org/sources';
  98. while (!$ok) {
  99. my $mirror = shift @mirrors;
  100. $mirror or die "No more mirrors to try - giving up.\n";
  101. download($mirror);
  102. -f "$target/$filename" and $ok = 1;
  103. }
  104. $SIG{INT} = \&cleanup;