|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Remove CMR ID association files for items in the input list. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python remove_association_files.py --list path/to/list.txt --env {uat,ops} |
| 7 | +
|
| 8 | +The list file should contain one item per line, where the first token on the |
| 9 | +line is the file identifier to remove. For example lines like: |
| 10 | + C3385050059-OB_CLOUD () |
| 11 | + C3385050045-OB_CLOUD () |
| 12 | +
|
| 13 | +This script will delete matching files from tests/cmr/l2ss-py/{env}/<fileid>. |
| 14 | +""" |
| 15 | + |
| 16 | +import argparse |
| 17 | +from pathlib import Path |
| 18 | +import sys |
| 19 | + |
| 20 | + |
| 21 | +def parse_args() -> argparse.Namespace: |
| 22 | + parser = argparse.ArgumentParser( |
| 23 | + description=( |
| 24 | + "Remove corresponding files in tests/cmr/l2ss-py/{env}/ based on a list " |
| 25 | + "of items (first token per line is treated as the file id)." |
| 26 | + ) |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + "--list", |
| 30 | + required=True, |
| 31 | + dest="list_path", |
| 32 | + help="Path to input list file containing items; first token is the file id.", |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "--env", |
| 36 | + required=True, |
| 37 | + choices=["uat", "ops"], |
| 38 | + help="Environment directory under tests/cmr/l2ss-py to clean (uat or ops).", |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + "--dry-run", |
| 42 | + action="store_true", |
| 43 | + help="Only print what would be deleted without removing files.", |
| 44 | + ) |
| 45 | + return parser.parse_args() |
| 46 | + |
| 47 | + |
| 48 | +def iter_file_ids(list_file: Path): |
| 49 | + with list_file.open("r", encoding="utf-8") as fh: |
| 50 | + for raw_line in fh: |
| 51 | + line = raw_line.strip() |
| 52 | + if not line: |
| 53 | + continue |
| 54 | + # Extract the first token (before any whitespace or opening parenthesis) |
| 55 | + token = line.split()[0] |
| 56 | + # In case lines are like "ID (something)", ensure we strip trailing '(' if attached |
| 57 | + file_id = token.split("(")[0].strip() |
| 58 | + if file_id: |
| 59 | + yield file_id |
| 60 | + |
| 61 | + |
| 62 | +def main() -> int: |
| 63 | + args = parse_args() |
| 64 | + |
| 65 | + list_file = Path(args.list_path).expanduser().resolve() |
| 66 | + if not list_file.exists(): |
| 67 | + print(f"List file not found: {list_file}", file=sys.stderr) |
| 68 | + return 1 |
| 69 | + |
| 70 | + # Base directory: repo_root/tests/cmr/l2ss-py/{env} |
| 71 | + repo_root = Path(__file__).resolve().parent |
| 72 | + base_dir = repo_root / "tests" / "cmr" / "l2ss-py" / args.env |
| 73 | + if not base_dir.exists(): |
| 74 | + print(f"Environment directory not found: {base_dir}", file=sys.stderr) |
| 75 | + return 1 |
| 76 | + |
| 77 | + exit_code = 0 |
| 78 | + for file_id in iter_file_ids(list_file): |
| 79 | + target_path = base_dir / file_id |
| 80 | + if target_path.exists(): |
| 81 | + if args.dry_run: |
| 82 | + print(f"DRY-RUN would remove: {target_path}") |
| 83 | + else: |
| 84 | + try: |
| 85 | + target_path.unlink() |
| 86 | + print(f"Removed: {target_path}") |
| 87 | + except OSError as exc: |
| 88 | + print(f"Failed to remove {target_path}: {exc}", file=sys.stderr) |
| 89 | + exit_code = 1 |
| 90 | + else: |
| 91 | + print(f"Not found (skip): {target_path}") |
| 92 | + |
| 93 | + return exit_code |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + raise SystemExit(main()) |
| 98 | + |
| 99 | + |
0 commit comments