Repository.pm

filter: Statementstmt 87.7% 79 / 90 Branchbran 68.7% 11 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 84.6% 22 / 26 Podpod 0.0% 0 / 13 totaltotal 76.3% 113 / 148 SCAR 24.4
CC
11
Cov
84%
mkdir
8.7
add_author_distribution
4.0
initialize
2.0
CRAP 11.5
« Repository/Packages.pm ↑ Summary Repository/Role/File.pm »
1 package CPAN::Repository;
2 2 our $AUTHORITY = 'cpan:GETTY';
3 # ABSTRACT: API to access a directory which can be served as CPAN repository
4  
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::Path qw( make_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Spec::Functions ':ALL';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use CPAN::Repository::Mailrc;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use CPAN::Repository::Packages;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use CPAN::Repository::Perms;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶our $VERSION ||= '0.0development';
Condition50%1/2
our $VERSION ||= "0.0development"
l!l
02
MC/DC0%0/1
our $VERSION ||= "0.0development"
our $VERSION
Truth table50%1/2
A
A= our $VERSION
Aresult
11
00
14  
15 2 has dir => (
16 is => 'ro',
17 required => 1,
18 );
19  
20 2 has real_dir => (
21 is => 'ro',
22 lazy => 1,
23 builder => '_build_real_dir',
24 );
25  
26 2 ▶sub _build_real_dir { catdir(splitdir(shift->dir)) }
Subroutine _build_real_dir: called CC=1 SCAR=0.0
27  
28 9 ▶sub splitted_dir { splitdir(shift->real_dir) }
Subroutine splitted_dir: called CC=1 SCAR=0.0
Pod undocumented
29  
30 2 has url => (
31 is => 'ro',
32 lazy => 1,
33 builder => '_build_url',
34 );
35  
36 0 ▶sub _build_url { 'http://cpan.perl.org/' }
Subroutine _build_url: not called CC=1 SCAR=6.9
37  
38 2 has written_by => (
39 is => 'ro',
40 lazy => 1,
41 builder => '_build_written_by',
42 );
43  
44 0 ▶sub _build_written_by { (ref shift).' '.$VERSION }
Subroutine _build_written_by: not called CC=1 SCAR=6.9
45  
46 2 has mailrc => (
47 is => 'ro',
48 lazy => 1,
49 builder => '_build_mailrc',
50 handles => [qw(
51 get_alias
52 )],
53 );
54  
55 sub _build_mailrc {
56 2 ▶ my ( $self ) = @_;
Subroutine _build_mailrc: called CC=1 SCAR=0.0
57 2 return CPAN::Repository::Mailrc->new({
58 repository_root => $self->real_dir,
59 });
60 }
61  
62 2 has perms => (
63 is => 'ro',
64 lazy => 1,
65 builder => '_build_perms',
66 handles => [qw(
67 get_perms
68 get_perms_by_userid
69 )],
70 );
71  
72 sub _build_perms {
73 1 ▶ my ( $self ) = @_;
Subroutine _build_perms: called CC=1 SCAR=0.0
74 1 return CPAN::Repository::Perms->new({
75 repository_root => $self->real_dir,
76 written_by => $self->written_by,
77 });
78 }
79  
80  
81 2 has packages => (
82 is => 'ro',
83 lazy => 1,
84 builder => '_build_packages',
85 handles => [qw(
86 get_module
87 get_module_version
88 )],
89 );
90  
91 sub _build_packages {
92 2 ▶ my ( $self ) = @_;
Subroutine _build_packages: called CC=1 SCAR=0.0
93 2 return CPAN::Repository::Packages->new({
94 repository_root => $self->real_dir,
95 url => $self->url,
96 written_by => $self->written_by,
97 authorbase_path_parts => [$self->authorbase_path_parts],
98 });
99 }
100  
101 sub is_initialized {
102 4 ▶ my ( $self ) = @_;
Subroutine is_initialized: called CC=2 SCAR=6.9
Pod undocumented
103 4 ▶ $self->mailrc->exist && $self->packages->exist;
Branch: 1 branch
BranchTrueFalseTotal
$self->mailrc->exist and $self->packages->exist 2 2 4
104 }
105  
106 sub initialize {
107 1 ▶ my ( $self ) = @_;
Subroutine initialize: called CC=2 SCAR=7.0
Pod undocumented
108 1 ▶ die "there exist already a repository at ".$self->real_dir if $self->is_initialized;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_initialized 0 1 1
109 1 $self->mailrc->save;
110 1 $self->packages->save;
111 1 $self->perms->save;
112 }
113  
114 sub add_author_distribution {
115 3 ▶ my ( $self, $author, $distribution_filename, $path ) = @_;
Subroutine add_author_distribution: called CC=4 SCAR=13.9
Pod undocumented
116 3 my @fileparts = splitdir( $distribution_filename );
117 3 my $filename = pop(@fileparts);
118 3 my $author_path_filename;
119 3 my $target_dir = $self->mkauthordir($author);
120 3 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 1 2 3
121 1 my $path_dir = catfile( $self->splitted_dir, $self->authorbase_path_parts, $path );
122 1 ▶ $self->mkdir( $path_dir ) unless -d $path_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path_dir 1 0 1
123 1 $author_path_filename = catfile( $path, $filename );
124 } else {
125 2 $author_path_filename = catfile( $self->author_path_parts($author), $filename );
126 }
127 3 copy($distribution_filename,catfile( $self->splitted_dir, $self->authorbase_path_parts, $author_path_filename ));
128 3 $self->packages->add_distribution($author_path_filename)->save;
129 3 ▶ $self->mailrc->set_alias($author)->save unless defined $self->mailrc->aliases->{$author};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->mailrc->aliases->{$author} 2 1 3
130 3 return catfile( $self->authorbase_path_parts, $self->author_path_parts($author), $filename );
131 }
132  
133 sub set_perms {
134 0 ▶ my $self = shift;
Subroutine set_perms: not called CC=1 SCAR=6.9
Pod undocumented
135 0 $self->perms->set_perms(@_)->save;
136 }
137  
138 sub set_alias {
139 0 ▶ my ( $self, $author, $alias ) = @_;
Subroutine set_alias: not called CC=1 SCAR=6.9
Pod undocumented
140 0 $self->mailrc->set_alias($author,$alias)->save;
141 }
142  
143 sub mkauthordir {
144 3 ▶ my ( $self, $author ) = @_;
Subroutine mkauthordir: called CC=2 SCAR=6.9
Pod undocumented
145 3 my $authordir = $self->authordir($author);
146 3 ▶ $self->mkdir( $authordir ) unless -d $authordir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $authordir 2 1 3
147 3 return $authordir;
148 }
149  
150 sub author_path_parts {
151 8 ▶ my ( $self, $author ) = @_;
Subroutine author_path_parts: called CC=1 SCAR=0.0
Pod undocumented
152 8 return substr( $author, 0, 1 ), substr( $author, 0, 2 ), $author;
153 }
154  
155 14 ▶sub authorbase_path_parts { 'authors', 'id' }
Subroutine authorbase_path_parts: called CC=1 SCAR=0.0
Pod undocumented
156  
157 sub authordir {
158 3 ▶ my ( $self, $author ) = @_;
Subroutine authordir: called CC=1 SCAR=0.0
Pod undocumented
159 3 return catdir( $self->splitted_dir, $self->authorbase_path_parts, $self->author_path_parts($author) );
160 }
161  
162 sub modules {
163 1 ▶ my ( $self ) = @_;
Subroutine modules: called CC=2 SCAR=6.9
Pod undocumented
164 1 my %modules;
165 1 for (keys %{$self->packages->modules}) {
166 2 $modules{$_} = catfile( $self->splitted_dir, $self->authorbase_path_parts, splitdir( $self->packages->modules->{$_}->[1] ) );
167 }
168 1 return \%modules;
169 }
170  
171 1 ▶sub timestamp { shift->packages->timestamp }
Subroutine timestamp: called CC=1 SCAR=0.0
Pod undocumented
172  
173 #
174 # Utilities
175 #
176  
177 sub mkdir {
178 3 ▶ my ( $self, @path ) = @_;
Subroutine mkdir: called CC=4 SCAR=21.7
Pod undocumented
179 3 make_path(catdir(@path),{ error => \my $err });
180 3 ▶ if (@$err) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$err) 0 3 3
181 0 for my $diag (@$err) {
182 0 my ($file, $message) = %$diag;
183 0 ▶ if ($file eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '') { } 0 0 0
184 0 die "general error: $message\n";
185 } else {
186 0 die "problem making path $file: $message\n";
187 }
188 }
189 }
190 }
191  
192 2 1;
193  
194 __END__
195  
196 =pod
197  
198 =head1 NAME
199  
200 CPAN::Repository - API to access a directory which can be served as CPAN repository
201  
202 =head1 VERSION
203  
204 version 0.009
205  
206 =head1 SYNOPSIS
207  
208 use CPAN::Repository;
209  
210 my $repo = CPAN::Repository->new({
211 dir => '/var/www/greypan.company.org/htdocs/',
212 url => 'http://greypan.company.org/',
213 });
214
215 $repo->initialize unless $repo->is_initialized;
216
217 $repo->add_author_distribution('AUTHOR','My-Distribution-0.001.tar.gz');
218 $repo->add_author_distribution('AUTHOR2','Other-Dist-0.001.tar.gz','Custom/Own/Path');
219 $repo->set_alias('AUTHOR','The author <author@company.org>');
220 $repo->set_alias('AUTHOR2','The other author <author@company.org>');
221
222 my %modules = %{$repo->modules};
223
224 my $fullpath_to_authordir = $repo->authordir('SOMEONE');
225
226 my $packages = $repo->packages; # gives back a CPAN::Repository::Packages
227 my $mailrc = $repo->mailrc; # gives back a CPAN::Repository::Mailrc
228  
229 =head1 DESCRIPTION
230  
231 This module is made for representing a directory which can be used as own CPAN for modules, so it can be a GreyPAN, a DarkPAN or even can be
232 used to manage a mirror of real CPAN your own way. Some code parts are taken from CPAN::Dark of B<CHROMATIC> and L<CPAN::Mini::Inject> of B<MITHALDU>.
233  
234 =encoding utf8
235  
236 =head1 SEE ALSO
237  
238 L<CPAN::Repository::Packages>
239  
240 L<CPAN::Repository::Mailrc>
241  
242 =head1 SUPPORT
243  
244 IRC
245  
246 Join #duckduckgo on irc.freenode.net. Highlight Getty for fast reaction :).
247  
248 Repository
249  
250 http://github.com/Getty/p5-cpan-repository
251 Pull request and additional contributors are welcome
252  
253 Issue Tracker
254  
255 http://github.com/Getty/p5-cpan-repository/issues
256  
257 =head1 AUTHOR
258  
259 Torsten Raudssus <torsten@raudss.us> L<http://raudss.us/>
260  
261 =head1 COPYRIGHT AND LICENSE
262  
263 This software is copyright (c) 2012 by DuckDuckGo, Inc. L<http://duckduckgo.com/>.
264  
265 This is free software; you can redistribute it and/or modify it under
266 the same terms as the Perl 5 programming language system itself.
267  
268 =cut
« Repository/Packages.pm ↑ Summary Repository/Role/File.pm »