| 1 | package BackPAN::Index::Create; | ||||||||||||||||||||||||||||
| 2 | 5 | $BackPAN::Index::Create::VERSION = '0.13'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Exporter::Lite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Path::Iterator::Rule; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Scalar::Util qw/ reftype /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Module::Loader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | our @EXPORT_OK = qw(create_backpan_index); | |||||||||||||||||||||||||||
| 14 | 5 | my $FORMAT_REVISION = 1; | |||||||||||||||||||||||||||
| 15 | 5 | my $DEFAULT_ORDER = 'dist'; | |||||||||||||||||||||||||||
| 16 | 5 | my $PLUGIN_NAMESPACE = 'BackPAN::Index::Create::OrderBy'; | |||||||||||||||||||||||||||
| 17 | 5 | my $COMMA = q{,}; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub create_backpan_index | ||||||||||||||||||||||||||||
| 20 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
create_backpan_index: called CC=18 SCAR=31.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | if (@_ != 1 || reftype($_[0]) ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ != 1 or builtin::reftype($_[0]) ne "HASH"
MC/DC0%0/2
@_ != 1 or builtin::reftype($_[0]) ne "HASH"
@_ != 1
builtin::reftype($_[0]) ne "HASH"
Truth table33%1/3
A or B
A= @_ != 1B= builtin::reftype($_[0]) ne "HASH"
| |||||||||||||||||||||||||||||
| 22 | 0 | croak "create_backpan_index() expects a single hashref argument\n"; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 4 | my $argref = shift; | |||||||||||||||||||||||||||
| 25 | my $basedir = $argref->{basedir} | ||||||||||||||||||||||||||||
| 26 | 4 | ▶ | || croak "create_backpan_index() must be given a 'basedir'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$argref->{'basedir'} || croak("create_backpan_index() must be given a 'basedir'\n")
MC/DC0%0/1
$argref->{'basedir'} || croak("create_backpan_index() must be given a 'basedir'\n")
$argref->{'basedir'}
Truth table50%1/2
A
A= $argref->{'basedir'}
| |||||||||||||||||||||||||||||
| 27 | my $order = defined($argref->{order}) | ||||||||||||||||||||||||||||
| 28 | ? $argref->{order} | ||||||||||||||||||||||||||||
| 29 | 4 | ▶ | : $DEFAULT_ORDER; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 4 | my $author_dir = "$basedir/authors"; | |||||||||||||||||||||||||||
| 31 | 4 | my $stem = "$author_dir/id"; | |||||||||||||||||||||||||||
| 32 | 4 | ▶ | my $releases_only = $argref->{releases_only} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$argref->{'releases_only'} || 0
MC/DC100%1/1
$argref->{'releases_only'} || 0
$argref->{'releases_only'}
Truth table100%2/2
A
A= $argref->{'releases_only'}
| |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | my $loader = Module::Loader->new() | ||||||||||||||||||||||||||
|
Condition50%1/2
'Module::Loader'->new || croak("failed to instantiate Module::Loader\n")
MC/DC0%0/1
'Module::Loader'->new || croak("failed to instantiate Module::Loader\n")
'Module::Loader'->new
Truth table50%1/2
A
A= 'Module::Loader'->new
| |||||||||||||||||||||||||||||
| 34 | || croak "failed to instantiate Module::Loader\n"; | ||||||||||||||||||||||||||||
| 35 | 4 | my @plugins = $loader->find_modules($PLUGIN_NAMESPACE); | |||||||||||||||||||||||||||
| 36 | 4 | my @plugin_basenames = map { my $p = $_; $p =~ s/^.*:://; $p } @plugins; | |||||||||||||||||||||||||||
| 37 | 4 | my $fh; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | if (not -d $author_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | croak "create_backpan_index() can't find 'authors' directory in basedir ($basedir)\n"; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | my ($basename) = grep { lc($_) eq lc($order) } @plugin_basenames; | |||||||||||||||||||||||||||
| 45 | 4 | ▶ | if (not defined($basename)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | croak "order '$order' not known. Supported orders are ", | ||||||||||||||||||||||||||||
| 47 | 0 | join($COMMA, map { "'".lc($_)."'" } @plugin_basenames), "\n"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 4 | my $plugin_class = $PLUGIN_NAMESPACE.'::'.$basename; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | $loader->load($plugin_class); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | if (exists($argref->{output})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 4 | open($fh, '>', $argref->{output}); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||
| 57 | 0 | $fh = \*STDOUT; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | my $plugin = $plugin_class->new(filehandle => $fh) | ||||||||||||||||||||||||||
|
Condition50%1/2
$plugin_class->new('filehandle', $fh) || croak("failed to create instance of '${order}' plugin ($plugin_class)")
MC/DC0%0/1
$plugin_class->new('filehandle', $fh) || croak("failed to create instance of '${order}' plugin ($plugin_class)")
$plugin_class->new('filehandle', $fh)
Truth table50%1/2
A
A= $plugin_class->new('filehandle', $fh)
| |||||||||||||||||||||||||||||
| 61 | || croak "failed to create instance of '$order' plugin ($plugin_class)"; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 4 | print $fh "#FORMAT $FORMAT_REVISION\n"; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | my $rule = Path::Iterator::Rule->new(); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | $rule->file->name("*"); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | if ($releases_only) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | # A 'releases only' index contains just the tarballs | ||||||||||||||||||||||||||||
| 71 | # and the paths don't include the leading 'authors/id' | ||||||||||||||||||||||||||||
| 72 | # Does a BackPAN ever contain anything in a directory | ||||||||||||||||||||||||||||
| 73 | # other than authors? | ||||||||||||||||||||||||||||
| 74 | 3 | ▶ | $rule->and(sub { /\.(tar\.gz|tgz|zip)$/ }) if $releases_only; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 3 | $stem = "$author_dir/id"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 1 | $stem = $basedir; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | foreach my $path ($rule->all($author_dir)) { | |||||||||||||||||||||||||||
| 82 | 12 | ▶ | next if $path =~ /\s+\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 12 | ▶ | next if $path =~ /\n/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 12 | my $tail = $path; | |||||||||||||||||||||||||||
| 85 | 12 | $tail =~ s!^\Q${stem}\E[^A-Za-z0-9]+!!; | |||||||||||||||||||||||||||
| 86 | 12 | ▶ | $tail =~ s!\\!/!g if $^O eq 'MSWin32'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 12 | my @stat = stat($path); | |||||||||||||||||||||||||||
| 88 | 12 | my $time = $stat[9]; | |||||||||||||||||||||||||||
| 89 | 12 | my $size = $stat[7]; | |||||||||||||||||||||||||||
| 90 | # printf $fh "%s %d %d\n", $tail, $time, $size; | ||||||||||||||||||||||||||||
| 91 | 12 | $plugin->add_file($tail, $time, $size); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | $plugin->finish(); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | ▶ | close($fh) if exists($argref->{output}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | 1; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 NAME | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | BackPAN::Index::Create - generate an index file for a BackPAN mirror | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | use BackPAN::Index::Create qw/ create_backpan_index /; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | create_backpan_index({ | ||||||||||||||||||||||||||||
| 110 | basedir => '/path/to/backpan' | ||||||||||||||||||||||||||||
| 111 | releases_only => 0 | 1, | ||||||||||||||||||||||||||||
| 112 | output => 'backpan-index.txt', | ||||||||||||||||||||||||||||
| 113 | order => 'dist' # or 'author' or 'age' | ||||||||||||||||||||||||||||
| 114 | }); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | B<BackPAN::Index::Create> provides a function C<create_backpan_index()> | ||||||||||||||||||||||||||||
| 119 | that will create a text index file for a BackPAN CPAN mirror. | ||||||||||||||||||||||||||||
| 120 | A BackPAN CPAN mirror is like a regular CPAN mirror, but it has everything | ||||||||||||||||||||||||||||
| 121 | that has ever been released to CPAN. | ||||||||||||||||||||||||||||
| 122 | The canonical BackPAN mirror is L<backpan.perl.org|http://backpan.perl.org>. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | By default the generated index will look like this: | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | #FORMAT 1 | ||||||||||||||||||||||||||||
| 127 | authors/id/B/BA/BARBIE/Acme-CPANAuthors-British-1.01.meta.txt 1395991503 1832 | ||||||||||||||||||||||||||||
| 128 | authors/id/B/BA/BARBIE/Acme-CPANAuthors-British-1.01.readme.txt 1395991503 1912 | ||||||||||||||||||||||||||||
| 129 | authors/id/B/BA/BARBIE/Acme-CPANAuthors-British-1.01.tar.gz 1395991561 11231 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | The first line is a comment that identifies the revision number of the index format. | ||||||||||||||||||||||||||||
| 132 | For each file in the BackPAN mirror the index will then contain one line. | ||||||||||||||||||||||||||||
| 133 | Each line contains three items: | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =over 4 | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =item * path | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item * timestamp | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =item * size (in bytes) | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =back | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | You can see indexes created using this module on the | ||||||||||||||||||||||||||||
| 146 | L<CPAN Testers BackPAN|http://backpan.cpantesters.org>, | ||||||||||||||||||||||||||||
| 147 | the home page of which also has more information about BackPAN mirrors. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 ARGUMENTS | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | The supported arguments are all shown in the SYNOPSIS. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 basedir | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | The path to the top of your BackPAN repository, without a trailing slash. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 releases_only | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | If the C<releases_only> option is true, then the index will only contain | ||||||||||||||||||||||||||||
| 160 | release tarballs, and the paths won't include the leading C<authors/id/>: | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | #FORMAT 1 | ||||||||||||||||||||||||||||
| 163 | B/BA/BARBIE/Acme-CPANAuthors-British-1.01.tar.gz 1395991561 11231 | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 output | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | The path to the file where the index should be generated. | ||||||||||||||||||||||||||||
| 168 | If the file already exists, it will be over-written. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 order | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Specifies what order the entries should be written to the index in. | ||||||||||||||||||||||||||||
| 173 | Currently supported values are: | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =over 4 | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item * 'dist' | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Entries are sorted first by dist name (as determined by L<CPAN::DistnameInfo>, | ||||||||||||||||||||||||||||
| 180 | and then by age. This means that when processing the file, you'll see | ||||||||||||||||||||||||||||
| 181 | entries dist by dist, and within each dist you'll see them in order they | ||||||||||||||||||||||||||||
| 182 | were released. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item * 'author' | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Entries are sorted first by author, and then by filename. | ||||||||||||||||||||||||||||
| 187 | This will cluster files from the same release. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =item * 'age' | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Entries are sorted first by age, and then by filename. | ||||||||||||||||||||||||||||
| 192 | Given that filenames are unique on CPAN, this should give | ||||||||||||||||||||||||||||
| 193 | a deterministic result for a specific BackPAN. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =back | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | The supported sort orders are defined by plugins in the | ||||||||||||||||||||||||||||
| 198 | C<BackPAN::Index::Create::OrderBy> namespace. | ||||||||||||||||||||||||||||
| 199 | C<Dist.pm>, C<Author.pm>, and C<Age.pm> are included in the base distribution. | ||||||||||||||||||||||||||||
| 200 | If you have installed additional plugins, they'll be automatically available. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Note: CPAN::DistnameInfo doesn't handle paths for files other than | ||||||||||||||||||||||||||||
| 203 | tarballs, so if you generate a full index, you may not get the results | ||||||||||||||||||||||||||||
| 204 | you expect. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 Note for Windows users | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | On Windows the generated index will use '/' as the directory separator, | ||||||||||||||||||||||||||||
| 209 | as I I<think> that's the right thing to do. | ||||||||||||||||||||||||||||
| 210 | Please let me know if you think I'm wrong. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | L<create-backpan-index> - a script that provides a command-line interface | ||||||||||||||||||||||||||||
| 215 | to this module, included in the same distribution. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | L<BackPAN::Index> - an interface to an alternate BackPAN index. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | L<CPAN Testers BackPAN|http://backpan.cpantesters.org> - | ||||||||||||||||||||||||||||
| 220 | one of the BackPAN mirrors. It provides four different indexes | ||||||||||||||||||||||||||||
| 221 | that are generated using this module. The script used to generate | ||||||||||||||||||||||||||||
| 222 | these can be found in the C<examples> directory of this distribution. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | L<https://github.com/neilbowers/BackPAN-Index-Create> | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Neil Bowers E<lt>neilb@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | This software is copyright (c) 2014 by Neil Bowers <neilb@cpan.org>. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 237 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||