MMM/Mirror.pm

filter: Statementstmt 63.6% 49 / 77 Branchbran 45.8% 11 / 24 Conditioncond 30.0% 9 / 30 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 68.1% 15 / 22 Podpod 100% 16 / 16 totaltotal 54.2% 102 / 188 CC 26 SCAR 22.7
CC
26
Cov
53%
xml_output
30.0
sync_mirror
16.1
same_mirror
12.0
CRAP 9.7
« MMM/Host.pm ↑ Summary MMM/Sync.pm »
1 package MMM::Mirror;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use MMM::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 MMM::Mirror
12  
13 =head1 DESCRIPTION
14  
15 An object to retain per mirror information
16  
17 =head1 METHODS
18  
19 =head2 new
20  
21 Create a MMM::Mirror object from information found in hash passed
22 as arguments.
23  
24 my $mirror MMM::Mirror->new( url => 'rsync://host/foo/' );
25  
26 =cut
27  
28 3 ▶sub _rev {
Subroutine _rev: called CC=1 SCAR=0.0
29 3 strftime( '%Y%m%d%H%M%S', gmtime(time) );
30 }
31  
32 5 ▶sub new {
Subroutine new: called CC=8 SCAR=22.3
Pod documented
33 5 my ( $class, %infos ) = @_;
34 5 ▶ $infos{url} or return;
Branch: 1 branch
BranchTrueFalseTotal
$infos{"url"} or return 4 1 5
35 4 ▶ $infos{uri} ||= URI->new( $infos{url} );
Condition33%1/3
$infos{"uri"} ||= "URI"->new($infos{"url"})
l!l&&r!l&&!r
040
MC/DC0%0/2
$infos{"uri"} ||= "URI"->new($infos{"url"})
$infos{"uri"} "URI"->new($infos{"url"})
Truth table33%1/3
A ||= B
A= $infos{"uri"}B= "URI"->new($infos{"url"})
ABresult
1X1
011
000
36 4 ▶ $infos{uri} && $infos{uri}->can('host') or return;
Branch: 1 branch
BranchTrueFalseTotal
$infos{"uri"} and $infos{"uri"}->can("host") or return 3 1 4
Condition66%2/3
$infos{"uri"} and $infos{"uri"}->can("host")
!ll&&!rl&&r
013
MC/DC50%1/2
$infos{"uri"} and $infos{"uri"}->can("host")
$infos{"uri"} $infos{"uri"}->can("host")
Truth table66%2/3
A and B
A= $infos{"uri"}B= $infos{"uri"}->can("host")
ABresult
0X0
100
111
37 3 my $path = $infos{uri}->path;
38 3 $path =~ s://*:/:g;
39 $infos{url} = sprintf( "%s://%s%s%s",
40 $infos{uri}->scheme, lc( $infos{uri}->authority ),
41 3 ▶ $path, ( $infos{uri}->query ? '?' . $infos{uri}->query : '' ) );
Branch: 1 branch
BranchTrueFalseTotal
$infos{'uri'}->query ? : 0 3 3
42 3 $infos{host} = lc( $infos{uri}->host() );
43 $infos{hostinfo} ||=
44 3 ▶ MMM::Host->new( %infos, hostname => $infos{uri}->host );
Condition33%1/3
$infos{"hostinfo"} ||= "MMM::Host"->new(%infos, "hostname", $infos{"uri"}->host)
l!l&&r!l&&!r
030
MC/DC0%0/2
$infos{"hostinfo"} ||= "MMM::Host"->new(%infos, "hostname", $infos{"uri"}->host)
$infos{"hostinfo"} "MMM::Host"->new(%infos, "hostname", $infos{"uri"}->host)
Truth table33%1/3
A ||= B
A= $infos{"hostinfo"}B= "MMM::Host"->new(%infos, "hostname", $infos{"uri"}->host)
ABresult
1X1
011
000
45 3 ▶ $infos{revision} ||= _rev();
Condition33%1/3
$infos{"revision"} ||= _rev()
l!l&&r!l&&!r
030
MC/DC0%0/2
$infos{"revision"} ||= _rev()
$infos{"revision"} _rev()
Truth table33%1/3
A ||= B
A= $infos{"revision"}B= _rev()
ABresult
1X1
011
000
46 3 bless( {%infos}, $class );
47 }
48  
49 =head2 url
50  
51 Return the url of the mirror
52  
53 =cut
54  
55 1 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod documented
56 1 my ($self) = @_;
57 1 $self->{url};
58 }
59  
60 =head2 host
61  
62 Return the hostname of the mirror found in url
63  
64 =cut
65  
66 0 ▶sub host {
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
67 0 my ($self) = @_;
68 0 return $self->{host};
69 }
70  
71 =head2 level
72  
73 Return the level of this mirror in mirrors hierarchy
74  
75 =cut
76  
77 2 ▶sub level {
Subroutine level: called CC=2 SCAR=7.2
Pod documented
78 2 my ($self) = @_;
79 2 ▶ defined( $self->{level} ) ? $self->{level} : 3;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'level'} ? : 2 0 2
80 }
81  
82 =head2 frequency
83  
84 Period in minutes between sync performed by this mirror
85  
86 =cut
87  
88 2 ▶sub frequency {
Subroutine frequency: called CC=2 SCAR=7.2
Pod documented
89 2 my ($self) = @_;
90 2 ▶ $self->{frequency} || 120;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"frequency"} or 120 2 0 2
91 }
92  
93 =head2 source
94  
95 Return the mirror source name from which this mirror is part of.
96  
97 =cut
98  
99 2 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
100 2 my ($self) = @_;
101 2 $self->{source};
102 }
103  
104 =head2 set_source($source)
105  
106 Set the source name for this mirror.
107  
108 =cut
109  
110 1 ▶sub set_source {
Subroutine set_source: called CC=1 SCAR=0.0
Pod documented
111 1 my ( $self, $source ) = @_;
112 1 $self->{source} = $source;
113 }
114  
115 =head2 hostinfo
116  
117 Return a MMM::Host object proper to the mirror if any
118  
119 =cut
120  
121 2 ▶sub hostinfo {
Subroutine hostinfo: called CC=1 SCAR=0.0
Pod documented
122 2 my ($self) = @_;
123 2 return $self->{hostinfo};
124 }
125  
126 =head2 get_geo
127  
128 Load host geo info if any
129  
130 =cut
131  
132 0 ▶sub get_geo {
Subroutine get_geo: not called CC=2 SCAR=17.9
Pod documented
133 0 my ($self) = @_;
134 0 ▶ if($self->{hostinfo}->get_geo()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'hostinfo'}->get_geo) { } 0 0 0
135 0 return 1;
136 } else {
137 0 return;
138 }
139 }
140  
141 =head2 random
142  
143 Return a cached random value assigned to this mirror.
144  
145 =cut
146  
147 0 ▶sub random {
Subroutine random: not called CC=2 SCAR=17.9
Pod documented
148 0 my ($self) = @_;
149 0 ▶ $self->{random} ||= rand();
Condition0%0/3
$self->{"random"} ||= rand()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"random"} ||= rand()
$self->{"random"} rand()
Truth table0%0/3
A ||= B
A= $self->{"random"}B= rand()
ABresult
1X1
011
000
150 }
151  
152 =head2 info
153  
154 Return textual information about this mirror
155  
156 =cut
157  
158 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
159 0 my ($self) = @_;
160 return
161 0 sprintf( '%s (%d/%d)', $self->{url}, $self->level, $self->frequency );
162 }
163  
164 =head2 revision
165  
166 Return the revision of the entry. The revision is an id to identify if an
167 entry is newer than another for same mirror.
168  
169 =cut
170  
171 5 ▶sub revision {
Subroutine revision: called CC=1 SCAR=0.0
Pod documented
172 5 my ($self) = @_;
173 5 $self->{revision};
174 }
175  
176 =head2 refresh_revision
177  
178 Reset revision to current timestamp
179  
180 =cut
181  
182 0 ▶sub refresh_revision {
Subroutine refresh_revision: not called CC=1 SCAR=6.9
Pod documented
183 0 my ($self) = @_;
184 0 $self->{revision} = _rev();
185 }
186  
187 =head2 same_mirror($mirror)
188  
189 Compare this mirror with another and return 1 if both entries refer to same
190 mirror
191  
192 =cut
193  
194 0 ▶sub same_mirror {
Subroutine same_mirror: not called CC=3 SCAR=24.8
Pod documented
195 0 my ( $self, $mirror ) = @_;
196 0 ▶ if ( $self->host eq $mirror->host && $self->source eq $mirror->source ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->host eq $mirror->host and $self->source eq $mirror->source) 0 0 0
Condition0%0/3
$self->host eq $mirror->host and $self->source eq $mirror->source
!ll&&!rl&&r
000
MC/DC0%0/2
$self->host eq $mirror->host and $self->source eq $mirror->source
$self->host eq $mirror->host $self->source eq $mirror->source
Truth table0%0/3
A and B
A= $self->host eq $mirror->hostB= $self->source eq $mirror->source
ABresult
0X0
100
111
197 0 return 1;
198 }
199 0 return;
200 }
201  
202 =head2 sync_mirror($mirror)
203  
204 Get unknown values from $mirror if defined.
205  
206 =cut
207  
208 1 ▶sub sync_mirror {
Subroutine sync_mirror: called CC=9 SCAR=27.8
Pod documented
209 1 my ( $self, $mirror ) = @_;
210 1 foreach (qw(level frequency)) {
211 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{$_} or defined $mirror->{$_} and $mirror->revision > $self->revision) 1 1 2
Condition50%3/6
not defined $self->{$_} or defined $mirror->{$_} and $mirror->revision > $self->revision
l!l&&r!l&&!r
101
defined $mirror->{$_} and $mirror->revision > $self->revision
!ll&&!rl&&r
010
MC/DC33%1/3
not defined $self->{$_} or defined $mirror->{$_} and $mirror->revision > $self->revision
not defined $self->{$_} defined $mirror->{$_} $mirror->revision > $self->revision
Truth table50%2/4
not(A) or B and C
A= not defined $self->{$_}B= defined $mirror->{$_}C= $mirror->revision > $self->revision
ABCresult
1XX1
0111
00X0
0100
212 ( !defined( $self->{$_} ) )
213 || ( defined( $mirror->{$_} )
214 && $mirror->revision > $self->revision )
215 )
216 {
217 1 $self->{$_} = $mirror->{$_};
218 }
219 }
220 1 ▶ if ( $self->{hostinfo} && $mirror->{hostinfo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'hostinfo'} and $mirror->{'hostinfo'}) { } 1 0 1
Condition33%1/3
$self->{"hostinfo"} and $mirror->{"hostinfo"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"hostinfo"} and $mirror->{"hostinfo"}
$self->{"hostinfo"} $mirror->{"hostinfo"}
Truth table33%1/3
A and B
A= $self->{"hostinfo"}B= $mirror->{"hostinfo"}
ABresult
0X0
100
111
221 1 $self->{hostinfo}->sync_host( $mirror->{hostinfo} );
222 }
223 else {
224 0 ▶ $self->{hostinfo} ||= $mirror->{hostinfo};
Condition0%0/3
$self->{"hostinfo"} ||= $mirror->{"hostinfo"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"hostinfo"} ||= $mirror->{"hostinfo"}
$self->{"hostinfo"} $mirror->{"hostinfo"}
Truth table0%0/3
A ||= B
A= $self->{"hostinfo"}B= $mirror->{"hostinfo"}
ABresult
1X1
011
000
225 }
226  
227 1 ▶ if ( $mirror->revision > $self->revision ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mirror->revision > $self->revision) 0 1 1
228 0 $self->{revision} = $mirror->{revision};
229 }
230 }
231  
232 =head2 xml_output
233  
234 Return a xml string describing this mirror.
235  
236 See also <MMM::MirrorList::xml_output>
237  
238 =cut
239  
240 0 ▶sub xml_output {
Subroutine xml_output: not called CC=5 SCAR=34.0
Pod documented
241 0 my ($self) = @_;
242 0 my $xml = "\t\t<mirror>\n";
243  
244 0 foreach (qw(url level frequency revision)) {
245 0 ▶ if ( defined( $self->{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$_}) 0 0 0
246 0 $xml .= sprintf( "\t\t\t<%s>%s</%s>\n", $_, $self->{$_}, $_ );
247 }
248 }
249 0 foreach (qw(password ssh)) {
250 0 ▶ if ( $self->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$_}) 0 0 0
251 0 $xml .= sprintf( "\t\t\t<%s>%s</%s>\n", $_, $self->{$_}, $_ );
252 }
253 }
254  
255 0 $xml .= "\t\t</mirror>\n";
256  
257 0 $xml;
258 }
259  
260 5 1;
261  
262 =head1 AUTHOR
263  
264 Olivier Thauvin <nanardon@nanardon.zarb.org>
265  
266 =head1 COPYRIGHT AND LICENSE
267  
268 Copyright (C) 2006 Olivier Thauvin
269  
270 This program is free software; you can redistribute it and/or
271 modify it under the terms of the GNU General Public License
272 as published by the Free Software Foundation; either version 2
273 of the License, or (at your option) any later version.
274  
275 This program is distributed in the hope that it will be useful,
276 but WITHOUT ANY WARRANTY; without even the implied warranty of
277 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
278 GNU General Public License for more details.
279  
280 You should have received a copy of the GNU General Public License
281 along with this program; if not, write to the Free Software
282 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
283  
284 =cut
285  
« MMM/Host.pm ↑ Summary MMM/Sync.pm »