File: run.py

package info (click to toggle)
python-spython 0.3.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: python: 3,299; sh: 61; makefile: 28
file content (124 lines) | stat: -rw-r--r-- 3,515 bytes parent folder | download
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
# 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 json

from spython.logger import bot
from spython.utils import stream_command


def run(
    self,
    image=None,
    args=None,
    app=None,
    sudo=False,
    writable=False,
    contain=False,
    bind=None,
    stream=False,
    nv=False,
    options=None,
    singularity_options=None,
    return_result=False,
    quiet=False,
    background=False,
):
    """
    run will run the container, with or withour arguments (which
    should be provided in a list)

    Parameters
    ==========
    image: full path to singularity image
    args: args to include with the run
    app: if not None, execute a command in context of an app
    writable: This option makes the file system accessible as read/write
    options: an optional list of options to provide to run.
    singularity_options: a list of options to provide to the singularity client
    contain: This option disables the automatic sharing of writable
             filesystems on your host
    bind: list or single string of bind paths.
          This option allows you to map directories on your host system to
          directories within your container using bind mounts
    stream: if True, return <generator> for the user to run
    nv: if True, load Nvidia Drivers in runtime (default False)
    return_result: if True, return entire json object with return code
         and message result (default is False)
    quiet: print the command to the user
    """
    from spython.utils import check_install

    check_install()

    cmd = self._init_command("run", singularity_options)

    # Does the user want to see the command printed?
    quiet = quiet or self.quiet

    # nv option leverages any GPU cards
    if nv:
        cmd += ["--nv"]

    # No image provided, default to use the client's loaded image
    if image is None:
        image = self._get_uri()

    # If an instance is provided, grab it's name
    if isinstance(image, self.instance):
        image = image.get_uri()

    # If image is still None, not defined by user or previously with client
    if image is None:
        bot.exit("Please load or provide an image.")

    # Does the user want to use bind paths option?
    if bind is not None:
        cmd += self._generate_bind_list(bind)

    # Does the user want to run an app?
    if app is not None:
        cmd = cmd + ["--app", app]

    # Does the user want writable?
    if writable:
        cmd.append("--writable")

    # Add options
    if options is not None:
        cmd = cmd + options

    cmd = cmd + [image]

    if args is not None:
        if not isinstance(args, list):
            args = args.split(" ")
        cmd = cmd + args

    if not quiet:
        bot.info(" ".join(cmd))

    if background:
        return self._run_command(cmd, sudo=sudo, background=True)

    elif not stream:
        result = self._run_command(cmd, sudo=sudo, return_result=return_result)
    else:
        return stream_command(cmd, sudo=sudo)

    # If the user wants the raw result object
    if return_result:
        return result

    # Otherwise, we parse the result if it was successful
    if result:
        result = result.strip("\n")

        try:
            result = json.loads(result)
        except Exception:
            pass
        return result