blib/lib/CPAN/Indexer/Mirror.pm

filter: Statementstmt 100% 71 / 71 Branchbran 55.5% 10 / 18 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 11 totaltotal 82.5% 109 / 132 CC 12 SCAR 11.0
CC
12
Cov
89%
run
8.9
new
3.1
timestamp
2.1
CRAP 3.0
↑ Summary
1 package CPAN::Indexer::Mirror;
2  
3 =pod
4  
5 =head1 NAME
6  
7 CPAN::Indexer::Mirror - Creates the mirror.yml and mirror.json files
8  
9 =head1 SYNOPSIS
10  
11 use CPAN::Indexer::Mirror ();
12
13 CPAN::Indexer::Mirror->new(
14 root => '/cpan/root/directory',
15 )->run;
16  
17 =head1 DESCRIPTION
18  
19 This module is used to implement a small piece of functionality inside the
20 CPAN/PAUSE indexer which generates F<mirror.yml> and F<mirror.json>.
21  
22 These files are used to allow CPAN clients (via the L<Mirror::YAML> or
23 L<Mirror::JSON> modules) to implement mirror validation and automated
24 selection.
25  
26 =head1 METHODS
27  
28 Anyone who needs to know more detail than the SYNOPSIS should read the
29 (fairly straight forward) code.
30  
31 =cut
32  
33 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use File::Remove ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use YAML::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use URI::http ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use IO::AtomicFile ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use Parse::CPAN::MirroredBy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 2 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 $VERSION = '0.05';
47 }
48  
49  
50  
51  
52  
53 #####################################################################
54 # Constructor and Accessor Methods
55  
56 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod undocumented
57 1 my $class = shift;
58 1 my $self = bless { @_ }, $class;
59  
60 # Apply defaults
61 1 ▶ $self->{name} ||= 'Comprehensive Perl Archive Network';
Condition50%1/2
$self->{"name"} ||= "Comprehensive Perl Archive Network"
l!l
01
MC/DC0%0/1
$self->{"name"} ||= "Comprehensive Perl Archive Network"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
62 1 ▶ $self->{master} ||= 'http://www.cpan.org/';
Condition50%1/2
$self->{"master"} ||= "http://www.cpan.org/"
l!l
01
MC/DC0%0/1
$self->{"master"} ||= "http://www.cpan.org/"
$self->{"master"}
Truth table50%1/2
A
A= $self->{"master"}
Aresult
11
00
63  
64 1 return $self;
65 }
66  
67 3 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod undocumented
68 3 $_[0]->{root};
69 }
70  
71 1 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
72 1 $_[0]->{name};
73 }
74  
75 1 ▶sub master {
Subroutine master: called CC=1 SCAR=0.0
Pod undocumented
76 1 $_[0]->{master};
77 }
78  
79 1 ▶sub timestamp {
Subroutine timestamp: called CC=2 SCAR=7.6
Pod undocumented
80 1 ▶ $_[0]->{timestamp} || $_[0]->now;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"timestamp"} or $_[0]->now 0 1 1
81 }
82  
83 1 ▶sub mirrored_by {
Subroutine mirrored_by: called CC=1 SCAR=0.0
Pod undocumented
84 1 File::Spec->catfile( $_[0]->root, 'MIRRORED.BY' );
85 }
86  
87 1 ▶sub mirror_yml {
Subroutine mirror_yml: called CC=1 SCAR=0.0
Pod undocumented
88 1 File::Spec->catfile( $_[0]->root, 'mirror.yml' );
89 }
90  
91 1 ▶sub mirror_json {
Subroutine mirror_json: called CC=1 SCAR=0.0
Pod undocumented
92 1 File::Spec->catfile( $_[0]->root, 'mirror.json' );
93 }
94  
95  
96  
97  
98  
99 #####################################################################
100 # Process Methods
101  
102 1 ▶sub run {
Subroutine run: called CC=8 SCAR=21.9
Pod undocumented
103 1 ▶ my $self = ref $_[0] ? shift : shift->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
104  
105 # Always randomise the mirror order, to protect against
106 # weak programmers on the other end scanning them in
107 # sequential order.
108 1 my @mirrors = sort { rand() <=> rand() }
109 $self->parser->parse_file( $self->mirrored_by );
110  
111 # Generate the data structure for the files
112 1 my $data = {
113 version => '1.0',
114 name => $self->name,
115 master => $self->master,
116 timestamp => $self->timestamp,
117 mirrors => \@mirrors,
118 };
119  
120 # Write the mirror.yml and mirror.json file.
121 # Make sure the closes (and thus commits) are as close together
122 # as we can possibly get them, minimising race conditions.
123 SCOPE: {
124 1 local $!;
125 1 my $yaml_file = $self->mirror_yml;
126 1 my $json_file = $self->mirror_json;
127 1 ▶ my $yaml_fh = IO::AtomicFile->open($yaml_file, "w") or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $yaml_fh = "IO::AtomicFile"->open($yaml_file, "w") or die "open: $!" 1 0 1
128 1 ▶ my $json_fh = IO::AtomicFile->open($json_file, "w") or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $json_fh = "IO::AtomicFile"->open($json_file, "w") or die "open: $!" 1 0 1
129 1 ▶ $yaml_fh->print( YAML::Tiny::Dump($data) ) or die "print: $!";
Branch: 1 branch
BranchTrueFalseTotal
$yaml_fh->print(YAML::Tiny::Dump($data)) or die "print: $!" 1 0 1
130 1 ▶ $json_fh->print( JSON->new->pretty->encode($data) ) or die "print: $!";
Branch: 1 branch
BranchTrueFalseTotal
$json_fh->print("JSON"->new->pretty->encode($data)) or die "print: $!" 1 0 1
131 1 ▶ $yaml_fh->close or die "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
$yaml_fh->close or die "close: $!" 1 0 1
132 1 ▶ $json_fh->close or die "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
$json_fh->close or die "close: $!" 1 0 1
133 }
134  
135 1 return 1;
136 }
137  
138 1 ▶sub parser {
Subroutine parser: called CC=1 SCAR=0.0
Pod undocumented
139 1 my $parser = Parse::CPAN::MirroredBy->new;
140 1 ▶ $parser->add_map( sub { $_[0]->{dst_http} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
141 242 ▶ $parser->add_grep( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
142 242 ▶ defined $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ m[/$] 183 59 242
143 and
144 $_[0] =~ /\/$/
145 1 } );
146 1 ▶ $parser->add_map( sub { URI->new( $_[0], 'http' )->canonical->as_string } );
Subroutine __ANON__: called CC=1 SCAR=0.0
147 1 return $parser;
148 }
149  
150 1 ▶sub now {
Subroutine now: called CC=1 SCAR=0.0
Pod undocumented
151 1 my @t = gmtime time;
152 1 return sprintf( "%04u-%02u-%02uT%02u:%02u:%02uZ",
153 $t[5] + 1900,
154 $t[4] + 1,
155 $t[3],
156 $t[2],
157 $t[1],
158 $t[0],
159 );
160 }
161  
162 2 1;
163  
164 =pod
165  
166 =head1 SUPPORT
167  
168 Bugs should be reported via the CPAN bug tracker at
169  
170 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPAN-Indexer-Mirror>
171  
172 =head1 AUTHOR
173  
174 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
175  
176 =head1 SEE ALSO
177  
178 L<Parse::CPAN::Authors>, L<Parse::CPAN::Packages>,
179 L<Parse::CPAN::Modlist>, L<Parse::CPAN::Meta>,
180 L<Parse::CPAN::MirroredBy>
181  
182 =head1 COPYRIGHT
183  
184 Copyright 2008 Adam Kennedy.
185  
186 This program is free software; you can redistribute
187 it and/or modify it under the same terms as Perl itself.
188  
189 The full text of the license can be found in the
190 LICENSE file included with this module.
191  
192 =cut
↑ Summary