Quiq/ApplicationPaths.pm

filter: Statementstmt 94.5% 52 / 55 Branchbran 80.0% 8 / 10 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 94.1% 80 / 85 CC 7 SCAR 6.1
CC
7
Cov
93%
new
3.0
prefix
2.1
homeDir
2.0
CRAP 1.8
« Quiq/Dbms.pm ↑ Summary Quiq/Excel/Writer.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::ApplicationPaths - Ermittele Pfade einer Unix-Applikation
8  
9 =head1 SYNOPSIS
10  
11 # Homedir: <prefix>/opt/<application> (<prefix> kann leer sein)
12
13 use FindBin qw/$Bin/;
14 use lib "$Bin/../lib/perl5"; # .. d.h. $depth == 1
15 use Quiq::ApplicationPaths;
16
17 my $app = Quiq::ApplicationPaths->new;
18
19 my $name = $app->name; # <application>
20 my $prefix = $app->prefix; # <prefix>
21
22 my $homeDir = $app->homeDir; # <prefix>/opt/<application>
23 my $etcDir = $app->etcDir; # <prefix>/etc/opt/<application>
24 my $varDir = $app->varDir; # <prefix>/var/opt/<application>
25  
26 =head1 DESCRIPTION
27  
28 Die Klasse ermöglicht einer Perl-Applikation unter Unix ohne
29 hartkodierte absolute Pfade auszukommen. Alle Pfade, unter denen
30 sich die verschiedenen Teile der Applikation (opt-, etc-,
31 var-Bereich) im Dateisystem befinden, werden von der Klasse aus
32 dem Pfad des ausgeführten Programms hergeleitet.
33  
34 Das Layout entspricht der opt-Installationsstruktur eines
35 Unix-Systems:
36  
37 =over 2
38  
39 =item *
40  
41 /opt/<application> (Programmcode und statische Daten)
42  
43 =item *
44  
45 /etc/opt/<application> (Konfiguration)
46  
47 =item *
48  
49 /var/opt/<application> (Bewegungsdaten)
50  
51 =back
52  
53 Die Pfade müssen nicht im Root-Verzeichnis beginnen, ihnen kann
54 auch ein Präfix-Pfad <prefix> vorangestellt sein. Z.B. kann sich die
55 Struktur im Home-Verzeichnis des Benutzers befinden
56 (siehe Abschnitt L<EXAMPLES|"EXAMPLES">).
57  
58 =head1 EXAMPLES
59  
60 /opt/<application>/...
61  
62 prefix() : (Leerstring)
63 name() : <application>
64 homeDir() : /opt/<application>
65 etcDir() : /etc/opt/<application>
66 varDir() : /var/opt/<application>
67  
68 /home/<user>/opt/<application>/...
69  
70 prefix() : /home/<user>
71 name() : <application>
72 homeDir() : /home/<user>/opt/<application>
73 etcDir() : /home/<user>/etc/opt/<application>
74 varDir() : /home/<user>/var/opt/<application>
75  
76 =cut
77  
78 # -----------------------------------------------------------------------------
79  
80 package Quiq::ApplicationPaths;
81  
82 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 1 our $VERSION = '1.240';
87  
88 1 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 ▶use Hash::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 # -----------------------------------------------------------------------------
92  
93 =head1 METHODS
94  
95 =head2 Konstruktor
96  
97 =head3 new() - Konstruktor
98  
99 =head4 Synopsis
100  
101 $app = $class->new;
102 $app = $class->new($depth);
103  
104 =head4 Arguments
105  
106 =over 4
107  
108 =item $depth (Default: 1)
109  
110 Gibt an, wie viele Subverzeichnisse tief das Programm unterhalb des
111 Applikations-Homedir (<prefix>/opt/<application>) angesiedelt ist.
112  
113 =back
114  
115 =head4 Description
116  
117 Instantiiere ein Objekt der Klasse und liefere dieses zurück.
118  
119 =cut
120  
121 # -----------------------------------------------------------------------------
122  
123 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
124 3 my $class = shift;
125 3 ▶ my $depth = shift || 1;
Condition100%2/2
shift() || 1
l!l
12
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
126  
127 # Wir arbeiten nicht mit den Variablen von FindBin, da diese
128 # Symlinks auflösen und wir keinen Symbolischen Namen wie
129 # 'prod' als Version erhalten, wenn dies ein Symlink auf eine
130 # reale Version ist.
131  
132 3 my $path = $0;
133 3 ▶ if ($path !~ m|^/|) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[^/]) 0 3 3
134 # Einen relativen Pfad ergänzen wir um das aktuelle Verzeichnis.
135 # Achtung: getcwd liefert möglicherweise nicht den Versionspfad
136 # den wir meinen, wenn wir unterhalb von version/<version> stehen.
137 # Denn getcwd liefert den realen Pfad und beinhaltet keine Symlinks.
138 # Wenn <version> ein Symlink ist, erscheint der reale Name im Pfad.
139  
140 0 $path =~ s|^./||;
141 0 $path = sprintf '%s/%s',Cwd::getcwd,$path;
142 }
143  
144 # HomeDir bestimmen, indem wir das Programm und $depth
145 # Verzeichnisse darüber vom Pfad entfernen
146
147 3 my @path = split m|/|,$path;
148 3 splice @path,-($depth+1);
149 3 my $homeDir = join '/',@path;
150  
151 # <application> ist die letzte Pfadkomponente
152  
153 3 my ($application,$etcPath,$varPath);
154 3 $application = pop @path;
155  
156 # <prefix> erhalten wir nach dem Entfernen des
157 # Verzeichnisses oberhalb von <application>
158  
159 3 pop @path; # opt entfernen
160 3 my $prefix = join('/',@path);
161  
162 3 my $etcDir = "$prefix/etc/opt/$application";
163 3 my $varDir = "$prefix/var/opt/$application";
164  
165 3 my $self = bless {
166 name => $application,
167 prefix => $prefix,
168 homeDir => $homeDir,
169 etcDir => $etcDir,
170 varDir => $varDir,
171 },$class;
172 3 Hash::Util::lock_ref_keys($self);
173  
174 3 return $self;
175 }
176  
177 # -----------------------------------------------------------------------------
178  
179 =head2 Objektmethoden
180  
181 =head3 name() - Name der Applikation
182  
183 =head4 Synopsis
184  
185 $name = $app->name;
186  
187 =head4 Description
188  
189 Liefere den Namen <name> der Applikation.
190  
191 =cut
192  
193 # -----------------------------------------------------------------------------
194  
195 3 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
196 3 shift->{'name'};
197 }
198  
199 # -----------------------------------------------------------------------------
200  
201 =head3 prefix() - Pfad-Präfix der Installation
202  
203 =head4 Synopsis
204  
205 $prefix = $app->prefix;
206 $prefix = $app->prefix($subPath);
207  
208 =head4 Description
209  
210 Liefere den Pfad-Präfix <prefix> der Applikations-Installation,
211 also den Pfad oberhalb des opt-Verzeichnisses. Ist die Applikation
212 in /opt (opt im Wurzelverzeichnis) installiert, wird ein
213 Leerstring geliefert. Ist Zeichenkette $subPath angegeben,
214 wird diese mit '/' getrennt angefügt.
215  
216 =cut
217  
218 # -----------------------------------------------------------------------------
219  
220 3 ▶sub prefix {
Subroutine prefix: called CC=2 SCAR=7.4
Pod documented
221 3 my $self = shift;
222 # @_: $subPath
223  
224 3 my $path = $self->{'prefix'};
225 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
226 0 $path .= '/'.shift;
227 }
228  
229 3 return $path;
230 }
231  
232 # -----------------------------------------------------------------------------
233  
234 =head3 homeDir() - Home-Verzeichnis der Applikation
235  
236 =head4 Synopsis
237  
238 $homeDir = $app->homeDir;
239 $homeDir = $app->homeDir($subPath);
240  
241 =head4 Description
242  
243 Liefere das Verzeichnis, in dem der Programmcode und die
244 statischen Daten der Applikation abgelegt sind. Ist Zeichenkette
245 $subPath angegeben, wird diese mit '/' getrennt angefügt.
246  
247 =cut
248  
249 # -----------------------------------------------------------------------------
250  
251 6 ▶sub homeDir {
Subroutine homeDir: called CC=2 SCAR=6.9
Pod documented
252 6 my $self = shift;
253 # @_: $subPath
254  
255 6 my $path = $self->{'homeDir'};
256 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
257 3 $path .= '/'.shift;
258 }
259  
260 6 return $path;
261 }
262  
263 # -----------------------------------------------------------------------------
264  
265 =head3 etcDir() - Konfigurations-Verzeichnis der Applikation
266  
267 =head4 Synopsis
268  
269 $etcDir = $app->etcDir;
270 $etcDir = $app->etcDir($subPath);
271  
272 =head4 Description
273  
274 Liefere das Verzeichnis, in dem die Konfigurationsdateien der
275 Applikation abgelegt sind. Ist Zeichenkette $subPath angegeben,
276 wird diese mit '/' getrennt angefügt.
277  
278 =cut
279  
280 # -----------------------------------------------------------------------------
281  
282 6 ▶sub etcDir {
Subroutine etcDir: called CC=2 SCAR=6.9
Pod documented
283 6 my $self = shift;
284 # @_: $subPath
285  
286 6 my $path = $self->{'etcDir'};
287 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
288 3 $path .= '/'.shift;
289 }
290  
291 6 return $path;
292 }
293  
294 # -----------------------------------------------------------------------------
295  
296 =head3 varDir() - Bewegungsdaten-Verzeichnis der Applikation
297  
298 =head4 Synopsis
299  
300 $varDir = $app->varDir;
301 $varDir = $app->varDir($subPath);
302  
303 =head4 Description
304  
305 Liefere das Verzeichnis, in dem die Applikation Bewegungsdaten speichert.
306 Ist Zeichenkette $subPath angegeben, wird diese mit '/' getrennt angefügt.
307  
308 =cut
309  
310 # -----------------------------------------------------------------------------
311  
312 6 ▶sub varDir {
Subroutine varDir: called CC=2 SCAR=6.9
Pod documented
313 6 my $self = shift;
314 # @_: $subPath
315  
316 6 my $path = $self->{'varDir'};
317 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
318 3 $path .= '/'.shift;
319 }
320  
321 6 return $path;
322 }
323  
324 # -----------------------------------------------------------------------------
325  
326 =head1 DETAILS
327  
328 =head2 Mögliche Erweiterungen
329  
330 =head3 Andere Layouts
331  
332 Andere Layouts sind möglich und könnten von der Klasse ebenfalls
333 behandelt werden. Bei Bedarf kann der Konstruktor um eine Option
334 C<< -layout=>$layout >> erweitert und das betreffende Layout innerhalb
335 des Konstruktors behandelt werden. Beispiele:
336  
337 Installation mit Unterscheidung nach Versionsnummer:
338  
339 <prefix>/opt/<application>/<version>
340 <prefix>/etc/opt/<application>/<version>
341 <prefix>/var/opt/<application>/<version>
342  
343 Installation mit Unterscheidung nach Versionsnummer in eigenem
344 Subverzeichnis:
345  
346 <prefix>/opt/<application>/version/<version>
347 <prefix>/etc/opt/<application>/<version>
348 <prefix>/var/opt/<application>/<version>
349  
350 Kein opt-Unterverzeichnis in etc und var:
351  
352 <prefix>/opt/<application>
353 <prefix>/etc/<application>
354 <prefix>/var/<application>
355  
356 =head3 Optionaler Trenner bei etcDir() und varDir()
357  
358 Die Methoden etcDir() und varDir() könnten um eine Variante mit
359 zwei Parametern erweitert werden, die die Vorgabe des
360 Trennzeichens erlaubt:
361  
362 $path = $app->etcDir('','.conf');
363 # <prefix>/etc/opt/<application>.conf
364
365 $path = $app->varDir('','.log');
366 # <prefix>/etc/opt/<application>.log
367  
368 =head1 VERSION
369  
370 1.240
371  
372 =head1 AUTHOR
373  
374 Frank Seitz, L<http://fseitz.de/>
375  
376 =head1 COPYRIGHT
377  
378 Copyright (C) 2026 Frank Seitz
379  
380 =head1 LICENSE
381  
382 This code is free software; you can redistribute it and/or modify
383 it under the same terms as Perl itself.
384  
385 =cut
386  
387 # -----------------------------------------------------------------------------
388  
389 1 1;
390  
391 # eof
« Quiq/Dbms.pm ↑ Summary Quiq/Excel/Writer.pm »