spython.utils package

Submodules

spython.utils.fileio module

# 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/.

spython.utils.fileio.mkdir_p(path)[source]

mkdir_p attempts to get the same functionality as mkdir -p :param path: the path to create.

spython.utils.fileio.read_file(filename, mode='r', readlines=True)[source]

write_file will open a file, “filename” and write content, “content” and properly close the file

spython.utils.fileio.read_json(filename, mode='r')[source]

read_json reads in a json file and returns the data structure as dict.

spython.utils.fileio.write_file(filename, content, mode='w')[source]

write_file will open a file, “filename” and write content, “content” and properly close the file

spython.utils.fileio.write_json(json_obj, filename, mode='w', print_pretty=True)[source]

write_json will (optionally,pretty print) a json object to file :param json_obj: the dict to print to json :param filename: the output file to write to :param pretty_print: if True, will use nicer formatting

spython.utils.terminal module

# 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/.

spython.utils.terminal.check_install(software='singularity', quiet=True)[source]

check_install will attempt to run the singularity command, and return True if installed. The command line utils will not run without this check.

spython.utils.terminal.format_container_name(name, special_characters=None)[source]

format_container_name will take a name supplied by the user, remove all special characters (except for those defined by “special-characters” and return the new image name.

spython.utils.terminal.get_installdir()[source]

get_installdir returns the installation directory of the application

spython.utils.terminal.get_singularity_version()[source]

get the full singularity client version as reported by singularity –version […]. For Singularity 3.x, this means: “singularity version 3.0.1-1”

spython.utils.terminal.get_singularity_version_info()[source]

get the full singularity client version as a semantic version”

spython.utils.terminal.get_userhome()[source]

get the user home based on the effective uid

spython.utils.terminal.get_username()[source]

get the user name based on the effective uid

spython.utils.terminal.remove_uri(container)[source]

remove_uri will remove docker:// or shub:// or library:// from the uri

spython.utils.terminal.run_command(cmd, sudo=False, capture=True, no_newline_regexp='Progess', quiet=False, sudo_options=None, environ=None)[source]

run_command uses subprocess to send a command to the terminal. If capture is True, we use the parent stdout, so the progress bar (and other commands of interest) are piped to the user. This means we don’t return the output to parse.

Parameters:
  • cmd (the command to send, should be a list for subprocess)
  • sudo (if needed, add to start of command)
  • no_newline_regexp (the regular expression to determine skipping a) – newline. Defaults to finding Progress
  • capture (if True, don’t set stdout and have it go to console. This) – option can print a progress bar, but won’t return the lines as output.
  • sudo_options (string or list of strings that will be passed as options to sudo)
spython.utils.terminal.split_uri(container)[source]

Split the uri of a container into the protocol and image part

An empty protocol is returned if none found. A trailing slash is removed from the image part.

spython.utils.terminal.stream_command(cmd, no_newline_regexp='Progess', sudo=False, sudo_options=None, output_type='stdout')[source]

stream a command (yield) back to the user, as each line is available.

# Example usage: results = [] for line in stream_command(cmd):

print(line, end=”“) results.append(line)
Parameters:
  • cmd (the command to send, should be a list for subprocess)
  • no_newline_regexp (the regular expression to determine skipping a) – newline. Defaults to finding Progress
  • sudo_options (string or list of strings that will be passed as options to sudo)
spython.utils.terminal.which(software='singularity')[source]

which returns the full path to where software is installed.

Module contents