Repository.pm

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