blib/script/narada-setup-cron

filter: Statementstmt 92.2% 83 / 90 Branchbran 73.0% 19 / 26 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 94.7% 18 / 19 Podpod n/a totaltotal 89.5% 129 / 144 CC 15 SCAR 14.1
CC
15
Cov
89%
set_user_crontab
12.0
main
8.0
get_user_crontab
3.3
CRAP 4.1
« lib/Narada/Lock.pm ↑ Summary blib/lib/Narada/Log.pm »
1 #!/usr/bin/env perl
2 44 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 44 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 44 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 42 our $VERSION = 'v2.3.8';
8  
9 44 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 44 ▶use lib "$FindBin::Bin/../lib/perl5";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 44 ▶use Narada;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 44 ▶use Narada::Config qw( get_config set_config );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 44 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 42 ▶main(@ARGV) if !caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 42 0 42
17  
18  
19 44 ▶sub err { die "narada-setup-cron: @_\n" }
Subroutine err: called CC=1 SCAR=0.0
20  
21 58 ▶sub main {
Subroutine main: called CC=8 SCAR=20.8
22 58 ▶ die "Usage: narada-setup-cron [--clean]\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 or @_ == 1 and $_[0] ne "--clean" 4 54 58
Condition100%6/6
@_ > 1 or @_ == 1 and $_[0] ne "--clean"
l!l&&r!l&&!r
2254
@_ == 1 and $_[0] ne "--clean"
!ll&&!rl&&r
8462
MC/DC100%3/3
@_ > 1 or @_ == 1 and $_[0] ne "--clean"
@_ > 1 @_ == 1 $_[0] ne "--clean"
Truth table100%4/4
A or B and C
A= @_ > 1B= @_ == 1C= $_[0] ne "--clean"
ABCresult
1XX1
0111
00X0
0100
23 if (@_ > 1)
24 || (@_ == 1 && $_[0] ne '--clean');
25  
26 54 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 46 8 54
27 46 path('var/use/cron')->remove;
28 46 del_cron();
29 }
30 else {
31 8 ▶ if (Narada::detect() eq 'narada') {
Branch: 1 branch
BranchTrueFalseTotal
if (Narada::detect() eq "narada") 5 3 8
32 5 path('var/use/cron')->touch;
33 }
34 8 my $c = path('config/crontab');
35 8 ▶ my @configs = map {m{\Aconfig/(.*)}ms} grep {$_->is_file}
Branch: 1 branch
BranchTrueFalseTotal
$c->is_dir ? : 5 3 8
36 $c->is_dir ? $c->children : $c;
37 8 ▶ if (!@configs) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @configs) { } 2 6 8
38 2 del_cron();
39 }
40 else {
41 6 my $crontab = join "\n", map { get_config($_) } @configs;
42 4 $crontab = process($crontab);
43 4 set_cron($crontab);
44 }
45 }
46  
47 10 return;
48 }
49  
50 82 ▶sub get_project_dir {
Subroutine get_project_dir: called CC=2 SCAR=6.9
51 82 chomp (my $project_dir = `pwd`);
52 82 ▶ $project_dir !~ /\n/xms or err 'Project directory must not contain \\n';
Branch: 1 branch
BranchTrueFalseTotal
$project_dir =~ /\n/msx and err("Project directory must not contain \\n") 2 80 82
53 80 return $project_dir;
54 }
55  
56 6 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
57 6 my ($crontab) = @_;
58 6 my $project_dir = get_project_dir();
59 6 $project_dir = quotemeta $project_dir;
60 6 $project_dir =~ s{\\([/,._-])}{$1}xmsg; # unquote safe chars for readability
61 6 $crontab =~ s/^(\s*(?!\#)(?:\S+\s+){5})/${1}cd $project_dir || exit; /xmsg;
62 6 return $crontab;
63 }
64  
65 68 ▶sub get_markers {
Subroutine get_markers: called CC=1 SCAR=0.0
66 68 my $project_dir = get_project_dir();
67 68 my $start = "# ENTER Narada: $project_dir";
68 68 my $end = "# LEAVE Narada: $project_dir";
69 68 my $re = qr/^\Q$start\E\n.*?^\Q$end\E(?:\n|\z)/xms;
70 68 return ($re, $start, $end);
71 }
72  
73 42 ▶sub get_user_crontab {
Subroutine get_user_crontab: called CC=3 SCAR=12.0
74 42 local $/ = undef;
75 # WORKAROUND If user has no crontab then `crontab -l` output string
76 # "no crontab for USERNAME". So we've to use `crontab -e`
77 # instead, to get empty output in this case.
78 42 ▶ open my $cron, q{-|}, 'VISUAL=cat EDITOR=cat crontab -e' or err "crontab -e: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cron, "-|", "VISUAL=cat EDITOR=cat crontab -e" or err("crontab -e: $!") 42 0 42
79 42 my $crontab = <$cron>;
80 42 ▶ close $cron or err "crontab -e: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $cron or err("crontab -e: $!") 0 42 42
81 0 return $crontab;
82 }
83  
84 0 ▶sub set_user_crontab {
Subroutine set_user_crontab: not called CC=3 SCAR=24.8
85 0 my ($crontab) = @_;
86 0 ▶ open my $cron, q{|-}, 'crontab -' or err "crontab -e: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cron, "|-", "crontab -" or err("crontab -e: $!") 0 0 0
87 0 print {$cron} $crontab;
88 0 ▶ close $cron or err "crontab -e: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $cron or err("crontab -e: $!") 0 0 0
89 0 return;
90 }
91  
92 180 ▶sub force_last_cr {
Subroutine force_last_cr: called CC=2 SCAR=6.9
93 180 my ($s) = @_;
94 180 ▶ if ($s =~ /[^\n]\z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /[^\n]\z/msx) 68 112 180
95 68 $s .= "\n";
96 }
97 180 return $s;
98 }
99  
100 60 ▶sub set_cron {
Subroutine set_cron: called CC=2 SCAR=6.9
101 60 my ($crontab) = @_;
102 60 $crontab = force_last_cr($crontab);
103  
104 60 my $user_crontab = get_user_crontab();
105 60 my ($re, $start, $end) = get_markers();
106 60 ▶ if ($user_crontab !~ /$re/xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_crontab =~ /$re/msx) 30 30 60
107 30 $user_crontab = force_last_cr($user_crontab);
108 30 $user_crontab .= "$start\n$end\n";
109 }
110  
111 60 $user_crontab =~ s/$re/$start\n$crontab$end\n/xms;
112 60 set_user_crontab($user_crontab);
113 60 return;
114 }
115  
116 48 ▶sub del_cron {
Subroutine del_cron: called CC=1 SCAR=0.0
117 48 my $user_crontab = get_user_crontab();
118 6 my ($re) = get_markers();
119 6 $user_crontab =~ s/$re//xms;
120 6 set_user_crontab($user_crontab);
121 6 return;
122 }
123  
124  
125 1; # Magic true value required at end of module
126 __END__
« lib/Narada/Lock.pm ↑ Summary blib/lib/Narada/Log.pm »