File Coverage

blib/lib/DBIx/QuickDB/Util.pm
Criterion Covered Total %
statement 24 51 47.0
branch 0 26 0.0
condition 0 6 0.0
subroutine 8 13 61.5
pod 0 2 0.0
total 32 98 32.6


line stmt bran cond sub pod time code
1             package DBIx::QuickDB::Util;
2 19     19   145 use strict;
  19         38  
  19         762  
3 19     19   130 use warnings;
  19         36  
  19         1531  
4              
5             our $VERSION = '0.000039';
6              
7 19     19   173 use File::Path qw/remove_tree/;
  19         85  
  19         1442  
8 19     19   1798 use IPC::Cmd qw/can_run/;
  19         162265  
  19         1203  
9 19     19   122 use Carp qw/confess/;
  19         53  
  19         1191  
10              
11 19     19   4069 use Importer Importer => 'import';
  19         42615  
  19         209  
12              
13             our @EXPORT_OK = qw/clone_dir strip_hash_defaults/;
14              
15             my ($RSYNC, $CP);
16              
17             BEGIN {
18 19     19   2261 local $@;
19 19         101 $RSYNC = can_run('rsync');
20 19         2508473 $CP = can_run('cp');
21             }
22              
23             sub clone_dir {
24 0 0   0 0   return _clone_dir_rsync(@_) if $RSYNC;
25 0 0         return _clone_dir_cp(@_) if $CP;
26 0           return _clone_dir_fcr(@_);
27             }
28              
29             sub _clone_dir_rsync {
30 0     0     my ($src, $dest, %params) = @_;
31 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 0     0     my ($src, $dest, %params) = @_;
36 0           my $err;
37 0 0         remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
38 0 0         system($CP, '-a', $params{verbose} ? ( '-v' ) : (), "$src/", $dest) and die "$CP returned $?";
    0          
39             }
40              
41             sub _clone_dir_fcr {
42 0     0     my ($src, $dest, %params) = @_;
43 0           require File::Copy::Recursive;
44              
45 0           my $err;
46 0 0         remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
47 0 0         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 19     19   16415 no warnings 'numeric';
  19         47  
  19         3374  
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;