-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwc.py
executable file
·74 lines (58 loc) · 2.08 KB
/
dwc.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
# coding: utf-8
import glob
import os
def matched_exclude(fn, patterns):
for pat in patterns.split(','):
if glob.fnmatch.fnmatch(fn, pat.strip()):
return True
return False
def count_lines(directory, ext=None,
exclude_patterns=None, exfuncs=None, detail=False):
'''Count lines in each file inside directory
ext: file extension to count
exclude_patterns: comma separated patterns to ignore
exfuncs: additional functions to exclude_patterns files from result
'''
if not exclude_patterns:
exclude_patterns = '*.swp'
if exfuncs is None:
exfuncs = []
if ext and not ext.startswith('.'):
ext = '.' + ext
exfuncs.append(lambda fn: not fn.endswith(ext))
total_lines = 0
for root, _, files in os.walk(directory):
for f in files:
c = 0
path = os.path.join(root, f)
if matched_exclude(path, exclude_patterns):
continue
if any(func(path) for func in exfuncs):
continue
try:
with open(path) as fi:
for line in fi:
c += 1
except UnicodeDecodeError as e:
# pyc files, binary files...
continue
if detail:
print("%7d: %s" % (c, path))
total_lines += c
return total_lines
def cli():
import argparse
argp = argparse.ArgumentParser()
argp.add_argument('directory', default=['.'], nargs='*')
argp.add_argument('-t', '--filetype', type=str, default='')
argp.add_argument('-d', '--debug', action='store_true')
argp.add_argument(
'-e', '--exclude_patterns', type=str, default='',
help='exclude_patterns: comma separated patterns to ignore'
)
args = argp.parse_args()
ignore_git = [lambda f: '.git/' in f]
for d in args.directory:
total_lines = count_lines(d, args.filetype, args.exclude_patterns,
detail=args.debug, exfuncs=ignore_git)
print('%7d: %s' % (total_lines, d))