-
Notifications
You must be signed in to change notification settings - Fork 0
/
what.py
executable file
·316 lines (264 loc) · 11.1 KB
/
what.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
#
# Copyright (c) 2005-2024, Neville-Neil Consulting
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# Neither the name of Neville-Neil Consulting nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Author: George V. Neville-Neil
#
# Description: This python script will recursively descend a source
# tree and print out several relevant statistics
"""This script is meant to be run the first time you encounter a new
source base. It recursively descends a file tree and reports several
statistics of interest to code spelunkers. These include:
- The total number of directories that contain code
- The total number of files in all directories.
- The total number of files in each of the following programming
languages: Awk, C, C++, HTML, IDL, Java, Perl, PHP, Java, Python, TCL
The code that determines what computer language a file contains is
flexible in how it does its checking. Right now this is done based on
the file's extension, but can be extended to call an interpreter to
determine if the file is valid code of a particular type.
There are two arguments that can be used.
The -w or --where argument tells the program that for each type of
code it is to list all the directories that contain that type of code.
The -e or --exclude argument excludes certain sub-directories from the
search. The exclude argument can be used several times on the same
line like this:
The -l or --language option says to only find one language, and not
all of them.
The -p or --pretty option allows the user to select HTML or LaTeX
table output format instead of plain text.
what.py -e HTML -e DoxyFiles
The program automatically ignores any file in a CVS diretory.
"""
import os
from os import F_OK
# This is the file extension mapping code. Change this array if you
# wish to change the determination of the files. The format is "key",
# "value" in which the key is the extension and the value is the file
# type. Both the key and value are strings.
def filetype(name):
file_ext_map = { "awk" : "AWK",
"py" : "Python",
"pyl" : "Python",
"el" : "Emacs Lisp",
"elc" : "Emacs Lisp",
"h" : "C or C++ Header Files",
"c" : "C",
"cc" : "C++",
"cpp" : "C++",
"c++" : "C++",
"m" : "Objective C",
"tcl" : "TCL",
"html" : "HTML",
"htm" : "HTML",
"java" : "Java",
"idl" : "Interface Definition Language",
"xml" : "XML",
"xsl" : "XML",
"php" : "PHP",
"inc" : "PHP include",
"sh" : "Shell Script",
"ksh" : "Shell Script",
"bash" : "Shell Script",
"csh" : "Shell Script",
"tcsh" : "Shell Script",
"mk" : "Makefile",
"am" : "Automake",
"m4" : "M4"
}
(root, extension) = os.path.splitext(name)
# Special case the search for Makefiles as these are important but
# do not have an extension
if (len(extension) <= 0):
if (root == "Makefile"):
return "Makefile"
else:
if (root == "configure"):
return "Autoconf"
else:
return "Unknown"
# According to the rules if we have an extension then it ALWAYS has
# a . at the beginning. Strip that .
extension = extension[1:len(extension)]
if (extension not in file_ext_map):
return "Unknown"
return file_ext_map[extension]
# Find out how many lines are in a file.
# We know that "name" is a file because it was passed to us by
# os.walk() in main().
def filelines(name):
try:
file = open(name)
except:
return -1
try:
lines = file.readlines()
except:
lines = ""
file.close()
return len(lines)
def main():
file_map = {} # The map of file type to the number that exist.
size_map = {} # The map of file type to the number of lines.
location_map = {} # A map of the file type to a list of directories
dir_num = 0 # How many directories are there?
exclude_list = [] # List of directories to exclude, can be empty
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-w", "--where",
action="store_true", dest="where", default=False,
help="Print where files are located")
parser.add_option("-e", "--exclude", action="append", type="string",
dest="exclude_list", help="Directories to exclude")
parser.add_option("-l", "--language", action="store", type="string",
dest="language",
help="Find only code of a particular language")
parser.add_option("-p", "--pretty", action="store", type="string",
dest="pretty",
help="Pretty print in HTML or LaTeX table format")
(options, args) = parser.parse_args()
where = options.where
exclude_list = options.exclude_list
language = options.language
pretty = options.pretty
for root, dirs, files in os.walk("."):
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
if exclude_list:
for dir in exclude_list:
if dir in dirs:
dirs.remove(dir)
for name in files:
fullname = os.path.join(root, name)
if not os.access(fullname, F_OK):
continue
type = filetype(fullname)
if (language and (type != language)):
continue
if type not in file_map:
file_map[type] = 1
else:
file_map[type] += 1
lines = filelines(fullname)
if lines == -1:
continue
if type not in size_map:
size_map[type] = lines
else:
size_map[type] += lines
if type == "Unknown":
continue
if type not in location_map:
location_map[type] = {root : 1}
dir_num += 1
continue
if root not in location_map[type]:
location_map[type][root] = 1
dir_num += 1
continue
location_map[type][root] += 1
dir_num += 1
# Now do our fancy printing
if (pretty == "HTML"):
print("<table><tr><td>Type</td><td>Number</td><td>Lines</td></tr>")
elif (pretty == "LaTeX"):
print("\\begin{tabular}{|l|l|c|}\n\\hline\nType & Number & Lines\\\\\n\\hline")
else:
print("Type\t\tNumber\t\tLines")
types = file_map.keys()
total_files = 0
total_lines = 0
for key in types:
if key == "Unknown":
continue
total_files += file_map[key]
total_lines += size_map[key]
if (pretty == "HTML"):
print("<tr><td>%s</td><td>%d</td><td>%d</td></tr>") % \
(key, file_map[key], size_map[key])
elif (pretty == "LaTeX"):
print(key, "&", file_map[key], "& \\\\", size_map[key])
else:
print(key, "\t\t", file_map[key], "\t\t", size_map[key])
if (where == True):
dirs = location_map[key].keys()
dirs.sort()
for dir in dirs:
if (pretty == "HTML"):
print ("<tr><td>%s</td><td>%d</td></tr>") % \
(dir, location_map[key][dir])
elif (pretty == "LaTeX"):
print("%s&%d\\\\" % (dir, location_map[key][dir]))
else:
print("\t%s\t%d" % (dir, location_map[key][dir]))
if (pretty == "HTML"):
print("</table><br>")
print("<table>")
print("<tr><td></td><td>Files</td><td>Lines</td></tr>")
print("<tr><td>Identified Code</td><td>%d</td><td>%d</td></tr>") % \
(total_files, total_lines)
elif (pretty == "LaTeX"):
print("\\hline \\hline\n&Files & Lines\\\\")
print("Identified Code & %d & %d\\\\") % (total_files, total_lines)
else:
print("\n\t\t\tFiles\tLines")
print("Identified Code\t\t", total_files, "\t", total_lines, "\n")
if "Unknown" in file_map.keys() and "Unknown" in size_map.keys():
if (pretty == "HTML"):
print("<tr><td>Unknown</td><td>%d</td><td>%d</td></tr>") % \
(file_map["Unknown"], size_map["Unknown"])
elif (pretty == "LaTeX"):
print("Unknown & %d & %d\\\\\n\\hline") % \
(file_map["Unknown"], size_map["Unknown"])
else:
print("Unknown\t\t\t", file_map["Unknown"], "\t", size_map["Unknown"], "\n")
total_files += file_map["Unknown"]
total_lines += size_map["Unknown"]
if (pretty == "HTML"):
print("<tr><td>Total</td><td>%d</td><td>%d</td></tr>") % \
(total_files, total_lines)
elif (pretty == "LaTeX"):
print("Total & %d & %d\\\\\\hline") % (total_files, total_lines)
else:
print("Total\t\t\t", total_files, "\t", total_lines, "\n")
if (where == True):
if (pretty == "HTML"):
print("<tr><td>Number of directories</td><td>%d</td></tr>") % \
dir_num
elif (pretty == "LaTeX"):
print("Number of directories & %d") % dir_num
else:
print("Number of directories\t%d") % dir_num
if (pretty == "HTML"):
print("</table>")
elif (pretty == "LaTeX"):
print("\\end{tabular}")
main()