|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# This script is used to create a list of old builds of a package-container. |
| 4 | +# The singularity containers here are to a large degree based on Bioconda packages. Hence, we can have for a package with |
| 5 | +# a specific version, let's call it `mehr-licht:1.2`, multiple containers. For example the `mehr-licht:1.2--r341_0` container. |
| 6 | +# The intention of this script is to return containers with old builds to safe space. To be clear there is still a container |
| 7 | +# with the same version `mehr-licht:1.2`, just build against a newer R/Python/... version. |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +from collections import defaultdict |
| 12 | + |
| 13 | +image_dict = defaultdict(list) |
| 14 | +image_to_archive = list() |
| 15 | + |
| 16 | +for image in os.listdir(sys.argv[1]): |
| 17 | + if '--' in image: |
| 18 | + image_name, image_build_string = image.rsplit('--', 1) |
| 19 | + if '_' in image_build_string: |
| 20 | + try: |
| 21 | + build_string, build_number = image_build_string.rsplit('_', 1) |
| 22 | + image_dict[image_name].append( (build_string, int(build_number)) ) |
| 23 | + except: |
| 24 | + # some wired image names, needs to be investigated |
| 25 | + pass |
| 26 | + |
| 27 | +for k, v in image_dict.items(): |
| 28 | + if not v: |
| 29 | + continue |
| 30 | + # We want to have the latest build so we need to sort for the latest build-number. |
| 31 | + # However, we can have the same build-number multiple-times with a different build-string. |
| 32 | + # In this case we sort the build-string. This is a bit arbritrary, but should cover hopefully most cases. |
| 33 | + v.sort(key=lambda x: (x[1], x[0])) |
| 34 | + # pop the latest element after sorting. The latest element should be the most recent build, all remaining |
| 35 | + # images stay in the list and can be archived. |
| 36 | + v.pop() |
| 37 | + for build_string, build_number in v: |
| 38 | + name = f'{k}--{build_string}_{build_number}' |
| 39 | + if os.path.exists(name): |
| 40 | + image_to_archive.append(name) |
| 41 | + print(name) |
| 42 | + else: |
| 43 | + sys.exit(f'image "{name}" does not exist') |
| 44 | + |
| 45 | +#do something useful with "image_to_archive" |
| 46 | + |
0 commit comments