File Coverage

blib/lib/DBIx/QuickDB/Util.pm
Criterion Covered Total %
statement 33 55 60.0
branch 5 28 17.8
condition 0 12 0.0
subroutine 10 14 71.4
pod 0 3 0.0
total 48 112 42.8


line stmt bran cond sub pod time code
1             package DBIx::QuickDB::Util;
2 27     27   230441 use strict;
  27         68  
  27         866  
3 27     27   112 use warnings;
  27         44  
  27         1663  
4              
5             our $VERSION = '0.000050';
6              
7 27     27   119 use File::Path qw/remove_tree/;
  27         58  
  27         1334  
8 27     27   2742 use IPC::Cmd qw/can_run/;
  27         201693  
  27         1106  
9 27     27   168 use Carp qw/confess/;
  27         67  
  27         1096  
10              
11 27     27   5116 use Importer Importer => 'import';
  27         47515  
  27         193  
12              
13             our @EXPORT_OK = qw/clone_dir strip_hash_defaults env_timeout/;
14              
15             # Read a positive-integer timeout (in seconds) from an environment variable,
16             # falling back to $default when it is unset or not a positive integer. Used to
17             # make the server start/stop timeouts generous-but-tunable so slow hosts (e.g.
18             # CPAN smoke boxes) do not spuriously time out.
19             sub env_timeout {
20 0     0 0 0 my ($name, $default) = @_;
21 0         0 my $val = $ENV{$name};
22 0 0 0     0 return $val if defined($val) && $val =~ /^\d+$/ && $val > 0;
      0        
23 0         0 return $default;
24             }
25              
26             my ($RSYNC, $CP);
27              
28             BEGIN {
29 27     27   4589 local $@;
30 27         113 $RSYNC = can_run('rsync');
31 27         2518521 $CP = can_run('cp');
32             }
33              
34             sub clone_dir {
35 0 0   0 0 0 return _clone_dir_rsync(@_) if $RSYNC;
36 0 0       0 return _clone_dir_cp(@_) if $CP;
37 0         0 return _clone_dir_fcr(@_);
38             }
39              
40             sub _clone_dir_rsync {
41 0     0   0 my ($src, $dest, %params) = @_;
42 0 0       0 system($RSYNC, '-a', '--delete', '--exclude' => '.nfs*', $params{checksum} ? ('-c') : (), $params{verbose} ? ( '-vP' ) : (), "$src/", $dest) and die "$RSYNC returned $?";
    0          
    0          
43             }
44              
45             sub _clone_dir_cp {
46 2     2   174969 my ($src, $dest, %params) = @_;
47 2         4 my $err;
48 2 50       612 remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
49 2 50       16535 system($CP, '-a', $params{verbose} ? ( '-v' ) : (), "$src/.", $dest) and die "$CP returned $?";
    50          
50             }
51              
52             sub _clone_dir_fcr {
53 2     2   14734 my ($src, $dest, %params) = @_;
54 2         29 require File::Copy::Recursive;
55              
56 2         4 my $err;
57 2 50       671 remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
58 2 50       40 File::Copy::Recursive::dircopy($src, $dest) or die "$!";
59             }
60              
61             sub strip_hash_defaults {
62 0     0 0   my ($hash, $defaults) = @_;
63              
64 0           my $out = {%$hash};
65              
66 0           for my $key (keys %$defaults) {
67 0           my $refout = ref($out->{$key});
68 0           my $refdef = ref($defaults->{$key});
69              
70 0 0 0       if ($refout eq $refdef && $refdef eq 'HASH') {
71 0           $out->{$key} = strip_hash_defaults($out->{$key}, $defaults->{$key});
72 0           next;
73             }
74              
75 0 0         if ($refout ne $refdef) {
76 0           delete $out->{$key};
77 0           next;
78             }
79              
80 27     27   15629 no warnings 'numeric';
  27         49  
  27         3273  
81 0 0 0       delete $out->{$key} if $out->{$key} && $out->{$key} eq $defaults->{$key};
82             }
83              
84 0           return $out;
85             }
86              
87             1;