| 1 | package OrePAN2::Index; | ||||||||||||
| 2 | |||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 6 | ▶ | use autodie; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 6 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 6 | ▶ | use IO::Uncompress::Gunzip ('$GunzipError'); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 6 | ▶ | use OrePAN2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | sub new { | ||||||||||||
| 12 | 6 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||
| 13 | 6 | ▶ | my %args = @_ == 1 ? %{ $_[0] } : @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 14 | 6 | bless { | |||||||||||
| 15 | index => {}, | ||||||||||||
| 16 | %args, | ||||||||||||
| 17 | }, $class; | ||||||||||||
| 18 | } | ||||||||||||
| 19 | |||||||||||||
| 20 | sub load { | ||||||||||||
| 21 | 4 | ▶ | my ( $self, $fname ) = @_; | ||||||||||
|
Subroutine
load: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||
| 22 | |||||||||||||
| 23 | 4 | my $fh = do { | |||||||||||
| 24 | 4 | ▶ | if ( $fname =~ /\.gz\z/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | 1 | ▶ | IO::Uncompress::Gunzip->new($fname) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | or die "gzip failed: $GunzipError\n"; | ||||||||||||
| 27 | } | ||||||||||||
| 28 | else { | ||||||||||||
| 29 | 3 | open my $fh, '<', $fname; | |||||||||||
| 30 | 3 | $fh; | |||||||||||
| 31 | } | ||||||||||||
| 32 | }; | ||||||||||||
| 33 | |||||||||||||
| 34 | # skip headers | ||||||||||||
| 35 | 4 | ▶ | while (<$fh>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 36 | ▶ | last unless /\S/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 4 | ▶ | while (<$fh>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 20 | ▶ | if (/^(\S+)\s+(\S+)\s+(.*)$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 20 | ▶ | $self->add_index( $1, $2 eq 'undef' ? undef : $2, $3 ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | } | ||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 4 | close $fh; | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | sub lookup { | ||||||||||||
| 49 | 7 | ▶ | my ( $self, $package ) = @_; | ||||||||||
|
Subroutine
lookup: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 50 | 7 | ▶ | if ( my $entry = $self->{index}->{$package} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 6 | return @$entry; | |||||||||||
| 52 | } | ||||||||||||
| 53 | 1 | return; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | sub packages { | ||||||||||||
| 57 | 2 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
packages: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 58 | 2 | sort { lc $a cmp lc $b } keys %{ $self->{index} }; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | sub delete_index { | ||||||||||||
| 62 | 1 | ▶ | my ( $self, $package ) = @_; | ||||||||||
|
Subroutine
delete_index: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 63 | 1 | delete $self->{index}->{$package}; | |||||||||||
| 64 | 1 | return; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | # Order of preference is last updated. So if some modules maintain the same | ||||||||||||
| 68 | # version number across multiple uploads, we'll point to the module in the | ||||||||||||
| 69 | # latest archive. | ||||||||||||
| 70 | |||||||||||||
| 71 | sub add_index { | ||||||||||||
| 72 | 23 | ▶ | my ( $self, $package, $version, $archive_file ) = @_; | ||||||||||
|
Subroutine
add_index: called CC=4 SCAR=16.8
Pod
documented
| |||||||||||||
| 73 | |||||||||||||
| 74 | 23 | ▶ | if ( $self->{index}{$package} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 1 | my ($orig_ver) = @{ $self->{index}{$package} }; | |||||||||||
| 76 | |||||||||||||
| 77 | 1 | ▶ | if ( version->parse($orig_ver) > version->parse($version) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 0 | ▶ | $version //= 'undef'; | ||||||||||
|
Condition0%0/2
$version //= "undef"
MC/DC0%0/1
$version //= "undef"
$version
Truth table0%0/2
A
A= $version
| |||||||||||||
| 79 | 0 | print STDERR "[INFO] Not adding $package in $archive_file\n"; | |||||||||||
| 80 | 0 | print STDERR | |||||||||||
| 81 | "[INFO] Existing version $orig_ver is greater than $version\n"; | ||||||||||||
| 82 | 0 | return; | |||||||||||
| 83 | } | ||||||||||||
| 84 | } | ||||||||||||
| 85 | 23 | $self->{index}->{$package} = [ $version, $archive_file ]; | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | sub as_string { | ||||||||||||
| 89 | 2 | ▶ | my ( $self, $opts ) = @_; | ||||||||||
|
Subroutine
as_string: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||
| 90 | 2 | ▶ | $opts ||= +{}; | ||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||
| 91 | 2 | ▶ | my $simple = $opts->{simple} || 0; | ||||||||||
|
Condition50%1/2
$opts->{'simple'} || 0
MC/DC0%0/1
$opts->{'simple'} || 0
$opts->{'simple'}
Truth table50%1/2
A
A= $opts->{'simple'}
| |||||||||||||
| 92 | |||||||||||||
| 93 | 2 | my @buf; | |||||||||||
| 94 | |||||||||||||
| 95 | 2 | ▶ | push @buf, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | ( | ||||||||||||
| 97 | 'File: 02packages.details.txt', | ||||||||||||
| 98 | 'URL: http://www.perl.com/CPAN/modules/02packages.details.txt', | ||||||||||||
| 99 | 'Description: DarkPAN', | ||||||||||||
| 100 | 'Columns: package name, version, path', | ||||||||||||
| 101 | 'Intended-For: Automated fetch routines, namespace documentation.', | ||||||||||||
| 102 | $simple | ||||||||||||
| 103 | ? () | ||||||||||||
| 104 | : ( | ||||||||||||
| 105 | "Written-By: OrePAN2 $OrePAN2::VERSION", | ||||||||||||
| 106 | 2 | "Line-Count: @{[ scalar(keys %{$self->{index}}) ]}", | |||||||||||
| 107 | 2 | "Last-Updated: @{[ scalar localtime ]}", | |||||||||||
| 108 | ), | ||||||||||||
| 109 | '', | ||||||||||||
| 110 | ); | ||||||||||||
| 111 | |||||||||||||
| 112 | 2 | for my $pkg ( $self->packages ) { | |||||||||||
| 113 | 6 | my $entry = $self->{index}{$pkg}; | |||||||||||
| 114 | |||||||||||||
| 115 | # package name, version, path | ||||||||||||
| 116 | 6 | ▶ | push @buf, sprintf '%-22s %-22s %s', $pkg, $entry->[0] || 'undef', | ||||||||||
|
Condition100%2/2
$entry->[0] || 'undef'
MC/DC100%1/1
$entry->[0] || 'undef'
$entry->[0]
Truth table100%2/2
A
A= $entry->[0]
| |||||||||||||
| 117 | $entry->[1]; | ||||||||||||
| 118 | } | ||||||||||||
| 119 | 2 | return join( "\n", @buf ) . "\n"; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 6 | 1; | |||||||||||
| 123 | __END__ | ||||||||||||
| 124 | |||||||||||||
| 125 | =head1 NAME | ||||||||||||
| 126 | |||||||||||||
| 127 | OrePAN2::Index - Index | ||||||||||||
| 128 | |||||||||||||
| 129 | =head1 DESCRIPTION | ||||||||||||
| 130 | |||||||||||||
| 131 | This is a module to manipulate 02packages.details.txt. | ||||||||||||
| 132 | |||||||||||||
| 133 | =head1 METHODS | ||||||||||||
| 134 | |||||||||||||
| 135 | =over 4 | ||||||||||||
| 136 | |||||||||||||
| 137 | =item C<< my $index = OrePAN2::Index->new(%attr) >> | ||||||||||||
| 138 | |||||||||||||
| 139 | =item C<< $index->load($filename) >> | ||||||||||||
| 140 | |||||||||||||
| 141 | Load an existing 02.packages.details.txt | ||||||||||||
| 142 | |||||||||||||
| 143 | =item C<< my ($version, $path) = $index->lookup($package) >> | ||||||||||||
| 144 | |||||||||||||
| 145 | Perform a package lookup on the index. | ||||||||||||
| 146 | |||||||||||||
| 147 | =item C<< $index->delete_index($package) >> | ||||||||||||
| 148 | |||||||||||||
| 149 | Delete a package from the index. | ||||||||||||
| 150 | |||||||||||||
| 151 | =item C<< $index->add_index($package, $version, $path) >> | ||||||||||||
| 152 | |||||||||||||
| 153 | Add a new entry to the index. | ||||||||||||
| 154 | |||||||||||||
| 155 | =item C<< $index->as_string() >> | ||||||||||||
| 156 | |||||||||||||
| 157 | Returns the content of the index as a string. Some of the index metadata can | ||||||||||||
| 158 | cause merge conflicts when multiple developers are working on the same project. | ||||||||||||
| 159 | You can avoid this problem by using a paring down the metadata. "simple" | ||||||||||||
| 160 | defaults to 0. | ||||||||||||
| 161 | |||||||||||||
| 162 | $index->as_string( simple => 1 ); | ||||||||||||
| 163 | |||||||||||||
| 164 | Make index as string. | ||||||||||||
| 165 | |||||||||||||
| 166 | =back | ||||||||||||