File Coverage

blib/lib/SVN/Notify/Snapshot.pm
Criterion Covered Total %
statement 21 67 31.3
branch 0 20 0.0
condition 0 4 0.0
subroutine 7 14 50.0
pod 2 2 100.0
total 30 107 28.0


line stmt bran cond sub pod time code
1             package SVN::Notify::Snapshot;
2             $SVN::Notify::Snapshot::VERSION = '0.04';
3              
4 1     1   3759 use strict;
  1         2  
  1         28  
5 1     1   4 use File::Spec;
  1         3  
  1         22  
6 1     1   5 use File::Path qw( mkpath );
  1         2  
  1         66  
7 1     1   1075 use File::Temp qw( tempdir );
  1         36065  
  1         88  
8 1     1   12 use File::Basename qw( dirname fileparse );
  1         2  
  1         70  
9 1     1   1604 use SVN::Notify ();
  1         26794  
  1         67  
10             @SVN::Notify::Snapshot::ISA = qw(SVN::Notify);
11              
12 1         826 use constant SuffixMap => {
13             '.tar' => '_tar',
14             '.tar.gz' => '_tar_gzip',
15             '.tgz' => '_tar_gzip',
16             '.tbz' => '_tar_bzip2',
17             '.tbz2' => '_tar_bzip2',
18             '.tar.bz2' => '_tar_bzip2',
19             '.zip' => '_zip',
20 1     1   10 };
  1         2  
21              
22             __PACKAGE__->register_attributes(
23             handle_path => 'handle-path=s',
24             append_rev => 'append-rev',
25             tag_regex => 'tag-regex=s',
26             );
27              
28             sub prepare {
29 0     0 1   my $self = shift;
30 0           $self->prepare_recipients;
31 0           $self->prepare_files;
32             }
33              
34             sub execute {
35 0     0 1   my ($self) = @_;
36 0 0         my $repos = $self->{repos_path} or return;
37 0 0         my $path = $self->{handle_path} or die "Must specify handle_path";
38 0           $DB::single = 1;
39 0           foreach my $to ( @{$self->{to}} ) {
  0            
40 0           my $temp = tempdir( CLEANUP => 0 );
41              
42 0           my ($to_base, $to_path, $to_suffix) = fileparse($to, qr{\..*});
43 0 0         my $method = $self->SuffixMap->{lc($to_suffix)}
44             or die "Unknown suffix: $to_suffix";
45              
46 0 0         my $base = (
47             defined($self->{snapshot_base})
48             ? $self->{snapshot_base} : $to_base
49             );
50              
51 0 0         if ( $self->append_rev ) {
52 0           $to = "$to_path/$to_base-".$self->{revision}.$to_suffix;
53 0           $base .= '-'.$self->{revision};
54             }
55              
56 0 0         if ( defined $self->{tag_regex} ) {
57 0           my $regex = $self->{tag_regex};
58 0           my ($tag) = grep /$regex/, @{$self->{'files'}->{'A'}};
  0            
59 0 0         return unless $tag;
60 0           $path = $tag;
61 0 0         unless ( $self->append_rev ) {
62 0           $tag =~ s/^.+\/tags\/(.+)/$1/;
63 0           $base = $tag;
64             }
65             }
66              
67 0           my $from = File::Spec->catdir($temp, $base);
68 0 0         mkpath([ dirname($from) ]) unless -d dirname($from);
69              
70 0           $self->_run(
71             'svn', 'export',
72             -r => $self->{revision},
73             "file://$repos/$path" => $from,
74             );
75              
76 0           $self->can($method)->($self, $temp, $from, $to);
77             }
78             }
79              
80             sub _tar {
81 0     0     my ($self, $temp, $from, $to, $mode) = @_;
82 0   0       my $TAR = SVN::Notify->find_exe('tar') || '/bin/tar';
83              
84 0   0       $mode ||= '-cf';
85 0           $self->_run( $TAR, $mode, $to, '-C' => $temp, '.' ) ;
86             }
87              
88             sub _tar_gzip {
89 0     0     my $self = shift;
90 0           $self->_tar(@_, '-czf');
91             }
92              
93             sub _tar_bzip2 {
94 0     0     my $self = shift;
95 0           $self->_tar(@_, '-cjf');
96             }
97              
98             sub _zip {
99 0     0     my ($self, $temp, $from, $to, $mode) = @_;
100 0           my $ZIP = SVN::Notify->find_exe('zip');
101              
102 0           require Cwd;
103 0           my $dir = Cwd::getcwd();
104 0           chdir $temp;
105              
106 0           $self->_run( $ZIP, -r => $to, '.' );
107             }
108              
109             sub _run {
110 0     0     my $self = shift;
111 0 0         (system { $_[0] } @_) == 0 or die "Running [@_] failed with $?: $!";
  0            
112             }
113              
114             1;
115              
116             __END__