]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoDownload.pm
Only have one CGI-Script and move most of the code from the
[deb/packages.git] / lib / Packages / DoDownload.pm
1 package Packages::DoDownload;
2
3 use strict;
4 use warnings;
5
6 use CGI ();
7 use DB_File;
8 use Benchmark;
9 use Exporter;
10
11 use Deb::Versions;
12 use Packages::HTML ();
13 use Packages::Search qw( :all );
14 use Packages::Config qw( $HOME $DBDIR @SUITES @ARCHIVES @SECTIONS @ARCHITECTURES );
15 use Packages::CGI;
16 use Packages::DB;
17
18 our @ISA = qw( Exporter );
19 our @EXPORT = qw( do_download );
20
21 # TODO: find a way to get the U.S. mirror list from a more authoritive
22 # location automatically. might not be overly smart to automatize it
23 # completely, since I hand pick sites that are up-to-date, fast, and
24 # have HTTP on a reasonably short URL
25 #   -- Joy
26
27 # hint:
28 # grep-dctrl -F Site,Alias -e '(udel|bigfoot|kernel|crosslink|internap|cerias|lcs.mit|progeny)' Mirrors.masterlist | timestamps/archive_mirror_check.py
29 our @north_american_sites = (
30         "ftp.us.debian.org/debian",
31         "http.us.debian.org/debian",
32         "ftp.debian.org/debian",
33 #       "ftp.ca.debian.org/debian",
34         "ftp.egr.msu.edu/debian",
35         "mirrors.kernel.org/debian",
36         "archive.progeny.com/debian",
37         "debian.crosslink.net/debian",
38         "ftp-mirror.internap.com/pub/debian",
39         "ftp.cerias.purdue.edu/pub/os/debian",
40         "ftp.lug.udel.edu/debian",
41         "debian.lcs.mit.edu/debian",
42         "debian.teleglobe.net",
43         "debian.rutgers.edu",
44         "debian.oregonstate.edu/debian",
45         );
46 our @european_sites = (
47         "ftp.de.debian.org/debian",
48         "ftp.at.debian.org/debian",
49         "ftp.bg.debian.org/debian",
50         "ftp.cz.debian.org/debian",
51         "ftp.dk.debian.org/debian",
52         "ftp.ee.debian.org/debian",
53         "ftp.fi.debian.org/debian",
54         "ftp.fr.debian.org/debian",
55         "ftp.hr.debian.org/debian",
56         "ftp.hu.debian.org/debian",
57         "ftp.ie.debian.org/debian",
58         "ftp.is.debian.org/debian",
59         "ftp.it.debian.org/debian",
60         "ftp.nl.debian.org/debian",
61         "ftp.no.debian.org/debian",
62         "ftp.pl.debian.org/debian",
63         "ftp.si.debian.org/debian",
64         "ftp.es.debian.org/debian",
65         "ftp.se.debian.org/debian",
66         "ftp.tr.debian.org/debian",
67         "ftp.uk.debian.org/debian",
68         );
69 our @south_american_sites = (
70         "ftp.br.debian.org/debian",
71         "ftp.cl.debian.org/debian",
72         );
73 our @australian_sites = (
74         "ftp.au.debian.org/debian",
75         "ftp.wa.au.debian.org/debian",
76         "ftp.nz.debian.org/debian",
77         );
78 our @asian_sites = (
79         "ftp.jp.debian.org/debian",
80 #       "ftp.kr.debian.org/debian",
81         "linux.csie.nctu.edu.tw/debian",
82         "debian.linux.org.tw/debian",
83         "linux.cdpa.nsysu.edu.tw/debian",
84         ); 
85
86 our @volatile_european_sites = (
87         "volatile.debian.net/debian-volatile",
88         "ftp2.de.debian.org/debian-volatile",
89         "ftp.sk.debian.org/debian-volatile",
90                                );
91 our @backports_european_sites = (
92         "www.backports.org/debian",
93         "debian.sil.at/backports.org/",
94         "backports.debian.or.at/backports.org",
95         "mirror.realroute.net/backports.org",
96         "backports.cisbg.com",
97         "backports.linuxdediziert.de/backports.org",
98         "debian.netcologne.de/debian-backports",
99         "ftp.de.debian.org/backports.org",
100         "mirror.buildd.net/backports.org",
101         "ftp.estpak.ee/backports.org",
102         "debian.acantho.net/backports.org",
103         "backports.essentkabel.com/backports.org",
104         "backports.sipo.nl",
105         "ftp.tuke.sk",
106                                );
107 our @backports_asian_sites = (
108         "backports.mithril-linux.org",
109                              );
110 our @backports_australian_sites = (
111         "mirror.linux.org.au/backports.org",
112                                   );
113 our @amd64_european_sites = (
114         "amd64.debian.net/debian",
115         "ftp.de.debian.org/debian-amd64/debian",
116         "bach.hpc2n.umu.se/debian-amd64/debian",
117         "bytekeeper.as28747.net/debian-amd64/debian",
118         "mirror.switch.ch/debian-amd64/debian",
119         "ftp.nl.debian.org/debian-amd64/debian",
120                             );
121 our @amd64_asian_sites = (
122         "hanzubon.jp/debian-amd64/debian",
123                          );
124 our @amd64_north_american_sites = (
125         "mirror.espri.arizona.edu/debian-amd64/debian",
126                                   );
127 our @kfreebsd_north_american_sites = (
128         "www.gtlib.gatech.edu/pub/gnuab/debian",
129                                      );
130 our @kfreebsd_european_sites = (
131         # master site, aka ftp.gnuab.org
132         "kfreebsd-gnu.debian.net/debian",
133         "ftp.easynet.be/ftp/gnuab/debian",
134         "ftp.de.debian.org/debian-kfreebsd",
135                                );
136 our @nonus_north_american_sites = (
137 #       "ftp.ca.debian.org/debian-non-US",
138         "debian.yorku.ca/debian/non-US",
139         "mirror.direct.ca/linux/debian-non-US",
140         );
141 our @nonus_european_sites = (
142         "non-us.debian.org/debian-non-US",
143         "ftp.de.debian.org/debian-non-US",
144         "ftp.at.debian.org/debian-non-US",
145         "ftp.bg.debian.org/debian-non-US",
146         "ftp.cz.debian.org/debian-non-US",
147         "ftp.fi.debian.org/debian-non-US",
148         "ftp.fr.debian.org/debian-non-US",
149         "ftp.hr.debian.org/debian-non-US",
150         "ftp.hu.debian.org/debian-non-US",
151         "ftp.ie.debian.org/debian-non-US",
152         "ftp.is.debian.org/debian-non-US",
153         "ftp.it.debian.org/debian-non-US",
154         "ftp.nl.debian.org/debian-non-US",
155         "ftp.no.debian.org/debian-non-US",
156         "ftp.pl.debian.org/debian/non-US",
157         "ftp.si.debian.org/debian-non-US",
158         "ftp.es.debian.org/debian-non-US",
159         "ftp.se.debian.org/debian-non-US",
160         "ftp.tr.debian.org/debian-non-US",
161         "ftp.uk.debian.org/debian/non-US",
162         );
163 our @nonus_australian_sites = (
164         "ftp.au.debian.org/debian-non-US",
165         "ftp.wa.au.debian.org/debian-non-US",
166         "ftp.nz.debian.org/debian-non-US",
167         );
168 our @nonus_asian_sites = (
169         "ftp.jp.debian.org/debian-non-US",
170 #       "ftp.kr.debian.org/debian-non-US",
171         "linux.csie.nctu.edu.tw/debian-non-US",
172         "debian.linux.org.tw/debian-non-US",
173         "linux.cdpa.nsysu.edu.tw/debian-non-US",
174         );
175 our @nonus_south_american_sites = (
176         "ftp.br.debian.org/debian-non-US",
177         "ftp.cl.debian.org/debian-non-US",
178         );
179
180 # list of architectures
181 our %arches = (
182         i386    => 'Intel x86',
183         m68k    => 'Motorola 680x0',
184         sparc   => 'SPARC',
185         alpha   => 'Alpha',
186         powerpc => 'PowerPC',
187         arm     => 'ARM',
188         hppa    => 'HP PA-RISC',
189         ia64    => 'Intel IA-64',
190         mips    => 'MIPS',
191         mipsel  => 'MIPS (DEC)',
192         s390    => 'IBM S/390',
193         "hurd-i386" => 'Hurd (i386)',
194         amd64   => 'AMD64',
195         "kfreebsd-i386" => 'GNU/kFreeBSD (i386)'
196 );
197
198 sub do_download {
199     my ($params, $opts, $html_header, $menu, $page_content) = @_;
200
201     if ($params->{errors}{package}) {
202         fatal_error( "package not valid or not specified" );
203     }
204     if ($params->{errors}{suite}) {
205         fatal_error( "suite not valid or not specified" );
206     }
207     if ($params->{errors}{arch}) {
208         fatal_error( "arch not valid or not specified" );
209     }
210     if (@{$opts->{suite}} > 1) {
211         fatal_error( "more than one suite specified for download (@{$opts->{suite}})" );
212     }
213     if (@{$opts->{arch}} > 1) {
214         fatal_error( "more than one architecture specified for download (@{$opts->{arch}})" );
215     }
216
217     $opts->{h_sections} = { map { $_ => 1 } @SECTIONS };
218     my $pkg = $opts->{package};
219     my $suite = $opts->{suite}[0];
220     my $arch = $opts->{arch}[0] ||'';
221
222     our (%packages_all);
223     my (@results);
224     my ($final_result, $file, $filen, $md5sum, @file_components, $archive) = ("")x5;
225
226     sub gettext { return $_[0]; };
227
228     my $st0 = new Benchmark;
229     unless (@Packages::CGI::fatal_errors) {
230         tie %packages_all, 'DB_File', "$DBDIR/packages_all_$suite.db",
231         O_RDONLY, 0666, $DB_BTREE
232             or die "couldn't tie DB $DBDIR/packages_all_$suite.db: $!";
233         
234         read_entry( \%packages, $pkg, \@results, $opts );
235
236         unless (@results) {
237             fatal_error( "No such package".
238                          "{insert link to search page with substring search}" );            
239         } else {
240             my $final_result = shift @results;
241             foreach (@results) {
242                 if (version_cmp( $_->[7], $final_result->[7] ) > 0) {
243                     $final_result = $_;
244                 }
245             }
246             
247             $archive = $final_result->[1];
248             my %data = split /\000/, $packages_all{"$pkg $arch $final_result->[7]"};
249             $file = $data{filename};
250             @file_components = split('/', $file);
251             $filen = pop(@file_components);
252             
253             $md5sum = $data{md5sum};
254         }
255     }
256
257     my $arch_string = $arch ne 'all' ? "on $arches{$arch} machines" : "";
258     
259     %$html_header = ( title => "Package Download Selection",
260                       lang => "en",
261                       print_title => 1 );
262
263     if ($file) {
264         $$page_content .= "<h2>Download Page for <kbd>$filen</kbd> $arch_string</h2>\n".
265             "<p>You can download the requested file from the <tt>";
266         $$page_content .= join( '/', @file_components).'/';
267         $$page_content .= "</tt> subdirectory at";
268         $$page_content .= $archive ne 'security' ? " any of these sites:" : ":";
269         $$page_content .= "</p>\n";
270         
271         if ($archive eq 'security') {
272             
273             $$page_content .= <<END;
274 <ul>
275     <li><a href="http://security.debian.org/debian-security/$file">security.debian.org/debian-security</a></li>
276     </ul>
277     
278     <p>Debian security updates are currently officially distributed only via
279     security.debian.org.</p>
280 END
281 ;
282         } elsif ($arch eq 'amd64') {
283
284             $$page_content .= print_links( "North America", $file, @amd64_north_american_sites );
285             $$page_content .= print_links( "Europe", $file, @amd64_european_sites );
286 #    $$page_content .= print_links( "Australia and New Zealand", $file,
287 #                @nonus_australian_sites );
288             $$page_content .= print_links( "Asia", $file, @amd64_asian_sites );
289 #    $$page_content .= print_links( "South America", $file, @nonus_south_american_sites );
290
291             $$page_content .= <<END;
292 <p>Note that AMD64 is not officialy included in the Debian archive
293     yet, but the AMD64 porter group keeps their archive in sync with
294     the official archive as close as possible. See the
295     <a href="http://www.debian.org/ports/amd64/">AMD64 ports page</a> for
296     current information.</p>
297 END
298 ;
299         } elsif ($arch eq 'kfreebsd-i386') {
300
301             $$page_content .= print_links( "North America", $file, @kfreebsd_north_american_sites );
302             $$page_content .= print_links( "Europe", $file, @kfreebsd_european_sites );
303 #    $$page_content .= print_links( "Australia and New Zealand", $file,
304 #                @nonus_australian_sites );
305 #    $$page_content .= print_links( "Asia", $file, @amd64_asian_sites );
306 #    $$page_content .= print_links( "South America", $file, @nonus_south_american_sites );
307         
308             $$page_content .= <<END;
309 <p>Note that GNU/kFreeBSD is not officialy included in the Debian archive
310     yet, but the GNU/kFreeBSD porter group keeps their archive in sync with
311     the official archive as close as possible. See the
312     <a href="http://www.debian.org/ports/kfreebsd-gnu/">GNU/kFreeBSD ports page</a> for
313     current information.</p>
314 END
315 ;
316         } elsif ($archive eq 'non-US') {
317
318             $$page_content .= print_links( "North America", $file, @nonus_north_american_sites );
319             $$page_content .= print_links( "Europe", $file, @nonus_european_sites );
320             $$page_content .= print_links( "Australia and New Zealand", $file,
321                          @nonus_australian_sites );
322             $$page_content .= print_links( "Asia", $file, @nonus_asian_sites );
323             $$page_content .= print_links( "South America", $file, @nonus_south_american_sites );
324             
325             $$page_content .= <<END;
326 <p>If none of the above sites are fast enough for you, please see our
327     <a href="http://www.debian.org/mirror/list-non-US">complete mirror list</a>.</p>
328 END
329 ;
330         } elsif ($archive eq 'backports') {
331         
332 #    $$page_content .= print_links( "North America", $file, @nonus_north_american_sites );
333             $$page_content .= '<div class="cardleft">';
334             $$page_content .= print_links( "Europe", $file, @backports_european_sites );
335             $$page_content .= '</div><div class="cardright">';
336             $$page_content .= print_links( "Australia and New Zealand", $file,
337                          @backports_australian_sites );
338             $$page_content .= print_links( "Asia", $file, @backports_asian_sites );
339 #    $$page_content .= print_links( "South America", $file, @nonus_south_american_sites );
340             $$page_content .= '</div>';
341         
342             $$page_content .= <<END;
343 <p style="clear:both">If none of the above sites are fast enough for you, please see our
344     <a href="http://www.backports.org/debian/README.mirrors.html">complete mirror list</a>.</p>
345 END
346 ;
347         } elsif ($archive eq 'volatile') {
348             
349 #    $$page_content .= print_links( "North America", $file, @nonus_north_american_sites );
350             $$page_content .=_links( "Europe", $file, @volatile_european_sites );
351 #    $$page_content .= print_links( "Australia and New Zealand", $file,
352 #                @nonus_australian_sites );
353 #    $$page_content .= print_links( "Asia", $file, @nonus_asian_sites );
354 #    $$page_content .= print_links( "South America", $file, @nonus_south_american_sites );
355
356             $$page_content .= <<END;
357 <p>If none of the above sites are fast enough for you, please see our
358     <a href="http://volatile.debian.net/mirrors.html">complete mirror list</a>.</p>
359 END
360 ;
361         } elsif ($archive eq 'us') {
362             
363             $$page_content .= '<div class="cardleft">';
364             $$page_content .= print_links( "North America", $file, @north_american_sites );
365             $$page_content .= '</div><div class="cardright">';
366             $$page_content .= print_links( "Europe", $file, @european_sites );
367             $$page_content .= '</div><div class="cardleft">';
368             $$page_content .= print_links( "Australia and New Zealand", $file, @australian_sites );
369             $$page_content .= '</div><div class="cardright">';
370             $$page_content .= print_links( "Asia", $file, @asian_sites );
371             $$page_content .= '</div><div class="cardleft">';
372             $$page_content .= print_links( "South America", $file, @south_american_sites );
373             $$page_content .= '</div>';
374             
375             $$page_content .= <<END;
376 <p style="clear:both">If none of the above sites are fast enough for you, please see our
377     <a href="http://www.debian.org/mirror/list">complete mirror list</a>.</p>
378 END
379 ;
380         }
381     
382         $$page_content .= <<END;
383 <p>Note that in some browsers you will need to tell your browser you want
384     the file saved to a file. For example, in Netscape or Mozilla, you should
385     hold the Shift key when you click on the URL.</p>
386 END
387 ;
388         $$page_content .= "<p>The MD5sum for <tt>$filen</tt> is <strong>$md5sum</strong></p>\n"
389             if $md5sum;
390     }
391 }
392
393 sub print_links {
394     my ( $title, $file, @servers ) = @_;
395
396     my $str = "<p><em>$title</em></p>";
397     $str .= "<ul>";
398     foreach (@servers) {
399         $str .= "<li><a href=\"http://$_/$file\">$_</a></li>\n";
400         # $str .= "<li><a href=\"ftp://$_/$file\">$_</a></li>\n";
401     }
402     $str .= "</ul>";
403
404     return $str;
405 }
406
407 1;