Skip to content

Commit 0dffb1e

Browse files
committed
find docker hub orphans script (for pruning)
1 parent 39bfeec commit 0dffb1e

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env -S pkgx [email protected] uv run --script
2+
# /// script
3+
# dependencies = [
4+
# "requests"
5+
# ]
6+
# ///
7+
8+
import requests
9+
from collections import defaultdict
10+
from pprint import pprint
11+
12+
def get_all_tags_and_digests(image_name):
13+
"""
14+
Fetches all tags and their digests for a Docker Hub image, handling pagination.
15+
"""
16+
base_url = f"https://registry.hub.docker.com/v2/repositories/{image_name}/tags"
17+
tags_and_digests = []
18+
url = base_url
19+
20+
while url:
21+
response = requests.get(url)
22+
if response.status_code != 200:
23+
print(f"Failed to fetch data: {response.status_code}, {response.text}")
24+
return []
25+
26+
data = response.json()
27+
for result in data['results']:
28+
tag = result['name']
29+
digest = result['digest']
30+
if digest:
31+
tags_and_digests.append((tag, digest))
32+
33+
url = data.get('next') # Get the next page URL
34+
35+
return tags_and_digests
36+
37+
def find_orphaned_tags(tags_and_digests):
38+
"""Identifies tags with unique digests."""
39+
digest_count = defaultdict(int)
40+
for _, digest in tags_and_digests:
41+
digest_count[digest] += 1
42+
43+
orphaned_tags = [tag for tag, digest in tags_and_digests if digest_count[digest] == 1]
44+
return orphaned_tags
45+
46+
if __name__ == "__main__":
47+
image_name = "pkgxdev/pkgx"
48+
tags_and_digests = get_all_tags_and_digests(image_name)
49+
if tags_and_digests:
50+
orphaned_tags = find_orphaned_tags(tags_and_digests)
51+
if len(orphaned_tags) > 0:
52+
print("orphans:")
53+
for tag in orphaned_tags:
54+
print(tag)
55+
else:
56+
print("no orphans, here’s the tags instead:")
57+
tags = [tag for tag, _ in tags_and_digests if tag.startswith('v')]
58+
pprint(sorted(tags))

0 commit comments

Comments
 (0)