-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPods.pl
executable file
·356 lines (274 loc) · 7.52 KB
/
getPods.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env perl
use strict;
use warnings;
use MetaCPAN::Client;
use Data::Printer;
use Data::Dumper;
use FileHandle;
use DBI;
use Time::HiRes qw( usleep );
use HTTP::Tiny;
use File::Path qw(make_path remove_tree);
my $DB_NAME = './CPAN.docset/Contents/Resources/docSet.dsidx';
my $uPAUSE = "250000"; # How long to pause so as not to slam metacpan in usec
my $BATCH_SIZE = "100"; # How many items to process in a batch.
my $URL_BASE = "http://metacpan.org/pod/";
my $BASE_DIR = "./CPAN.docset/Contents/Resources/Documents";
# used to give us some stats as to how many items we found links for
my $SKIPPED = 0;
my $STORED = 0;
my $TOTAL = 0;
my $DBH; #tracks the database handle
main();
sub main
{
cleanUP();
createDB() if ( !-e $DB_NAME );
getModules();
getDocs();
print "Stored: $STORED\n";
print "Skipped: $SKIPPED\n";
print "TOTAL: $TOTAL\n";
print "Skip %: " . int( ( $SKIPPED / $TOTAL ) * 100 ) . "\n";
}
#
# Get the list of all of the availble modules
# The loop through all of the packages(?) in the module
# and build a list of names
#
sub getModules
{
my $module;
my $names;
my $types;
my $paths;
my $i;
my $mcpan = MetaCPAN::Client->new( ua_args => [ agent => 'CPANDash' ] );
my $all_modules = $mcpan->all( 'modules', { fields => [ "name", "module" ] } );
while ( $module = $all_modules->next )
{
my $mod_list = $module->module;
next unless defined( $mod_list->[0]{name} );
foreach my $mod ( @{$mod_list} )
{
push( @$names, $mod->{name} );
push( @$types, "Package" );
push( @$paths, buildPath( $mod->{name} ) . "/" . $mod->{name} . ".html" );
}
$i++;
# store these in batches of $BATCH_SIZE
if ( $i % $BATCH_SIZE == 0 )
{
storeData( $names, $paths, $types );
undef $names;
undef $paths;
undef $types;
}
}
# now store any that are left
storeData( $names, $paths, $types );
}
# Write a doc row to the database
sub storeData
{
my ( $names, $paths, $types ) = @_;
my $rows;
my $dbh = getDBH();
my $sql = qq{
INSERT OR IGNORE INTO searchIndex(name, path, type) VALUES (?,?,?);
};
my $sth = $dbh->prepare($sql);
( undef, $rows ) = $sth->execute_array( {}, $names, $paths, $types ) or die 'failed to insert:' . $DBI::errstr;
}
# Delete a row from the db
sub deleteByID
{
my ($row_id) = @_;
my $dbh = getDBH();
my $sql = qq{
DELETE FROM searchIndex WHERE id = $row_id ;
};
my $sth = $dbh->prepare($sql);
$sth->execute() or die "error deleting:" . $DBI::errstr;
}
#
# Now that we have all of the package names, go through the
# DB and try to find docs for each package. if the doc
# exists, write it to disk, otherwise remove the row from the DB
#
sub getDocs
{
my $result;
my $dbh = getDBH();
my $sql = qq{
SELECT * FROM searchIndex
};
my $sth = $dbh->prepare($sql);
$sth->execute() or die 'Error loading: ' . $DBI::errstr;
while ( $result = $sth->fetchrow_hashref )
{
$TOTAL++;
my $pod;
eval { $pod = getPod( $result->{name} ) };
# If we get a pod, store it
if ( defined($pod) && $pod !~ /^\s+$/ && $result->{name} !~ /^_.*/)
{
$STORED++;
print "STORE: " . $result->{id} . " " . $result->{name} . "\n";
writePod( $result->{name}, $pod );
}
# otherwise remove the entry from the database
else
{
$SKIPPED++;
print "\tSKIPPING: " . $result->{id} . " " . $result->{name} . "\n";
deleteByID( $result->{id} );
}
}
}
# Get the pod in html form and return it.
sub getPod
{
my ($package) = @_;
my $mcpan = MetaCPAN::Client->new( ua_args => [ agent => 'CPANDash' ] );
my $pod = $mcpan->pod($package)->html;
return $pod;
}
# WRite the pod to disk creating path as needed
sub writePod
{
my ( $package, $pod ) = @_;
my $mkdir_error;
my $path_name = buildPath($package);
my $filename = $package . ".html";
# Figure out our path depth so we can set the relative link correclty in the html
my @split_path = split /\//, $path_name;
my $depth = scalar @split_path;
make_path("$BASE_DIR/$path_name");
my $fh = FileHandle->new("> $BASE_DIR/$path_name/$filename") or die "Couldn't open file for write: $!";
print $fh updateHTML( $depth, $pod, $package );
$fh->close;
}
# Construct the path string
sub buildPath
{
my ($package) = @_;
$package =~ s/::/\//g;
return $package;
}
# Creates the database
sub createDB
{
my $dbh = getDBH();
my $schema = qq{
CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);
};
my $index = qq{
CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);
};
my $sths = $dbh->prepare($schema) or die 'failed to prepare:' . $DBI::errstr;
$sths->execute();
my $sthi = $dbh->prepare($index) or die 'failed to prepare:' . $DBI::errstr;
$sthi->execute();
}
# gets the database handle
sub getDBH
{
if ( defined($DBH) )
{
return $DBH;
}
else
{
$DBH = DBI->connect( "dbi:SQLite:dbname=$DB_NAME", '', '' );
return $DBH;
}
}
sub updateHTML
{
my ( $depth, $html, $title ) = @_;
my $path = '../' x $depth;
my $header = <<"HEADER";
<html>
<head>
<title>$title</title>
<link rel="stylesheet" type="text/css" href="${path}style.css" />
<link rel="stylesheet" href="${path}default.min.css" />
<script src="${path}highlight.min.js"></script>
</head>
<body>
<script>hljs.initHighlightingOnLoad();</script>
HEADER
my $footer = <<"FOOTER";
</body>
</html>
FOOTER
return $header . $html . $footer;
}
# Clean out the destination directories and files
sub cleanUP
{
unlink $DB_NAME;
opendir( DH, $BASE_DIR );
my @files = readdir(DH);
close(DH);
foreach my $file (@files)
{
next if ( $file =~ /^\.$/ );
next if ( $file =~ /^\.\.$/ );
next unless ( -d $BASE_DIR . "/" . $file );
remove_tree( $BASE_DIR . "/" . $file );
}
}
#
# These two methods were for URL validation
# not used for pod retreival. Left here for no good reason
#
sub validateDB
{
my $result;
my $counter;
my $dbh = getDBH();
my $sql = qq{
SELECT * FROM searchIndex;
};
my $sth = $dbh->prepare($sql) or die 'failed to prepare:' . $DBI::errstr;
$sth->execute();
while ( $result = $sth->fetchrow_hashref() )
{
my $url = checkURL( $result->{name} );
if ( defined($url) )
{
$STORED++;
print "OK " . $result->{name} . "\n";
}
else
{
$SKIPPED++;
print "\tSKIPPING " . $result->{name} . "\n";
deleteByID( $result->{id} );
}
$counter++;
# Pause every $BATHCSIZE iterations
if ( $counter % $BATCH_SIZE == 0 )
{
usleep($uPAUSE);
}
}
}
sub checkURL
{
my ($pod_name) = @_;
my $http = HTTP::Tiny->new();
# Check to see if there are docs where we expect them on metacpan
# use a head request to conserve bandwidth
my $resp = $http->head( $URL_BASE . $pod_name );
if ( $resp->{success} == 1 )
{
return $URL_BASE . $pod_name;
}
else
{
return;
}
}