|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Cleanup orphaned accounts from interrupted tests. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python cleanup_orphaned_accounts.py |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import json |
| 11 | +from fxa.core import Client |
| 12 | +from fxa.errors import ClientError, ServerError |
| 13 | + |
| 14 | +ACCT_TRACKING_FILE = os.path.join(os.path.dirname(__file__), ".accounts_tracking.json") |
| 15 | +FXA_API_HOST = os.environ.get("FXA_API_HOST", "https://api-accounts.stage.mozaws.net") |
| 16 | + |
| 17 | + |
| 18 | +def load_tracked_accounts(): |
| 19 | + if not os.path.exists(ACCT_TRACKING_FILE): |
| 20 | + return [] |
| 21 | + |
| 22 | + try: |
| 23 | + with open(ACCT_TRACKING_FILE, "r") as f: |
| 24 | + return json.load(f) |
| 25 | + except (json.JSONDecodeError, IOError) as e: |
| 26 | + print(f"Warning: Could not load tracking file: {e}") |
| 27 | + return [] |
| 28 | + |
| 29 | + |
| 30 | +def save_tracked_accounts(accounts): |
| 31 | + try: |
| 32 | + if not accounts: |
| 33 | + if os.path.exists(ACCT_TRACKING_FILE): |
| 34 | + os.remove(ACCT_TRACKING_FILE) |
| 35 | + else: |
| 36 | + with open(ACCT_TRACKING_FILE, "w") as f: |
| 37 | + json.dump(accounts, f, indent=2) |
| 38 | + except IOError as e: |
| 39 | + print(f"Warning: Could not save tracking file: {e}") |
| 40 | + raise |
| 41 | + |
| 42 | + |
| 43 | +def remove_account_from_tracking(email): |
| 44 | + accounts = load_tracked_accounts() |
| 45 | + accounts = [acc for acc in accounts if acc["email"] != email] |
| 46 | + save_tracked_accounts(accounts) |
| 47 | + |
| 48 | + |
| 49 | +def cleanup_account(client, account): |
| 50 | + email = account["email"] |
| 51 | + password = account["password"] |
| 52 | + |
| 53 | + try: |
| 54 | + client.destroy_account(email, password) |
| 55 | + print(f" ✓ Deleted: {email}") |
| 56 | + return True |
| 57 | + except (ServerError, ClientError) as ex: |
| 58 | + print(f" ✗ Delete failed: {email} - {ex}") |
| 59 | + return False |
| 60 | + except Exception as ex: |
| 61 | + print(f" ✗ Delete error: {email} - {ex}") |
| 62 | + return False |
| 63 | + |
| 64 | + |
| 65 | +def cleanup_all_accounts(): |
| 66 | + accounts = load_tracked_accounts() |
| 67 | + |
| 68 | + if not accounts: |
| 69 | + print("No accounts to clean up.") |
| 70 | + return 0, 0 |
| 71 | + |
| 72 | + print(f"\nFound {len(accounts)} accounts") |
| 73 | + print("\nAttempting to delete accounts...\n") |
| 74 | + |
| 75 | + client = Client(FXA_API_HOST) |
| 76 | + successful = 0 |
| 77 | + failed = 0 |
| 78 | + |
| 79 | + for account in accounts: |
| 80 | + if cleanup_account(client, account): |
| 81 | + successful += 1 |
| 82 | + remove_account_from_tracking(account["email"]) |
| 83 | + else: |
| 84 | + failed += 1 |
| 85 | + |
| 86 | + print(f"\nResults: {successful} deleted, {failed} failed") |
| 87 | + |
| 88 | + return successful, failed |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + cleanup_all_accounts() |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments