source-tree-marathon.pl untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 37 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 196 CC 13 SCAR 42.2
CC
13
Cov
0%
check_stats
132.0
display
12.0
BEGIN
2.0
CRAP 68.4
1 #!/usr/bin/perl -Ilib
2  
3 use Acme::KeyboardMarathon;
4 use Cwd 'abs_path';
5 use DB_File;
6 use File::Find;
7 use File::Slurp;
8 use Math::BigInt lib => 'GMP';
9 use strict;
10 use warnings;
11  
12 =head1 source-tree-marathon.pl
13  
14 This script is designed to recursively crawl a directory of source files
15 to generate a Keyboard-Marathon report for your whole project.
16  
17 In does it's best to skip binary files. It will not uncompact compressed
18 files.
19  
20 To conserve ram, it will create a local berkley DB (called
21 "marathon.db") in the current working directory. If you want to run this
22 script on a regular basis this will vastly accelerate calculations as
23 only new and changed files will be processed. (Deleted files will
24 automatically be pruned from the DB and from calculations.)
25  
26 The report will include the grand total distance, as well as a breakdown by
27 file type.
28  
29 Processing status is presented on STDERR. The report is output on STDOUT. So
30 it is very easy to redirect the output to a file:
31  
32 %> ./source-tree-marathon.pl /my/source/directory > report.txt
33  
34 This script requires the following perl modules: File::Find,
35 File::Slurp, Math::BigInt and Acme::KeyboardMarathon
36  
37 =cut
38  
39 ### Conf
40  
41 my $dbfile = 'marathon.db';
42  
43 my $base_dir;
44  
45 if ( $ARGV[0] and -d $ARGV[0] ) {
46 $base_dir = abs_path($ARGV[0]);
47 }
48  
49 unless ( $base_dir ) {
50 print STDERR "Usage: ./source-tree-marathon.pl /source/directory/to/crawl > report.txt\n";
51 exit 1;
52 }
53  
54 ### Constants
55  
56 my $skip_file_extension_regex = qr{\.(binmode|bmp|docx|exe|gif|gz|ico|jar|jpe?g|o|obj|pdf|png|pptx|pyc|so|tar(\.xz)?|tiff?|tgz|ttf|vsd|zip)$};
57 my $skip_dirs_regex = qr/^(\.git|tpc|debian|linux-kernel)/;
58  
59 ### Bootstrap
60  
61  
62 my %data;
63  
64 if ( -f $dbfile ) {
65 print STDERR "Reusing file: $dbfile\n";
66 } else {
67 print STDERR "Creating file: $dbfile\n";
68 }
69  
70 my $ref = tie %data, 'DB_File', $dbfile;
71  
72 my $akm = new Acme::KeyboardMarathon;
73
74 ### Main
75  
76 # Remove file stats for missing files
77 for my $file ( keys %data ) {
78 next if -f $file;
79 print STDERR "DEL: $file\n";
80 delete $data{$file}
81 }
82  
83 ### Store stats
84  
85 $| = 1; # autoflush
86  
87 my $skip = 0;
88 my $add = 0;
89 my $cache = 0;
90  
91 find( \&check_stats, $base_dir );
92  
93 print STDERR "\nDB Stats:\nADD: $add\nCACHE: $cache\nSKIP: $skip\n";
94  
95  
96 if ( $add ) {
97 print STDERR "\nSyncing...\n";
98 $ref->sync();
99 }
100  
101 ### Process stats
102  
103 my %filecounts;
104 my %filedists;
105  
106 my $grand_total = Math::BigInt->new();
107  
108 for my $file ( keys %data ) {
109 next unless $file =~ /\.([^\.\/]+)$/;
110  
111 my $type = $1;
112 $filecounts{$type}++;
113  
114 my ($mtime,$size,$dist) = split ':', $data{$file}, 3;
115  
116 $filedists{$type} = Math::BigInt->new() unless defined $filedists{$type};
117 $filedists{$type} += $dist;
118 $grand_total += $dist;
119 }
120  
121 print "Generated on " . scalar(localtime) . "\n\n";
122  
123 print "\nGrand total: ", display($grand_total), "\n\nTop 10 distance:\n\n";
124  
125 my $i = 1;
126 for my $type ( sort { $filedists{$b} <=> $filedists{$a} } keys %filedists ) {
127 printf "%20s : %4s files : %s\n", $type, $filecounts{$type}, display($filedists{$type});
128 last if $i++ > 10;
129 }
130  
131 print "\nDistances by file count:\n\n";
132  
133 for my $type ( sort { $filecounts{$b} <=> $filecounts{$a} } keys %filecounts ) {
134 printf "%20s : %4s files : %s\n", $type, $filecounts{$type}, display($filedists{$type});
135 }
136  
137 ### Subroutines
138  
139 sub check_stats {
140 if ( -d $_ ) {
141 my $localdir = $File::Find::name;
142 $localdir = $1 if $localdir =~ /^$base_dir\/(.+)$/;
143 print STDERR "DIR: $localdir\n";
144 return;
145 }
146 $skip++ and print STDERR "SKIP: $_ (regex)\n" and return if $_ =~ /$skip_file_extension_regex/i;
147  
148 my $localdir = $File::Find::dir;
149 $localdir = $1 if $localdir =~ /^$base_dir\/(.+)$/;
150 $skip++ and print STDERR "SKIP: $localdir (directory)\n" and return if $localdir =~ /$skip_dirs_regex/;
151  
152 $skip++ and print STDERR "SKIP: $_ (symlink)\n" and return if -l $File::Find::name;
153 $skip++ and print STDERR "SKIP: $_ (zero size)\n" and return if -z $File::Find::name;
154 $skip++ and print STDERR "SKIP: $_ (binary)\n" and return if -B $File::Find::name;
155  
156 my @stat = stat($File::Find::name);
157 my $mtime = $stat[9];
158 my $size = $stat[7];
159  
160 if ( defined $data{$File::Find::name} and $data{$File::Find::name} =~ /^$mtime\:$size\:/ ) {
161 $cache++ and print STDERR "CACHE: $_\n";
162 return;
163 }
164  
165 $add++ and print STDERR "ADD: $_ ";
166  
167 my $text = read_file($File::Find::name);
168 my $dist = $akm->distance($text);
169  
170 $data{$File::Find::name} = "$mtime:$size:$dist";
171  
172 print STDERR "(".display($dist).")\n";
173  
174 unless ( $add % 250 ) {
175 print STDERR "syncing...\n";
176 $ref->sync();
177 }
178 }
179  
180 sub display {
181 my $total = "$_[0]"; # Convert to
182 if ( $total > 100000 ) {
183 $total /= 100000;
184 return sprintf('%0.2f',$total) . ' km';
185 } elsif ( $total > 100 ) {
186 $total /= 100;
187 return sprintf('%0.2f',$total) . ' m';
188 } else {
189 return $total . ' cm';
190 }
191 }