File Coverage

blib/lib/DBIx/QuickDB/Util.pm
Criterion Covered Total %
statement 33 51 64.7
branch 5 26 19.2
condition 0 6 0.0
subroutine 10 13 76.9
pod 0 2 0.0
total 48 98 48.9


line stmt bran cond sub pod time code
1             package DBIx::QuickDB::Util;
2 24     24   274870 use strict;
  24         55  
  24         714  
3 24     24   124 use warnings;
  24         54  
  24         1449  
4              
5             our $VERSION = '0.000045';
6              
7 24     24   108 use File::Path qw/remove_tree/;
  24         69  
  24         1181  
8 24     24   2265 use IPC::Cmd qw/can_run/;
  24         172684  
  24         942  
9 24     24   115 use Carp qw/confess/;
  24         48  
  24         902  
10              
11 24     24   4618 use Importer Importer => 'import';
  24         43180  
  24         181  
12              
13             our @EXPORT_OK = qw/clone_dir strip_hash_defaults/;
14              
15             my ($RSYNC, $CP);
16              
17             BEGIN {
18 24     24   1989 local $@;
19 24         90 $RSYNC = can_run('rsync');
20 24         2286618 $CP = can_run('cp');
21             }
22              
23             sub clone_dir {
24 0 0   0 0 0 return _clone_dir_rsync(@_) if $RSYNC;
25 0 0       0 return _clone_dir_cp(@_) if $CP;
26 0         0 return _clone_dir_fcr(@_);
27             }
28              
29             sub _clone_dir_rsync {
30 0     0   0 my ($src, $dest, %params) = @_;
31 0 0       0 system($RSYNC, '-a', '--delete', '--exclude' => '.nfs*', $params{checksum} ? ('-c') : (), $params{verbose} ? ( '-vP' ) : (), "$src/", $dest) and die "$RSYNC returned $?";
    0          
    0          
32             }
33              
34             sub _clone_dir_cp {
35 2     2   219331 my ($src, $dest, %params) = @_;
36 2         5 my $err;
37 2 50       786 remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
38 2 50       21910 system($CP, '-a', $params{verbose} ? ( '-v' ) : (), "$src/.", $dest) and die "$CP returned $?";
    50          
39             }
40              
41             sub _clone_dir_fcr {
42 2     2   18105 my ($src, $dest, %params) = @_;
43 2         39 require File::Copy::Recursive;
44              
45 2         9 my $err;
46 2 50       955 remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
47 2 50       38 File::Copy::Recursive::dircopy($src, $dest) or die "$!";
48             }
49              
50             sub strip_hash_defaults {
51 0     0 0   my ($hash, $defaults) = @_;
52              
53 0           my $out = {%$hash};
54              
55 0           for my $key (keys %$defaults) {
56 0           my $refout = ref($out->{$key});
57 0           my $refdef = ref($defaults->{$key});
58              
59 0 0 0       if ($refout eq $refdef && $refdef eq 'HASH') {
60 0           $out->{$key} = strip_hash_defaults($out->{$key}, $defaults->{$key});
61 0           next;
62             }
63              
64 0 0         if ($refout ne $refdef) {
65 0           delete $out->{$key};
66 0           next;
67             }
68              
69 24     24   14295 no warnings 'numeric';
  24         46  
  24         3141  
70 0 0 0       delete $out->{$key} if $out->{$key} && $out->{$key} eq $defaults->{$key};
71             }
72              
73 0           return $out;
74             }
75              
76             1;