blib/lib/File/Dedup.pm

filter: Statementstmt 88.9% 105 / 118 Branchbran 71.1% 37 / 52 Conditioncond 43.3% 13 / 30 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 90.0% 18 / 20 Podpod 83.3% 5 / 6 totaltotal 74.9% 182 / 243 CC 41 SCAR 21.9
CC
41
Cov
78%
_purge_files
22.9
new
11.1
_dirwalk
10.5
CRAP 9.0
↑ Summary
1 package File::Dedup;
2 # ABSTRACT: Deduplicate files across directories
3 5 $File::Dedup::VERSION = '0.007';
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 my @VALID_OPTIONS = qw(ask directory group recursive);
11 14 ▶sub new {
Subroutine new: called CC=11 SCAR=24.1
Pod undocumented
12 14 my ($class, %opts) = @_;
13  
14 die "Must pass a directory to process"
15 14 ▶ unless exists $opts{directory};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"directory"} 1 13 14
16 die "Supplied directory argument '$opts{directory}' is not a directory"
17 13 ▶ unless -d $opts{directory};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $opts{"directory"} 1 12 13
18 warn "Supplied option 'group' not implemented yet"
19 12 ▶ if exists $opts{group} and defined $opts{group};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"group"} and defined $opts{"group"} 1 11 12
Condition66%2/3
exists $opts{"group"} and defined $opts{"group"}
!ll&&!rl&&r
1101
MC/DC50%1/2
exists $opts{"group"} and defined $opts{"group"}
exists $opts{"group"} defined $opts{"group"}
Truth table66%2/3
A and B
A= exists $opts{"group"}B= defined $opts{"group"}
ABresult
0X0
100
111
20
21 # do not allow undefined options
22 12 foreach my $opt ( keys %opts ) {
23 die "Invalid argument '$opt' passed to new"
24 23 ▶ unless grep { $_ eq $opt } @VALID_OPTIONS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $opt;} @VALID_OPTIONS 1 22 23
25 }
26
27 # default to always asking before purging
28 $opts{ask} = 1
29 11 ▶ unless exists $opts{ask} && defined $opts{ask};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"ask"} and defined $opts{"ask"} 4 7 11
Condition66%2/3
exists $opts{"ask"} and defined $opts{"ask"}
!ll&&!rl&&r
407
MC/DC50%1/2
exists $opts{"ask"} and defined $opts{"ask"}
exists $opts{"ask"} defined $opts{"ask"}
Truth table66%2/3
A and B
A= exists $opts{"ask"}B= defined $opts{"ask"}
ABresult
0X0
100
111
30
31 # default to non-recursive
32 $opts{recursive} = 0
33 11 ▶ unless exists $opts{recursive} && defined $opts{recursive};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"recursive"} and defined $opts{"recursive"} 8 3 11
Condition66%2/3
exists $opts{"recursive"} and defined $opts{"recursive"}
!ll&&!rl&&r
803
MC/DC50%1/2
exists $opts{"recursive"} and defined $opts{"recursive"}
exists $opts{"recursive"} defined $opts{"recursive"}
Truth table66%2/3
A and B
A= exists $opts{"recursive"}B= defined $opts{"recursive"}
ABresult
0X0
100
111
34
35 11 return bless \%opts, $class;
36 }
37  
38 2 ▶sub directory {
Subroutine directory: called CC=1 SCAR=0.0
Pod documented
39 2 return shift->{directory};
40 }
41  
42 2 ▶sub recursive {
Subroutine recursive: called CC=1 SCAR=0.0
Pod documented
43 2 return shift->{recursive};
44 }
45  
46 15 ▶sub ask {
Subroutine ask: called CC=1 SCAR=0.0
Pod documented
47 15 return shift->{ask};
48 }
49  
50 0 ▶sub group {
Subroutine group: not called CC=1 SCAR=6.9
Pod documented
51 0 return shift->{group};
52 }
53  
54 3 ▶sub _file_digest {
Subroutine _file_digest: called CC=2 SCAR=7.0
55 3 my ($filename) = @_;
56  
57 3 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "$!" 3 0 3
58 or die "$!";
59
60 3 my $checksum = Digest::SHA->new->addfile($fh)->hexdigest;
61 3 close($fh);
62  
63 3 return $checksum;
64 }
65  
66 1 ▶sub dedup {
Subroutine dedup: called CC=2 SCAR=6.9
Pod documented
67 1 my ($self) = @_;
68
69 my @results = $self->_dirwalk(
70 $self->directory,
71 3 ▶ sub { [ $_[0], _file_digest($_[0]) ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 ▶ sub { shift; @_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 );
74 5 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 print Dumper \@results;
76 1 my %files_by_hashsum;
77 1 foreach my $result ( @results ) {
78 3 my ($filename, $digest) = @$result;
79 3 push @{ $files_by_hashsum{$digest} }, $filename;
80 }
81  
82 my %duplicates_by_hashsum =
83 1 map { $_ => [ sort @{$files_by_hashsum{$_}} ] }
84 1 grep { @{ $files_by_hashsum{$_} } > 1 } keys %files_by_hashsum;
85  
86 1 my @files_to_purge = $self->_handle_duplicates(\%duplicates_by_hashsum);
87 1 $self->_purge_files(\@files_to_purge);
88
89 1 return;
90 }
91  
92 4 ▶sub _handle_duplicates {
Subroutine _handle_duplicates: called CC=8 SCAR=20.8
93 4 my ($self, $duplicates) = @_;
94 4 ▶ return unless keys %$duplicates;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$duplicates 0 4 4
95  
96 4 my @files_to_purge;
97 4 ▶ while ( my ($digest, $files) = each %$duplicates ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$duplicates) 7 4 11
98 7 my $to_keep;
99 7 ▶ if ( $self->ask ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ask) { } 4 3 7
100 4 say 'The following files are duplicates '
101 . " indicate which one(s) you would like to keep\n"
102 . '(-1 to SKIP or CTRL-C to quit):';
103
104 4 my $number_of_files = $#{ $files };
105 4 foreach my $i ( 0 .. $number_of_files ) {
106 8 my $file = $files->[$i];
107 8 say "[ $i]\t$file";
108 }
109 4 say "[ -1]\tSKIP";
110 4 say "[C-c]\tQUIT";
111 4 $to_keep = _get_numeric_response($number_of_files);
112 4 ▶ next if ! defined $to_keep || defined $to_keep && $to_keep == -1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $to_keep or defined $to_keep and $to_keep == -1 2 2 4
Condition66%4/6
not defined $to_keep or defined $to_keep and $to_keep == -1
l!l&&r!l&&!r
022
defined $to_keep and $to_keep == -1
!ll&&!rl&&r
022
MC/DC33%1/3
not defined $to_keep or defined $to_keep and $to_keep == -1
not defined $to_keep defined $to_keep $to_keep == -1
Truth table50%2/4
not(A) or B and C
A= not defined $to_keepB= defined $to_keepC= $to_keep == -1
ABCresult
1XX1
0111
00X0
0100
113 }
114 else { # if ask = 0 keep the first duplicate
115 3 $to_keep = 0;
116 }
117
118 push @files_to_purge,
119 5 grep { $_ ne $files->[$to_keep] } @$files;
120 }
121  
122 4 return sort @files_to_purge;
123 }
124  
125 2 ▶sub _purge_files {
Subroutine _purge_files: called CC=8 SCAR=31.3
126 2 my ($self, $files) = @_;
127  
128 2 foreach my $file ( @$files ) {
129 3 print "purging file: $file\n";
130 3 my $response;
131 3 ▶ if ( $self->ask ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ask) 0 3 3
132 do {
133 0 print "About to delete '$file'; continue? [Y/n] ";
134 0 $response = _prompt();
135 }
136 0 ▶ while ( !grep { $response eq $_ } ('y', 'Y', 'n', 'N', '') );
Branch: 1 branch
BranchTrueFalseTotal
grep {$response eq $_;} "y", "Y", "n", "N", "" or do { do { print "About to delete '${file}'; continue? [Y/n] "; $response = _prompt() } } 0 0 0
137 }
138  
139 3 ▶ _delete_file($file)
Branch: 1 branch
BranchTrueFalseTotal
if not $self->ask or $self->ask and $response eq "" || $response =~ /^[yY]$/ 3 0 3
Condition11%1/9
not $self->ask or $self->ask and $response eq "" || $response =~ /^[yY]$/
l!l&&r!l&&!r
300
$self->ask and $response eq "" || $response =~ /^[yY]$/
!ll&&!rl&&r
000
$response eq "" or $response =~ /^[yY]$/
l!l&&r!l&&!r
000
MC/DC0%0/4
not $self->ask or $self->ask and $response eq "" || $response =~ /^[yY]$/
not $self->ask $self->ask $response eq "" $response =~ /^[yY]$/
Truth table20%1/5
not(A) or B and C or D
A= not $self->askB= $self->askC= $response eq ""D= $response =~ /^[yY]$/
ABCDresult
1XXX1
011X1
01011
00XX0
01000
140 if !$self->ask
141 || ($self->ask
142 && ($response eq '' || $response =~ m/^[yY]$/));
143 }
144  
145 2 return;
146 }
147  
148 3 ▶sub _delete_file {
Subroutine _delete_file: called CC=2 SCAR=7.2
149 3 my ($file) = @_;
150  
151 3 ▶ unlink($file)
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "Unable to delete file '${file}': $!" 3 0 3
152 or die "Unable to delete file '$file': $!";
153 }
154  
155 4 ▶sub _get_numeric_response {
Subroutine _get_numeric_response: called CC=6 SCAR=20.7
156 4 my ($max) = @_;
157  
158 4 my $input;
159 4 my $valid_response = 0;
160 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$valid_response or do { do { print "\n>> "; $input = _prompt(); if (not defined $input) { say "You did not enter any input."; } elsif (not $input =~ /^\-?\d+$/) { say "You must enter a number between 0 and $max"; } elsif ($input and $input > $max) { say "You must enter a number between 0 and $max"; } else { $valid_response = 1; } } } 4 0 4
161 4 print "\n>> ";
162 4 $input = _prompt();
163  
164 4 ▶ if ( ! defined $input ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $input) { } 0 4 4
elsif (not $input =~ /^\-?\d+$/) { } 0 4 4
elsif ($input and $input > $max) { } 0 4 4
165 0 say 'You did not enter any input.';
166 }
167 elsif ( $input !~ m/^\-?\d+$/ ) {
168 0 say "You must enter a number between 0 and $max";
169 }
170 ▶ elsif ( $input && $input > $max ) {
Condition33%1/3
$input and $input > $max
!ll&&!rl&&r
040
MC/DC0%0/2
$input and $input > $max
$input $input > $max
Truth table33%1/3
A and B
A= $inputB= $input > $max
ABresult
0X0
100
111
171 0 say "You must enter a number between 0 and $max";
172 }
173 else {
174 4 $valid_response = 1;
175 }
176 } while( !$valid_response );
177  
178 4 print "AFTER get_numeric_response: $input\n";
179 4 return $input;
180 }
181  
182 0 ▶sub _prompt {
Subroutine _prompt: not called CC=1 SCAR=6.9
183 0 my $input = <STDIN>;
184 0 chomp($input);
185
186 0 return $input;
187 }
188  
189 4 ▶sub _dirwalk {
Subroutine _dirwalk: called CC=9 SCAR=23.5
190 4 my ($self, $top, $filefunc, $dirfunc) = @_;
191  
192 4 ▶ if ( -d $top ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $top) 1 3 4
193 # stop processing non-recursive searches when a directory that
194 # was not the starting directory is encountered
195 return
196 1 ▶ if $top ne $self->directory && !$self->recursive;
Branch: 1 branch
BranchTrueFalseTotal
if $top ne $self->directory and not $self->recursive 0 1 1
Condition33%1/3
$top ne $self->directory and not $self->recursive
!ll&&!rl&&r
100
MC/DC0%0/2
$top ne $self->directory and not $self->recursive
$top ne $self->directory not $self->recursive
Truth table33%1/3
A and not(B)
A= $top ne $self->directoryB= not $self->recursive
ABresult
0X0
100
111
197
198 1 my $DIR;
199 1 ▶ unless ( opendir $DIR, $top ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir $DIR, $top) 0 1 1
200 0 warn "Couldn't open directory '$top': $!; skipping.\n";
201 0 return;
202 }
203  
204 1 my @results;
205 1 ▶ while ( my $file = readdir $DIR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $DIR)) 5 1 6
206 5 ▶ next if $file =~ m/^\./; # ignore hidden files, '.', and '..'
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^\./ 2 3 5
207
208 3 push @results, $self->_dirwalk("$top/$file", $filefunc, $dirfunc);
209 }
210 1 ▶ return $dirfunc ? $dirfunc->($top, @results) : ();
Branch: 1 branch
BranchTrueFalseTotal
$dirfunc ? : 1 0 1
211 }
212
213 3 ▶ return $filefunc ? $filefunc->($top) : ();
Branch: 1 branch
BranchTrueFalseTotal
$filefunc ? : 3 0 3
214 }
215  
216 5 1;
217  
218 __END__
↑ Summary