1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#!/usr/bin/env python
# Copyright (C) 2017-2024 Vanessa Sochat.
# This Source Code Form is subject to the terms of the
# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
import argparse
import os
import sys
def get_parser():
parser = argparse.ArgumentParser(
description="Singularity Client",
formatter_class=argparse.RawTextHelpFormatter,
add_help=False,
)
# Global Options
parser.add_argument(
"--debug",
"-d",
dest="debug",
help="use verbose logging to debug.",
default=False,
action="store_true",
)
parser.add_argument(
"--quiet",
"-q",
dest="quiet",
help="suppress all normal output",
default=False,
action="store_true",
)
parser.add_argument(
"--version",
dest="version",
help="show singularity and spython version",
default=False,
action="store_true",
)
subparsers = parser.add_subparsers(
help="description",
title="actions",
description="actions for Singularity",
dest="command",
metavar="general usage",
)
# Recipes
recipe = subparsers.add_parser("recipe", help="Recipe conversion and parsing")
recipe.add_argument(
"--entrypoint",
dest="entrypoint",
help="define custom entry point and prevent discovery",
default=None,
type=str,
)
recipe.add_argument(
"--json",
dest="json",
help="dump the (base) recipe content as json to the terminal",
default=False,
action="store_true",
)
recipe.add_argument(
"--force",
dest="force",
help="if the output file exists, overwrite.",
default=False,
action="store_true",
)
recipe.add_argument(
"files",
nargs="*",
help="the recipe input file and [optional] output file",
type=str,
)
recipe.add_argument(
"--parser",
type=str,
default="auto",
dest="parser",
choices=["auto", "docker", "singularity"],
help="Is the input a Dockerfile or Singularity recipe?",
)
recipe.add_argument(
"--writer",
type=str,
default="auto",
dest="writer",
choices=["auto", "docker", "singularity"],
help="Should we write to Dockerfile or Singularity recipe?",
)
# General Commands
subparsers.add_parser("shell", help="Interact with singularity python")
subparsers.add_parser("test", help="""Container testing (TBD)""")
return parser
def set_verbosity(args):
"""determine the message level in the environment to set based on args."""
level = "INFO"
if args.debug:
level = "DEBUG"
elif args.quiet:
level = "QUIET"
os.environ["MESSAGELEVEL"] = level
os.putenv("MESSAGELEVEL", level)
os.environ["SINGULARITY_MESSAGELEVEL"] = level
os.putenv("SINGULARITY_MESSAGELEVEL", level)
# Import logger to set
from spython.logger import bot
bot.debug("Logging level %s" % level)
import spython
bot.debug("Singularity Python Version: %s" % spython.__version__)
def version():
"""version prints the version, both for the user and help output"""
import spython
return spython.__version__
def main():
parser = get_parser()
def print_help(return_code=0):
"""print help, including the software version and active client
and exit with return code.
"""
v = version()
print("\nSingularity Python [v%s]\n" % (v))
parser.print_help()
sys.exit(return_code)
if len(sys.argv) == 1:
print_help()
try:
# We capture all primary arguments, and take secondary to pass on
args, options = parser.parse_known_args()
except Exception:
sys.exit(0)
# The main function
func = None
# If the user wants the version
if args.version:
print(version())
sys.exit(0)
# if environment logging variable not set, make silent
set_verbosity(args)
# Does the user want help for a subcommand?
if args.command == "recipe":
from .recipe import main as func
elif args.command == "shell":
from .shell import main as func
elif args.command == "test":
from .test import main as func
else:
print_help()
# Pass on to the correct parser
if args.command is not None:
func(args=args, options=options, parser=parser)
if __name__ == "__main__":
main()
|