OpenInteract2/Repository.pm

filter: Statementstmt 19.7% 36 / 182 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 32.2% 10 / 31 Podpod 20.0% 2 / 10 totaltotal 15.3% 48 / 313 CC 44 SCAR 36.1
CC
44
Cov
14%
_save_repository
110.0
new
90.0
add_package
42.0
CRAP 37.0
« OpenInteract2/Action/CommonDisplay.pm ↑ Summary HTTP/Daemon/OpenInteract2.pm »
1 package OpenInteract2::Repository;
2  
3 # $Id: Repository.pm,v 1.25 2005/03/17 14:57:58 sjn Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( Exporter Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 87 ▶use OpenInteract2::Package;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 #use Scalar::Util qw( blessed );
15  
16 87 $OpenInteract2::Repository::VERSION = sprintf("%d.%02d", q$Revision: 1.25 $ =~ /(\d+)\.(\d+)/);
17 87 @OpenInteract2::Repository::EXPORT_OK = qw( REPOSITORY_FILE );
18  
19 87 ▶use constant REPOSITORY_FILE => 'repository.ini';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 87 my @FIELDS = qw( config_dir package_dir repository_file );
22 87 __PACKAGE__->mk_accessors( @FIELDS );
23  
24 87 my ( $log );
25  
26 ########################################
27 # CONSTRUCTOR
28  
29 # Open up the repository, using the OpenInteract2::Config::Bootstrap object
30 # or a specified website directory. Can also open up from a separate
31 # file if specified.
32  
33 # The properties 'config_dir' and 'package_dir' should be defined in
34 # the created object no matter what
35  
36 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
37 0 my ( $class, $item ) = @_;
38 0 my $self = bless( { _package_info => [],
39 _package_cache => {} }, $class );
40  
41 0 my $repository_file = REPOSITORY_FILE;
42 0 my $typeof = ref $item;
43 0 ▶ if ( $typeof eq 'OpenInteract2::Config::Bootstrap' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($typeof eq 'OpenInteract2::Config::Bootstrap') { } 0 0 0
elsif ($typeof eq 'HASH') { } 0 0 0
elsif ($item) { } 0 0 0
44 0 $self->website_dir( $item->website_dir );
45 0 $self->config_dir( $item->config_dir );
46 0 $self->package_dir( $item->package_dir );
47 }
48 elsif ( $typeof eq 'HASH' ) {
49 0 ▶ $item->{config_dir} ||= 'conf';
Condition0%0/2
$item->{"config_dir"} ||= "conf"
l!l
00
MC/DC0%0/1
$item->{"config_dir"} ||= "conf"
$item->{"config_dir"}
Truth table0%0/2
A
A= $item->{"config_dir"}
Aresult
11
00
50 0 $self->config_dir( $item->{config_dir} );
51 0 ▶ $item->{package_dir} ||= 'pkg';
Condition0%0/2
$item->{"package_dir"} ||= "pkg"
l!l
00
MC/DC0%0/1
$item->{"package_dir"} ||= "pkg"
$item->{"package_dir"}
Truth table0%0/2
A
A= $item->{"package_dir"}
Aresult
11
00
52 0 $self->package_dir( $item->{package_dir} );
53 0 $self->website_dir( $item->{website_dir} );
54 0 ▶ if ( $item->{repository_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"repository_file"}) 0 0 0
55 0 $repository_file = $item->{repository_file};
56 }
57 }
58 elsif ( $item ) {
59 0 oi_error "Argument passed to new() was not used since it was ",
60 "not of the type that I expected. (Got: $typeof)";
61 }
62 else {
63 0 $self->config_dir( 'conf' );
64 0 $self->package_dir( 'pkg' );
65 }
66 0 ▶ if ( $self->website_dir and $self->config_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->website_dir and $self->config_dir) 0 0 0
Condition0%0/3
$self->website_dir and $self->config_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$self->website_dir and $self->config_dir
$self->website_dir $self->config_dir
Truth table0%0/3
A and B
A= $self->website_dirB= $self->config_dir
ABresult
0X0
100
111
67 0 $self->_create_repository_filename( $repository_file );
68 0 $self->_read_repository;
69 }
70 0 return $self;
71 }
72  
73  
74 0 ▶sub full_config_dir {
Subroutine full_config_dir: not called CC=1 SCAR=6.9
Pod undocumented
75 0 my ( $self ) = @_;
76 0 return catfile( $self->website_dir, $self->config_dir );
77 }
78  
79  
80 0 ▶sub full_package_dir {
Subroutine full_package_dir: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my ( $self ) = @_;
82 0 return catfile( $self->website_dir, $self->package_dir );
83 }
84  
85 0 ▶sub website_dir {
Subroutine website_dir: not called CC=3 SCAR=24.8
Pod documented
86 0 my ( $self, $website_dir ) = @_;
87 0 ▶ if ( $website_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($website_dir) 0 0 0
88 0 $self->{_website_dir} = $website_dir;
89 0 ▶ $self->_create_repository_filename if ( $self->config_dir );
Branch: 1 branch
BranchTrueFalseTotal
if $self->config_dir 0 0 0
90 # $self->_clear_package_info;
91 # $self->_read_repository;
92 }
93 0 return $self->{_website_dir};
94 }
95  
96 0 ▶sub _create_repository_filename {
Subroutine _create_repository_filename: not called CC=2 SCAR=17.9
97 0 my ( $self, $base_file ) = @_;
98 0 ▶ $base_file ||= REPOSITORY_FILE;
Condition0%0/2
$base_file ||= "repository.ini"
l!l
00
MC/DC0%0/1
$base_file ||= "repository.ini"
$base_file
Truth table0%0/2
A
A= $base_file
Aresult
11
00
99 0 $self->repository_file( catfile( $self->website_dir,
100 $self->config_dir,
101 $base_file ) );
102 }
103  
104 ########################################
105 # PACKAGE REFERENCE MANIPULATION
106  
107 # TODO: Is this ->installed_date() correct? Is that the only piece of
108 # information the repository maintains about the package outside the
109 # actual package?
110  
111 0 ▶sub fetch_package {
Subroutine fetch_package: not called CC=5 SCAR=34.0
Pod undocumented
112 0 my ( $self, $name ) = @_;
113 0 ▶ unless ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
114 0 oi_error "Must pass in package name to fetch a package from the repository";
115 }
116 0 ▶ if ( my $pkg = $self->_package_cache->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pkg = $self->_package_cache->{$name}) 0 0 0
117 0 return $pkg;
118 }
119 0 foreach my $pkg_info ( @{ $self->_package_info } ) {
120 0 ▶ if ( $pkg_info->{name} eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg_info->{"name"} eq $name) 0 0 0
121 my $pkg = OpenInteract2::Package->new({
122 directory => $pkg_info->{directory},
123 0 repository => $self,
124 });
125 0 $pkg->installed_date( $pkg_info->{installed} );;
126 0 $self->_add_package_cache( $pkg );
127 0 return $pkg;
128 }
129 }
130 0 return undef;
131 }
132  
133 0 ▶sub get_package_info {
Subroutine get_package_info: not called CC=4 SCAR=30.0
Pod undocumented
134 0 my ( $self, $name ) = @_;
135 0 ▶ return undef unless ( $name );
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
136 0 foreach my $pkg_info ( @{ $self->_package_info } ) {
137 0 ▶ if ( $pkg_info->{name} eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg_info->{"name"} eq $name) 0 0 0
138 return {
139 name => $name,
140 version => $pkg_info->{version},
141 directory => $pkg_info->{directory},
142 0 };
143 }
144 }
145 0 return undef;
146 }
147  
148  
149 0 ▶sub fetch_all_packages {
Subroutine fetch_all_packages: not called CC=2 SCAR=17.9
Pod undocumented
150 0 my ( $self ) = @_;
151 0 my @packages = ();
152 0 foreach my $pkg_info ( @{ $self->_package_info } ) {
153 0 my $pkg = $self->fetch_package( $pkg_info->{name} );
154 0 push @packages, $pkg;
155 }
156 0 return \@packages;
157 }
158  
159  
160 0 ▶sub add_package {
Subroutine add_package: not called CC=6 SCAR=37.4
Pod undocumented
161 0 my ( $self, $package, $options ) = @_;
162 # TODO: Re-enable this when we can test (when Test::MockObject supports isa()
163 # unless ( blessed $package and $package->isa( 'OpenInteract2::Package' ) ) {
164 # oi_error "Must pass in a package object to add";
165 # }
166  
167 0 ▶ if ( my $old_info = $self->get_package_info( $package->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old_info = $self->get_package_info($package->name)) 0 0 0
168 0 $self->_remove_package_info( $old_info->{name}, { transient => 'yes' } );
169 0 $self->_remove_package_cache( $old_info->{name} );
170 }
171 0 ▶ my %new_info = (
Condition0%0/2
$package->installed_date || scalar localtime
l!l
00
MC/DC0%0/1
$package->installed_date || scalar localtime
$package->installed_date
Truth table0%0/2
A
A= $package->installed_date
Aresult
11
00
172 name => $package->name,
173 version => $package->version,
174 directory => $package->directory,
175 installed => $package->installed_date || scalar localtime
176 );
177 0 $self->_add_package_info( \%new_info );
178 0 ▶ unless ( $options->{transient} and $options->{transient} eq 'yes' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"transient"} and $options->{"transient"} eq "yes") 0 0 0
Condition0%0/3
$options->{"transient"} and $options->{"transient"} eq "yes"
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"transient"} and $options->{"transient"} eq "yes"
$options->{"transient"} $options->{"transient"} eq "yes"
Truth table0%0/3
A and B
A= $options->{"transient"}B= $options->{"transient"} eq "yes"
ABresult
0X0
100
111
179 0 eval { $self->_save_repository };
180 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
181 0 $self->_remove_package_info( $package->name );
182 0 oi_error "Could not save repository with new information: $@";
183 }
184 }
185 0 $self->_add_package_cache( $package );
186 0 return $self;
187 }
188  
189  
190 0 ▶sub remove_package {
Subroutine remove_package: not called CC=5 SCAR=34.0
Pod undocumented
191 0 my ( $self, $package, $options ) = @_;
192 0 ▶ return unless ( $package );
Branch: 1 branch
BranchTrueFalseTotal
unless $package 0 0 0
193 0 $self->_remove_package_info( $package->{name} );
194 0 ▶ unless ( $options->{transient} && 'yes' eq $options->{transient} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"transient"} and "yes" eq $options->{"transient"}) 0 0 0
Condition0%0/3
$options->{"transient"} and "yes" eq $options->{"transient"}
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"transient"} and "yes" eq $options->{"transient"}
$options->{"transient"} "yes" eq $options->{"transient"}
Truth table0%0/3
A and B
A= $options->{"transient"}B= "yes" eq $options->{"transient"}
ABresult
0X0
100
111
195 0 eval { $self->_save_repository };
196 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
197 0 $self->_add_package_info( $package );
198 0 oi_error "Could not save repository with new information: $@";
199 }
200 }
201 0 $self->_remove_package_cache( $package->name );
202 0 return $self;
203 }
204  
205  
206 0 ▶sub _package_cache {
Subroutine _package_cache: not called CC=1 SCAR=6.9
207 0 my ( $self ) = @_;
208 0 return $self->{_package_cache};
209 }
210  
211 0 ▶sub _clear_package_cache {
Subroutine _clear_package_cache: not called CC=1 SCAR=6.9
212 0 my ( $self ) = @_;
213 0 $self->{_package_cache} = {};
214 }
215  
216 0 ▶sub _add_package_cache {
Subroutine _add_package_cache: not called CC=1 SCAR=6.9
217 0 my ( $self, $pkg ) = @_;
218 0 $self->{_package_cache}{ $pkg->name } = $pkg;
219 }
220  
221 0 ▶sub _remove_package_cache {
Subroutine _remove_package_cache: not called CC=1 SCAR=6.9
222 0 my ( $self, $name ) = @_;
223 0 delete $self->{_package_cache}{ $name };
224 }
225  
226 0 ▶sub _package_info {
Subroutine _package_info: not called CC=1 SCAR=6.9
227 0 my ( $self ) = @_;
228 0 return $self->{_package_info};
229 }
230  
231 0 ▶sub _clear_package_info {
Subroutine _clear_package_info: not called CC=1 SCAR=6.9
232 0 my ( $self ) = @_;
233 0 $self->{_package_info} = [];
234 }
235  
236 0 ▶sub _add_package_info {
Subroutine _add_package_info: not called CC=1 SCAR=6.9
237 0 my ( $self, $info ) = @_;
238 0 push @{ $self->{_package_info} }, $info;
239 }
240  
241 0 ▶sub _remove_package_info {
Subroutine _remove_package_info: not called CC=3 SCAR=24.8
242 0 my ( $self, $name ) = @_;
243 0 my @keep = ();
244 0 for ( @{ $self->{_package_info} } ) {
245 0 ▶ push @keep, $_ unless ( $_->{name} eq $name );
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"name"} eq $name 0 0 0
246 }
247 0 $self->{_package_info} = \@keep;
248 }
249  
250  
251 ########################################
252 # I/O
253  
254 0 ▶sub find_file {
Subroutine find_file: not called CC=3 SCAR=24.8
Pod undocumented
255 0 my ( $self, $package_name, @files ) = @_;
256 0 ▶ unless ( $package_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package_name) 0 0 0
257 0 oi_error "Must supply package name to repository to find a file.";
258 }
259 0 my $package = $self->fetch_package( $package_name );
260 0 ▶ unless ( $package ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package) 0 0 0
261 0 oi_error "No package found with name '$package_name'";
262 }
263 0 return $package->find_file( @files );
264 }
265  
266 0 ▶sub _read_repository {
Subroutine _read_repository: not called CC=3 SCAR=24.8
267 0 my ( $self ) = @_;
268 0 my $ini_file = $self->repository_file;
269 0 ▶ unless ( -f $ini_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $ini_file) 0 0 0
270 0 oi_error "Cannot read repository because file '$ini_file' ",
271 "does not exist";
272 }
273 0 my $ini = OpenInteract2::Config::IniFile->read_config({
274 filename => $ini_file
275 });
276 0 foreach my $package_name ( $ini->sections ) {
277 0 my $info = $ini->{ $package_name };
278 0 $info->{name} = $package_name;
279 0 $self->_add_package_info( $info );
280 }
281 0 return $self;
282 }
283  
284  
285 0 ▶sub _save_repository {
Subroutine _save_repository: not called CC=10 SCAR=47.0
286 0 my ( $self ) = @_;
287 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
288  
289 0 my $ini_file = $self->repository_file;
290 0 my $tmp_ini_file = "$ini_file.tmp";
291 0 ▶ if ( -f $tmp_ini_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $tmp_ini_file) 0 0 0
292 0 ▶ unlink( $tmp_ini_file ) ||
Branch: 1 branch
BranchTrueFalseTotal
unlink $tmp_ini_file or $log->warn("Failed to remove old tmp file; will overwrite. ", "Error: $!") 0 0 0
293 $log->warn( "Failed to remove old tmp file; will overwrite. ",
294 "Error: $!" );
295 }
296 0 my $ini = OpenInteract2::Config::IniFile->ini_factory();
297 0 foreach my $pkg_info ( @{ $self->_package_info } ) {
298 $ini->set( $pkg_info->{name},
299 0 version => $pkg_info->{version} );
300 $ini->set( $pkg_info->{name},
301 0 installed => $pkg_info->{installed} );
302 $ini->set( $pkg_info->{name},
303 0 directory => $pkg_info->{directory} );
304 }
305 0 eval { $ini->write_file( $tmp_ini_file ) };
306 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
307 0 ▶ unlink( $tmp_ini_file ) ||
Branch: 1 branch
BranchTrueFalseTotal
unlink $tmp_ini_file or $log->error("Failed to remove tmp file after save ", "failure: $!") 0 0 0
308 $log->error( "Failed to remove tmp file after save ",
309 "failure: $!" );
310 0 oi_error "Failed to write new repository. Error: $@. ",
311 "Existing repository untouched.";
312 }
313  
314 # We only do this if we're modifying an existing repository
315  
316 0 ▶ if ( -f $ini_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $ini_file) 0 0 0
317 0 my $old_ini_file = "$ini_file.old";
318 0 ▶ rename( $ini_file, $old_ini_file )
Branch: 1 branch
BranchTrueFalseTotal
rename $ini_file, $old_ini_file or oi_error("New repository saved, but cannot rename ", "old repository to make space for the new ", "repository: $@") 0 0 0
319 || oi_error "New repository saved, but cannot rename ",
320 "old repository to make space for the new ",
321 "repository: $@";
322 }
323 0 ▶ rename( $tmp_ini_file, $ini_file )
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp_ini_file, $ini_file or oi_error("New repository saved and old repository ", "renamed, but cannot rename new repository ", "to the proper file. THIS MEANS YOU DO NOT ", "HAVE A VALID REPOSITORY FILE. Please rename ", "by hand the file '${tmp_ini_file}' to ", "'${ini_file}' and the system should function ", "ok. Renaming error: $!") 0 0 0
324 || oi_error "New repository saved and old repository ",
325 "renamed, but cannot rename new repository ",
326 "to the proper file. THIS MEANS YOU DO NOT ",
327 "HAVE A VALID REPOSITORY FILE. Please rename ",
328 "by hand the file '$tmp_ini_file' to ",
329 "'$ini_file' and the system should function ",
330 "ok. Renaming error: $!";
331 0 return $ini_file;
332 }
333  
334  
335 87 1;
336  
337 __END__
« OpenInteract2/Action/CommonDisplay.pm ↑ Summary HTTP/Daemon/OpenInteract2.pm »