File: sutils.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 (73 lines) | stat: -rw-r--r-- 1,907 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
# Singularity Image utils for interacting with the Image/Instance
#           classes from the client

# Copyright (C) 2017-2022 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 os
import re

from spython.logger import bot


def load(self, image=None):
    """load an image, either an actual path on the filesystem or a uri.

    Parameters
    ==========
    image: the image path or uri to load (e.g., docker://ubuntu

    """
    from spython.image import Image
    from spython.instance import Instance

    self.simage = Image(image)

    if image is not None:
        if image.startswith("instance://"):
            self.simage = Instance(image)
        bot.info(self.simage)


def setenv(self, variable, value):
    """set an environment variable for Singularity

    Parameters
    ==========
    variable: the variable to set
    value: the value to set
    """
    os.environ[variable] = value
    os.putenv(variable, value)
    bot.debug("%s set to %s" % (variable, value))


def get_filename(self, image, ext="sif", pwd=True):
    """return an image filename based on the image uri.

    Parameters
    ==========
    ext: the extension to use
    pwd: derive a filename for the pwd
    """
    if pwd:
        image = os.path.basename(image)
    image = re.sub("^.*://", "", image)
    if not image.endswith(ext):
        image = "%s.%s" % (image, ext)
    return image


def get_uri(self):
    """check if the loaded image object (self.simage) has an associated uri
    return if yes, None if not.
    """
    if hasattr(self, "simage"):
        if self.simage is not None:
            if self.simage.image not in ["", None]:
                # Concatenates the <uri>://<image>
                return str(self.simage)