#!/usr/bin/perl -w use strict; $|++; ### CONFIG # the CPAN url to fetch my $REMOTE = "http://www.cpan.org/"; # my $REMOTE = "file://Users/merlyn/MIRROR/CPAN/"; # my $REMOTE = "http://fi.cpan.org/"; # my $REMOTE = "http://au.cpan.org/"; # the path to the local mirror # warning: unknown files below this dir are deleted! my $LOCAL = "/Users/merlyn/Perl/MINICPAN/"; # how verbose? false means nothing but errors my $TRACE = 1; ## END CONFIG ## core: use File::Path qw(mkpath); use File::Basename qw(dirname); use File::Spec::Functions qw(catfile); use File::Find qw(find); ## LWP: use URI (); use LWP::Simple qw(mirror RC_OK RC_NOT_MODIFIED); ## Compress::Zlib use Compress::Zlib qw(gzopen $gzerrno); ## first, get index files my_mirror($_) for qw( authors/01mailrc.txt.gz modules/02packages.details.txt.gz modules/03modlist.data.gz ); ## now walk the packages list my $gz = gzopen(catfile($LOCAL, "modules/02packages.details.txt.gz"), "rb") or die "Cannot open details: $gzerrno"; my $state = 1; while ($gz->gzreadline($_) > 0) { if ($state == 1) { # in header $state = 2 unless /\S/; next; } if ($state == 2) { # blank following header $state = 3; next; } my ($module, $version, $path) = split; my_mirror("authors/id/$path"); } ## finally, clean the files we didn't stick there clean_unmirrored(); exit 0; BEGIN { my %mirrored; sub my_mirror { my $path = shift; my $remote_uri = URI->new_abs($path, $REMOTE)->as_string; my $local_file = catfile($LOCAL, $path); return if $mirrored{$local_file}++; ## presume "authors/id/*" is up to date if it is present return if $path =~ m{^authors/id} and -f $local_file; print "$remote_uri -> $local_file\n" if $TRACE; mkpath(dirname($local_file), 1, 0711); my $status = mirror($remote_uri, $local_file); return if $status == RC_OK or $status == RC_NOT_MODIFIED; warn "$remote_uri: $status!\n"; } sub clean_unmirrored { find sub { return unless -f and not $mirrored{$File::Find::name}; print "removing $File::Find::name\n" if $TRACE; unlink $_ or warn "Cannot remove $File::Find::name: $!"; }, $LOCAL; } }