/[perl]/mirror_cpan.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /mirror_cpan.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Sat Feb 1 00:40:22 2003 UTC (21 years, 2 months ago) by dpavlin
Branch: MAIN
File MIME type: text/plain
Mirror CPAN latest archive.

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     my $LOCAL = "/mirrors/cpan/CPAN/";
25    
26     my $TRACE = 0;
27    
28     ### END CONFIG
29    
30     ## core -
31     use File::Path qw(mkpath);
32     use File::Basename qw(dirname);
33     use File::Spec::Functions qw(catfile devnull);
34     use File::Find qw(find);
35    
36     ## LWP -
37     use URI ();
38     use LWP::Simple qw(mirror RC_OK RC_NOT_MODIFIED);
39    
40     ## Compress::Zlib -
41     use Compress::Zlib qw(gzopen $gzerrno);
42    
43     ## Archive::Tar -
44     use Archive::Tar qw();
45    
46     ## first, get index files
47     my_mirror($_) for qw(
48     authors/01mailrc.txt.gz
49     modules/02packages.details.txt.gz
50     modules/03modlist.data.gz
51     );
52    
53     ## now walk the packages list
54     my $details = catfile($LOCAL, qw(modules 02packages.details.txt.gz));
55     my $gz = gzopen($details, "rb") or die "Cannot open details: $gzerrno";
56     my $inheader = 1;
57     while ($gz->gzreadline($_) > 0) {
58     if ($inheader) {
59     $inheader = 0 unless /\S/;
60     next;
61     }
62    
63     my ($module, $version, $path) = split;
64     next if $path =~ m{/perl-5}; # skip Perl distributions
65     my_mirror("authors/id/$path", 1);
66     }
67    
68     ## finally, clean the files we didn't stick there
69     clean_unmirrored();
70    
71     exit 0;
72    
73     BEGIN {
74     ## %mirrored tracks the already done, keyed by filename
75     ## 1 = local-checked, 2 = remote-mirrored
76     my %mirrored;
77    
78     sub my_mirror {
79     my $path = shift; # partial URL
80     my $skip_if_present = shift; # true/false
81    
82     my $remote_uri = URI->new_abs($path, $REMOTE)->as_string; # full URL
83     my $local_file = catfile($LOCAL, split "/", $path); # native absolute file
84     my $checksum_might_be_up_to_date = 1;
85    
86     if ($skip_if_present and -f $local_file) {
87     ## upgrade to checked if not already
88     $mirrored{$local_file} = 1 unless $mirrored{$local_file};
89     } elsif (($mirrored{$local_file} || 0) < 2) {
90     ## upgrade to full mirror
91     $mirrored{$local_file} = 2;
92    
93     mkpath(dirname($local_file), $TRACE, 0711);
94     print $path if $TRACE;
95     my $status = mirror($remote_uri, $local_file);
96    
97     if ($status == RC_OK) {
98     $checksum_might_be_up_to_date = 0;
99     print " ... updated\n" if $TRACE;
100     } elsif ($status != RC_NOT_MODIFIED) {
101     warn "\n$remote_uri: $status\n";
102     return;
103     } else {
104     print " ... up to date\n" if $TRACE;
105     }
106     }
107    
108     if ($path =~ m{^authors/id}) { # maybe fetch CHECKSUMS
109     my $checksum_path =
110     URI->new_abs("CHECKSUMS", $remote_uri)->rel($REMOTE);
111     if ($path ne $checksum_path) {
112     my_mirror($checksum_path, $checksum_might_be_up_to_date);
113     }
114     }
115     }
116    
117     sub clean_unmirrored {
118     find sub {
119     return if /\.readme$/; # don't erase readme files
120     check_readme($File::Find::name);
121     return unless -f and not $mirrored{$File::Find::name};
122     print "$File::Find::name ... removed\n" if $TRACE;
123     unlink $_ or warn "Cannot remove $File::Find::name: $!";
124     my $path = $File::Find::name;
125     if ($path =~ s/\.tar\.gz/.readme/g && -f $path) {
126     # only if we erase archive also!
127     unlink $path or warn "Cannot remove $path: $!";
128     }
129     }, $LOCAL;
130     }
131    
132     sub check_readme {
133    
134     my $path = shift;
135    
136     # fixup some things
137     my $readme_path = $path;
138     $readme_path =~ s/\.tar\.gz/.readme/g || return; # just .tar.gz is supported!
139    
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     }

  ViewVC Help
Powered by ViewVC 1.1.26