|
Revision 126
(checked in by fumanchu, 5 years ago)
|
New LOC module.
|
| Line | |
|---|
| 1 |
"""Calculate LOC (lines of code) for a given package directory.""" |
|---|
| 2 |
|
|---|
| 3 |
import os |
|---|
| 4 |
import re |
|---|
| 5 |
|
|---|
| 6 |
def loc(path, pattern="^.*\.py$"): |
|---|
| 7 |
"""Return the number of lines of code for all files in the given path. |
|---|
| 8 |
|
|---|
| 9 |
If the 'pattern' argument is provided, it must be a regular expression |
|---|
| 10 |
against which each filename will be matched. By default, all filenames |
|---|
| 11 |
ending in ".py" are analyzed. |
|---|
| 12 |
""" |
|---|
| 13 |
lines = 0 |
|---|
| 14 |
for root, dirs, files in os.walk(path): |
|---|
| 15 |
for name in files: |
|---|
| 16 |
if re.match(pattern, name): |
|---|
| 17 |
f = open(os.path.join(root, name), 'rb') |
|---|
| 18 |
for line in f: |
|---|
| 19 |
line = line.strip() |
|---|
| 20 |
if line and not line.startswith("#"): |
|---|
| 21 |
lines += 1 |
|---|
| 22 |
f.close() |
|---|
| 23 |
return lines |
|---|