Paul Sokolovsky | 0d91816 | 2024-08-12 18:05:20 +0300 | [diff] [blame] | 1 | #!/usr/bin/env -S python3 -u |
| 2 | # |
| 3 | # Copyright (c) 2024 Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | import argparse |
| 8 | import datetime |
| 9 | import re |
| 10 | from subprocess import check_call, check_output |
| 11 | |
| 12 | |
| 13 | REMOTE = "ssh://%s@review.trustedfirmware.org:29418/TF-A/trusted-firmware-a" |
Paul Sokolovsky | 3769d28 | 2024-08-22 13:06:56 +0300 | [diff] [blame] | 14 | # Remove references having timestamps older than so many days. |
| 15 | CUTOFF_DAYS = 30 |
Paul Sokolovsky | 0d91816 | 2024-08-12 18:05:20 +0300 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def check_call_maybe_dry(args, cmd): |
| 19 | if args.dry_run: |
| 20 | print("Would run:", " ".join(cmd)) |
| 21 | return |
| 22 | check_call(cmd) |
| 23 | |
| 24 | |
| 25 | def main(): |
| 26 | argp = argparse.ArgumentParser(description="Clean up old sandbox tags/branches in TF-A project") |
| 27 | argp.add_argument("--user", help="user to connect to Gerrit") |
| 28 | argp.add_argument("--limit", type=int, default=-1, help="limit deletions to this number") |
| 29 | argp.add_argument("--dry-run", action="store_true", help="don't perform any changes") |
| 30 | args = argp.parse_args() |
| 31 | |
| 32 | if not args.user: |
| 33 | argp.error("--user parameter is required") |
| 34 | global REMOTE |
| 35 | REMOTE = REMOTE % args.user |
| 36 | |
Paul Sokolovsky | 3769d28 | 2024-08-22 13:06:56 +0300 | [diff] [blame] | 37 | cutoff = (datetime.datetime.now() - datetime.timedelta(days=CUTOFF_DAYS)).strftime("%Y%m%d") |
Paul Sokolovsky | 0d91816 | 2024-08-12 18:05:20 +0300 | [diff] [blame] | 38 | |
| 39 | print("Cutoff date:", cutoff) |
| 40 | |
| 41 | del_cnt = 0 |
| 42 | |
| 43 | def process_ref_pattern(ref_pat): |
| 44 | nonlocal del_cnt |
| 45 | |
| 46 | out = check_output(["git", "ls-remote", REMOTE, ref_pat], text=True) |
| 47 | |
| 48 | for line in out.rstrip().split("\n"): |
Paul Sokolovsky | cdfc4cc | 2024-09-09 18:07:13 +0300 | [diff] [blame^] | 49 | if not line: |
| 50 | break |
Paul Sokolovsky | 0d91816 | 2024-08-12 18:05:20 +0300 | [diff] [blame] | 51 | rev, tag = line.split(None, 1) |
| 52 | m = re.match(r".+-(\d{8}T\d{4})", tag) |
| 53 | delete = False |
| 54 | if not m: |
| 55 | print("Warning: Cannot parse timestamp from tag '%s', assuming stray ref and deleting" % tag) |
| 56 | delete = True |
| 57 | else: |
| 58 | tstamp = m.group(1) |
| 59 | delete = tstamp < cutoff |
| 60 | |
| 61 | if delete: |
| 62 | check_call_maybe_dry(args, ["git", "push", REMOTE, ":" + tag]) |
| 63 | del_cnt += 1 |
| 64 | if del_cnt == args.limit: |
| 65 | break |
| 66 | |
| 67 | process_ref_pattern("refs/tags/sandbox/*") |
| 68 | if del_cnt != args.limit: |
| 69 | process_ref_pattern("refs/heads/sandbox/*") |
| 70 | |
| 71 | |
| 72 | if __name__ == "__main__": |
| 73 | main() |