68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Count Python files and lines in directories."""
|
|
import os
|
|
import sys
|
|
|
|
def count_python_files(dir_path, extensions=('.py',)):
|
|
"""Count Python files and their lines."""
|
|
total_files = 0
|
|
total_lines = 0
|
|
file_details = []
|
|
|
|
for root, dirs, files in os.walk(dir_path):
|
|
for f in files:
|
|
if f.endswith(extensions):
|
|
filepath = os.path.join(root, f)
|
|
try:
|
|
with open(filepath, 'r', encoding='utf-8', errors='ignore') as fh:
|
|
lines = len(fh.readlines())
|
|
total_files += 1
|
|
total_lines += lines
|
|
file_details.append((filepath, lines))
|
|
except Exception as e:
|
|
file_details.append((filepath, f"ERROR: {e}"))
|
|
|
|
return total_files, total_lines, file_details
|
|
|
|
if __name__ == '__main__':
|
|
target_dirs = ['/root/.hermes', '/root/troupe-standup'] if os.path.exists('/root/troupe-standup') else ['/root/.hermes']
|
|
|
|
results = {}
|
|
grand_total_files = 0
|
|
grand_total_lines = 0
|
|
|
|
for dir_path in target_dirs:
|
|
if os.path.exists(dir_path):
|
|
files, lines, details = count_python_files(dir_path)
|
|
results[dir_path] = {'files': files, 'lines': lines, 'details': details}
|
|
grand_total_files += files
|
|
grand_total_lines += lines
|
|
|
|
# Write output file
|
|
output_lines = [
|
|
"=" * 60,
|
|
"PYTHON CODE STATISTICS",
|
|
"=" * 60,
|
|
"",
|
|
f"Total Python files found: {grand_total_files}",
|
|
f"Total lines of Python code: {grand_total_lines}",
|
|
"",
|
|
]
|
|
|
|
for dir_path, data in results.items():
|
|
output_lines.append(f"Directory: {dir_path}")
|
|
output_lines.append(f" Files: {data['files']}, Lines: {data['lines']}")
|
|
output_lines.append(" File breakdown:")
|
|
for filepath, lines in data['details']:
|
|
output_lines.append(f" {filepath}: {lines} lines")
|
|
output_lines.append("")
|
|
|
|
output_text = "\n".join(output_lines)
|
|
print(output_text)
|
|
|
|
# Save to file
|
|
output_file = '/root/.hermes/wizard-outputs/spark/python-stats.txt'
|
|
with open(output_file, 'w') as f:
|
|
f.write(output_text)
|
|
|
|
print(f"\nOutput saved to: {output_file}") |