Skip to content

Commit

Permalink
Make failsafe configurable and remove redundant option
Browse files Browse the repository at this point in the history
  • Loading branch information
hancush committed Sep 6, 2023
1 parent 1c9d673 commit c925e79
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions pupa/cli/commands/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def add_args(self):
"objects not seen in this many days will be deleted from the database"
),
)
self.add_argument(
"--max",
type=int,
default=10,
help="max number of objects to delete without triggering failsafe",
)
self.add_argument(
"--report",
action="store_true",
Expand All @@ -39,11 +45,6 @@ def add_args(self):
" would delete without making any changes to the database"
),
)
self.add_argument(
"--noinput",
action="store_true",
help="delete objects without getting user confirmation",
)
self.add_argument(
"--yes",
action="store_true",
Expand Down Expand Up @@ -97,34 +98,23 @@ def handle(self, args, other):
stale_objects = list(self.get_stale_objects(args.window))
num_stale_objects = len(stale_objects)

if args.noinput and args.yes:
self.remove_stale_objects(args.window)
sys.exit()

if args.noinput:
# Fail-safe to avoid deleting a large amount of objects
# without explicit confimation
if num_stale_objects > 10:
print(
f"This command would delete {num_stale_objects} objects: "
f"\n{stale_objects}"
"\nIf you're sure, re-run without --noinput to provide confirmation."
"\nOr re-run with --yes to assume a yes answer to all prompts."
)
sys.exit(1)
else:
print(
f"{num_stale_objects} objects in your database have not been seen "
f"in {args.window} days:\n{stale_objects}"
)

if num_stale_objects > args.max:
print(
f"This will permanently delete"
f" {num_stale_objects} objects from your database"
f" that have not been scraped within the last {args.window}"
" days. Are you sure? (Y/N)"
f"WARNING: {num_stale_objects} exceeds the failsafe limit of {args.max}."
)
resp = input()
if resp != "Y":
sys.exit()

print(
"Removing objects that haven't been seen in a scrape within"
f" the last {args.window} days..."
)
self.remove_stale_objects(args.window)
if args.yes:
print("Proceeding to deletion because you specified --yes.")

else:
print(f"Permanently delete {num_stale_objects} objects? [Y/n]")
response = input()

if args.yes or response == "Y":
self.remove_stale_objects(args.window)
print(f"Removed {num_stale_objects} from your database.")

0 comments on commit c925e79

Please sign in to comment.