| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package AnyPAN::Index; | 
| 2 | 2 |  |  | 2 |  | 14 | use strict; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 56 |  | 
| 3 | 2 |  |  | 2 |  | 8 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 64 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 15 | use Class::Accessor::Lite ro => [qw/headers packages/], new => 1; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 1026 | use AnyPAN::PackageInfo; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 60 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 2 |  |  | 2 |  | 1231 | use IO::Compress::Gzip; | 
|  | 2 |  |  |  |  | 85647 |  | 
|  | 2 |  |  |  |  | 101 |  | 
| 10 | 2 |  |  | 2 |  | 1169 | use IO::Uncompress::Gunzip; | 
|  | 2 |  |  |  |  | 30442 |  | 
|  | 2 |  |  |  |  | 1199 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | my @WELLKNOWN_HEADERS = qw/ | 
| 13 |  |  |  |  |  |  | File | 
| 14 |  |  |  |  |  |  | URL | 
| 15 |  |  |  |  |  |  | Description | 
| 16 |  |  |  |  |  |  | Columns | 
| 17 |  |  |  |  |  |  | Intended-For | 
| 18 |  |  |  |  |  |  | Written-By | 
| 19 |  |  |  |  |  |  | Line-Count | 
| 20 |  |  |  |  |  |  | Last-Updated | 
| 21 |  |  |  |  |  |  | /; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub parse { | 
| 24 | 4 |  |  | 4 | 0 | 60 | my ($class, $index_path, $source) = @_; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 4 |  |  |  |  | 18 | my $fh = IO::Uncompress::Gunzip->new($index_path->openr_raw); | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 4 |  |  |  |  | 9435 | my %headers; | 
| 29 |  |  |  |  |  |  | my @packages; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 4 |  |  |  |  | 11 | my $context = 'header'; | 
| 32 | 4 |  |  |  |  | 20 | while (defined(my $line = <$fh>)) { | 
| 33 | 41 |  |  |  |  | 2774 | chomp $line; | 
| 34 | 41 | 100 |  |  |  | 90 | if ($line eq '') { | 
| 35 | 4 |  |  |  |  | 8 | $context = 'index'; | 
| 36 | 4 |  |  |  |  | 21 | next; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 37 | 100 |  |  |  | 64 | if ($context eq 'header') { | 
|  |  | 50 |  |  |  |  |  | 
| 40 | 32 |  |  |  |  | 171 | my ($key, $value) = split /\s*:\s*/, $line; | 
| 41 | 32 |  |  |  |  | 129 | $headers{$key} = $value; | 
| 42 |  |  |  |  |  |  | } elsif ($context eq 'index') { | 
| 43 | 5 |  |  |  |  | 25 | my ($module, $version, $path) = split /\s+/, $line; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 5 |  |  |  |  | 38 | push @packages => AnyPAN::PackageInfo->new( | 
| 46 |  |  |  |  |  |  | source  => $source, | 
| 47 |  |  |  |  |  |  | module  => $module, | 
| 48 |  |  |  |  |  |  | version => $version, | 
| 49 |  |  |  |  |  |  | path    => $path, | 
| 50 |  |  |  |  |  |  | ); | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 4 |  |  |  |  | 97 | return $class->new( | 
| 55 |  |  |  |  |  |  | headers  => \%headers, | 
| 56 |  |  |  |  |  |  | packages => \@packages, | 
| 57 |  |  |  |  |  |  | ); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub save { | 
| 61 | 1 |  |  | 1 | 0 | 4 | my ($self, $storage) = @_; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 1 |  |  |  |  | 51 | my $tempfile = Path::Tiny->tempfile(UNKINK => 1); | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # write index | 
| 66 | 1 |  |  |  |  | 757 | my $fh = $tempfile->openw_raw(); | 
| 67 | 1 |  |  |  |  | 138 | $self->_write_to($fh); | 
| 68 | 1 | 50 |  |  |  | 517 | close $fh or die "$!: $tempfile"; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 1 |  |  |  |  | 8 | $storage->copy($tempfile, 'modules/02packages.details.txt.gz'); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub _write_to { | 
| 74 | 1 |  |  | 1 |  | 4 | my ($self, $raw_fh) = @_; | 
| 75 | 1 | 50 |  |  |  | 13 | my $fh = IO::Compress::Gzip->new($raw_fh) | 
| 76 |  |  |  |  |  |  | or die $IO::Compress::Gzip::GzipError; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 1 |  |  |  |  | 1718 | my %header = %{ $self->headers }; | 
|  | 1 |  |  |  |  | 19 |  | 
| 79 | 1 |  |  |  |  | 18 | for my $name (@WELLKNOWN_HEADERS) { | 
| 80 | 8 |  |  |  |  | 523 | my $value = delete $header{$name}; | 
| 81 | 8 |  |  |  |  | 28 | printf $fh "%-14s%s\n", "$name:", $value; | 
| 82 |  |  |  |  |  |  | } | 
| 83 | 1 |  |  |  |  | 66 | for my $name (sort keys %header) { | 
| 84 | 0 |  |  |  |  | 0 | my $value = $header{$name}; | 
| 85 | 0 |  |  |  |  | 0 | printf $fh "%-14s%s\n", "$name:", $value; | 
| 86 |  |  |  |  |  |  | } | 
| 87 | 1 |  |  |  |  | 8 | print $fh "\n"; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 1 |  |  |  |  | 68 | for my $package_info (@{ $self->packages }) { | 
|  | 1 |  |  |  |  | 7 |  | 
| 90 | 2 |  |  |  |  | 93 | printf $fh "%-35s %6s  %s\n", $package_info->module, $package_info->version, $package_info->path; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 1 | 50 |  |  |  | 74 | close $fh | 
| 94 |  |  |  |  |  |  | or die $IO::Compress::Gzip::GzipError; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | 1; | 
| 98 |  |  |  |  |  |  | __END__ |