Quiq/System.pm

filter: Statementstmt 86.6% 65 / 75 Branchbran 70.0% 14 / 20 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 94.1% 16 / 17 Podpod 100% 7 / 7 totaltotal 76.9% 107 / 139 CC 16 SCAR 13.1
CC
16
Cov
79%
numberOfCpus
6.6
searchProgram
6.3
ip
6.0
CRAP 3.7
« Quiq/AxisTick.pm ↑ Summary Quiq/PlotlyJs/TimeSeries.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::System - Information über das System und seine Umgebung
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::System;
18 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 270 our $VERSION = '1.240';
25  
26 270 ▶use Quiq::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 270 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 270 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 270 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 270 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 270 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # -----------------------------------------------------------------------------
34  
35 =head1 METHODS
36  
37 =head2 Host
38  
39 =head3 numberOfCpus() - Anzahl der CPUs
40  
41 =head4 Synopsis
42  
43 $n = $this->numberOfCpus;
44  
45 =head4 Description
46  
47 Liefere die Anzahl der CPUs des Systems. Diese Methode ist nicht
48 portabel, sie basiert auf /proc/cpuinfo des Linux-Kernels bzw.
49 dem dem Kommando 'sysctl -n hw.ncpu' von FreeBSD. Im Falle eines
50 unbekannten Systems liefert die Methode 1.
51  
52 =cut
53  
54 # -----------------------------------------------------------------------------
55  
56 12 ▶sub numberOfCpus {
Subroutine numberOfCpus: called CC=6 SCAR=18.9
Pod documented
57 12 my $this = shift;
58  
59 12 state $n = 0;
60 12 ▶ if (!$n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 12 0 12
61 12 ▶ if ($^O eq 'freebsd') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'freebsd') { } 0 12 12
elsif ($^O eq 'linux') { } 12 0 12
62 # Fix: CPAN Testers
63 0 $n = Quiq::Shell->exec('sysctl -n hw.ncpu',-capture=>'stdout');
64 0 chomp $n;
65 }
66 elsif ($^O eq 'linux') {
67 12 my $fh = Quiq::FileHandle->new('<','/proc/cpuinfo');
68 12 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 4212 12 4224
69 4212 ▶ if (/^processor/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^processor/) 156 4056 4212
70 156 $n++;
71 }
72 }
73 12 $fh->close;
74 }
75 else {
76 # Default
77 0 $n = 1;
78 }
79 }
80  
81 12 return $n;
82 }
83  
84 # -----------------------------------------------------------------------------
85  
86 =head3 hostname() - Hostname des Systems oder zu IP
87  
88 =head4 Synopsis
89  
90 $hostname = $this->hostname;
91 $hostname = $this->hostname($ip);
92  
93 =head4 Description
94  
95 Liefere "den" Hostnamen des Systems. Es ist der Name, den die
96 Methode Sys::Hostname::hostname() liefert.
97  
98 =head4 See Also
99  
100 Sys::Hostname
101  
102 =cut
103  
104 # -----------------------------------------------------------------------------
105  
106 1 ▶sub hostname {
Subroutine hostname: called CC=2 SCAR=8.4
Pod documented
107 1 my $this = shift;
108 # @_: $ip
109  
110 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
111 0 my $ip = shift;
112 # FIXME: Fehlerbehandlung
113 0 return gethostbyaddr(Socket::inet_aton($ip),Socket::AF_INET);
114 }
115  
116 1 return Sys::Hostname::hostname;
117 }
118  
119 # -----------------------------------------------------------------------------
120  
121 =head3 ip() - IP des Systems oder zu Hostname
122  
123 =head4 Synopsis
124  
125 $ip = $this->ip;
126 $ip = $this->ip($hostname);
127  
128 =head4 Description
129  
130 Liefere die IP-Adresse des Systems (Aufruf ohne Parameter) oder die
131 IP-Adresse für $hostname.
132  
133 Die IP-Adresse des Systems ist die IP-Adresse zu dem Hostnamen,
134 den Quiq::System->hostname() liefert.
135  
136 =cut
137  
138 # -----------------------------------------------------------------------------
139  
140 0 ▶sub ip {
Subroutine ip: not called CC=2 SCAR=17.9
Pod documented
141 0 my $this = shift;
142 0 ▶ my $host = shift || $this->hostname;
Condition0%0/3
shift() || $this->hostname
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $this->hostname
shift() $this->hostname
Truth table0%0/3
A || B
A= shift()B= $this->hostname
ABresult
1X1
011
000
143  
144 0 return Socket::inet_ntoa(scalar gethostbyname $host);
145 }
146  
147 # -----------------------------------------------------------------------------
148  
149 =head2 Encoding
150  
151 =head3 encoding() - Character-Encoding der Umgebung
152  
153 =head4 Synopsis
154  
155 $encoding = $this->encoding;
156  
157 =head4 Description
158  
159 Liefere das in der Umgebung eingestellte Character-Encoding. In dieses
160 Encoding sollten Ausgaben auf das Terminal gewandelt werden.
161  
162 Wir ermitteln das Encoding durch Aufruf der internen Funktion
163 _get_locale_encoding() des Pragmas encoding.
164  
165 =head4 See Also
166  
167 Pragma encoding
168  
169 =head4 Example
170  
171 Gib non-ASCII-Zeichen im Encoding der Umgebung auf STDOUT aus:
172  
173 my $encoding = Quiq::System->encoding;
174 binmode STDOUT,":encoding($encoding)";
175 print "äöüßÄÖÜ\n";
176  
177 =cut
178  
179 # -----------------------------------------------------------------------------
180  
181 266 ▶sub encoding {
Subroutine encoding: called CC=1 SCAR=0.0
Pod documented
182 266 my $this = shift;
183 266 require encoding;
184 266 my $encoding = encoding::_get_locale_encoding();
185 266 $encoding =~ s/-strict$//; # Korrektur utf-8-strict
186 266 return $encoding;
187 }
188  
189 # -----------------------------------------------------------------------------
190  
191 =head2 User
192  
193 =head3 user() - Benutzername zu User-Id
194  
195 =head4 Synopsis
196  
197 $user = $this->user;
198 $user = $this->user($uid);
199  
200 =head4 Description
201  
202 Liefere den Namen des Benutzers mit User-Id (UID) $uid. Ist keine
203 User-Id angegeben, verwende die effektive User-Id des laufenden
204 Prozesses.
205  
206 =cut
207  
208 # -----------------------------------------------------------------------------
209  
210 3 ▶sub user {
Subroutine user: called CC=3 SCAR=11.8
Pod documented
211 3 my $this = shift;
212 3 ▶ my $uid = shift // $>;
Condition33%1/3
shift() // $>
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() // $>
shift() $>
Truth table33%1/3
A // B
A= shift()B= $>
ABresult
1X1
011
000
213  
214 3 ▶ return getpwuid($uid) // do {
Condition66%2/3
getpwuid $uid // do { $this->throw("SYS-00001: Benutzer existiert nicht", "Uid", $uid, "Error", "$!") }
l!l&&r!l&&!r
201
MC/DC0%0/2
getpwuid $uid // do { $this->throw("SYS-00001: Benutzer existiert nicht", "Uid", $uid, "Error", "$!") }
getpwuid $uid do { $this->throw("SYS-00001: Benutzer existiert nicht", "Uid", $uid, "Error", "$!") }
Truth table33%1/3
A // B
A= getpwuid $uidB= do { $this->throw("SYS-00001: Benutzer existiert nicht", "Uid", $uid, "Error", "$!") }
ABresult
1X1
011
000
215 1 $this->throw(
216 'SYS-00001: Benutzer existiert nicht',
217 Uid => $uid,
218 Error => "$!",
219 );
220 };
221 }
222  
223 # -----------------------------------------------------------------------------
224  
225 =head3 uid() - User-Id zu Benutzername
226  
227 =head4 Synopsis
228  
229 $uid = $this->uid($user);
230  
231 =head4 Description
232  
233 Liefere die User-Id (UID) des Benutzers mit dem Namen $user.
234  
235 =cut
236  
237 # -----------------------------------------------------------------------------
238  
239 2 ▶sub uid {
Subroutine uid: called CC=2 SCAR=7.0
Pod documented
240 2 my ($this,$user) = @_;
241  
242 2 ▶ return getpwnam($user) // do {
Condition66%2/3
getpwnam $user // do { $this->throw("SYS-00001: Benutzer existiert nicht", "User", $user, "Error", "$!") }
l!l&&r!l&&!r
110
MC/DC0%0/2
getpwnam $user // do { $this->throw("SYS-00001: Benutzer existiert nicht", "User", $user, "Error", "$!") }
getpwnam $user do { $this->throw("SYS-00001: Benutzer existiert nicht", "User", $user, "Error", "$!") }
Truth table33%1/3
A // B
A= getpwnam $userB= do { $this->throw("SYS-00001: Benutzer existiert nicht", "User", $user, "Error", "$!") }
ABresult
1X1
011
000
243 1 $this->throw(
244 'SYS-00001: Benutzer existiert nicht',
245 User => $user,
246 Error => "$!",
247 );
248 };
249 }
250  
251 # -----------------------------------------------------------------------------
252  
253 =head2 Suchpfad
254  
255 =head3 searchProgram() - Suche Programm via PATH
256  
257 =head4 Synopsis
258  
259 $path = $class->searchProgram($program);
260  
261 =head4 Options
262  
263 =over 4
264  
265 =item -sloppy => $bool (Default: 0)
266  
267 Wirf keine Exception, wenn das Programm nicht gefunden wird,
268 sondern liefere C<undef>.
269  
270 =back
271  
272 =cut
273  
274 # -----------------------------------------------------------------------------
275  
276 2 ▶sub searchProgram {
Subroutine searchProgram: called CC=6 SCAR=18.4
Pod documented
277 2 my ($class,$program) = splice @_,0,2;
278  
279 2 my $sloppy = 0;
280  
281 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
282 1 Quiq::Option->extract(\@_,
283 -sloppy => \$sloppy,
284 );
285 }
286  
287 2 ▶ if (substr($program,0,1) eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($program, 0, 1) eq "/") 0 2 2
288 # Wenn absoluter Pfad, diesen liefern
289 0 return $program;
290 }
291  
292 # PATH absuchen
293  
294 2 for my $path (split /:/,$ENV{'PATH'}) {
295 18 ▶ if (-e "$path/$program") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$path/$program") 1 17 18
296 1 return "$path/$program";
297 }
298 }
299  
300 # Nicht gefunden
301  
302 1 ▶ if ($sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sloppy) 1 0 1
303 1 return undef;
304 }
305  
306 $class->throw(
307 'PATH-00020: Programm/Skript nicht gefunden',
308 Program => $program,
309 0 Paths => $ENV{'PATH'},
310 );
311 }
312  
313 # -----------------------------------------------------------------------------
314  
315 =head1 VERSION
316  
317 1.240
318  
319 =head1 AUTHOR
320  
321 Frank Seitz, L<http://fseitz.de/>
322  
323 =head1 COPYRIGHT
324  
325 Copyright (C) 2026 Frank Seitz
326  
327 =head1 LICENSE
328  
329 This code is free software; you can redistribute it and/or modify
330 it under the same terms as Perl itself.
331  
332 =cut
333  
334 # -----------------------------------------------------------------------------
335  
336 270 1;
337  
338 # eof
« Quiq/AxisTick.pm ↑ Summary Quiq/PlotlyJs/TimeSeries.pm »