blib/script/narada-setup-qmail

filter: Statementstmt 99.0% 104 / 105 Branchbran 67.3% 31 / 46 Conditioncond 76.1% 16 / 21 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 19 / 19 Podpod n/a totaltotal 87.1% 177 / 203 CC 35 SCAR 22.1
CC
35
Cov
88%
setup_qmail
19.9
main
12.0
lock_qmail
4.1
CRAP 9.1
« blib/lib/Narada/Lock.pm ↑ Summary blib/lib/Narada/Config.pm »
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 File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Cwd qw( cwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 ▶main(@ARGV) if !caller;
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 2 2
19  
20  
21 6 ▶sub err { die "narada-setup-qmail: @_\n" }
Subroutine err: called CC=1 SCAR=0.0
22  
23 sub main {
24 17 ▶ die "Usage: narada-setup-qmail [--clean|--lock]\n"
Subroutine main: called CC=12 SCAR=24.9
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 or @_ == 1 and $_[0] ne "--clean" and $_[0] ne "--lock" 4 13 17
Condition100%9/9
@_ > 1 or @_ == 1 and $_[0] ne "--clean" and $_[0] ne "--lock"
l!l&&r!l&&!r
2213
@_ == 1 and $_[0] ne "--clean" and $_[0] ne "--lock"
!ll&&!rl&&r
942
@_ == 1 and $_[0] ne "--clean"
!ll&&!rl&&r
456
MC/DC75%3/4
@_ > 1 or @_ == 1 and $_[0] ne "--clean" and $_[0] ne "--lock"
@_ > 1 @_ == 1 $_[0] ne "--clean" $_[0] ne "--lock"
Truth table80%4/5
A or B and C and D
A= @_ > 1B= @_ == 1C= $_[0] ne "--clean"D= $_[0] ne "--lock"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
25 if (@_ > 1)
26 || (@_ == 1 && $_[0] ne '--clean' && $_[0] ne '--lock');
27 13 ▶ my $clean = @_ && $_[0] eq '--clean';
Condition100%3/3
@_ && $_[0] eq '--clean'
!ll&&!rl&&r
445
MC/DC100%2/2
@_ && $_[0] eq '--clean'
@_ $_[0] eq '--clean'
Truth table100%3/3
A && B
A= @_B= $_[0] eq '--clean'
ABresult
0X0
100
111
28 13 ▶ my $lock = @_ && $_[0] eq '--lock';
Condition100%3/3
@_ && $_[0] eq '--lock'
!ll&&!rl&&r
454
MC/DC100%2/2
@_ && $_[0] eq '--lock'
@_ $_[0] eq '--lock'
Truth table100%3/3
A && B
A= @_B= $_[0] eq '--lock'
ABresult
0X0
100
111
29  
30 13 ▶ if ($clean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean) { } 5 8 13
31 5 path('var/use/qmail')->remove;
32 5 for my $file (ls_qmail()) {
33 7 ▶ unlink $file or err "unlink($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or err("unlink($file): $!") 0 7 7
34 }
35 5 unlink '.lock.qmail';
36 }
37 else {
38 8 ▶ if (Narada::detect() eq 'narada') {
Branch: 1 branch
BranchTrueFalseTotal
if (Narada::detect() eq "narada") 7 1 8
39 7 path('var/use/qmail')->touch;
40 }
41 8 ▶ if ($lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock) { } 4 4 8
42 4 lock_qmail();
43 }
44 else {
45 4 setup_qmail();
46 }
47 }
48  
49 13 return;
50 }
51  
52 sub lock_qmail {
53 4 ▶ my $cwd = cwd();
Subroutine lock_qmail: called CC=4 SCAR=14.0
54 4 my $lock = "$cwd/.lock.qmail";
55 4 replacefile($lock, '|false');
56  
57 4 my $temp = 'tmp/.qmail.symlink';
58 4 unlink $temp;
59 4 for my $qmail (ls_qmail()) {
60 5 ▶ symlink $lock, $temp or err "symlink($lock,$temp): $!";
Branch: 1 branch
BranchTrueFalseTotal
symlink $lock, $temp or err("symlink($lock,$temp): $!") 0 5 5
61 5 ▶ rename $temp, $qmail or err "rename($temp,$qmail): $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $temp, $qmail or err("rename($temp,$qmail): $!") 0 5 5
62 }
63  
64 4 return;
65 }
66  
67 sub setup_qmail {
68 4 ▶ my $cwd = cwd();
Subroutine setup_qmail: called CC=14 SCAR=29.9
69 4 my $lock = "$cwd/.lock.qmail";
70 4 my $temp = 'tmp/.qmail.symlink';
71 4 unlink $temp;
72  
73 4 for my $file (ls('config/qmail')) {
74 # read from config/qmail/
75 5 my $data = get_config("qmail/$file");
76 5 $data =~ s/^[|]/|cd \Q$cwd\E || exit(100); /gmxs;
77 # write to var/qmail/
78 5 my $path = "$cwd/var/qmail/$file";
79 5 replacefile($path, $data);
80 # symlink at ~/.qmail-
81 5 my $qmail = "$ENV{HOME}/.qmail-$file";
82 5 ▶ if (! -e $qmail) {
Branch: 3 branches
BranchTrueFalseTotal
if (not -e $qmail) { } 4 1 5
elsif (-l $qmail and _readlink($qmail) eq $lock) { } 1 0 1
elsif (not -l $qmail && _readlink($qmail) eq $path) { } 0 0 0
83 4 ▶ symlink $path, $qmail or err "symlink($path,$qmail): $!";
Branch: 1 branch
BranchTrueFalseTotal
symlink $path, $qmail or err("symlink($path,$qmail): $!") 0 4 4
84 }
85 ▶ elsif (-l $qmail && _readlink($qmail) eq $lock) {
Condition33%1/3
-l $qmail and _readlink($qmail) eq $lock
!ll&&!rl&&r
001
MC/DC0%0/2
-l $qmail and _readlink($qmail) eq $lock
-l $qmail _readlink($qmail) eq $lock
Truth table33%1/3
A and B
A= -l $qmailB= _readlink($qmail) eq $lock
ABresult
0X0
100
111
86 1 ▶ symlink $path, $temp or err "symlink($path,$temp): $!";
Branch: 1 branch
BranchTrueFalseTotal
symlink $path, $temp or err("symlink($path,$temp): $!") 0 1 1
87 1 ▶ rename $temp, $qmail or err "rename($temp,$qmail): $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $temp, $qmail or err("rename($temp,$qmail): $!") 0 1 1
88 }
89 ▶ elsif (! (-l $qmail && _readlink($qmail) eq $path)) {
Condition0%0/3
-l $qmail && _readlink($qmail) eq $path
!ll&&!rl&&r
000
MC/DC0%0/2
-l $qmail && _readlink($qmail) eq $path
-l $qmail _readlink($qmail) eq $path
Truth table0%0/3
A && B
A= -l $qmailB= _readlink($qmail) eq $path
ABresult
0X0
100
111
90 0 err "Conflict detected on $qmail";
91 }
92 }
93  
94 # cleanup
95 4 for my $file (grep {! -f "config/qmail/$_"} ls('var/qmail')) {
96 5 ▶ unlink "var/qmail/$file" or err "unlink(var/qmail/$file): $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink "var/qmail/$file" or err("unlink(var/qmail/$file): $!") 0 5 5
97 }
98 4 unlink $lock;
99 4 for my $file (grep {! -f} ls_qmail()) {
100 5 ▶ unlink $file or err "unlink($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or err("unlink($file): $!") 0 5 5
101 }
102  
103 4 return;
104 }
105  
106 sub ls_qmail {
107 26 ▶ my $cwd = cwd();
Subroutine ls_qmail: called CC=2 SCAR=6.9
108 my @files =
109 104 ▶ grep {-l && _readlink($_) =~ m{\A\Q$cwd\E/}xms}
Branch: 1 branch
BranchTrueFalseTotal
-l $_ and _readlink($_) =~ m[\A\Q$cwd\E/]msx 80 24 104
110 104 map {"$ENV{HOME}/$_"}
111 106 grep { m/\A[.]qmail-/xms }
112 26 ls($ENV{HOME});
113 26 return @files;
114 }
115  
116 sub replacefile {
117 25 ▶ my ($file, $data) = @_;
Subroutine replacefile: called CC=3 SCAR=11.1
118 25 (my $dir = $file) =~ s{/[^/]+\z}{}xms;
119 25 my ($fh, $temp) = tempfile(DIR => $dir);
120 23 print {$fh} $data;
121 23 ▶ close $fh or err "close($temp): $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or err("close($temp): $!") 0 23 23
122 23 ▶ rename $temp, $file or err "rename($temp,$file): $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $temp, $file or err("rename($temp,$file): $!") 0 23 23
123 23 return;
124 }
125  
126 sub ls {
127 46 ▶ my ($dir) = @_;
Subroutine ls: called CC=4 SCAR=13.9
128 46 ▶ opendir my $d, $dir or err "opendir($dir): $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $d, $dir or err("opendir($dir): $!") 4 42 46
129 42 ▶ my @files = grep { $_ ne q{.} && $_ ne q{..} } readdir $d;
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "." and $_ ne ".." 189 42 231
130 42 ▶ closedir $d or err "closedir($dir): $!";
Branch: 1 branch
BranchTrueFalseTotal
closedir $d or err("closedir($dir): $!") 0 42 42
131 42 return @files;
132 }
133  
134 sub _readlink {
135 89 ▶ my ($link) = @_;
Subroutine _readlink: called CC=2 SCAR=6.9
136 89 ▶ my $path = readlink $link or err "readlink($link): $!";
Branch: 1 branch
BranchTrueFalseTotal
my $path = readlink $link or err("readlink($link): $!") 2 87 89
137 87 return $path;
138 }
139  
140  
141 2 1; # Magic true value required at end of module
142 __END__
143  
144 =encoding utf8
145  
146 =head1 NAME
147  
148 narada-setup-qmail - install/remove project qmail configuration
149  
150  
151 =head1 VERSION
152  
153 This document describes narada-setup-qmail version v2.3.4
154  
155  
156 =head1 USAGE
157  
158 narada-setup-qmail [--clean|--lock]
159  
160  
161 =head1 DESCRIPTION
162  
163 Should be executed in project deploy directory (or Narada 1.x project root
164 directory).
165  
166 Sync Narada project's qmail configuration with user's C<~/.qmail-*> files.
167  
168 When executed with C<--clean> option will remove all C<~/.qmail-*> files
169 related to this project.
170  
171 When executed with C<--lock> option will setup all C<~/.qmail-*> files related
172 to this project to exit with code which means that the delivery failed but
173 should be tried again in a little while (soft error). To undo this effect run
174 again without options or with C<--clean>.
175  
176  
177 =head1 SYNTAX OF "config/qmail/files"
178  
179 Syntax of "config/qmail/files" is same as described in "man dot-qmail",
180 but commands in project's qmail configuration will be executed in project's
181 root directory instead of user's home directory. If config file have piped
182 command (started with "|") it will be modified to
183 "|cd /path/to/project || exit(100); " command will be added on-the-fly before
184 user command, i.e. every command line in "config/qmail/file_with_command" like:
185  
186 |some_script some_opt;
187  
188 will turn into line in user's dir like:
189  
190 |cd /path/to/project || exit(100); some_script some_opt;
191  
192  
193 =head1 DIAGNOSTICS
194  
195 =over
196  
197 =item C<< conflict detected on ~/.qmail-NAME >>
198  
199 This project have config/qmail/NAME, but ~/.qmail-NAME already exists and
200 it isn't symlink to this project's var/qmail/NAME file.
201  
202 You should manually resolve this conflict by either renaming
203 config/qmail/NAME file in this project or removing ~/.qmail-NAME.
204 After that you should run narada-setup-qmail again.
205  
206 =back
207  
208  
209 =head1 CONFIGURATION AND ENVIRONMENT
210  
211 config/qmail/*
212 var/qmail/*
213 ~/.qmail-*
214 var/use/qmail
215 .lock.qmail
216  
217  
218 =head1 SUPPORT
219  
220 =head2 Bugs / Feature Requests
221  
222 Please report any bugs or feature requests through the issue tracker
223 at L<https://github.com/powerman/Narada/issues>.
224 You will be notified automatically of any progress on your issue.
225  
226 =head2 Source Code
227  
228 This is open source software. The code repository is available for
229 public review and contribution under the terms of the license.
230 Feel free to fork the repository and submit pull requests.
231  
232 L<https://github.com/powerman/Narada>
233  
234 git clone https://github.com/powerman/Narada.git
235  
236 =head2 Resources
237  
238 =over
239  
240 =item * MetaCPAN Search
241  
242 L<https://metacpan.org/search?q=Narada>
243  
244 =item * CPAN Ratings
245  
246 L<http://cpanratings.perl.org/dist/Narada>
247  
248 =item * AnnoCPAN: Annotated CPAN documentation
249  
250 L<http://annocpan.org/dist/Narada>
251  
252 =item * CPAN Testers Matrix
253  
254 L<http://matrix.cpantesters.org/?dist=Narada>
255  
256 =item * CPANTS: A CPAN Testing Service (Kwalitee)
257  
258 L<http://cpants.cpanauthors.org/dist/Narada>
259  
260 =back
261  
262  
263 =head1 AUTHOR
264  
265 Alex Efros E<lt>powerman@cpan.orgE<gt>
266  
267 Nick Levchenko C<< <nick-lev@ya.ru> >>
268  
269  
270 =head1 COPYRIGHT AND LICENSE
271  
272 This software is Copyright (c) 2008-2016 by Alex Efros E<lt>powerman@cpan.orgE<gt>.
273  
274 This is free software, licensed under:
275  
276 The MIT (X11) License
277  
278 =cut
« blib/lib/Narada/Lock.pm ↑ Summary blib/lib/Narada/Config.pm »