| 1 | package MMM::Sync; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 6 | ▶ | use IPC::Open3; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 6 | ▶ | use IO::Select; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 6 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | MMM::Sync - A module to run application | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 FUNCTIONS | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =head2 new($source, $dest, %options) | ||||||||||||||
| 17 | |||||||||||||||
| 18 | Create a new MMM::Sync object where $source is the url to sync, | ||||||||||||||
| 19 | $dest the local directory, and %options options to pass to sync tool. | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =cut | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 6 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 24 | 6 | my ($class, $source, $dest, %options) = @_; | |||||||||||||
| 25 | 6 | ▶ | $source =~ m:/$: or $source .= '/'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 6 | ▶ | $dest =~ m:/$: or $dest .= '/'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 6 | my $sync = { | |||||||||||||
| 29 | source => $source, | ||||||||||||||
| 30 | dest => $dest, | ||||||||||||||
| 31 | options => { %options }, | ||||||||||||||
| 32 | }; | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 6 | ▶ | my $uri = URI->new($source) or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 6 | my $type = ucfirst(lc($uri->scheme())); | |||||||||||||
| 36 | 6 | ▶ | eval "use MMM::Sync::$type"; | ||||||||||||
|
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
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 6 | bless($sync, "MMM::Sync::$type"); | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =head2 get_output | ||||||||||||||
| 41 | |||||||||||||||
| 42 | Return the log resulting of sync() | ||||||||||||||
| 43 | |||||||||||||||
| 44 | =cut | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | ▶ | sub get_output { | ||||||||||||
|
Subroutine
get_output: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 47 | 0 | my ($self) = @_; | |||||||||||||
| 48 | $self->{output} | ||||||||||||||
| 49 | 0 | } | |||||||||||||
| 50 | |||||||||||||||
| 51 | =head2 reset_output | ||||||||||||||
| 52 | |||||||||||||||
| 53 | Reset internal log | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =cut | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | ▶ | sub reset_output { | ||||||||||||
|
Subroutine
reset_output: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 58 | 0 | my ($self) = @_; | |||||||||||||
| 59 | 0 | $self->{output} = undef; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =head2 buildcmd | ||||||||||||||
| 63 | |||||||||||||||
| 64 | Return the command to run to sync the tree | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =head2 sync | ||||||||||||||
| 67 | |||||||||||||||
| 68 | Run the synchronzation process. | ||||||||||||||
| 69 | Return 0 on success, 1 when retry is suggest, 2 for unccorectable error. | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =cut | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 2 | ▶ | sub sync { | ||||||||||||
|
Subroutine
sync: called CC=10 SCAR=30.4
Pod
documented
| |||||||||||||||
| 74 | 2 | my ($self) = @_; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 2 | ▶ | my @command = $self->buildcmd() or return 2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 2 | my $pid; | |||||||||||||
| 78 | 0 | ▶ | my $call_exit = sub { kill(15, $pid) if ($pid); }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||||||
| 79 | 2 | local $SIG{'TERM'} = $call_exit; | |||||||||||||
| 80 | 2 | local $SIG{'KILL'} = $call_exit; | |||||||||||||
| 81 | 2 | local $SIG{'PIPE'} = $call_exit; | |||||||||||||
| 82 | 2 | ▶ | if ( $pid = open3( my $in, my $out, my $err, @command ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 2 | close($in); | |||||||||||||
| 84 | 2 | my $ios = IO::Select->new( $out, $err ); | |||||||||||||
| 85 | 2 | ▶ | while ( my @handle = $ios->can_read() ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 2 | foreach my $h (@handle) { | |||||||||||||
| 87 | 2 | my $l = <$h>; | |||||||||||||
| 88 | 2 | ▶ | if ($l) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | 0 | chomp($l); | |||||||||||||
| 90 | 0 | my $err_src = undef; | |||||||||||||
| 91 | 0 | ▶ | if ($h eq $out) { $err_src = 'STDOUT'; } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 92 | 0 | elsif ($h eq $err) { $err_src = 'STDERR'; } | |||||||||||||
| 93 | 0 | my $aline = $self->_analyze_output($err_src, $l); | |||||||||||||
| 94 | 0 | ▶ | push(@{ $self->{output} }, $aline) if ($aline); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | } | ||||||||||||||
| 96 | 2 | ▶ | $ios->remove($h) if ( eof($h) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | } | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 2 | waitpid( $pid, 0 ); | |||||||||||||
| 101 | 2 | my $exitstatus = $? >> 8; | |||||||||||||
| 102 | 2 | return $self->_exitcode($exitstatus); | |||||||||||||
| 103 | } else { | ||||||||||||||
| 104 | 0 | return ( 2 ); | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 0 | ▶ | sub _analyze_output { | ||||||||||||
|
Subroutine
_analyze_output: not called CC=2 SCAR=17.9
| |||||||||||||||
| 109 | 0 | my ($self, $src, $line) = @_; | |||||||||||||
| 110 | 0 | ▶ | if ($src eq 'STDERR') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 0 | return $line; | |||||||||||||
| 112 | } else { | ||||||||||||||
| 113 | 0 | return; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | ▶ | sub _exitcode { | ||||||||||||
|
Subroutine
_exitcode: not called CC=2 SCAR=17.9
| |||||||||||||||
| 118 | 0 | my ($self, $exitcode) = @_; | |||||||||||||
| 119 | 0 | ▶ | return ($exitcode == 0 ? 0 : 1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | =head2 AUTHOR | ||||||||||||||
| 123 | |||||||||||||||
| 124 | Olivier Thauvin <nanardon@nanardon.zarb.org> | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =cut | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 6 | 1; | |||||||||||||