-
Notifications
You must be signed in to change notification settings - Fork 6
/
cpandb.pl
131 lines (119 loc) · 3.32 KB
/
cpandb.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use strict;
use warnings;
use DBI;
use CPAN::DistnameInfo;
use IO::Zlib;
use File::Fetch;
use LWP::Simple;
use File::Spec;
my $verbose = shift;
$|=1;
my $tables = {
mods => [
'mod_name VARCHAR(300) NOT NULL',
'dist_name VARCHAR(190) NOT NULL',
'dist_vers VARCHAR(20)',
'cpan_id VARCHAR(20) NOT NULL',
'mod_vers VARCHAR(30)',
],
dists => [
'dist_name VARCHAR(190) NOT NULL',
'cpan_id VARCHAR(20) NOT NULL',
'dist_file VARCHAR(400) NOT NULL',
'dist_vers VARCHAR(20)',
],
auths => [
'cpan_id VARCHAR(20) NOT NULL',
'fullname VARCHAR(60) NOT NULL',
'email TEXT',
],
};
my $mirror = 'ftp://localhost/CPAN/';
my $packages_file = '02packages.details.txt.gz';
my $mailrc_file = '01mailrc.txt.gz';
fetch_indexes('.',$mirror,$mailrc_file,$packages_file);
my $dbh = DBI->connect("dbi:SQLite:dbname=meep.db","","");
create_tables($dbh);
print "Populating auths ... ";
populate_auths($dbh,$mailrc_file);
print "DONE\nPopulating dists and mods ... ";
populate_dists($dbh,$packages_file);
print "DONE\n";
exit 0;
sub create_tables {
my $handle = shift;
foreach my $table ( keys %$tables ) {
my $sql = 'CREATE TABLE IF NOT EXISTS ' . $table . ' ( ';
$sql .= join ', ', @{ $tables->{$table} };
$sql .= ' )';
$handle->do($sql) or die $handle->errstr;
$handle->do('DELETE FROM ' . $table) or die $handle->errstr;
}
return 1;
}
sub populate_dists {
my ($handle,$pfile) = @_;
my $fh = IO::Zlib->new( $pfile, "rb" ) or die "$!\n";
my %dists;
while (<$fh>) {
last if /^\s*$/;
}
while (<$fh>) {
chomp;
my ($module,$version,$package_path) = split ' ', $_;
my $d = CPAN::DistnameInfo->new( $package_path );
next unless $d;
my $metaname = $d->pathname;
my $extension = $d->extension;
next unless $extension;
unless ( exists $dists{$package_path} ) {
print ".INSERT '", $d->dist, "'\n" if $verbose;
my $sth = $handle->prepare(qq{INSERT INTO dists values (?,?,?,?)}) or die $handle->errstr;
$sth->execute(
$d->dist,
$d->cpanid,
$d->pathname,
$d->version,
) or die $handle->errstr;
$dists{$package_path}++;
}
# Insert into table dists and mods
print ".INSERT '$module'\n" if $verbose;
my $sth = $handle->prepare(qq{INSERT INTO mods values (?,?,?,?,?)}) or die $handle->errstr;
$sth->execute(
$module,
$d->dist,
$d->version,
$d->cpanid,
$version,
) or die $handle->errstr;
}
return 1;
}
sub populate_auths {
my ($handle,$mfile) = @_;
my $fh = IO::Zlib->new( $mfile, "rb" ) or die "$!\n";
while (<$fh>) {
chomp;
my ( $alias, $pauseid, $long ) = split ' ', $_, 3;
$long =~ s/^"//;
$long =~ s/"$//;
my ($name, $email) = $long =~ /(.*) <(.+)>$/;
print ".INSERT '$pauseid'\n" if $verbose;
my $sth = $handle->prepare(qq{INSERT INTO auths values (?,?,?)}) or die $handle->errstr;
$sth->execute( $pauseid, $name, $email ) or die $handle->errstr;
}
return 1;
}
sub fetch_indexes {
my ($location,$mirror,$mailrc,$packages) = @_;
$mailrc = join '/', 'authors', $mailrc;
$packages = join '/', 'modules', $packages;
foreach my $file ( $mailrc, $packages ) {
my $url = join '', $mirror, $file;
my $ff = File::Fetch->new( uri => $url );
my $stat = $ff->fetch( to => $location );
next unless $stat;
print "Downloaded '$url' to '$stat'\n";
}
}