Util/OS.pm

filter: Statementstmt 91.5% 54 / 59 Branchbran 61.1% 11 / 18 Conditioncond 83.3% 10 / 12 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 9 / 9 totaltotal 86.2% 100 / 116 CC 18 SCAR 14.9
CC
18
Cov
84%
ipc_run_c
8.0
ipc_run_e
6.1
is_linux
2.3
CRAP 4.4
« Util/Backup.pm ↑ Summary Util/Sem.pm »
1 package Dev::Util::OS;
2  
3 3 ▶use Dev::Util::Syntax;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use IPC::Cmd qw[can_run run];
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = version->declare("v2.19.42");
9  
10 3 our @EXPORT_OK = qw(
11 get_os
12 get_hostname
13 is_linux
14 is_mac
15 is_freebsd
16 is_openbsd
17 is_sunos
18 ipc_run_c
19 ipc_run_e
20 );
21  
22 3 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
23  
24 10 ▶sub get_os {
Subroutine get_os: called CC=1 SCAR=0.0
Pod documented
25 10 my $OS = qx(uname -s);
26 10 chomp $OS;
27  
28 10 return $OS;
29 }
30  
31 1 ▶sub get_hostname {
Subroutine get_hostname: called CC=1 SCAR=0.0
Pod documented
32 1 my $host = qx(uname -n);
33 1 chomp $host;
34  
35 1 return $host;
36 }
37  
38 2 ▶sub is_linux {
Subroutine is_linux: called CC=2 SCAR=8.1
Pod documented
39 2 ▶ if ( get_os() eq "Linux" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_os() eq 'Linux') { } 2 0 2
40 2 return 1;
41 }
42 else {
43 0 return 0;
44 }
45 }
46  
47 3 ▶sub is_mac {
Subroutine is_mac: called CC=2 SCAR=8.1
Pod documented
48 3 ▶ if ( get_os() eq "Darwin" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_os() eq 'Darwin') { } 0 3 3
49 0 return 1;
50 }
51 else {
52 3 return 0;
53 }
54 }
55  
56 1 ▶sub is_freebsd {
Subroutine is_freebsd: called CC=2 SCAR=8.1
Pod documented
57 1 ▶ if ( get_os() eq "FreeBSD" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_os() eq 'FreeBSD') { } 0 1 1
58 0 return 1;
59 }
60 else {
61 1 return 0;
62 }
63 }
64  
65 1 ▶sub is_openbsd {
Subroutine is_openbsd: called CC=2 SCAR=8.1
Pod documented
66 1 ▶ if ( get_os() eq "OpenBSD" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_os() eq 'OpenBSD') { } 0 1 1
67 0 return 1;
68 }
69 else {
70 1 return 0;
71 }
72 }
73  
74 2 ▶sub is_sunos {
Subroutine is_sunos: called CC=2 SCAR=8.1
Pod documented
75 2 ▶ if ( get_os() eq "SunOS" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_os() eq 'SunOS') { } 0 2 2
76 0 return 1;
77 }
78 else {
79 2 return 0;
80 }
81 }
82  
83 3 my %osx_distro_ver = (
84 10.0 => 'Cheetah',
85 10.1 => 'Puma',
86 10.2 => 'Jaguar',
87 10.3 => 'Panther',
88 10.4 => 'Tiger',
89 10.5 => 'Leopard',
90 10.6 => 'Snow Leopard',
91 10.7 => 'Lion',
92 10.8 => 'Mountain Lion',
93 10.9 => 'Mavericks',
94 10.10 => 'Yosemite',
95 10.11 => 'El Capitan',
96 10.12 => 'Sierra',
97 10.13 => 'High Sierra',
98 10.14 => 'Mojave',
99 10.15 => 'Catalina',
100 );
101 3 my %macos_distro_ver = (
102 11 => 'Big Sur',
103 12 => 'Monterey',
104 13 => 'Ventura',
105 14 => 'Sonoma',
106 15 => 'Sequioa',
107 16 => 'Tahoe',
108 );
109  
110 3 my %ubuntu_distro_ver = (
111 '4.10' => 'Warty Warthog',
112 '5.04' => 'Hoary Hedgehog',
113 '5.10' => 'Breezy Badger',
114 '6.06' => 'Dapper Drake',
115 '6.10' => 'Edgy Eft',
116 '7.04' => 'Feisty Fawn',
117 '7.10' => 'Gutsy Gibbon',
118 '8.04' => 'Hardy Heron',
119 '8.10' => 'Intrepid Ibex',
120 '9.04' => 'Jaunty Jackalope',
121 '9.10' => 'Karmic Koala',
122 '10.04' => 'Lucid Lynx',
123 '10.10' => 'Maverick Meerkat',
124 '11.04' => 'Natty Narwhal',
125 '11.10' => 'Oneiric Ocelot',
126 '12.04' => 'Precise Pangolin',
127 '12.10' => 'Quantal Quetzal',
128 '13.04' => 'Raring Ringtail',
129 '13.10' => 'Saucy Salamander',
130 '14.04' => 'Trusty Tahr',
131 '14.10' => 'Utopic Unicorn',
132 '15.04' => 'Vivid Vervet',
133 '15.10' => 'Wily Werewolf',
134 '16.04' => 'Xenial Xerus',
135 '16.10' => 'Yakkety Yak',
136 '17.04' => 'Zesty Zapus',
137 '17.10' => 'Artful Aardvark',
138 '18.04' => 'Bionic Beaver',
139 '18.10' => 'Cosmic Cuttlefish',
140 '19.04' => 'Disco Dingo',
141 '19.10' => 'Eoan Ermine',
142 '20.04' => 'Focal Fossa',
143 '20.10' => 'Groovy Gorilla',
144 '21.04' => 'Hirsute Hippo',
145 '21.10' => 'Impish Indri',
146 '22.04' => 'Jammy Jellyfish',
147 '22.10' => 'Kinetic Kudu',
148 '23.04' => 'Lunar Lobster',
149 '23.10' => 'Mantic Minotaur',
150 '24.04' => 'Noble Numbat',
151 '24.10' => 'Oracular Oriole',
152 '25.04' => 'Plucky Puffin',
153 '25.10' => 'Questing Quokka',
154 '26.04' => 'Resolute Raccoon',
155 );
156  
157 3 my %opensuse_distro_ver = (
158 11.2 => 'Emerald',
159 11.3 => 'Teal',
160 11.4 => 'Celadon',
161 12.1 => 'Asparagus',
162 12.2 => 'Mantis',
163 12.3 => 'Dartmoth',
164 13.1 => 'Bottle',
165 13.2 => 'Harlequin',
166 42.1 => 'Malacite',
167 );
168  
169 3 my %debian_distro_ver = (
170 '1.1' => 'buzz',
171 '1.2' => 'rex',
172 '1.3' => 'bo',
173 '2.0' => 'hamm',
174 '2.1' => 'slink',
175 '2.2' => 'potato',
176 '3.0' => 'woody',
177 '3.1' => 'sarge',
178 '4.0' => 'etch',
179 '5.0' => 'lenny',
180 '6.0' => 'squeeze',
181 '7' => 'wheezy',
182 '8' => 'jessie',
183 '9' => 'stretch',
184 '10' => 'buster',
185 '11' => 'bullseye',
186 '12' => 'bookworm',
187 '13' => 'trixie',
188 '14' => 'forky',
189 '15' => 'duke',
190 );
191  
192 3 my %redhat_distro_ver = (
193 '2.1' => 'Pensacola',
194 '3' => 'Taroon',
195 '4' => 'Nahant',
196 '5' => 'Tikanga',
197 '6' => 'Santiago',
198 '7' => 'Maipo',
199 '8' => 'Ootpa',
200 '9' => 'Plow',
201 '10' => 'Coughlan',
202 );
203  
204 3 my %raspbian_distro_ver = (
205 '7' => 'wheezy',
206 '8' => 'jessie',
207 '9' => 'stretch',
208 '10' => 'buster',
209 '11' => 'bullseye',
210 '12' => 'bookworm',
211 '13' => 'trixie',
212 );
213  
214 # execute the cmd return 1 on success 0 on failure
215 5 ▶sub ipc_run_e {
Subroutine ipc_run_e: called CC=6 SCAR=18.0
Pod documented
216 5 my ($arg_ref) = @_;
217 5 ▶ $arg_ref->{ debug } ||= 0;
Condition50%1/2
$arg_ref->{"debug"} ||= 0
l!l
05
MC/DC0%0/1
$arg_ref->{"debug"} ||= 0
$arg_ref->{"debug"}
Truth table50%1/2
A
A= $arg_ref->{"debug"}
Aresult
11
00
218 5 ▶ warn "cmd: $arg_ref->{ cmd }\n" if $arg_ref->{ debug };
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"debug"} 0 5 5
219  
220 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (scalar run("command", $arg_ref->{"cmd"}, "buffer", $arg_ref->{"buf"}, "verbose", $arg_ref->{"verbose"} || 0, "timeout", $arg_ref->{"timeout"} || 10)) 4 1 5
Condition100%4/4
$arg_ref->{'verbose'} || 0
l!l
14
$arg_ref->{'timeout'} || 10
l!l
14
MC/DC100%1/1
$arg_ref->{'verbose'} || 0
$arg_ref->{'verbose'}
MC/DC100%1/1
$arg_ref->{'timeout'} || 10
$arg_ref->{'timeout'}
Truth table100%2/2
A
A= $arg_ref->{'verbose'}
Aresult
11
00
Truth table100%2/2
A
A= $arg_ref->{'timeout'}
Aresult
11
00
221 scalar run(
222 command => $arg_ref->{ cmd },
223 buffer => $arg_ref->{ buf },
224 verbose => $arg_ref->{ verbose } || 0,
225 timeout => $arg_ref->{ timeout } || 10,
226 )
227 )
228 {
229 4 return 1;
230 }
231 1 return 0;
232 }
233  
234 # capture the output of the cmd and return it as an array or undef on failure
235 3 ▶sub ipc_run_c {
Subroutine ipc_run_c: called CC=8 SCAR=20.9
Pod documented
236 3 my ($arg_ref) = @_;
237 3 ▶ $arg_ref->{ debug } ||= 0;
Condition50%1/2
$arg_ref->{"debug"} ||= 0
l!l
03
MC/DC0%0/1
$arg_ref->{"debug"} ||= 0
$arg_ref->{"debug"}
Truth table50%1/2
A
A= $arg_ref->{"debug"}
Aresult
11
00
238 3 ▶ warn "cmd: $arg_ref->{ cmd }\n" if $arg_ref->{ debug };
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"debug"} 0 3 3
239  
240 my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf )
241 = run(
242 command => $arg_ref->{ cmd },
243 verbose => $arg_ref->{ verbose } || 0,
244 3 ▶ timeout => $arg_ref->{ timeout } || 10,
Condition100%4/4
$arg_ref->{'verbose'} || 0
l!l
12
$arg_ref->{'timeout'} || 10
l!l
12
MC/DC100%1/1
$arg_ref->{'verbose'} || 0
$arg_ref->{'verbose'}
MC/DC100%1/1
$arg_ref->{'timeout'} || 10
$arg_ref->{'timeout'}
Truth table100%2/2
A
A= $arg_ref->{'verbose'}
Aresult
11
00
Truth table100%2/2
A
A= $arg_ref->{'timeout'}
Aresult
11
00
245 );
246  
247 # each element of $stdout_buf can contain multiple lines
248 # flatten to one line per element in result returned
249 3 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 2 1 3
250 2 my @result;
251 2 foreach my $lines ( @{ $stdout_buf } ) {
252 2 foreach my $line ( split( /\n/, $lines ) ) {
253 11 push @result, $line;
254 }
255 }
256 2 return @result;
257 }
258 1 return;
259 }
260  
261 3 1; # End of Dev::Util::OS
262  
263 =pod
264  
265 =encoding utf-8
266  
267 =head1 NAME
268  
269 Dev::Util::OS - OS discovery and functions
270  
271 =head1 VERSION
272  
273 Version v2.19.42
274  
275 =head1 SYNOPSIS
276  
277 OS discovery and functions
278  
279 use Disk::SmartTools::OS;
280  
281 my $OS = get_os();
282 my $hostname = get_hostname();
283 my $system_is_linux = is_linux();
284 ...
285 my $status = ipc_run_e( { cmd => 'echo hello world', buf => \$buf } );
286 my @seq = ipc_run_c( { cmd => 'seq 1 10', } );
287  
288 =head1 EXPORT
289  
290 get_os
291 get_hostname
292 is_linux
293 is_mac
294 is_freebsd
295 is_openbsd
296 is_sunos
297 ipc_run_e
298 ipc_run_c
299  
300 =head1 SUBROUTINES
301  
302 =head2 B<get_os>
303  
304 Return the OS of the current system.
305  
306 my $OS = get_os();
307  
308 =head2 B<get_hostname>
309  
310 Return the hostname of the current system.
311  
312 my $hostname = get_hostname();
313  
314 =head2 B<is_linux>
315  
316 Return true if the current system is Linux.
317  
318 my $system_is_linux = is_linux();
319  
320 =head2 B<is_mac>
321  
322 Return true if the current system is MacOS (Darwin).
323  
324 my $system_is_macOS = is_mac();
325  
326 =head2 B<is_freebsd>
327  
328 Return true if the current system is FreeBSD.
329  
330 my $system_is_FreeBSD = is_freebsd();
331  
332 =head2 B<is_openbsd>
333  
334 Return true if the current system is OpenBSD.
335  
336 my $system_is_OpenBSD = is_openbsd();
337  
338 =head2 B<is_sunos>
339  
340 Return true if the current system is SunOS.
341  
342 my $system_is_sunOS = is_sunos();
343  
344 =head2 B<ipc_run_e(ARGS_HASH)>
345  
346 Execute an external program and return the status of it's execution.
347  
348 B<ARGS_HASH:>
349 { cmd => CMD, buf => BUFFER_REF, verbose => VERBOSE_BOOL, timeout => SECONDS, debug => DEBUG_BOOL }
350  
351 C<CMD> The external command to execute
352  
353 C<BUFFER_REF> A reference to a buffer
354  
355 C<VERBOSE_BOOL:optional> 1 (default) for verbose output, 0 not so much
356  
357 C<SECONDS:optional> number of seconds to wait for CMD to execute, default: 10 sec
358  
359 C<DEBUG_BOOL: optional> Debug flag, default: 0
360  
361 my $status = ipc_run_e( { cmd => 'echo hello world', verbose => 1, timeout => 8 } );
362  
363 =head2 B<ipc_run_c(ARGS_HASH)>
364  
365 Capture the output of an external program. Return the output or return undef on failure.
366  
367 B<ARGS_HASH:>
368 { cmd => CMD, buf => BUFFER_REF, verbose => VERBOSE_BOOL, timeout => SECONDS, debug => DEBUG_BOOL }
369  
370 my @seq = ipc_run_c( { cmd => 'seq 1 10', } );
371  
372 =head1 AUTHOR
373  
374 Matt Martini, C<< <matt at imaginarywave.com> >>
375  
376 =head1 BUGS
377  
378 Please report any bugs or feature requests to C<bug-dev-util at rt.cpan.org>, or through
379 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dev-Util>. I will
380 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
381  
382 =head1 SUPPORT
383  
384 You can find documentation for this module with the perldoc command.
385  
386 perldoc Dev::Util::OS
387  
388 You can also look for information at:
389  
390 =over 4
391  
392 =item * RT: CPAN's request tracker (report bugs here)
393  
394 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Dev-Util>
395  
396 =item * Search CPAN
397  
398 L<https://metacpan.org/release/Dev-Util>
399  
400 =back
401  
402 =head1 ACKNOWLEDGMENTS
403  
404 =head1 LICENSE AND COPYRIGHT
405  
406 This software is Copyright © 2019-2026 by Matt Martini.
407  
408 This is free software, licensed under:
409  
410 The GNU General Public License, Version 3, June 2007
411  
412 =cut
413  
414 __END__
« Util/Backup.pm ↑ Summary Util/Sem.pm »