Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup script to search and remove rageshakes from applications based on a time #61

Merged
merged 10 commits into from
Jan 16, 2023
52 changes: 37 additions & 15 deletions scripts/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
# No dependencies required beyond a modern python3.


class Cleanup(object):
class Cleanup:
"""
Cleanup a rageshake bug repository.

Once created, call cleanup() to begin the actual operation. Statistics are available after cleanup completes.
"""
def __init__(
michaelkaye marked this conversation as resolved.
Show resolved Hide resolved
self,
limits: Dict[str, int],
Expand All @@ -26,6 +31,15 @@ def __init__(
root_path: str,
mxids_to_exclude: List[str],
):
"""
Set options for a cleanup run of a rageshake repository.

@param limits: Map of app name to integer number of days that application's rageshakes should be retained
@param days_to_check: List of ints each representing "days ago" that should be checked for rageshakes to delete
@param dry_run: If set, perform all actions but do not complete deletion of files
@param root_path: Base path to rageshake bug repository
@param mxids_to_exclude: Rageshakes sent by this list of mxids should always be preserved.
"""
self._limits = limits
self._days_to_check = days_to_check
self._dry_run = dry_run
Expand All @@ -42,7 +56,9 @@ def __init__(

def cleanup(self) -> None:
"""
Check for rageshakes to remove according to settings
Check for rageshakes to remove according to settings.

Do not run multiple times as statistics are generated internally during each call.
"""
today = datetime.today()
for days_ago in self._days_to_check:
Expand Down Expand Up @@ -93,35 +109,41 @@ def _check_rageshake(
self, rageshake_folder_path: str, applications_to_delete: Set[str]
) -> bool:
"""
Checks a given rageshake folder, returning True if the rageshake was deleted
Checks a given rageshake folder against the application and userid lists.

If the folder matches, and dryrun mode is disabled, the folder is deleted.

@returns: True if the rageshake matched, False if it was skipped.
"""
app_name = None
mxid = None

try:
app_name = None
mxid = None
with gzip.open(rageshake_folder_path + "/details.log.gz") as details:
for line in details.readlines():
parts = line.decode("utf-8").split(":", maxsplit=1)
if parts[0] == "Application":
app_name = parts[1].strip()
if parts[0] == "user_id":
mxid = parts[1].strip()
if app_name in applications_to_delete:
if mxid in self._mxids_to_exclude:
self.excluded_count_by_user[mxid] += 1
else:
self._delete(rageshake_folder_path)
return True

except FileNotFoundError as e:
print(
f"W Unable to open {e.filename} to check for application name. Ignoring this folder."
)
return False

if app_name in applications_to_delete:
if mxid in self._mxids_to_exclude:
self.excluded_count_by_user[mxid] += 1
else:
self._delete(rageshake_folder_path)
return True

return False

def _delete(self, rageshake_folder_path: str) -> None:
"""
Delete a given rageshake folder
Delete a given rageshake folder, unless dryrun mode is enabled
"""
files = glob.glob(rageshake_folder_path + "/*")
for file in files:
Expand Down Expand Up @@ -180,8 +202,8 @@ def main():

args = parser.parse_args()
application_limits: Dict[str, int] = {}
for x in args.limits:
parts = x.rsplit(":", 1)
for l in args.limits:
parts = l.rsplit(":", 1)
Comment on lines +205 to +206
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l may be a poor name for a var due to the confusion with I and 1, but 🤷‍♂️

try:
if len(parts) < 2:
raise ValueError("missing :")
Expand Down