-
Notifications
You must be signed in to change notification settings - Fork 1
/
count-klocs.pl
executable file
·143 lines (111 loc) · 3.17 KB
/
count-klocs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env perl
=head1 NAME
count-klocs.pl
=head1 SYNOPSIS
count-klocs.pl [options] toradocu-test-directory
Options:
-help brief help message
-details include details of each test run
-man full documentation
=head1 OPTIONS
=over 4
=item B<-help>
Print a brief help message and exits.
=item B<-details>
Include coverage details for each test.
[default is summary only]
=item B<-man>
Prints the manual page and exits.
=back
=head1 DESCRIPTION
This perl script is intended for use by Randoop developers to go through
the Toradocu corpus and output the KLOCS of each of the tests to standard
output. For more details on the KLOC process see:
https://gitlab.cs.washington.edu/randoop/coverage-tools/blob/master/KLOCS-README.md
=cut
use strict;
use warnings;
use POSIX qw(strftime);
use Getopt::Long qw(GetOptions);
use Pod::Usage qw(pod2usage);
use File::Find;
use File::Temp 'tempfile';
my $help = 0;
my $details = 0;
my $man = 0;
# Parse options and print usage if there is a syntax error,
# or if usage was explicitly requested.
GetOptions('help|?' => \$help, details => \$details, man => \$man) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $man;
# Check for too many filenames
pod2usage("$0: Too many arguments.\n") if (@ARGV > 1);
pod2usage("$0: Must supply Toradocu directory name.\n") if (@ARGV < 1);
# locate the java_count tool
my $java_count = $ENV{'JAVA_COUNT_TOOL'};
if (! defined($java_count)) {
print "JAVA_COUNT_TOOL environment variable not set\n";
exit 1;
}
my $filename;
my $output;
my $srcdir;
my $suitename;
my $targetname;
my $testdir;
my $tfh;
my $tfname;
if ($details) {
print(strftime("\nToday's date: %Y-%m-%d %H:%M:%S", localtime), "\n");
}
$srcdir = $ARGV[0];
opendir(my $sdh, $srcdir) or die "Could not open $srcdir\n";
chdir $srcdir;
$suitename = `basename $srcdir`;
chomp $suitename;
if ($details) {
printf("%s %s\n", $srcdir, $suitename);
}
$testdir = "resources";
opendir(my $tdh, $testdir) or die "Could not open $testdir\n";
my $subtestnumber = 1;
# get the list of class files for this subtest
$filename = $testdir . "/classlist.txt";
open(my $fh, '<', $filename)
or die "Could not open file '$filename'. $!.\n";
if ($details) {
printf("Processing file: %s\n", $filename);
}
# get a temp file for the list of corresponding java files we are going to create
($tfh, $tfname) = tempfile();
while (my $path = <$fh>) {
chomp $path;
# skip inner classes
next if ($path =~ /\$/);
$path =~ s/\./\\\//g;
$targetname = "$path.java";
# find and process the matching source file
if ($suitename eq 'freecol') {
find(\&search_for_file, "inputs/FreeCol");
} else {
find(\&search_for_file, "inputs/$suitename");
}
}
my $cmd = "$java_count -f $tfname";
# print "$cmd\n";
$output = `$cmd`;
chomp $output;
# remove the "Total:" line
$output =~ s/.*\n//;
print "$suitename $subtestnumber: $output\n";
close $tfh;
unlink $tfname;
closedir($sdh);
closedir($tdh);
sub search_for_file
{
my $file = $File::Find::name;
return if ($file =~ /Android/);
#print "$file $targetname\n" if ($file =~ /$targetname/);
print $tfh "$file\n" if ($file =~ /$targetname/);
}