| 1 | package CPAN::Repository::Packages; | ||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GETTY'; | |||||||||
| 3 | # ABSTRACT: 02packages | ||||||||||
| 4 | 2 | $CPAN::Repository::Packages::VERSION = '0.009'; | |||||||||
| 5 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | with qw( | |||||||||
| 8 | CPAN::Repository::Role::File | ||||||||||
| 9 | ); | ||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Dist::Data; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use File::Spec::Functions ':ALL'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use DateTime::Format::RFC3339; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use DateTime::Format::Epoch::Unix; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 27 | ▶ | sub file_parts { 'modules', '02packages.details.txt' } | ||||||||
|
Subroutine
file_parts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 18 | 5 | ▶ | sub file_parts_stamp { 'modules', '02STAMP' } | ||||||||
|
Subroutine
file_parts_stamp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | |||||||||||
| 20 | 2 | has modules => ( | |||||||||
| 21 | is => 'ro', | ||||||||||
| 22 | lazy => 1, | ||||||||||
| 23 | builder => '_build_modules', | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | sub _build_modules { | ||||||||||
| 27 | 2 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
_build_modules: called CC=5 SCAR=16.1
| |||||||||||
| 28 | 2 | ▶ | return {} unless $self->exist; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 1 | my @lines = $self->get_file_lines; | |||||||||
| 30 | 1 | my %modules; | |||||||||
| 31 | 1 | for (@lines) { | |||||||||
| 32 | 11 | chomp($_); | |||||||||
| 33 | 11 | ▶ | next if ($_ =~ /[^:]:[ \t]/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 3 | ▶ | if ($_ =~ m/^([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t]+)$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | # $1 = module | ||||||||||
| 36 | # $2 = version | ||||||||||
| 37 | # $3 = path (inside repository) | ||||||||||
| 38 | 2 | $modules{$1} = [ $2, $3 ]; | |||||||||
| 39 | } | ||||||||||
| 40 | } | ||||||||||
| 41 | 1 | return \%modules; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 2 | has authorbase_path_parts => ( | |||||||||
| 45 | is => 'ro', | ||||||||||
| 46 | required => 1, | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 2 | has url => ( | |||||||||
| 50 | is => 'ro', | ||||||||||
| 51 | required => 1, | ||||||||||
| 52 | ); | ||||||||||
| 53 | |||||||||||
| 54 | 2 | has written_by => ( | |||||||||
| 55 | is => 'ro', | ||||||||||
| 56 | required => 1, | ||||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | sub get_module { | ||||||||||
| 60 | 0 | ▶ | my ( $self, $module ) = @_; | ||||||||
|
Subroutine
get_module: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 61 | return defined $self->modules->{$module} | ||||||||||
| 62 | 0 | ▶ | ? $self->modules->{$module}->[1] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | : (); | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | sub get_module_version { | ||||||||||
| 67 | 0 | ▶ | my ( $self, $module ) = @_; | ||||||||
|
Subroutine
get_module_version: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 68 | return defined $self->modules->{$module} | ||||||||||
| 69 | 0 | ▶ | ? $self->modules->{$module}->[0] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | : (); | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | sub set_module { | ||||||||||
| 74 | 3 | ▶ | my ( $self, $module, $version, $path ) = @_; | ||||||||
|
Subroutine
set_module: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 75 | 3 | return $self->modules->{$module} = [ $version, $path ]; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub add_distribution { | ||||||||||
| 79 | 3 | ▶ | my ( $self, $author_distribution_path ) = @_; | ||||||||
|
Subroutine
add_distribution: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 80 | 3 | my $filename = catfile( $self->repository_root, @{$self->authorbase_path_parts}, splitdir( $author_distribution_path ) ); | |||||||||
| 81 | 3 | my $dist = Dist::Data->new( $filename ); | |||||||||
| 82 | 3 | for (keys %{$dist->packages}) { | |||||||||
| 83 | 3 | $self->set_module($_, $dist->packages->{$_}->{version}, $author_distribution_path); | |||||||||
| 84 | } | ||||||||||
| 85 | 3 | return $self; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub stamp_filename { | ||||||||||
| 89 | 5 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
stamp_filename: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 90 | 5 | catfile( $self->repository_root, $self->file_parts_stamp ); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | after save => sub { | ||||||||||
| 94 | 4 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 95 | 4 | ▶ | my $stamp = IO::File->new($self->stamp_filename, "w") or die "cant write to ".$self->stamp_filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 4 | my $now = DateTime->now; | |||||||||
| 97 | 4 | print $stamp (DateTime::Format::Epoch::Unix->format_datetime($now).' '.DateTime::Format::RFC3339->new->format_datetime($now)."\n"); | |||||||||
| 98 | 4 | $stamp->close; | |||||||||
| 99 | 2 | }; | |||||||||
| 100 | |||||||||||
| 101 | sub timestamp { | ||||||||||
| 102 | 1 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
timestamp: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||
| 103 | 1 | ▶ | my $stamp = IO::File->new($self->stamp_filename, "r") or die "cant read ".$self->stamp_filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 1 | my ( $line ) = <$stamp>; | |||||||||
| 105 | 1 | chomp($line); | |||||||||
| 106 | 1 | ▶ | if ($line =~ /^(\d+) /) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 1 | return DateTime::Format::Epoch::Unix->parse_datetime($1); | |||||||||
| 108 | } else { | ||||||||||
| 109 | 0 | die "cant find unix timestamp from ".$self->stamp_filename; | |||||||||
| 110 | } | ||||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | sub generate_content { | ||||||||||
| 114 | 4 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
generate_content: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 115 | 4 | my @file_parts = $self->file_parts; | |||||||||
| 116 | 4 | my $content = ""; | |||||||||
| 117 | 4 | $content .= $self->generate_header_line('File:',(pop @file_parts)); | |||||||||
| 118 | 4 | $content .= $self->generate_header_line('URL:',$self->url.$self->path_inside_root); | |||||||||
| 119 | 4 | $content .= $self->generate_header_line('Description:','Package names found in directory $CPAN/authors/id/'); | |||||||||
| 120 | 4 | $content .= $self->generate_header_line('Columns:','package name, version, path'); | |||||||||
| 121 | 4 | $content .= $self->generate_header_line('Intended-For:','Automated fetch routines, namespace documentation.'); | |||||||||
| 122 | 4 | $content .= $self->generate_header_line('Written-By:',$self->written_by); | |||||||||
| 123 | 4 | $content .= $self->generate_header_line('Line-Count:',scalar keys %{$self->modules}); | |||||||||
| 124 | 4 | $content .= $self->generate_header_line('Last-Updated:',DateTime->now->strftime('%a, %e %b %y %T %Z')); | |||||||||
| 125 | 4 | $content .= "\n"; | |||||||||
| 126 | 4 | for (sort { $a cmp $b } keys %{$self->modules}) { | |||||||||
| 127 | 5 | ▶ | $content .= sprintf("%-60s %-20s %s\n",$_,$self->modules->{$_}->[0] ? $self->modules->{$_}->[0] : 'undef',$self->modules->{$_}->[1]); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | } | ||||||||||
| 129 | 4 | return $content; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | sub generate_header_line { | ||||||||||
| 133 | 32 | ▶ | my ( $self, $key, $value ) = @_; | ||||||||
|
Subroutine
generate_header_line: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 134 | 32 | return sprintf("%-13s %s\n",$key,$value); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 2 | 1; | |||||||||
| 138 | |||||||||||
| 139 | __END__ | ||||||||||
| 140 | |||||||||||
| 141 | =pod | ||||||||||
| 142 | |||||||||||
| 143 | =head1 NAME | ||||||||||
| 144 | |||||||||||
| 145 | CPAN::Repository::Packages - 02packages | ||||||||||
| 146 | |||||||||||
| 147 | =head1 VERSION | ||||||||||
| 148 | |||||||||||
| 149 | version 0.009 | ||||||||||
| 150 | |||||||||||
| 151 | =head1 SYNOPSIS | ||||||||||
| 152 | |||||||||||
| 153 | use CPAN::Repository::Packages; | ||||||||||
| 154 | |||||||||||
| 155 | my $packages = CPAN::Repository::Packages->new({ | ||||||||||
| 156 | repository_root => $fullpath_to_root, | ||||||||||
| 157 | url => $url, | ||||||||||
| 158 | written_by => $written_by, | ||||||||||
| 159 | authorbase_path_parts => ['authors','id'], | ||||||||||
| 160 | }); | ||||||||||
| 161 | |||||||||||
| 162 | =encoding utf8 | ||||||||||
| 163 | |||||||||||
| 164 | =head1 SEE ALSO | ||||||||||
| 165 | |||||||||||
| 166 | L<CPAN::Repository> | ||||||||||
| 167 | |||||||||||
| 168 | =head1 SUPPORT | ||||||||||
| 169 | |||||||||||
| 170 | IRC | ||||||||||
| 171 | |||||||||||
| 172 | Join #duckduckgo on irc.freenode.net. Highlight Getty for fast reaction :). | ||||||||||
| 173 | |||||||||||
| 174 | Repository | ||||||||||
| 175 | |||||||||||
| 176 | http://github.com/Getty/p5-cpan-repository | ||||||||||
| 177 | Pull request and additional contributors are welcome | ||||||||||
| 178 | |||||||||||
| 179 | Issue Tracker | ||||||||||
| 180 | |||||||||||
| 181 | http://github.com/Getty/p5-cpan-repository/issues | ||||||||||
| 182 | |||||||||||
| 183 | =head1 AUTHOR | ||||||||||
| 184 | |||||||||||
| 185 | Torsten Raudssus <torsten@raudss.us> L<http://raudss.us/> | ||||||||||
| 186 | |||||||||||
| 187 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 188 | |||||||||||
| 189 | This software is copyright (c) 2012 by DuckDuckGo, Inc. L<http://duckduckgo.com/>. | ||||||||||
| 190 | |||||||||||
| 191 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 192 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 193 | |||||||||||
| 194 | =cut | ||||||||||