|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Match biocontainers on the local filesystem to registry entries. |
| 4 | +# Install won't copy the container, but rather just use the provided path! |
| 5 | +# You should still set up your module_base before doing this (as we need |
| 6 | +# to write module files. |
| 7 | + |
| 8 | +# **ALWAYS** dry run first! |
| 9 | +# python biocontainer-match.py --dry-run |
| 10 | +# At the root of your depot do: |
| 11 | +# python biocontainer-match.py |
| 12 | +# Force module reinstall (rewrite of module file) |
| 13 | +# python biocontainer-match.py --force |
| 14 | +# From another directory do: |
| 15 | +# python biocontainer-match.py --containers /path/to/depot/root |
| 16 | +# If the namespace ever changes: |
| 17 | +# python biocontainer-match.py --containers /path/to/depot/root --namespace quay.io/biocontainers |
| 18 | +# Only one namespace is currently supported! |
| 19 | + |
| 20 | +import argparse |
| 21 | +import os |
| 22 | +import re |
| 23 | +import sys |
| 24 | + |
| 25 | +from shpc.logger import logger |
| 26 | +from shpc.main import get_client |
| 27 | + |
| 28 | + |
| 29 | +def get_parser(): |
| 30 | + parser = argparse.ArgumentParser( |
| 31 | + description="SHPC BioContainer Adder", |
| 32 | + formatter_class=argparse.RawTextHelpFormatter, |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "--dry-run", |
| 36 | + help="Dry run only! This will preview installs.", |
| 37 | + default=False, |
| 38 | + action="store_true", |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + "--force", help="Force reinstall of module", default=False, action="store_true" |
| 42 | + ) |
| 43 | + parser.add_argument("--containers", help="Path to depot root.", default=os.getcwd()) |
| 44 | + parser.add_argument( |
| 45 | + "--namespace", |
| 46 | + help="Namespace to use for containers (quay.io/biocontainers)", |
| 47 | + default="quay.io/biocontainers", |
| 48 | + ) |
| 49 | + return parser |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + |
| 54 | + parser = get_parser() |
| 55 | + |
| 56 | + # If an error occurs while parsing the arguments, the interpreter will exit with value 2 |
| 57 | + args, extra = parser.parse_known_args() |
| 58 | + |
| 59 | + # Show args to the user |
| 60 | + print(" containers: %s" % args.containers) |
| 61 | + |
| 62 | + if not os.path.exists(args.containers): |
| 63 | + sys.exit(f"Path {args.containers} does not exist.") |
| 64 | + depot = os.path.abspath(args.containers) |
| 65 | + cli = get_client() |
| 66 | + |
| 67 | + # Keep a record of repos we've seen and don't repeat |
| 68 | + seen = set() |
| 69 | + |
| 70 | + # Ensure we start with the populated modules |
| 71 | + cli.registry.iter_modules() |
| 72 | + |
| 73 | + # Find all paths that match the pattern of a name:tag |
| 74 | + for path in os.listdir(depot): |
| 75 | + if not re.search("^(.*):(.*)$", path) or os.path.isdir(path): |
| 76 | + continue |
| 77 | + repo, tag = path.split(":", 1) |
| 78 | + |
| 79 | + # Don't need to parse twice |
| 80 | + if repo in seen: |
| 81 | + continue |
| 82 | + |
| 83 | + container = f"{args.namespace}/{repo}" |
| 84 | + tagged = f"{container}:{tag}" |
| 85 | + |
| 86 | + match = cli.registry.find(tagged) |
| 87 | + if not match: |
| 88 | + tagged = container |
| 89 | + match = cli.registry.find(tagged) |
| 90 | + |
| 91 | + if not match: |
| 92 | + logger.warning(f"Warning: no match for {path}") |
| 93 | + continue |
| 94 | + |
| 95 | + if args.dry_run: |
| 96 | + print(f"Would be installing {path} to {tagged}") |
| 97 | + seen.add(repo) |
| 98 | + continue |
| 99 | + |
| 100 | + print(f"Installing {path} to {tagged}") |
| 101 | + |
| 102 | + # We found a match! Install it (forcing keep path to not copy the container) |
| 103 | + cli.install(tagged, force=args.force, container_image=path, keep_path=True) |
| 104 | + seen.add(repo) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments