-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinecount
More file actions
executable file
·153 lines (136 loc) · 4.27 KB
/
linecount
File metadata and controls
executable file
·153 lines (136 loc) · 4.27 KB
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
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl
# Copyright (C) 2004-, Mike Wille, Flowz
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
package main;
my @params;
while(my $param = shift) {
push(@params, $param);
}
if(@params < 1) {
PrintHelp();
exit;
}
my $SearchPath = "/";
my $Recurse = "false";
my $FileTypes = "java|js|pl|cpp|h|c|jsp";
my $FileCount = 0;
for(my $counter = 0; $counter < @params; $counter++) {
if($params[$counter] eq "--help" || $params[$counter] eq "-h") {
PrintHelp();
exit;
} elsif($params[$counter] =~ /^\-{1,2}path=(.*)$/) {
$SearchPath = $1;
if($SearchPath != /^(.*?)\/$/) {
$SearchPath .= "/";
}
} elsif($params[$counter] eq /^\-{1,2}recurse$/ || $params[$counter] eq "-r") {
$Recurse = "true";
} elsif($params[$counter] =~ /^\-{1,2}types=(.*)$/) {
$FileTypes = $1;
$FileTypes =~ s/\,/\|/g;
}
}
print "Recursing ($Recurse)\n";
print "Search Path: $SearchPath\n";
print "File Types: $FileTypes\n";
$TotalCount = TraverseDir($SearchPath);
print "\nThere were a total of $TotalCount lines found in $FileCount files under $SearchPath\n";
sub TraverseDir {
my $dir = shift;
my $Total = 0;
if($Recurse eq "false") {
# if not recursing, we can just do a double grep and forget about directories...
opendir(DIR, $dir);
# First grep out . and .., then grep for FileTypes
my(@files) = grep(/^(.*?)\.($FileTypes)$/, grep(!/^\.\.?$/, readdir (DIR)));
closedir(DIR);
foreach my $file (@files) {
$FileCount++;
print "Found file: $file, checking...";
my $fileTotal = CountFile($dir.$file);
$Total += $fileTotal;
print "$fileTotal\n";
}
return $Total;
} else {
opendir(DIR, $dir);
# First grep out . and .., then grep for FileTypes
my(@files) = grep(!/^\.\.?$/, readdir (DIR));
closedir(DIR);
foreach my $file (@files) {
if(-d $dir.$file) {
$Total += TraverseDir($dir.$file."/");
} elsif($file =~ /^(.*?)\.($FileTypes)$/) {
$FileCount++;
print "Found file: $file, checking...";
my $fileTotal = CountFile($dir.$file);
$Total += $fileTotal;
print "$fileTotal\n";
}
}
return $Total;
}
}
sub CountFile {
my $count = 0;
my $file = shift;
open(IN, $file) or die("Unable to open $file! Reason: $!");
while(<IN>) {
if($_ =~ /(\S){2,}/) {
$count++;
}
}
close(IN);
return $count;
}
######
# Utility functions below...
sub FatalError {
my $message = shift;
print "Fatal Error: $message\n";
Error("$message");
exit;
}
sub Warn {
my $message = shift;
print "Error: $message\n";
Error("$message");
}
sub Log {
my $text = shift;
my $TimeStamp = localtime(time);
open(LOG, ">>$LogFile") or print STDERR "Unable to open log: $LogFile! Reason: $!\n";
print LOG "[$TimeStamp] [info] $text\n";
close(LOG);
}
sub Error {
my $text = shift;
my $TimeStamp = localtime(time);
open(LOG, ">>$LogFile") or print STDERR "Unable to open log: $LogFile! Reason: $!\n";
print LOG "[$TimeStamp] [error] $text\n";
close(LOG);
}
sub PrintHelp {
print "\nUsage: linecount [--path=/path/to/search] [--recurse] [--types=java,cpp,pl,etc.]\n";
print "Seaches the path listed, recursing directories if needed, for the specified files types.\n";
print "Each file found will be opened and the number of lines that have at least 2 characters\n";
print "are counted and accumulated for a final total.\n\n";
print "--path \t\tThe directory to search for files.\n";
print "--recurse,-r\tIf specified, linecount will traverse into subdirectories looking for files.\n";
print "--types\t\tA ',' or ',' separated list of file extensions to search for. (Do not include the '.')\n";
print "\t\t\t\tDefaults to: java|js|pl|cpp|h|c\n";
print "--help \t\t\tPrints this message.\n\n";
}