File: logs.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 (68 lines) | stat: -rw-r--r-- 1,928 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
# 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 os
import platform

from spython.logger import bot
from spython.utils import get_userhome, get_username


def error_logs(self, print_logs=False):
    """For Singularity 3.5 and later, we are able to programatically
    derive the name of the log. In this case, return the content
    to the user. See
    https://github.com/sylabs/singularity/issues/1115#issuecomment-560457918
    for when this was added.

    Parameters
    ==========
    print_logs: boolean to indicate to print to the screen along with
                return (defaults to False to just return log string)
    """
    return self._logs(print_logs, "err")


def output_logs(self, print_logs=False):
    """Get output logs for the user, if they exist.

    Parameters
    ==========
    print_logs: boolean to indicate to print to the screen along with
                return (defaults to False to just return log string)
    """
    return self._logs(print_logs, "out")


def _logs(self, print_logs=False, ext="out"):
    """A shared function to print log files. The only differing element is
    the extension (err or out)
    """
    from spython.utils import check_install

    check_install()

    # Formulate the path of the logs
    hostname = platform.node()
    logpath = os.path.join(
        get_userhome(),
        ".singularity",
        "instances",
        "logs",
        hostname,
        get_username(),
        "%s.%s" % (self.name, ext),
    )

    if os.path.exists(logpath):
        with open(logpath, "r") as filey:
            logs = filey.read()
        if print_logs is True:
            print(logs)
    else:
        bot.warning("No log files have been produced.")
    return logs