mirror of
https://github.com/jlengrand/bugsink.git
synced 2026-03-10 08:01:17 +00:00
@@ -7,6 +7,12 @@ from django.core.management.base import BaseCommand, CommandError
|
||||
from bugsink.app_settings import get_settings
|
||||
|
||||
|
||||
# Pattern for valid event IDs (32-character hex strings, lowercase, no dashes, as generated by
|
||||
# `get_filename_for_event_id`
|
||||
#
|
||||
EVENT_ID_PATTERN = re.compile(r'^[0-9a-f]{32}$')
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Clean up old files from the ingest directory. Removes files older than specified days (default: 7)."
|
||||
|
||||
@@ -39,44 +45,37 @@ class Command(BaseCommand):
|
||||
# Calculate cutoff time (files older than this will be removed)
|
||||
cutoff_time = time.time() - (days * 24 * 60 * 60)
|
||||
|
||||
# Pattern for valid event IDs (32-character hex strings)
|
||||
event_id_pattern = re.compile(r'^[0-9a-f]{32}$')
|
||||
|
||||
files_processed = 0
|
||||
files_removed = 0
|
||||
unexpected_files = []
|
||||
|
||||
try:
|
||||
for filename in os.listdir(ingest_dir):
|
||||
filepath = os.path.join(ingest_dir, filename)
|
||||
for filename in os.listdir(ingest_dir):
|
||||
filepath = os.path.join(ingest_dir, filename)
|
||||
|
||||
# Skip directories
|
||||
if os.path.isdir(filepath):
|
||||
continue
|
||||
# Skip directories
|
||||
if os.path.isdir(filepath):
|
||||
continue
|
||||
|
||||
files_processed += 1
|
||||
files_processed += 1
|
||||
|
||||
# Check if filename matches expected event ID format
|
||||
if not event_id_pattern.match(filename):
|
||||
unexpected_files.append(filename)
|
||||
continue
|
||||
# Check if filename matches expected event ID format
|
||||
if not EVENT_ID_PATTERN.match(filename):
|
||||
unexpected_files.append(filename)
|
||||
continue
|
||||
|
||||
# Check file age
|
||||
try:
|
||||
file_mtime = os.path.getmtime(filepath)
|
||||
if file_mtime < cutoff_time:
|
||||
age_days = (time.time() - file_mtime) / (24 * 60 * 60)
|
||||
if dry_run:
|
||||
self.stdout.write("Would remove: {} (age: {:.1f} days)".format(filename, age_days))
|
||||
else:
|
||||
os.remove(filepath)
|
||||
self.stdout.write("Removed: {} (age: {:.1f} days)".format(filename, age_days))
|
||||
files_removed += 1
|
||||
except OSError as e:
|
||||
self.stderr.write("Error processing {}: {}".format(filename, e))
|
||||
|
||||
except OSError as e:
|
||||
raise CommandError("Error accessing ingest directory {}: {}".format(ingest_dir, e))
|
||||
# Check file age
|
||||
try:
|
||||
file_mtime = os.path.getmtime(filepath)
|
||||
if file_mtime < cutoff_time:
|
||||
age_days = (time.time() - file_mtime) / (24 * 60 * 60)
|
||||
if dry_run:
|
||||
self.stdout.write("Would remove: {} (age: {:.1f} days)".format(filename, age_days))
|
||||
else:
|
||||
os.remove(filepath)
|
||||
self.stdout.write("Removed: {} (age: {:.1f} days)".format(filename, age_days))
|
||||
files_removed += 1
|
||||
except OSError as e:
|
||||
self.stderr.write("Error processing {}: {}".format(filename, e))
|
||||
|
||||
# Report results
|
||||
if dry_run:
|
||||
|
||||
Reference in New Issue
Block a user