spark: add scratch word count utility and report
This commit is contained in:
1 parent
650dd71539
commit
6c30d8ce41
2 files changed
+75
No files matched your search
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Count words in all text files in the scratch directory and output JSON report."""
|
||||
import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
SCRATCH_DIR = "/root/.hermes/profiles/spark/cache/scratch"
|
||||
OUTPUT_FILE = "/root/.hermes/wizard-outputs/spark/word_count_report.json"
|
||||
|
||||
def count_words_in_file(filepath):
|
||||
"""Count words in a file, return 0 if unreadable."""
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
|
||||
content = f.read()
|
||||
return len(content.split())
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
def main():
|
||||
results = []
|
||||
total_words = 0
|
||||
|
||||
scratch_path = Path(SCRATCH_DIR)
|
||||
if scratch_path.exists():
|
||||
for filepath in sorted(scratch_path.iterdir()):
|
||||
if filepath.is_file():
|
||||
word_count = count_words_in_file(filepath)
|
||||
total_words += word_count
|
||||
results.append({
|
||||
"file": filepath.name,
|
||||
"word_count": word_count
|
||||
})
|
||||
|
||||
report = {
|
||||
"scratch_directory": SCRATCH_DIR,
|
||||
"files_processed": len(results),
|
||||
"total_words": total_words,
|
||||
"files": results
|
||||
}
|
||||
|
||||
with open(OUTPUT_FILE, 'w') as f:
|
||||
json.dump(report, f, indent=2)
|
||||
|
||||
print(f"Word count report written to {OUTPUT_FILE}")
|
||||
print(f"Files: {len(results)}, Total words: {total_words}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"scratch_directory": "/root/.hermes/profiles/spark/cache/scratch",
|
||||
"files_processed": 5,
|
||||
"total_words": 789,
|
||||
"files": [
|
||||
{
|
||||
"file": ".last_prune",
|
||||
"word_count": 0
|
||||
},
|
||||
{
|
||||
"file": "hermes-snap-18c65962e510.sh",
|
||||
"word_count": 261
|
||||
},
|
||||
{
|
||||
"file": "hermes-snap-42e725078645.sh",
|
||||
"word_count": 261
|
||||
},
|
||||
{
|
||||
"file": "hermes-snap-717571bc4904.sh",
|
||||
"word_count": 261
|
||||
},
|
||||
{
|
||||
"file": "tmpnx0d8oia.json",
|
||||
"word_count": 6
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user