blib/script/narada-setup-cron

filter: Statementstmt 87.9% 80 / 91 Branchbran 65.3% 17 / 26 Conditioncond 100% 6 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 89.4% 17 / 19 Podpod n/a totaltotal 84.1% 122 / 145 CC 15 SCAR 15.8
CC
15
Cov
84%
get_user_crontab
12.0
set_user_crontab
12.0
main
8.0
CRAP 4.9
« blib/lib/Narada.pm ↑ Summary Build »
1 #!/usr/bin/env perl
2 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = 'v2.3.4';
8  
9 2 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use lib "$FindBin::Bin/../lib/perl5";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Narada;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Narada::Config qw( get_config set_config );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 2 ▶main(@ARGV) if !caller;
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 2 2
17  
18  
19 2 ▶sub err { die "narada-setup-cron: @_\n" }
Subroutine err: called CC=1 SCAR=0.0
20  
21 sub main {
22 16 ▶ die "Usage: narada-setup-cron [--clean]\n"
Subroutine main: called CC=8 SCAR=20.8
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 or @_ == 1 and $_[0] ne "--clean" 4 12 16
Condition100%6/6
@_ > 1 or @_ == 1 and $_[0] ne "--clean"
l!l&&r!l&&!r
2212
@_ == 1 and $_[0] ne "--clean"
!ll&&!rl&&r
842
MC/DC66%2/3
@_ > 1 or @_ == 1 and $_[0] ne "--clean"
@_ > 1 @_ == 1 $_[0] ne "--clean"
Truth table75%3/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 12 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 8 12
27 4 path('var/use/cron')->remove;
28 4 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 sub get_project_dir {
51 82 ▶ chomp (my $project_dir = `pwd`);
Subroutine get_project_dir: called CC=2 SCAR=6.9
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 sub process {
57 6 ▶ my ($crontab) = @_;
Subroutine process: called CC=1 SCAR=0.0
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 sub get_markers {
66 68 ▶ my $project_dir = get_project_dir();
Subroutine get_markers: called CC=1 SCAR=0.0
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 sub get_user_crontab {
74 0 ▶ local $/ = undef;
Subroutine get_user_crontab: not called CC=3 SCAR=24.8
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 0 ▶ open my $cron, q{-|}, 'EDITOR=cat crontab -e' or err "crontab -e: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cron, "-|", "EDITOR=cat crontab -e" or err("crontab -e: $!") 0 0 0
79 0 my $crontab = <$cron>;
80 0 ▶ close $cron or err "crontab -e: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $cron or err("crontab -e: $!") 0 0 0
81 0 return $crontab;
82 }
83  
84 sub set_user_crontab {
85 0 ▶ my ($crontab) = @_;
Subroutine set_user_crontab: not called CC=3 SCAR=24.8
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 sub force_last_cr {
93 180 ▶ my ($s) = @_;
Subroutine force_last_cr: called CC=2 SCAR=6.9
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 sub set_cron {
101 60 ▶ my ($crontab) = @_;
Subroutine set_cron: called CC=2 SCAR=6.9
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 sub del_cron {
117 6 ▶ my $user_crontab = get_user_crontab();
Subroutine del_cron: called CC=1 SCAR=0.0
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 2 1; # Magic true value required at end of module
126 __END__
127  
128 =encoding utf8
129  
130 =head1 NAME
131  
132 narada-setup-cron - synchronize project crontab with user crontab
133  
134  
135 =head1 VERSION
136  
137 This document describes narada-setup-cron version v2.3.4
138  
139  
140 =head1 USAGE
141  
142 narada-setup-cron [--clean]
143  
144  
145 =head1 DESCRIPTION
146  
147 Should be executed in project deploy directory (or Narada 1.x project root
148 directory).
149  
150 Install/remove your Narada project's cron configuration.
151  
152 When executed without params add/update project's cron configuration found
153 in "config/crontab" into user's crontab.
154  
155 When executed with --clean option or in case "config/crontab" doesn't exists
156 will remove project's cron configuration from user's crontab.
157  
158  
159 =head1 SYNTAX OF "config/crontab"
160  
161 Syntax of "config/crontab" is same as for system crontab, but commands in
162 project's cron configuration will be executed in project's root directory
163 instead of user's home directory. For this, the "cd /path/to/project ||
164 exit;" command will be added on-the-fly before user command, i.e. every
165 line in "config/crontab" like:
166  
167 * * * * * do something
168  
169 will turn into line in user's crontab like:
170  
171 * * * * * cd /path/to/project || exit; do something
172  
173  
174 =head1 DIAGNOSTICS
175  
176 =over
177  
178 =item C<< project directory must not contain \n >>
179  
180 Project root directory used in BEGIN/END markers in crontab, which has
181 line-based format and so directory name must not contain \n.
182  
183 Also project root directory used in "cd" command in crontab, which suffer
184 from same limitation.
185  
186 =item C<< crontab -e: %s >>
187  
188 Internal error, possible reason - user doesn't have access to system crontab.
189  
190 =back
191  
192  
193 =head1 CONFIGURATION AND ENVIRONMENT
194  
195 config/crontab/*
196 var/use/cron
197  
198 Narada 1.x project use C<config/crontab> instead of C<config/crontab/*>.
199  
200  
201 =head1 SUPPORT
202  
203 =head2 Bugs / Feature Requests
204  
205 Please report any bugs or feature requests through the issue tracker
206 at L<https://github.com/powerman/Narada/issues>.
207 You will be notified automatically of any progress on your issue.
208  
209 =head2 Source Code
210  
211 This is open source software. The code repository is available for
212 public review and contribution under the terms of the license.
213 Feel free to fork the repository and submit pull requests.
214  
215 L<https://github.com/powerman/Narada>
216  
217 git clone https://github.com/powerman/Narada.git
218  
219 =head2 Resources
220  
221 =over
222  
223 =item * MetaCPAN Search
224  
225 L<https://metacpan.org/search?q=Narada>
226  
227 =item * CPAN Ratings
228  
229 L<http://cpanratings.perl.org/dist/Narada>
230  
231 =item * AnnoCPAN: Annotated CPAN documentation
232  
233 L<http://annocpan.org/dist/Narada>
234  
235 =item * CPAN Testers Matrix
236  
237 L<http://matrix.cpantesters.org/?dist=Narada>
238  
239 =item * CPANTS: A CPAN Testing Service (Kwalitee)
240  
241 L<http://cpants.cpanauthors.org/dist/Narada>
242  
243 =back
244  
245  
246 =head1 AUTHOR
247  
248 Alex Efros E<lt>powerman@cpan.orgE<gt>
249  
250 Nick Levchenko E<lt>nick-lev@ya.ruE<gt>
251  
252  
253 =head1 COPYRIGHT AND LICENSE
254  
255 This software is Copyright (c) 2008-2016 by Alex Efros E<lt>powerman@cpan.orgE<gt>.
256  
257 This is free software, licensed under:
258  
259 The MIT (X11) License
260  
261  
262 =cut
« blib/lib/Narada.pm ↑ Summary Build »