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 23     23   334525 use strict;
  23         49  
  23         909  
3 23     23   131 use warnings;
  23         103  
  23         1875  
4              
5             our $VERSION = '0.000042';
6              
7 23     23   197 use File::Path qw/remove_tree/;
  23         43  
  23         1584  
8 23     23   2010 use IPC::Cmd qw/can_run/;
  23         186870  
  23         1481  
9 23     23   185 use Carp qw/confess/;
  23         53  
  23         1245  
10              
11 23     23   4261 use Importer Importer => 'import';
  23         41556  
  23         207  
12              
13             our @EXPORT_OK = qw/clone_dir strip_hash_defaults/;
14              
15             my ($RSYNC, $CP);
16              
17             BEGIN {
18 23     23   2741 local $@;
19 23         126 $RSYNC = can_run('rsync');
20 23         2773938 $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   218831 my ($src, $dest, %params) = @_;
36 2         5 my $err;
37 2 50       853 remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
38 2 50       21738 system($CP, '-a', $params{verbose} ? ( '-v' ) : (), "$src/.", $dest) and die "$CP returned $?";
    50          
39             }
40              
41             sub _clone_dir_fcr {
42 2     2   20126 my ($src, $dest, %params) = @_;
43 2         31 require File::Copy::Recursive;
44              
45 2         10 my $err;
46 2 50       810 remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
47 2 50       35 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 23     23   16693 no warnings 'numeric';
  23         51  
  23         3498  
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;