WE_Frontend/Publish/Rsync.pm

filter: Statementstmt 8.2% 14 / 170 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 40.0% 4 / 10 Podpod 0.0% 0 / 2 totaltotal 5.6% 18 / 318 CC 55 SCAR 69.6
CC
55
Cov
5%
publish_rsync
1640.0
publish_files
132.0
_rsyncexe
12.0
CRAP 1049.1
« WE_Frontend/Publish/Rdist.pm ↑ Summary WE_Frontend/Indexer/Htdig.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: Rsync.pm,v 1.16 2004/12/23 10:53:41 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2002 Online Office Berlin. All rights reserved.
8 # Copyright (C) 2002 Slaven Rezic.
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License, see the file COPYING.
11  
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16  
17 package WE_Frontend::Publish::Rsync;
18  
19 # package WE_Frontend::Main;
20  
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 $VERSION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/);
24  
25 1 ▶use WE_Frontend::Publish;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶sub _rsyncexe {
Subroutine _rsyncexe: not called CC=3 SCAR=24.8
30 0 my $self = shift;
31 ($self->Config->staging->stagingext &&
32 $self->Config->staging->stagingext->{'rsyncexe'}
33 0 ▶ ? $self->Config->staging->stagingext->{'rsyncexe'}
Branch: 1 branch
BranchTrueFalseTotal
$self->Config->staging->stagingext && $self->Config->staging->stagingext->{'rsyncexe'} ? : 0 0 0
Condition0%0/3
$self->Config->staging->stagingext and $self->Config->staging->stagingext->{"rsyncexe"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->Config->staging->stagingext and $self->Config->staging->stagingext->{"rsyncexe"}
$self->Config->staging->stagingext $self->Config->staging->stagingext->{"rsyncexe"}
Truth table0%0/3
A and B
A= $self->Config->staging->stagingextB= $self->Config->staging->stagingext->{"rsyncexe"}
ABresult
0X0
100
111
34 : 'rsync'
35 );
36 }
37  
38 0 ▶sub _deleteold {
Subroutine _deleteold: not called CC=3 SCAR=24.8
39 0 my $self = shift;
40 ($self->Config->staging->stagingext &&
41 $self->Config->staging->stagingext->{'deleteold'}
42 0 ▶ ? $self->Config->staging->stagingext->{'deleteold'}
Branch: 1 branch
BranchTrueFalseTotal
$self->Config->staging->stagingext && $self->Config->staging->stagingext->{'deleteold'} ? : 0 0 0
Condition0%0/3
$self->Config->staging->stagingext and $self->Config->staging->stagingext->{"deleteold"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->Config->staging->stagingext and $self->Config->staging->stagingext->{"deleteold"}
$self->Config->staging->stagingext $self->Config->staging->stagingext->{"deleteold"}
Truth table0%0/3
A and B
A= $self->Config->staging->stagingextB= $self->Config->staging->stagingext->{"deleteold"}
ABresult
0X0
100
111
43 : 0
44 );
45 }
46  
47 # for compatibility:
48 0 ▶sub WE_Frontend::Main::publish_rsync {
Subroutine publish_rsync: not called CC=1 SCAR=6.9
49 0 my($self, %args) = @_;
50 0 publish_rsync($self, %args);
51 }
52  
53 0 ▶sub publish_rsync {
Subroutine publish_rsync: not called CC=40 SCAR=74.0
Pod undocumented
54 0 my($self, %args) = @_;
55  
56 0 my $do_exec = !delete $args{-n};
57 0 my $v = delete $args{-verbose};
58 0 my $since = delete $args{-since};
59 0 ▶ if (defined $since) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $since) 0 0 0
60 0 warn "The -since option is ignored with publish_rsync!";
61 }
62 0 my $liveuser = $self->Config->staging->user;
63 0 my $livedirectory = $self->Config->staging->directory;
64 0 my $livecgidirectory = $self->Config->staging->cgidirectory;
65 0 my $livehost = $self->Config->staging->host;
66 0 my $pubhtmldir = $self->Config->paths->pubhtmldir;
67 0 my $cgidir = $self->Config->paths->cgidir;
68 my @extracgi = (ref $self->Config->project->stagingextracgi eq 'ARRAY'
69 0 ▶ ? @{ $self->Config->project->stagingextracgi }
Branch: 1 branch
BranchTrueFalseTotal
ref $self->Config->project->stagingextracgi eq 'ARRAY' ? : 0 0 0
70 : ()
71 );
72 # rsync 2.5.1 seems to be reliable, but previous version could hang.
73 # So give the user the chance to change the rsync executable path.
74 0 my $rsyncexe = $self->WE_Frontend::Publish::Rsync::_rsyncexe;
75 0 my $deleteold = $self->WE_Frontend::Publish::Rsync::_deleteold;
76  
77 0 ▶ if (defined $liveuser && !defined $livehost) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $liveuser and not defined $livehost) 0 0 0
Condition0%0/3
defined $liveuser and not defined $livehost
!ll&&!rl&&r
000
MC/DC0%0/2
defined $liveuser and not defined $livehost
defined $liveuser not defined $livehost
Truth table0%0/3
A and not(B)
A= defined $liveuserB= not defined $livehost
ABresult
0X0
100
111
78 0 die "\$livehost should be also set if \$liveuser is set";
79 }
80 0 ▶ if (!defined $pubhtmldir || $pubhtmldir eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pubhtmldir or $pubhtmldir eq "") 0 0 0
Condition0%0/3
not defined $pubhtmldir or $pubhtmldir eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $pubhtmldir or $pubhtmldir eq ""
not defined $pubhtmldir $pubhtmldir eq ""
Truth table0%0/3
not(A) or B
A= not defined $pubhtmldirB= $pubhtmldir eq ""
ABresult
1X1
011
000
81 0 die "The publish html directory is missing (config member WEsiteinfo->paths->pubhtmldir)";
82 }
83 0 ▶ if (@extracgi && (!defined $livecgidirectory || $livecgidirectory eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extracgi and !defined($livecgidirectory) || $livecgidirectory eq "") 0 0 0
Condition0%0/6
@extracgi and !defined($livecgidirectory) || $livecgidirectory eq ""
!ll&&!rl&&r
000
not defined $livecgidirectory or $livecgidirectory eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
@extracgi and !defined($livecgidirectory) || $livecgidirectory eq ""
@extracgi not defined $livecgidirectory $livecgidirectory eq ""
Truth table0%0/4
A and not(B) or C
A= @extracgiB= not defined $livecgidirectoryC= $livecgidirectory eq ""
ABCresult
0XX0
1000
11X1
1011
84 0 die "Extra CGI scripts are defined (@extracgi),
85 but the WEsiteinfo->staging->cgidirectory config is missing";
86 }
87  
88 0 ▶ if ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 0 0
89 0 print <<EOF
90 Using Rsync Protocol.
91 0 ▶Rsync remote host: @{[ defined $livehost ? $livehost : "localhost" ]}
Branch: 1 branch
BranchTrueFalseTotal
defined $livehost ? : 0 0 0
92 0 ▶Rsync remote user: @{[ defined $liveuser ? $liveuser : "current" ]}
Branch: 1 branch
BranchTrueFalseTotal
defined $liveuser ? : 0 0 0
93 Rsync remote directory: $livedirectory
94 0 ▶@{[ @extracgi ? "Rsync remote CGI directory: $livecgidirectory" : "" ]}
Branch: 1 branch
BranchTrueFalseTotal
@extracgi ? : 0 0 0
95 0 ▶@{[ $deleteold ? "Delete unused remote files" : "Leave unused remote files" ]}
Branch: 1 branch
BranchTrueFalseTotal
$deleteold ? : 0 0 0
96 EOF
97 }
98  
99 # XXX same as in Rdist.pm
100 0 my @cvs_exclude_pat = @{ WE_Frontend::Publish->cvs_exclude };
101 0 my @we_exclude_pat = @{ WE_Frontend::Publish->we_exclude };
102 0 my @exclude_pat = (@cvs_exclude_pat, @we_exclude_pat);
103 0 my @exclude;
104 my %additional;
105 0 ▶ if ($self->Config->project->stagingexceptpat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Config->project->stagingexceptpat) 0 0 0
106 0 push @exclude_pat, @{ $self->Config->project->stagingexceptpat };
107 }
108 0 ▶ if ($self->Config->project->stagingexcept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Config->project->stagingexcept) 0 0 0
109 0 push @exclude, @{ $self->Config->project->stagingexcept };
110 }
111 #XXX not yet used:
112 0 ▶ if ($self->Config->project->stagingadditional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Config->project->stagingadditional) 0 0 0
113 0 %additional = %{ $self->Config->project->stagingadditional };
114 }
115  
116 0 my @directories;
117 my @files;
118  
119 # first create target directories
120 0 my @mkdircmd;
121 0 $mkdircmd[0] = "mkdir -p $livedirectory";
122 0 ▶ if (@extracgi) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extracgi) 0 0 0
123 0 $mkdircmd[0] .= "; mkdir -p $livecgidirectory";
124 }
125 0 ▶ if (defined $livehost) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $livehost) 0 0 0
126 0 unshift @mkdircmd, ('ssh', '-l', $liveuser, $livehost);
127 }
128  
129 0 ▶ if (!$do_exec) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $do_exec) { } 0 0 0
130 0 print join(" ", @mkdircmd), "\n";
131 } else {
132 0 _system(@mkdircmd);
133 }
134  
135 0 my($rsync1, $rsync2, $src1, $src2, $cmd, $cmd2);
136 # Don't spread confusion! Now the rsync commands are constructed.
137 # They consist of
138 # $cmd = $rsync1 $src1 $rsync2 targetdirectory1
139 # $cmd2 = $rsync1 $src2 $rsync2 targetdirectory2
140  
141 0 $rsync1 = $rsyncexe; # command and first parameters
142 0 ▶ if (!$do_exec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($do_exec) 0 0 0
143 0 $rsync1 .= ' -n';
144 }
145 0 $rsync1 .= " -v"; # be verbose
146 0 $rsync1 .= " -l"; # XXX check for destination system and revert to -L if necessary
147 0 ▶ if (defined $livehost) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $livehost) 0 0 0
148 0 $rsync1 .= " -z"; # compress if remote
149 0 $rsync1 .= " -e ssh"; # just in case there's an old rsync defaulting to rsh
150 }
151 0 ▶ if ($deleteold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deleteold) 0 0 0
152 0 $rsync1 .= " --delete";
153 }
154  
155 0 $src1 = ""
156 # in/excludes
157 . " " . join(" ", map "--exclude \"$_\"", @exclude_pat)
158 . " " . join(" ", map "--exclude \"/$_\"", @exclude)
159 # source
160 . " -r $pubhtmldir/";
161  
162 $src2 = ""
163 # in/excludes
164 . " " . join(" ", map "--exclude \"/$_\"", @exclude)
165 . " --exclude .cvsignore"
166 # source
167 0 . " " . join(" ", map { "$cgidir/$_" } @extracgi);
168  
169 0 $rsync2 = " "; # destination without directory
170 0 ▶ if (defined $liveuser && defined $livehost) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $liveuser and defined $livehost) 0 0 0
Condition0%0/3
defined $liveuser and defined $livehost
!ll&&!rl&&r
000
MC/DC0%0/2
defined $liveuser and defined $livehost
defined $liveuser defined $livehost
Truth table0%0/3
A and B
A= defined $liveuserB= defined $livehost
ABresult
0X0
100
111
171 0 $rsync2 .= " $liveuser\@$livehost:";
172 }
173  
174 0 ▶ if ($self->Config->project->projectext &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Config->project->projectext and $self->Config->project->projectext->{"extrastagingsub"}) 0 0 0
Condition0%0/3
$self->Config->project->projectext and $self->Config->project->projectext->{"extrastagingsub"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->Config->project->projectext and $self->Config->project->projectext->{"extrastagingsub"}
$self->Config->project->projectext $self->Config->project->projectext->{"extrastagingsub"}
Truth table0%0/3
A and B
A= $self->Config->project->projectextB= $self->Config->project->projectext->{"extrastagingsub"}
ABresult
0X0
100
111
175 $self->Config->project->projectext->{'extrastagingsub'}) {
176 0 ▶ if ($do_exec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_exec) { } 0 0 0
177 0 $self->Config->project->projectext->{'extrastagingsub'}->();
178 } else {
179 0 print "Call subroutine " . $self->Config->project->projectext->{'extrastagingsub'} . "\n";
180 }
181 }
182  
183 # rsync is often in /usr/local/bin
184 0 local $ENV{PATH} = $ENV{PATH} . ":/usr/local/bin";
185  
186 0 $cmd .= "$rsync1$src1$rsync2$livedirectory";
187 0 ▶ if (!$do_exec) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $do_exec) { } 0 0 0
188 0 print "exec $cmd\n";
189 } else {
190 0 ▶ if (defined $v && $v > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v > 1) 0 0 0
Condition0%0/3
defined $v and $v > 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v and $v > 1
defined $v $v > 1
Truth table0%0/3
A and B
A= defined $vB= $v > 1
ABresult
0X0
100
111
191 0 print "Command: $cmd\n";
192 }
193 0 _system($cmd);
194 0 ▶ if ($?/256 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? / 256 != 0) 0 0 0
195 0 my $error = "Error code @{[ $?/256 ]} while doing: $cmd
196 PATH is $ENV{PATH}";
197 0 ▶ print "$error\n" if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
198 0 die $error;
199 }
200 }
201  
202 0 ▶ if (@extracgi) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extracgi) 0 0 0
203 0 $cmd2 = "$rsync1$src2$rsync2$livecgidirectory";
204 0 ▶ if (!$do_exec) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $do_exec) { } 0 0 0
205 0 print "exec $cmd2\n";
206 } else {
207 0 ▶ if (defined $v && $v > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v > 1) 0 0 0
Condition0%0/3
defined $v and $v > 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v and $v > 1
defined $v $v > 1
Truth table0%0/3
A and B
A= defined $vB= $v > 1
ABresult
0X0
100
111
208 0 print "Command: $cmd2\n";
209 }
210 0 _system($cmd2);
211 0 ▶ if ($?/256 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? / 256 != 0) 0 0 0
212 0 my $error = "Error code @{[ $?/256 ]} while doing: $cmd2
213 PATH is $ENV{PATH}";
214 0 ▶ print "$error\n" if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
215 0 die $error;
216 }
217 }
218 }
219  
220 0 return;
221 }
222  
223 # Apache.pm-friendly system()
224 0 ▶sub _system {
Subroutine _system: not called CC=2 SCAR=17.9
225 0 my $cmd = shift;
226 0 open(SYS, "$cmd|");
227 0 ▶ while(<SYS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline SYS)) 0 0 0
228 0 print $_;
229 }
230 0 close SYS;
231 }
232  
233 0 ▶sub publish_files {
Subroutine publish_files: not called CC=11 SCAR=48.8
Pod undocumented
234 0 my($self, $selected_files, %args) = @_;
235  
236 0 my $do_exec = !delete $args{-n};
237 0 my $v = delete $args{-verbose};
238  
239 0 require File::Temp;
240  
241 # rsync is often in /usr/local/bin
242 0 local $ENV{PATH} = $ENV{PATH} . ":/usr/local/bin";
243  
244 0 my(@files, %dir);
245 0 for my $file (@$selected_files) {
246 0 push @files, $file;
247 0 my @p = split m|/|, $file;
248 0 for my $i (0 .. $#p-1) {
249 0 $dir{join "/", @p[0 .. $i]}++;
250 }
251 }
252  
253 0 my($fh, $file) = File::Temp::tempfile(UNLINK => 1);
254 0 my @all_files = (keys(%dir), @files);
255 0 my $include = join("\n", map { "/$_" } sort @all_files) . "\n";
256 0 print $fh $include;
257 0 close $fh;
258  
259 0 ▶ if ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 0 0
260 0 print STDERR "Include:\n$include\n";
261 }
262  
263 #my @rsync_args = ("-r", "-a", "-p", "-t"); # XXX -a?
264 0 my @rsync_args = ("-rptgoD", "-vz", "-L", "--copy-unsafe-links"); # XXX -a?
265 0 ▶ if (!$do_exec) { push @rsync_args, "-n" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($do_exec) 0 0 0
266 # if ($v) { push @rsync_args, "-Pv" }
267 0 ▶ if ($self->WE_Frontend::Publish::Rsync::_deleteold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->WE_Frontend::Publish::Rsync::_deleteold) 0 0 0
268 0 push @rsync_args, "--delete";
269 }
270
271 0 my $liveuser = $self->Config->staging->user;
272 0 my $livehost = $self->Config->staging->host;
273 0 my $liversakey = $self->Config->staging->rsakey;
274  
275 0 ▶ if (defined $liveuser && defined $liversakey){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $liveuser and defined $liversakey) 0 0 0
Condition0%0/3
defined $liveuser and defined $liversakey
!ll&&!rl&&r
000
MC/DC0%0/2
defined $liveuser and defined $liversakey
defined $liveuser defined $liversakey
Truth table0%0/3
A and B
A= defined $liveuserB= defined $liversakey
ABresult
0X0
100
111
276 0 push @rsync_args, "-e ssh -l $liveuser -i $liversakey";
277 }
278  
279 0 my $from = $self->Config->paths->pubhtmldir;
280 0 $from =~ s{ (?<!/)$ }{/}x; # add trailing slash
281  
282 0 my $to = $self->Config->staging->directory;
283 0 $to =~ s{ (?<!/)$ }{/}x; # add trailing slash
284
285 0 ▶ if (defined $livehost) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $livehost) 0 0 0
286 0 $to = "$livehost:$to";
287 }
288  
289 0 my @cmd = ($self->WE_Frontend::Publish::Rsync::_rsyncexe, @rsync_args);
290 0 push @cmd, ("--include-from=$file", "--exclude=**");
291 0 push @cmd, $from, $to;
292 0 warn "@cmd";
293  
294 0 my $ret = 1;
295 0 ▶ if ($do_exec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_exec) 0 0 0
296 0 ▶ system(@cmd) and do {
Branch: 1 branch
BranchTrueFalseTotal
system @cmd and do { warn qq[Error code is $?, please see man rsync for explanation.\nPATH is $ENV{"PATH"}]; $ret = 0 } 0 0 0
297 0 warn "Error code is $?, please see man rsync for explanation.
298 PATH is $ENV{PATH}";
299 0 $ret = 0;
300 };
301 }
302  
303 0 unlink $file;
304  
305 0 $ret;
306 }
307  
308 1 1;
309  
310 __END__
« WE_Frontend/Publish/Rdist.pm ↑ Summary WE_Frontend/Indexer/Htdig.pm »