59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Line counter verification script - counts lines in Python files and outputs JSON report."""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def count_python_lines(filepath: str) -> dict:
|
|
"""Count lines in a Python file and return metadata."""
|
|
path = Path(filepath)
|
|
if not path.exists():
|
|
return {"error": "File not found", "filepath": filepath, "success": False}
|
|
|
|
try:
|
|
with open(filepath, 'r', encoding='utf-8', errors='replace') as f:
|
|
lines = f.readlines()
|
|
|
|
total = len(lines)
|
|
non_empty = sum(1 for line in lines if line.strip())
|
|
blank = total - non_empty
|
|
|
|
return {
|
|
"filepath": filepath,
|
|
"total_lines": total,
|
|
"non_empty_lines": non_empty,
|
|
"blank_lines": blank,
|
|
"success": True
|
|
}
|
|
except Exception as e:
|
|
return {"error": str(e), "filepath": filepath, "success": False}
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print(json.dumps({"error": "Usage: python_line_counter.py <file1> [file2...]"}))
|
|
sys.exit(1)
|
|
|
|
files = sys.argv[1:]
|
|
results = []
|
|
total_lines = 0
|
|
|
|
for f in files:
|
|
result = count_python_lines(f)
|
|
results.append(result)
|
|
if result.get("success"):
|
|
total_lines += result.get("total_lines", 0)
|
|
|
|
output = {
|
|
"files_processed": len(files),
|
|
"total_lines": total_lines,
|
|
"results": results
|
|
}
|
|
|
|
print(json.dumps(output, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |