Skip to content

Commit 5fb433f

Browse files
authored
adding biocontainers example install script (#613)
* adding biocontainers example install script Signed-off-by: vsoch <[email protected]>
1 parent 35ed2d5 commit 5fb433f

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

example/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Examples
2+
3+
This set of examples includes the following:
4+
5+
## Biocontainers Update
6+
7+
The intention of this script is to instantiate a client, and then match
8+
containers you have in some root from [Galaxy Project Depot](https://depot.galaxyproject.org/singularity/)
9+
on your local filesytem to install to an shpc registry (as modules). The script
10+
tries to be efficient and instantiate one remote registry that has all the containers.
11+
12+
### Usage

example/biocontainer-match.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)