1 |
dpavlin |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
use strict; |
3 |
|
|
$|++; |
4 |
|
|
|
5 |
|
|
# Mirror CPAN latest archive. Based on article "Mirroring your own mini-CPAN" |
6 |
|
|
# by Randal L. Schwartz for Linux Magazine Column 42 (Nov 2002) available on |
7 |
|
|
# http://www.stonehenge.com/merlyn/LinuxMag/col42.html |
8 |
|
|
# http://www.stonehenge.com/merlyn/LinuxMag/col42.listing.txt |
9 |
|
|
|
10 |
|
|
# TODO: |
11 |
|
|
# - support for ZIP archives (and fix .tar.gz cludges) |
12 |
|
|
# - add version requirement for Archive::Tar (0.22 has a bug which |
13 |
|
|
# prevents it to extract some tars) |
14 |
|
|
|
15 |
|
|
### CONFIG |
16 |
|
|
|
17 |
|
|
#my $REMOTE = "http://ftp.linux.hr/CPAN/"; |
18 |
|
|
my $REMOTE = "http://www.cpan.org/"; |
19 |
|
|
# my $REMOTE = "http://fi.cpan.org/"; |
20 |
|
|
# my $REMOTE = "http://au.cpan.org/"; |
21 |
|
|
# my $REMOTE = "file://Users/merlyn/MIRROR/CPAN/"; |
22 |
|
|
|
23 |
|
|
## warning: unknown files below this dir are deleted! |
24 |
dpavlin |
1.2 |
#my $LOCAL = "/mirrors/cpan/CPAN/"; |
25 |
|
|
my $LOCAL = "/rest/cpan/CPAN/"; |
26 |
dpavlin |
1.1 |
|
27 |
|
|
my $TRACE = 0; |
28 |
|
|
|
29 |
|
|
### END CONFIG |
30 |
|
|
|
31 |
|
|
## core - |
32 |
|
|
use File::Path qw(mkpath); |
33 |
|
|
use File::Basename qw(dirname); |
34 |
|
|
use File::Spec::Functions qw(catfile devnull); |
35 |
|
|
use File::Find qw(find); |
36 |
|
|
|
37 |
|
|
## LWP - |
38 |
|
|
use URI (); |
39 |
|
|
use LWP::Simple qw(mirror RC_OK RC_NOT_MODIFIED); |
40 |
|
|
|
41 |
|
|
## Compress::Zlib - |
42 |
|
|
use Compress::Zlib qw(gzopen $gzerrno); |
43 |
|
|
|
44 |
|
|
## Archive::Tar - |
45 |
|
|
use Archive::Tar qw(); |
46 |
|
|
|
47 |
|
|
## first, get index files |
48 |
|
|
my_mirror($_) for qw( |
49 |
|
|
authors/01mailrc.txt.gz |
50 |
|
|
modules/02packages.details.txt.gz |
51 |
|
|
modules/03modlist.data.gz |
52 |
|
|
); |
53 |
|
|
|
54 |
|
|
## now walk the packages list |
55 |
|
|
my $details = catfile($LOCAL, qw(modules 02packages.details.txt.gz)); |
56 |
|
|
my $gz = gzopen($details, "rb") or die "Cannot open details: $gzerrno"; |
57 |
|
|
my $inheader = 1; |
58 |
|
|
while ($gz->gzreadline($_) > 0) { |
59 |
|
|
if ($inheader) { |
60 |
|
|
$inheader = 0 unless /\S/; |
61 |
|
|
next; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
my ($module, $version, $path) = split; |
65 |
|
|
next if $path =~ m{/perl-5}; # skip Perl distributions |
66 |
|
|
my_mirror("authors/id/$path", 1); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
## finally, clean the files we didn't stick there |
70 |
|
|
clean_unmirrored(); |
71 |
|
|
|
72 |
|
|
exit 0; |
73 |
|
|
|
74 |
|
|
BEGIN { |
75 |
|
|
## %mirrored tracks the already done, keyed by filename |
76 |
|
|
## 1 = local-checked, 2 = remote-mirrored |
77 |
|
|
my %mirrored; |
78 |
|
|
|
79 |
|
|
sub my_mirror { |
80 |
|
|
my $path = shift; # partial URL |
81 |
|
|
my $skip_if_present = shift; # true/false |
82 |
|
|
|
83 |
|
|
my $remote_uri = URI->new_abs($path, $REMOTE)->as_string; # full URL |
84 |
|
|
my $local_file = catfile($LOCAL, split "/", $path); # native absolute file |
85 |
|
|
my $checksum_might_be_up_to_date = 1; |
86 |
|
|
|
87 |
|
|
if ($skip_if_present and -f $local_file) { |
88 |
|
|
## upgrade to checked if not already |
89 |
|
|
$mirrored{$local_file} = 1 unless $mirrored{$local_file}; |
90 |
|
|
} elsif (($mirrored{$local_file} || 0) < 2) { |
91 |
|
|
## upgrade to full mirror |
92 |
|
|
$mirrored{$local_file} = 2; |
93 |
|
|
|
94 |
|
|
mkpath(dirname($local_file), $TRACE, 0711); |
95 |
|
|
print $path if $TRACE; |
96 |
|
|
my $status = mirror($remote_uri, $local_file); |
97 |
|
|
|
98 |
|
|
if ($status == RC_OK) { |
99 |
|
|
$checksum_might_be_up_to_date = 0; |
100 |
|
|
print " ... updated\n" if $TRACE; |
101 |
|
|
} elsif ($status != RC_NOT_MODIFIED) { |
102 |
|
|
warn "\n$remote_uri: $status\n"; |
103 |
|
|
return; |
104 |
|
|
} else { |
105 |
|
|
print " ... up to date\n" if $TRACE; |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
if ($path =~ m{^authors/id}) { # maybe fetch CHECKSUMS |
110 |
|
|
my $checksum_path = |
111 |
|
|
URI->new_abs("CHECKSUMS", $remote_uri)->rel($REMOTE); |
112 |
|
|
if ($path ne $checksum_path) { |
113 |
|
|
my_mirror($checksum_path, $checksum_might_be_up_to_date); |
114 |
|
|
} |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
sub clean_unmirrored { |
119 |
|
|
find sub { |
120 |
|
|
return if /\.readme$/; # don't erase readme files |
121 |
|
|
check_readme($File::Find::name); |
122 |
|
|
return unless -f and not $mirrored{$File::Find::name}; |
123 |
|
|
print "$File::Find::name ... removed\n" if $TRACE; |
124 |
|
|
unlink $_ or warn "Cannot remove $File::Find::name: $!"; |
125 |
|
|
my $path = $File::Find::name; |
126 |
dpavlin |
1.2 |
if ($path =~ s/(\.tar\.gz|\.tgz)/.readme/g && -f $path) { |
127 |
dpavlin |
1.1 |
# only if we erase archive also! |
128 |
|
|
unlink $path or warn "Cannot remove $path: $!"; |
129 |
|
|
} |
130 |
|
|
}, $LOCAL; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
sub check_readme { |
134 |
|
|
|
135 |
|
|
my $path = shift; |
136 |
|
|
# fixup some things |
137 |
|
|
my $readme_path = $path; |
138 |
dpavlin |
1.2 |
$readme_path =~ s/\.(tar\.gz|\.tgz)/.readme/g || return; # just .tar.gz is supported! |
139 |
dpavlin |
1.1 |
|
140 |
|
|
my $at = Archive::Tar->new($path) or die "Archive::Tar failed on $path\n"; |
141 |
|
|
|
142 |
|
|
if (! -f $readme_path) { |
143 |
|
|
# create readme file |
144 |
|
|
my @readmes = sort grep m{^[^/]+/README\z}, $at->list_files(); |
145 |
|
|
my $readme; |
146 |
|
|
|
147 |
|
|
if ($readme = shift @readmes) { |
148 |
|
|
open(R, "> $readme_path") || die "Cannot create $readme_path: $!"; |
149 |
|
|
print R $at->get_content($readme); |
150 |
|
|
close(R); |
151 |
|
|
|
152 |
|
|
print "$readme_path ... created\n" if $TRACE; |
153 |
|
|
|
154 |
|
|
} else { |
155 |
|
|
|
156 |
|
|
$readme_path =~ s/^.+\/(.+)$/$1/; |
157 |
|
|
print "can't find readme for $readme_path ...\n" if $TRACE; |
158 |
|
|
|
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
} |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
|
165 |
|
|
} |