eg/trimtrees.pl

filter: Statementstmt 83.3% 125 / 150 Branchbran 56.1% 46 / 82 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 93.3% 14 / 15 Podpod n/a totaltotal 72.5% 190 / 262 CC 42 SCAR 36.7
CC
42
Cov
73%
__ANON__
68.5
melt
12.7
register
6.6
CRAP 39.4
« blib/lib/Perl/Repository/APC2SVN.pm ↑ Summary scripts/patchaperlup »
1 #!/usr/bin/perl -w
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Dumpvalue;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Compare qw(compare);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Copy qw(cp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 our %Opt;
13 2 ▶GetOptions(\%Opt,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%main::Opt, "maxlinks=i") or die 2 0 2
14 "maxlinks=i",
15 ) or die;
16  
17 2 ▶my @dirs = @ARGV or die "Usage: $0 OPTIONS directories";
Branch: 1 branch
BranchTrueFalseTotal
my(@dirs) = @ARGV or die "Usage: $0 OPTIONS directories" 2 0 2
18 2 our $DEBUG = 0;
19 2 our $Signal = 0;
20 2 our %MD5;
21 2 our $Usedspc = 0;
22 2 our $Savedspc = 0;
23  
24 0 ▶$SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 warn "Caught SIGINT; please stand by, I'm leaving as soon as possible...\n";
26 0 $Signal++;
27 2 };
28 2 our $DV = Dumpvalue->new(tick => qq{"},
29 quoteHighBit => 1,
30 printUndef => 1,
31 );
32 2 our $WD = Cwd::cwd;
33  
34 498 ▶sub fmt ($) {
Subroutine fmt: called CC=1 SCAR=0.0
35 498 local $_ = shift;
36 498 s/(\d)(?=(\d{3})+$)/$1_/g;
37 498 $_;
38 }
39  
40 # $success = melt($some_file,$another)
41  
42 # $some_file is always absolute, $another is always in the current
43 # directory
44  
45 # melt dies on severe errors, returns 1 on success and 0 if it could
46 # not melt the files. It warns on suspect error conditions, but does
47 # not warn if they have no severe consequences.
48 4096 ▶sub melt ($$) {
Subroutine melt: called CC=8 SCAR=25.4
49 4096 my($first,$basename) = @_;
50 4096 my($fh,$tempfile) = tempfile("trimtrees1-XXXXXXXX", DIR => ".");
51 4096 ▶ unless ($tempfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempfile) 0 4096 4096
52 0 die "Could not create a temporary file: $!";
53 }
54 4096 ▶ unless (rename $basename, $tempfile){
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $basename, $tempfile) 2 4094 4096
55 2 warn sprintf(
56 "Cannot rename %s to %s (%s); Skipping.",
57 $DV->stringify($basename),
58 $DV->stringify($tempfile),
59 $!,
60 );
61 2 ▶ unlink $tempfile or die "Could not unlink '$tempfile': $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $tempfile or die "Could not unlink '${tempfile}': $!" 2 0 2
62 2 return 0;
63 }
64 4094 ▶ unless (link $first, $basename){
Branch: 1 branch
BranchTrueFalseTotal
unless (link $first, $basename) 0 4094 4094
65 0 my $link_err = $!;
66 0 ▶ if (rename $tempfile, $basename){
Branch: 1 branch
BranchTrueFalseTotal
if (rename $tempfile, $basename) { } 0 0 0
67 # We could rename back and no harm should be done. Don't warn.
68 0 return 0;
69 } else {
70 0 die sprintf(
71 "Could neither link %s to %s (%s) ".
72 "nor rename %s back to %s (%s).",
73 $DV->stringify($first),
74 $DV->stringify($basename),
75 $link_err,
76 $DV->stringify($tempfile),
77 $DV->stringify($basename),
78 $!
79 );
80 }
81 }
82 4094 ▶ close $fh or warn "Could not close the temporary filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or warn "Could not close the temporary filehandle: $!" 4094 0 4094
83 4094 ▶ unless (unlink $tempfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $tempfile) 0 4094 4094
84 0 die sprintf(
85 "Could not unlink %s (was %s): %s",
86 $DV->stringify($tempfile),
87 $DV->stringify($basename),
88 $!
89 );
90 }
91 4094 return 1;
92 }
93  
94 {
95 2 my %INODE;
96 7 ▶ sub register ($$;$) {
Subroutine register: called CC=6 SCAR=18.8
97 7 my($md5,$cand,$candstat) = @_;
98 7 $MD5{$md5} = $cand;
99 7 ▶ my @stat = $candstat ? @$candstat : stat $cand;
Branch: 1 branch
BranchTrueFalseTotal
$candstat ? : 2 5 7
100 7 my $size = $stat[7];
101 7 ▶ warn "size undefined" unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 7 7
102 7 ▶ warn "usedspc undefined" unless defined $Usedspc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main::Usedspc 0 7 7
103 7 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::DEBUG) 0 7 7
104 0 warn(sprintf "\nDEBUG: cand[%s]size[%s]stat[%s]", $cand, $size, join(":",@stat));
105 }
106 7 ▶ return if $INODE{$stat[1]}++; # don't count twice
Branch: 1 branch
BranchTrueFalseTotal
if $INODE{$stat[1]}++ 0 7 7
107 7 $Usedspc += $size;
108 }
109 }
110  
111 2 undef $/;
112 2 my %reported;
113 2 my $files = 0;
114 2 my $dirs = @dirs;
115 2 my $tl_dirs_todo = 0;
116 2 my $tl_dirs_doing = 0;
117  
118 83 ▶sub xreport () {
Subroutine xreport: called CC=1 SCAR=0.0
119 83 my $uniq_files = keys %MD5;
120 printf(
121 "\rtlds[%s]cur[%s]uniq[%s]fils[%s]spcused[%s]saved[%s]",
122 map {
123 83 fmt($_)
124 }
125 (
126 $tl_dirs_todo,
127 $tl_dirs_doing,
128 $uniq_files,
129 $files,
130 $Usedspc,
131 $Savedspc
132 )
133 );
134 }
135  
136 2 $| = 1;
137 2 for my $diri (0..$#dirs) {
138 2 my $root = $dirs[$diri];
139 find(
140 {
141 16390 ▶ wanted => sub {
Subroutine __ANON__: called CC=30 SCAR=42.3
142 16390 ▶ if ($Signal){
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Signal) 0 16390 16390
143 0 $File::Find::prune = 1;
144 0 return;
145 }
146 16390 ▶ if ($File::Find::name eq $root) {
Branch: 2 branches
BranchTrueFalseTotal
if ($File::Find::name eq $root) { } 2 16388 16390
elsif (-d $_) { } 8192 8196 16388
147 2 my $td = $_;
148 2 opendir my($dh), $td;
149 2 ▶ my(@tl) = grep { !/^\./ && -d "$td/$_" } readdir $dh;
Condition66%2/3
!/^\./ && -d "$td/$_"
!ll&&!rl&&r
4020
MC/DC50%1/2
!/^\./ && -d "$td/$_"
!/^\./ -d "$td/$_"
Truth table66%2/3
not(A) && B
A= !/^\./B= -d "$td/$_"
ABresult
0X0
100
111
150 2 $tl_dirs_todo = @tl;
151 } elsif (-d) {
152 8192 my $slashes = $File::Find::name =~ tr|/||;
153 8192 ▶ if ($slashes == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slashes == 1) 20 8172 8192
154 20 $tl_dirs_doing++;
155 }
156 }
157 16390 ▶ return if -l; # relative links would need special treatment that does not pay off
Branch: 1 branch
BranchTrueFalseTotal
if -l $_ 0 16390 16390
158 16390 ▶ return unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 8194 8196 16390
159 8196 ▶ return unless -s _; # empty files more risk that files with
Branch: 1 branch
BranchTrueFalseTotal
unless -s _ 0 8196 8196
160 # content and no gain
161 8196 $files++;
162 8196 my $basename = $_;
163 8196 my $fh;
164 8196 ▶ unless (open $fh, "<", $basename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $basename) 1 8195 8196
165 1 warn sprintf(
166 "Cannot read %s (%s); Skipping.",
167 $DV->stringify($File::Find::name),
168 $!,
169 );
170 1 $Usedspc += -s $basename;
171 1 return;
172 }
173 8195 my $data = <$fh>;
174 8195 close $fh;
175 8195 my $md5 = md5 $data;
176 8195 my $cand = $File::Find::name;
177 8195 ▶ if ($Opt{maxlinks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"maxlinks"}) 0 8195 8195
178 0 my(@maxlstat) = stat($cand);
179 0 ▶ if ($maxlstat[3] > $Opt{maxlinks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxlstat[3] > $main::Opt{"maxlinks"}) 0 0 0
180 # the case that we have to make a new file from a link
181 0 my($fh,$tempfile) = tempfile("trimtrees2-XXXXXXXX", DIR => ".");
182 0 ▶ unless ($tempfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempfile) 0 0 0
183 0 die "Could not create a temporary file: $!";
184 }
185 0 ▶ unless (rename $cand, $tempfile){
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $cand, $tempfile) 0 0 0
186 0 die sprintf(
187 "Could not rename %s to %s: %s",
188 $DV->stringify($cand),
189 $DV->stringify($tempfile),
190 $!
191 );
192 }
193 0 ▶ cp $tempfile, $cand or die sprintf(
Branch: 1 branch
BranchTrueFalseTotal
cp($tempfile, $cand) or die sprintf("Could not cp %s to %s: %s", $main::DV->stringify($tempfile), $main::DV->stringify($cand), $!) 0 0 0
194 "Could not cp %s to %s: %s",
195 $DV->stringify($tempfile),
196 $DV->stringify($cand),
197 $!,
198 );
199 0 unlink $tempfile;
200 0 $Savedspc -= $maxlstat[7];
201 }
202 }
203 8195 ▶ if (my $first = $MD5{$md5}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $first = $main::MD5{$md5}) { } 8190 5 8195
204 8190 ▶ unless (File::Spec->file_name_is_absolute($first)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($first)) 8190 0 8190
205 8190 $first = File::Spec->catfile($WD, $first);
206 }
207 8190 my(@firststat) = stat($first);
208 8190 ▶ die sprintf(
Branch: 1 branch
BranchTrueFalseTotal
unless $firststat[1] 0 8190 8190
209 "illegal firststat[%s]first[%s]",
210 join(":",@firststat),
211 $DV->stringify($first),
212 ) unless $firststat[1];
213 8190 my $different = compare $first, $basename;
214 8190 ▶ if ($different != 0 && $File::Compare::VERSION<1.1005) {
Branch: 1 branch
BranchTrueFalseTotal
if ($different != 0 and $File::Compare::VERSION < "1.1005") 0 8190 8190
Condition33%1/3
$different != 0 and $File::Compare::VERSION < "1.1005"
!ll&&!rl&&r
819000
MC/DC0%0/2
$different != 0 and $File::Compare::VERSION < "1.1005"
$different != 0 $File::Compare::VERSION < "1.1005"
Truth table33%1/3
A and B
A= $different != 0B= $File::Compare::VERSION < "1.1005"
ABresult
0X0
100
111
215 # workaround bug # 37716 in File::Compare
216 0 $different = compare "$first\0", "$basename\0";
217 }
218 8190 ▶ if ($different<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($different < 0) 0 8190 8190
219 0 warn sprintf(
220 "Cannot compare %s and %s (%s); Skipping.",
221 $DV->stringify($first),
222 $DV->stringify($cand),
223 $!,
224 );
225 0 goto XREPORT; # some error occurred
226 }
227 8190 ▶ die sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $different 0 8190 8190
228 "Sensation, %s and %s are not equal with same MD5",
229 $DV->stringify($first),
230 $DV->stringify($cand),
231 )
232 if $different;
233 8190 my(@candstat) = stat($basename);
234 8190 ▶ goto XREPORT unless $candstat[0] == $firststat[0]; # different file system
Branch: 1 branch
BranchTrueFalseTotal
unless $candstat[0] == $firststat[0] 0 8190 8190
235 8190 ▶ if ($candstat[1] == $firststat[1]){ # already same inode
Branch: 1 branch
BranchTrueFalseTotal
if ($candstat[1] == $firststat[1]) 4094 4096 8190
236 4094 if (0 && $Opt{maxlinks} && $firststat[3] > $Opt{maxlinks}) {
237 } else {
238 4094 goto XREPORT;
239 }
240 }
241 4096 ▶ if ($Opt{maxlinks} && $firststat[3] >= $Opt{maxlinks}){
Branch: 2 branches
BranchTrueFalseTotal
if ($main::Opt{'maxlinks'} and $firststat[3] >= $main::Opt{'maxlinks'}) { } 0 4096 4096
elsif (&melt($first, $basename)) { } 4094 2 4096
Condition33%1/3
$main::Opt{"maxlinks"} and $firststat[3] >= $main::Opt{"maxlinks"}
!ll&&!rl&&r
409600
MC/DC0%0/2
$main::Opt{"maxlinks"} and $firststat[3] >= $main::Opt{"maxlinks"}
$main::Opt{"maxlinks"} $firststat[3] >= $main::Opt{"maxlinks"}
Truth table33%1/3
A and B
A= $main::Opt{"maxlinks"}B= $firststat[3] >= $main::Opt{"maxlinks"}
ABresult
0X0
100
111
242 0 ▶ register($md5,$cand,\@candstat) if $candstat[3] < $Opt{maxlinks};
Branch: 1 branch
BranchTrueFalseTotal
if $candstat[3] < $main::Opt{"maxlinks"} 0 0 0
243 } elsif (melt($first,$basename)) {
244 4094 ▶ if ($candstat[3]==1) { # we don't save space otherwise
Branch: 1 branch
BranchTrueFalseTotal
if ($candstat[3] == 1) 4094 0 4094
245 4094 $Savedspc += $firststat[7];
246 }
247 } else {
248 2 register($md5,$cand,\@candstat);
249 }
250 } else {
251 5 register($md5,$cand);
252 }
253 8195 ▶ XREPORT:
Branch: 1 branch
BranchTrueFalseTotal
if $files % 100 8114 81 8195
254 return if $files % 100;
255 81 xreport;
256 },
257 2 no_chdir => 1,
258 },
259 $root
260 );
261 2 ▶ last if $Signal;
Branch: 1 branch
BranchTrueFalseTotal
if $main::Signal 0 2 2
262 }
263 2 xreport;
264 2 print "\nDONE\n";
265  
266 __END__
« blib/lib/Perl/Repository/APC2SVN.pm ↑ Summary scripts/patchaperlup »