Download scripts/cleanup_logs.py from dn-scribe/attstore: direct link, hf CLI and curl.
- Browser
- Download file 3.64 kB
-
https://huggingface.co/spaces/dn-scribe/attstore/resolve/main/scripts/cleanup_logs.py
- Command line
-
hf download hf://spaces/dn-scribe/attstore/scripts/cleanup_logs.py
-
curl -L -o cleanup_logs.py https://huggingface.co/spaces/dn-scribe/attstore/resolve/main/scripts/cleanup_logs.py
3.64 kB
| #!/usr/bin/env python3 | |
| """ | |
| Manual log cleanup script for Attestation Store. | |
| Usage: | |
| python scripts/cleanup_logs.py # Use default retention from LOG_RETENTION_DAYS | |
| python scripts/cleanup_logs.py --days 30 # Delete logs older than 30 days | |
| python scripts/cleanup_logs.py --dry-run # Preview what would be deleted | |
| """ | |
| import argparse | |
| import sys | |
| from datetime import datetime, timedelta | |
| # Add parent directory to path to import app modules | |
| import os | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from app import app | |
| from models import SystemLog | |
| def cleanup_logs(retention_days=None, dry_run=False): | |
| """ | |
| Delete logs older than the specified retention period. | |
| Args: | |
| retention_days (int, optional): Number of days to retain logs. | |
| If not provided, uses LOG_RETENTION_DAYS from config. | |
| dry_run (bool): If True, only show what would be deleted without deleting. | |
| Returns: | |
| int: Number of log entries that were (or would be) deleted | |
| """ | |
| with app.app_context(): | |
| from logging_utils import cleanup_old_logs | |
| from flask import current_app | |
| # Get retention period from config if not specified | |
| if retention_days is None: | |
| config = current_app.config.get('STORAGE_CONFIG') | |
| retention_days = config.LOG_RETENTION_DAYS if config else 10 | |
| # Skip cleanup if retention is 0 or negative | |
| if retention_days <= 0: | |
| print("Log cleanup disabled (retention_days <= 0)") | |
| return 0 | |
| # Calculate cutoff date | |
| cutoff_date = datetime.utcnow() - timedelta(days=retention_days) | |
| # Count logs to be deleted | |
| count = SystemLog.query.filter(SystemLog.timestamp < cutoff_date).count() | |
| if dry_run: | |
| print(f"DRY RUN: Would delete {count} log entries older than {retention_days} days (before {cutoff_date.isoformat()})") | |
| return count | |
| # Perform actual cleanup | |
| deleted = cleanup_old_logs(retention_days=retention_days) | |
| print(f"Deleted {deleted} log entries older than {retention_days} days (before {cutoff_date.isoformat()})") | |
| return deleted | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description='Clean up old logs from the Attestation Store database', | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=""" | |
| Examples: | |
| # Use default retention period from LOG_RETENTION_DAYS environment variable | |
| python scripts/cleanup_logs.py | |
| # Delete logs older than 30 days | |
| python scripts/cleanup_logs.py --days 30 | |
| # Preview what would be deleted without actually deleting | |
| python scripts/cleanup_logs.py --dry-run | |
| # Preview deletion with custom retention period | |
| python scripts/cleanup_logs.py --days 60 --dry-run | |
| """ | |
| ) | |
| parser.add_argument( | |
| '--days', | |
| type=int, | |
| metavar='N', | |
| help='Number of days to retain logs (logs older than N days will be deleted)' | |
| ) | |
| parser.add_argument( | |
| '--dry-run', | |
| action='store_true', | |
| help='Show what would be deleted without actually deleting' | |
| ) | |
| args = parser.parse_args() | |
| try: | |
| deleted = cleanup_logs(retention_days=args.days, dry_run=args.dry_run) | |
| if deleted == 0 and not args.dry_run: | |
| print("No logs were deleted.") | |
| sys.exit(0) | |
| except Exception as e: | |
| print(f"Error: {e}", file=sys.stderr) | |
| sys.exit(1) | |
| if __name__ == '__main__': | |
| main() | |