File Coverage

lib/Slaughter/Info/MSWin32.pm
Criterion Covered Total %
statement 24 45 53.3
branch 4 18 22.2
condition 1 6 16.6
subroutine 4 4 100.0
pod 2 2 100.0
total 35 75 46.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             =head1 NAME
4            
5             Slaughter::Info::MSWin32 - Determine information about a Windows host.
6            
7             =cut
8              
9             =head1 SYNOPSIS
10            
11             This module is the Windows version of the Slaughter information-gathering
12             module.
13            
14             Modules beneath the C<Slaughter::Info> namespace are loaded when slaughter
15             is executed, they are used to populate a hash with information about
16             the current host.
17            
18             This module is loaded only on Windows systems, and will determine such details
19             as the operating system version, the processor type, etc.
20            
21             The correct information-gathering module is loaded at run-time via the use of the C<$^O> variable, and if no system-specific module is available then the generic L<Slaughter::Info::generic> module is used as a fall-back.
22            
23             The information discovered can be dumped by running C<slaughter>
24            
25             =for example begin
26            
27             ~# slaughter --dump
28            
29             =for example end
30            
31             Usage of this module is as follows:
32            
33             =for example begin
34            
35             use Slaughter::Info::MSWin32;
36            
37             my $obj = Slaughter::Info::MSWin32->new();
38             my $data = $obj->getInformation();
39            
40             # use info now ..
41             print $data->{'arch'} . "-bit architecture\n";
42            
43             =for example end
44            
45             When this module is used an attempt is also made to load the module
46             C<Slaughter::Info::Local::MSWin32> - if that succeeds it will be used to
47             augment the information discovered and made available to slaughter
48             policies.
49            
50             =cut
51              
52             =head1 METHODS
53            
54             Now follows documentation on the available methods.
55            
56             =cut
57              
58              
59 1     1   1077 use strict;
  1         2  
  1         29  
60 1     1   3 use warnings;
  1         2  
  1         354  
61              
62              
63             package Slaughter::Info::MSWin32;
64              
65              
66             #
67             # The version of our release.
68             #
69             our $VERSION = "3.0.5";
70              
71              
72              
73             =head2 new
74            
75             Create a new instance of this object.
76            
77             =cut
78              
79             sub new
80             {
81 1     1 1 446     my ( $proto, %supplied ) = (@_);
82 1   33     6     my $class = ref($proto) || $proto;
83              
84 1         1     my $self = {};
85 1         2     bless( $self, $class );
86 1         2     return $self;
87              
88             }
89              
90              
91              
92             =head2 getInformation
93            
94             This function retrieves meta-information about the current host.
95            
96             The return value is a hash-reference of data determined dynamically.
97            
98            
99             B<NOTE> This module has only been tested under Strawberry perl.
100            
101             =cut
102              
103             sub getInformation
104             {
105 1     1 1 655     my ($self) = (@_);
106              
107             #
108             # The data we will return
109             #
110 1         1     my $ref;
111              
112             #
113             # Kernel version.
114             #
115 1         2     $ref->{ 'kernel' } = $ENV{ 'OS' };
116 1 50       4     chomp( $ref->{ 'kernel' } ) if ( $ref->{ 'kernel' } );
117              
118             #
119             # Are we i386/amd64?
120             #
121 1         1     my $type = $ENV{ 'PROCESSOR_ARCHITECTURE' };
122 1 50       3     if ($type)
123                 {
124 0 0       0         if ( $type =~ /x86/i )
125                     {
126 0         0             $ref->{ 'arch' } = "i386";
127 0         0             $ref->{ 'bits' } = 32;
128                     }
129                     else
130                     {
131 0         0             $ref->{ 'arch' } = "amd64";
132 0         0             $ref->{ 'bits' } = 64;
133                     }
134                 }
135                 else
136                 {
137 1         1         $ref->{ 'arch' } = "unknown";
138 1         2         $ref->{ 'bits' } = 0;
139                 }
140              
141             #
142             # This should be portable.
143             #
144 1         1     $ref->{ 'path' } = $ENV{ 'PATH' };
145              
146             #
147             # IP address(es).
148             #
149 1         1     my $ip = "ipconfig";
150              
151             #
152             # This if-test should always succeed, or this module wouldn't be loaded
153             # for real.
154             #
155             # It is present to skip this section of code when running the test-suite
156             # on a GNU/Linux host.
157             #
158             #
159 1 50       3     if ( $^O =~ /win32/i )
160                 {
161 0         0         my $count = 1;
162              
163 0         0         foreach my $line ( split( /[\r\n]/, `$ip` ) )
164                     {
165 0 0 0     0             next if ( !defined($line) || !length($line) );
166 0         0             chomp($line);
167              
168             #
169             # This matches something like:
170             #
171             # IP Address. . . . . . . . . . . . : 10.6.11.138
172             #
173             #
174 0 0       0             if ( $line =~ /IP Address.* : (.*)/ )
175                         {
176 0         0                 my $ip = $1;
177              
178             #
179             # Save away the IP address in "ip0", "ip1", "ip2" .. etc.
180             #
181 0         0                 $ref->{ "ip" . $count } = $ip;
182 0         0                 $count += 1;
183                         }
184                     }
185              
186 0 0       0         if ( $count > 0 )
187                     {
188 0         0             $ref->{ 'ipcount' } = ( $count - 1 );
189                     }
190                 }
191              
192              
193             #
194             # Find the name of our release.
195             #
196             # This if-test should always succeed, or this module wouldn't be loaded
197             # for real.
198             #
199             # It is present to skip this section of code when running the test-suite
200             # on a GNU/Linux host.
201             #
202 1 50       2     if ( $^O =~ /win32/i )
203                 {
204 0         0         my @win_info = Win32::GetOSVersion();
205 0         0         my $version = $win_info[0];
206 0         0         my $distrib = Win32::GetOSName();
207              
208             # work around for historical reasons
209 0 0       0         $distrib = 'WinXP' if $distrib =~ /^WinXP/;
210 0         0         $ref->{ 'version' } = $version;
211 0         0         $ref->{ 'distribution' } = $distrib;
212                 }
213              
214             #
215             # Return the data
216             #
217 1         3     return ($ref);
218             }
219              
220              
221              
222             1;
223              
224              
225             =head1 AUTHOR
226            
227             Steve Kemp <steve@steve.org.uk>
228            
229             =cut
230              
231             =head1 LICENSE
232            
233             Copyright (c) 2010-2015 by Steve Kemp. All rights reserved.
234            
235             This module is free software;
236             you can redistribute it and/or modify it under
237             the same terms as Perl itself.
238             The LICENSE file contains the full text of the license.
239            
240             =cut
241