File Coverage

lib/Slaughter/Packages/openbsd.pm
Criterion Covered Total %
statement 12 35 34.2
branch 0 12 0.0
condition 1 6 16.6
subroutine 3 7 42.8
pod 5 5 100.0
total 21 65 32.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             =head1 NAME
4            
5             Slaughter::Packages::openbsd - Abstractions for OpenBSD package management.
6            
7             =cut
8              
9             =head1 DESCRIPTION
10            
11             This module contains code for dealing with system packages, using the system
12             commands C<pkg_add>, C<pkg_info>, etc.
13            
14             =cut
15              
16             =head1 METHODS
17            
18             Now follows documentation on the available methods.
19            
20             =cut
21              
22              
23 1     1   1990 use strict;
  1         1  
  1         26  
24 1     1   2 use warnings;
  1         2  
  1         283  
25              
26              
27             package Slaughter::Packages::openbsd;
28              
29              
30             our $VERSION = "3.0.5";
31              
32              
33             =head2 new
34            
35             Create a new instance of this object.
36            
37             =cut
38              
39             sub new
40             {
41 1     1 1 428     my ( $proto, %supplied ) = (@_);
42 1   33     6     my $class = ref($proto) || $proto;
43              
44 1         1     my $self = {};
45              
46             #
47             # Allow user supplied values to override our defaults
48             #
49 1         3     foreach my $key ( keys %supplied )
50                 {
51 0         0         $self->{ lc $key } = $supplied{ $key };
52                 }
53              
54 1         2     bless( $self, $class );
55 1         2     return $self;
56              
57             }
58              
59              
60              
61             =head2 recognised
62            
63             Does the local system match a known type?
64            
65             =cut
66              
67             sub recognised
68             {
69 0     0 1       my ($self) = (@_);
70              
71             #
72             # RPM?
73             #
74 0 0 0           if ( ( -x "/usr/sbin/pkg_add" ) &&
75                      ( -x "/usr/sbin/pkg_info" ) )
76                 {
77 0                   return ("pkg");
78                 }
79              
80 0               return 0;
81             }
82              
83              
84              
85             =head2 isInstalled
86            
87             Is the specified package installed?
88            
89             =cut
90              
91             sub isInstalled
92             {
93 0     0 1       my ( $self, $package ) = (@_);
94              
95             #
96             # Get the type of the system, to make sure we can continue.
97             #
98 0               my $type = $self->recognised();
99 0 0             return 0 unless ($type);
100              
101             #
102             # Found it installed?
103             #
104 0               my $found = 0;
105              
106             #
107             # Output the information about the package - this contains
108             # "inst:" if the package is installed locally.
109             #
110 0 0             open my $handle, "-|", "/usr/sbin/pkg_info '$package' 2>/dev/null" or
111                   die "Failed to run pkg_info: $!";
112              
113 0               while ( my $line = <$handle> )
114                 {
115 0                   chomp($line);
116              
117 0 0                 $found = 1 if ( $line =~ /^Information for inst:/ );
118                 }
119 0               close($handle);
120              
121             #
122             # Return the result.
123             #
124 0               return ($found);
125             }
126              
127              
128              
129             =head2 installPackage
130            
131             Install a package upon the local system.
132            
133             =cut
134              
135             sub installPackage
136             {
137 0     0 1       my ( $self, $package ) = (@_);
138              
139             #
140             # Get the type of the system, to make sure we can continue.
141             #
142 0               my $type = $self->recognised();
143 0 0             return 0 unless ($type);
144              
145             #
146             # Add the package.
147             #
148 0               system( "/usr/sbin/pkg_add", $package );
149             }
150              
151              
152              
153             =head2 removePackage
154            
155             Remove the specified package.
156            
157             =cut
158              
159             sub removePackage
160             {
161 0     0 1       my ( $self, $package ) = (@_);
162              
163             #
164             # Get the type of the system, to make sure we can continue.
165             #
166 0               my $type = $self->recognised();
167 0 0             return 0 unless ($type);
168              
169             #
170             # Remove the package
171             #
172 0               system( "/usr/sbin/pkg_delete", $package );
173             }
174              
175              
176             1;
177              
178              
179              
180             =head1 AUTHOR
181            
182             Steve Kemp <steve@steve.org.uk>
183            
184             =cut
185              
186             =head1 LICENSE
187            
188             Copyright (c) 2010-2015 by Steve Kemp. All rights reserved.
189            
190             This module is free software;
191             you can redistribute it and/or modify it under
192             the same terms as Perl itself.
193             The LICENSE file contains the full text of the license.
194            
195             =cut
196