|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Helper script for working around a "feature" in Gitlab's pypi package repositories. |
| 4 | +Pypi does not support uploading packages of the same version, |
| 5 | +they believe the version should be bumped for any change. |
| 6 | +We're going to delete packages instead. |
| 7 | +
|
| 8 | +Call this script with positional args. |
| 9 | +The first is the token with read and write access to the pypi repository. |
| 10 | +This can be your personal access token or $CI_DEPLOY_PASSWORD. |
| 11 | +$CI_JOB_TOKEN does not work, it doesn't have write access. |
| 12 | +The remainder is an unlimited list of wheels to be uploaded. |
| 13 | +
|
| 14 | +If any local wheels find a match in name and version in the remote pypi repository, the remote will be deleted. |
| 15 | +""" |
| 16 | +import sys |
| 17 | +import requests |
| 18 | +import pkginfo |
| 19 | + |
| 20 | +TOKEN = sys.argv[1] |
| 21 | +for wheel_loc in sys.argv[2:]: |
| 22 | + wheel = pkginfo.Wheel(wheel_loc) |
| 23 | + # Get packages, handling pagination |
| 24 | + responses = [] |
| 25 | + response = requests.get( |
| 26 | + f'https://git.grammatech.com/api/v4/projects/1587/packages?package_name={wheel.name}', |
| 27 | + headers={'PRIVATE-TOKEN': TOKEN}, |
| 28 | + ) |
| 29 | + responses.append(response) |
| 30 | + while response.links.get('next'): |
| 31 | + response = requests.get(response.links.get('next')['url'], |
| 32 | + headers={'PRIVATE-TOKEN': TOKEN}) |
| 33 | + if response.status_code != 200: |
| 34 | + raise Exception(f'{response.status_code} status code while requesting package listings filtered by local name: {wheel.name}') |
| 35 | + responses.append(response) |
| 36 | + |
| 37 | + packages = [package for response in responses for package in response.json()] |
| 38 | + # Delete all matching packages |
| 39 | + for package in packages: |
| 40 | + if wheel.version == package['version'] and wheel.name == package['name']: |
| 41 | + print(f'Deleting {package["name"]} {package["version"]}.') |
| 42 | + response = requests.delete( |
| 43 | + f'https://git.grammatech.com/api/v4/projects/1587/packages/{package["id"]}', |
| 44 | + headers={'PRIVATE-TOKEN': TOKEN}, |
| 45 | + ) |
| 46 | + if response.status_code != 204: |
| 47 | + raise Exception(f'{response.status_code} status code while deleting this package: {package["name"]} {package["version"]}') |
0 commit comments