|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +"""Remove old packages from Gemfury.""" |
| 20 | + |
| 21 | +import datetime |
| 22 | +import os |
| 23 | +import random |
| 24 | +import time |
| 25 | + |
| 26 | +import requests |
| 27 | + |
| 28 | +BASE_URL = "https://api.fury.io/1" |
| 29 | + |
| 30 | + |
| 31 | +def main(): |
| 32 | + token = os.environ["GEMFURY_API_TOKEN"] |
| 33 | + cutoff = datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=7) |
| 34 | + |
| 35 | + with requests.Session() as session: |
| 36 | + session.headers = { |
| 37 | + "Authorization": f"Bearer {token}", |
| 38 | + "Accept": "application/json", |
| 39 | + } |
| 40 | + |
| 41 | + packages = session.get(f"{BASE_URL}/packages?limit=50").json() |
| 42 | + for i, package in enumerate(packages): |
| 43 | + if i > 0: |
| 44 | + time.sleep(random.randint(3, 8)) |
| 45 | + |
| 46 | + print("Cleaning", package["name"]) |
| 47 | + versions = session.get( |
| 48 | + f"{BASE_URL}/packages/{package['id']}/versions?limit=50" |
| 49 | + ).json() |
| 50 | + print(versions) |
| 51 | + versions.sort(key=lambda v: v["created_at"], reverse=True) |
| 52 | + |
| 53 | + # Always keep at least 1 version |
| 54 | + to_delete = [ |
| 55 | + version["id"] |
| 56 | + for version in versions[1:] |
| 57 | + if version["created_at"] < cutoff.isoformat() |
| 58 | + ] |
| 59 | + print("Removing", len(to_delete), "version(s) of", len(versions)) |
| 60 | + |
| 61 | + for version_id in to_delete: |
| 62 | + session.delete(f"{BASE_URL}/versions/{version_id}").raise_for_status() |
| 63 | + time.sleep(random.randint(1, 3)) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + main() |
0 commit comments