2025-02-07 19:08:39 -03:00
|
|
|
# backend/script_groups/example_group/x1.py
|
2025-02-07 19:35:59 -03:00
|
|
|
from backend.script_groups.base_script import BaseScript
|
2025-02-07 19:08:39 -03:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2025-02-08 11:23:14 -03:00
|
|
|
import json
|
|
|
|
import csv
|
|
|
|
from datetime import datetime
|
2025-02-07 19:08:39 -03:00
|
|
|
|
|
|
|
class FileCounter(BaseScript):
|
|
|
|
"""
|
2025-02-08 11:23:14 -03:00
|
|
|
File Analysis
|
|
|
|
Analyzes files in directory with configurable filters and reporting
|
2025-02-07 19:08:39 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
def run(self, work_dir: str, profile: dict) -> dict:
|
|
|
|
try:
|
2025-02-08 11:23:14 -03:00
|
|
|
# Get configuration
|
2025-02-07 19:08:39 -03:00
|
|
|
config = self.get_config(work_dir, "example_group")
|
2025-02-08 11:23:14 -03:00
|
|
|
|
|
|
|
# Process configuration values
|
|
|
|
exclude_dirs = [d.strip() for d in config.get("exclude_dirs", "").split(",") if d.strip()]
|
|
|
|
count_hidden = config.get("count_hidden", False)
|
|
|
|
min_size = config.get("min_size", 0)
|
|
|
|
save_report = config.get("save_report", True)
|
|
|
|
report_format = config.get("report_format", "json")
|
2025-02-07 19:08:39 -03:00
|
|
|
|
|
|
|
# Initialize counters
|
|
|
|
extension_counts = {}
|
|
|
|
total_files = 0
|
2025-02-08 11:23:14 -03:00
|
|
|
total_size = 0
|
|
|
|
skipped_files = 0
|
2025-02-07 19:08:39 -03:00
|
|
|
|
|
|
|
# Walk through directory
|
|
|
|
for root, dirs, files in os.walk(work_dir):
|
|
|
|
# Skip excluded directories
|
|
|
|
dirs[:] = [d for d in dirs if d not in exclude_dirs]
|
|
|
|
|
|
|
|
for file in files:
|
2025-02-08 11:23:14 -03:00
|
|
|
file_path = Path(root) / file
|
|
|
|
|
|
|
|
# Skip hidden files if not counting them
|
|
|
|
if not count_hidden and file.startswith('.'):
|
|
|
|
skipped_files += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Check file size
|
|
|
|
try:
|
|
|
|
file_size = file_path.stat().st_size
|
|
|
|
if file_size < min_size:
|
|
|
|
skipped_files += 1
|
|
|
|
continue
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Count file
|
2025-02-07 19:08:39 -03:00
|
|
|
total_files += 1
|
2025-02-08 11:23:14 -03:00
|
|
|
total_size += file_size
|
|
|
|
ext = file_path.suffix.lower() or 'no extension'
|
2025-02-07 19:08:39 -03:00
|
|
|
extension_counts[ext] = extension_counts.get(ext, 0) + 1
|
|
|
|
|
2025-02-08 11:23:14 -03:00
|
|
|
# Prepare results
|
|
|
|
results = {
|
|
|
|
"scan_time": datetime.now().isoformat(),
|
|
|
|
"total_files": total_files,
|
|
|
|
"total_size": total_size,
|
|
|
|
"skipped_files": skipped_files,
|
|
|
|
"extension_counts": extension_counts
|
|
|
|
}
|
|
|
|
|
|
|
|
# Save report if configured
|
|
|
|
if save_report:
|
|
|
|
report_path = Path(work_dir) / f"file_analysis.{report_format}"
|
|
|
|
if report_format == "json":
|
|
|
|
with open(report_path, 'w') as f:
|
|
|
|
json.dump(results, f, indent=2)
|
|
|
|
elif report_format == "csv":
|
|
|
|
with open(report_path, 'w', newline='') as f:
|
|
|
|
writer = csv.writer(f)
|
|
|
|
writer.writerow(["Extension", "Count"])
|
|
|
|
for ext, count in sorted(extension_counts.items()):
|
|
|
|
writer.writerow([ext, count])
|
|
|
|
else: # txt
|
|
|
|
with open(report_path, 'w') as f:
|
|
|
|
f.write(f"File Analysis Report\n")
|
|
|
|
f.write(f"Generated: {results['scan_time']}\n\n")
|
|
|
|
f.write(f"Total Files: {total_files}\n")
|
|
|
|
f.write(f"Total Size: {total_size:,} bytes\n")
|
|
|
|
f.write(f"Skipped Files: {skipped_files}\n\n")
|
|
|
|
f.write("Extension Counts:\n")
|
|
|
|
for ext, count in sorted(extension_counts.items()):
|
|
|
|
f.write(f"{ext}: {count}\n")
|
|
|
|
|
2025-02-07 19:08:39 -03:00
|
|
|
return {
|
|
|
|
"status": "success",
|
2025-02-08 11:23:14 -03:00
|
|
|
"data": results,
|
|
|
|
"output": f"Found {total_files:,} files ({total_size:,} bytes)\n" +
|
|
|
|
f"Skipped {skipped_files} files\n\n" +
|
|
|
|
"Extensions:\n" + "\n".join(
|
|
|
|
f"{ext}: {count:,} files"
|
2025-02-07 19:08:39 -03:00
|
|
|
for ext, count in sorted(extension_counts.items())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
return {
|
|
|
|
"status": "error",
|
|
|
|
"error": str(e)
|
2025-02-08 11:23:14 -03:00
|
|
|
}
|