Crane.pm

filter: Statementstmt 80.5% 83 / 103 Branchbran 41.1% 14 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 2 totaltotal 68.8% 115 / 167 CC 21 SCAR 34.4
CC
21
Cov
68%
import
61.2
create_package_alias
2.0
__ANON__
2.0
CRAP 31.2
↑ Summary Crane/Options.pm »
1 # -*- coding: utf-8 -*-
2  
3  
4 package Crane;
5  
6  
7 1 ▶use Crane::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Crane::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crane::Options qw( :opts options );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use File::Basename qw( basename dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec::Functions qw( catdir splitdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 1 our $VERSION = '1.03.0011';
17  
18  
19 10 ▶sub get_package_path {
Subroutine get_package_path: called CC=1 SCAR=0.0
Pod undocumented
20
21 10 my ( $package ) = @_;
22
23 10 my @path = split m{::}si, $package;
24 10 $path[-1] .= '.pm';
25
26 10 return catdir(@path);
27
28 }
29  
30  
31 5 ▶sub create_package_alias {
Subroutine create_package_alias: called CC=2 SCAR=6.9
Pod undocumented
32
33 5 my ( $original, $alias ) = @_;
34
35 eval qq{
36 package $alias;
37
38 use $original;
39
40 1;
41 1 ▶ } or do {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "\n package $alias;\n \n use $original;\n \n 1;\n " or do { confess($Crane::EVAL_ERROR) } 5 0 5
42 0 confess($EVAL_ERROR);
43 };
44
45 # Tell to Perl that module is read
46 5 $INC{ get_package_path($alias) } = $INC{ get_package_path($original) };
47
48 {
49 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
50
51 # Create alias
52 5 *{ "${alias}::" } = \*{ "${original}::" };
53 }
54
55 5 return;
56
57 }
58  
59  
60 3 ▶sub import {
Subroutine import: called CC=19 SCAR=41.1
61
62 3 my ( undef, %params ) = @_;
63
64 3 my $caller = caller;
65
66 3 ▶ Crane::Base->import(ref $params{'base'} eq 'ARRAY' ? @{ $params{'base'} } : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $params{'base'} eq 'ARRAY' ? : 1 2 3
67 3 Crane::Base->export_to_level(1, $caller);
68
69 {
70 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 3 push @{ "${caller}::ISA" }, @{ __PACKAGE__ . '::ISA' };
72 }
73
74 # Predefined options
75 my @options = (
76 3 ▶ [ 'daemon|M!', 'Run as daemon.', { 'default' => $params{'name'} ? 1 : 0 } ],
Branch: 1 branch
BranchTrueFalseTotal
$params{'name'} ? : 0 3 3
77 $OPT_SEPARATOR,
78 [ 'config|C=s', 'Path to configuration file.' ],
79 [ 'pid|P=s', 'Path to PID file.' ],
80 $OPT_SEPARATOR,
81 [ 'log|O=s', 'Path to log file.' ],
82 [ 'log-error|E=s', 'Path to error log file.' ],
83 $OPT_SEPARATOR,
84 [ 'debug|D!', 'Debug output.' ],
85 [ 'verbose|V!', 'Verbose output.' ],
86 $OPT_SEPARATOR,
87 $OPT_VERSION,
88 $OPT_HELP,
89 );
90
91 # Custom options are going to the head
92 3 ▶ if ( ref $params{'options'} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"options"} eq "ARRAY") 1 2 3
93 1 unshift @options, @{ $params{'options'} }, $OPT_SEPARATOR;
94 }
95
96 3 options(@options);
97
98 # User defined settings
99 3 ▶ if ( ref $params{'config'} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"config"} eq "HASH") 1 2 3
100 config(
101 $params{'config'},
102 1 ▶ options->{'config'} ? options->{'config'} : (),
Branch: 1 branch
BranchTrueFalseTotal
options()->{'config'} ? : 0 1 1
103 );
104 }
105
106 # Create namespace
107 3 ▶ if ( defined $params{'namespace'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"namespace"}) 1 2 3
108 1 ▶ no warnings 'File::Find';
Subroutine BEGIN: called CC=1 SCAR=0.0
109
110 1 my $path = catdir(dirname(__FILE__), __PACKAGE__);
111
112 # Create alias for root package
113 1 create_package_alias(__PACKAGE__, $params{'namespace'});
114
115 # Create alias for each subpackage
116 1 my @packages = ();
117
118 find(
119 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
120 5 ▶ if ( my ( $filename ) = $File::Find::name =~ m{^\Q$path\E/?(.+)[.]pm$}si ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($filename) = $File::Find::name =~ m[^\Q$path\E/?(.+)[.]pm$]isu) 4 1 5
121 4 push @packages, join '::', splitdir($filename);
122 }
123 },
124
125 1 $path,
126 );
127
128 1 foreach my $package ( @packages ) {
129 4 create_package_alias(__PACKAGE__ . "::$package", $params{'namespace'} . "::$package");
130 }
131 }
132
133 # Run as daemon
134 3 ▶ if ( options->{'daemon'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (options()->{"daemon"}) 0 3 3
135 0 local $OUTPUT_AUTOFLUSH = 1;
136
137 0 ▶ $params{'name'} //= basename($PROGRAM_NAME) =~ s{[.]p[lm]$}{}rsi;
Condition0%0/3
$params{"name"} //= basename($Crane::PROGRAM_NAME) =~ s/[.]p[lm]$//irsu
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"name"} //= basename($Crane::PROGRAM_NAME) =~ s/[.]p[lm]$//irsu
$params{"name"} basename($Crane::PROGRAM_NAME) =~ s/[.]p[lm]$//irsu
Truth table0%0/3
A //= B
A= $params{"name"}B= basename($Crane::PROGRAM_NAME) =~ s/[.]p[lm]$//irsu
ABresult
1X1
011
000
138
139 # Prepare PID file
140 0 ▶ my $pid_filename = options->{'pid'} || catdir('run', "$params{'name'}.pid");
Condition0%0/3
options()->{'pid'} || catdir('run', "$params{'name'}.pid")
l!l&&r!l&&!r
000
MC/DC0%0/2
options()->{'pid'} || catdir('run', "$params{'name'}.pid")
options()->{'pid'} catdir('run', "$params{'name'}.pid")
Truth table0%0/3
A || B
A= options()->{'pid'}B= catdir('run', "$params{'name'}.pid")
ABresult
1X1
011
000
141 0 my $pid_prev = undef;
142
143 0 ▶ open my $fh_pid, '+>>:encoding(UTF-8)', $pid_filename or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh_pid, "+>>:encoding(UTF-8)", $pid_filename or confess($Crane::OS_ERROR) 0 0 0
144 0 seek $fh_pid, 0, 0;
145
146 0 $pid_prev = <$fh_pid>;
147
148 0 ▶ if ( $pid_prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid_prev) 0 0 0
149 0 chomp $pid_prev;
150 }
151
152 # Check if process is already running
153 0 ▶ my $is_working = $pid_prev ? kill 0, $pid_prev : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pid_prev ? : 0 0 0
154
155 0 ▶ if ( not $is_working ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_working) { } 0 0 0
156 # Fork
157 0 ▶ if ( my $pid = fork ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) 0 0 0
158 0 truncate $fh_pid, 0;
159 0 ▶ print { $fh_pid } "$pid\n" or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
print {$fh_pid;} "$pid\n" or confess($Crane::OS_ERROR) 0 0 0
160 0 ▶ close $fh_pid or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
close $fh_pid or confess($Crane::OS_ERROR) 0 0 0
161
162 0 exit 0;
163 }
164 } else {
165 0 die "Process is already running: $pid_prev\n";
166 }
167
168 0 ▶ close $fh_pid or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
close $fh_pid or confess($Crane::OS_ERROR) 0 0 0
169 }
170
171 3 return;
172
173 }
174  
175  
176 1 1;
177  
178  
179 =head1 NAME
180  
181 Crane - Helpers for development in Perl
182  
183  
184 =head1 SYNOPSIS
185  
186 use Crane;
187
188 ...
189
190 use Crane ( 'name' => 'example' );
191  
192  
193 =head1 DESCRIPTION
194  
195 Helpers for development in Perl. Includes the most modern technics and rules.
196  
197 Also imports modules as L<Crane::Base/Crane::Base>;
198  
199  
200 =head2 Import options
201  
202 You can specify these options when using module:
203  
204 =over
205  
206 =item B<name>
207  
208 Script name, used when run as daemon.
209  
210 If defined, run as daemon by default. Use B<--no-daemon> command line option to
211 cancel this behavior.
212  
213 =item B<base>
214  
215 Array (reference) to list of base modules.
216  
217 =item B<options>
218  
219 Array (reference) of options which will be added to the head of L<default options|/"OPTIONS"> list.
220  
221 =item B<config>
222  
223 Hash (reference) with user defined default settings.
224  
225 =item B<namespace>
226  
227 Custom namespace. Please, look at L<examples|/"EXAMPLES"> below.
228  
229 =back
230  
231  
232 =head1 OPTIONS
233  
234 These options are available by default. You can define your custom options if
235 specify it in the import options.
236  
237 =over
238  
239 =item B<-M>, B<--daemon>, B<--no-daemon>
240  
241 Runs as daemon.
242  
243 =item B<-C> I<path/to/config>, B<--config>=I<path/to/config>
244  
245 Path to configuration file.
246  
247 =item B<-P> I<path/to/file_with.pid>, B<--pid>=I<path/to/file_with.pid>
248  
249 Path to PID file.
250  
251 =item B<-O> I<path/to/messages.log>, B<--log>=I<path/to/messages.log>
252  
253 Path to messages log file.
254  
255 =item B<-E> I<path/to/errors.log>, B<--log-error>=I<path/to/errors.log>
256  
257 Path to errors log file.
258  
259 =item B<-D>, B<--debug>, B<--no-debug>
260  
261 Debug output.
262  
263 =item B<-V>, B<--verbose>, B<--no-verbose>
264  
265 Verbose output.
266  
267 =item B<--version>
268  
269 Shows version information and exits.
270  
271 =item B<--help>
272  
273 Shows help and exits.
274  
275 =back
276  
277  
278 =head1 RETURN VALUE
279  
280 In case of running as daemon will return 1 if process is already running.
281  
282  
283 =head1 DIAGNOSTICS
284  
285 =over
286  
287 =item Process is already running: I<%d>
288  
289 Where I<%d> is a PID.
290  
291 You tried to run application as daemon while another copy is running.
292  
293 =back
294  
295  
296 =head1 EXAMPLES
297  
298  
299 =head2 Singleton usage
300  
301 use Crane;
302
303 ...
304
305 use Crane ( 'base' => qw( Mojolicious::Controller ) );
306  
307  
308 =head2 Daemon usage
309  
310 use Crane ( 'name' => 'example' );
311  
312  
313 =head2 Configure options
314  
315 use Crane ( 'options' => [
316 [ 'from|F=s', 'Start of the interval.', { 'required' => 1 } ],
317 [ 'to|F=s', 'End of the interval.', { 'required' => 1 } ],
318 ] );
319  
320 As a result you have these two options, a separator and default options.
321  
322  
323 =head2 Basic namespace usage
324  
325 package My;
326
327 use Crane (
328 'namespace' => 'My',
329
330 'config' => {
331 'my' => {
332 'autorun' => 1,
333
334 'hosts' => [
335 '127.0.0.1',
336 '127.0.0.2',
337 ],
338 },
339 },
340 );
341
342 1;
343
344 ...
345
346 use My;
347 use My::Config;
348 use My::Logger;
349
350 log_info(config->{'log'});
351  
352  
353 =head2 Advanced namespace usage
354  
355 package My;
356
357 use Crane::Base;
358 use Crane::Options qw( :opts );
359
360 require Crane;
361
362 sub import {
363 my ( $package, $name ) = @_;
364
365 Crane->import(
366 'namespace' => 'My',
367 'name' => $name,
368
369 'options' => [
370 [ 'run!', 'Do action at startup.' ],
371 $OPT_SEPARATOR,
372 [ 'host=s@', 'Host name(s).' ],
373 ],
374
375 'config' => {
376 'my' => {
377 'autorun' => 1,
378
379 'hosts' => [
380 '127.0.0.1',
381 '127.0.0.2',
382 ],
383 },
384 },
385 );
386
387 return;
388 }
389
390 1;
391
392 ...
393
394 use My 'my_script';
395
396 sub main {
397 ...
398
399 return 0;
400 }
401
402 exit main();
403  
404  
405 =head1 ENVIRONMENT
406  
407 See L<Crane::Base|Crane::Base/"ENVIRONMENT">.
408  
409  
410 =head1 FILES
411  
412 =over
413  
414 =item F<etc/*.conf>
415  
416 Configuration files. See L<Crane::Config|Crane::Config/"FILES">.
417  
418 =item F<log/*.log>
419  
420 Log files. See L<Crane::Logger|Crane::Logger/"FILES">.
421  
422 =item F<run/*.pid>
423  
424 Script's PID file.
425  
426 =back
427  
428  
429 =head1 BUGS
430  
431 Please report any bugs or feature requests to
432 L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Crane> or to
433 L<https://github.com/temoon/crane/issues>.
434  
435  
436 =head1 AUTHOR
437  
438 Tema Novikov, <novikov.tema@gmail.com>
439  
440  
441 =head1 COPYRIGHT AND LICENSE
442  
443 Copyright (C) 2013-2014 Tema Novikov.
444  
445 This library is free software; you can redistribute it and/or modify it under
446 the terms of the Artistic License 2.0. For details, see the full text of the
447 license in the file LICENSE.
448  
449  
450 =head1 SEE ALSO
451  
452 =over
453  
454 =item * B<RT Cpan>
455  
456 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Crane>
457  
458 =item * B<Github>
459  
460 L<https://github.com/temoon/crane>
461  
462 =back
↑ Summary Crane/Options.pm »