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 25     25   239151 use strict;
  25         33  
  25         734  
3 25     25   101 use warnings;
  25         35  
  25         1410  
4              
5             our $VERSION = '0.000048';
6              
7 25     25   135 use File::Path qw/remove_tree/;
  25         44  
  25         1269  
8 25     25   2338 use IPC::Cmd qw/can_run/;
  25         171599  
  25         1095  
9 25     25   119 use Carp qw/confess/;
  25         41  
  25         965  
10              
11 25     25   4304 use Importer Importer => 'import';
  25         39656  
  25         231  
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 25     25   4107 local $@;
30 25         104 $RSYNC = can_run('rsync');
31 25         2284709 $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   177603 my ($src, $dest, %params) = @_;
47 2         3 my $err;
48 2 50       592 remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
49 2 50       17852 system($CP, '-a', $params{verbose} ? ( '-v' ) : (), "$src/.", $dest) and die "$CP returned $?";
    50          
50             }
51              
52             sub _clone_dir_fcr {
53 2     2   14820 my ($src, $dest, %params) = @_;
54 2         35 require File::Copy::Recursive;
55              
56 2         10 my $err;
57 2 50       744 remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
58 2 50       29 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 25     25   14355 no warnings 'numeric';
  25         40  
  25         3059  
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;