Skip to content

Commit 1c9d673

Browse files
Merge pull request #350 from opencivicdata/add-clean-info
Add logging to pupa clean failsafe
2 parents 21d36c2 + 487d55c commit 1c9d673

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

pupa/cli/commands/clean.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def add_args(self):
4444
action="store_true",
4545
help="delete objects without getting user confirmation",
4646
)
47+
self.add_argument(
48+
"--yes",
49+
action="store_true",
50+
help="assumes an answer of 'yes' to all interactive prompts",
51+
default=False,
52+
)
4753

4854
def get_stale_objects(self, window):
4955
"""
@@ -88,15 +94,22 @@ def handle(self, args, other):
8894
)
8995
self.report_stale_objects()
9096
else:
91-
num_stale_objects = len(list(self.get_stale_objects(args.window)))
97+
stale_objects = list(self.get_stale_objects(args.window))
98+
num_stale_objects = len(stale_objects)
99+
100+
if args.noinput and args.yes:
101+
self.remove_stale_objects(args.window)
102+
sys.exit()
92103

93104
if args.noinput:
94105
# Fail-safe to avoid deleting a large amount of objects
95106
# without explicit confimation
96107
if num_stale_objects > 10:
97108
print(
98-
"This command would delete more than 10 objects."
99-
"If you're sure, re-run without --noinput to provide confirmation."
109+
f"This command would delete {num_stale_objects} objects: "
110+
f"\n{stale_objects}"
111+
"\nIf you're sure, re-run without --noinput to provide confirmation."
112+
"\nOr re-run with --yes to assume a yes answer to all prompts."
100113
)
101114
sys.exit(1)
102115
else:

pupa/tests/clean/test_clean.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_clean_command(subparsers):
8585

8686
# Call clean command
8787
Command(subparsers).handle(
88-
argparse.Namespace(noinput=True, report=False, window=7), []
88+
argparse.Namespace(noinput=True, report=False, window=7, yes=False), []
8989
)
9090

9191
expected_stale_objects = {stale_person, stale_membership}
@@ -117,5 +117,5 @@ def test_clean_command_failsafe(subparsers):
117117
with pytest.raises(SystemExit):
118118
# Should trigger failsafe exist when deleting more than 10 objects
119119
Command(subparsers).handle(
120-
argparse.Namespace(noinput=True, report=False, window=7), []
120+
argparse.Namespace(noinput=True, report=False, window=7, yes=False), []
121121
)

0 commit comments

Comments
 (0)