cloudmesh-common.cloudmesh.common.util

Module Contents

Functions

tempdir(*args, **kwargs)

A contextmanager to work in an auto-removed temporary directory

check_root(terminate=True)

Tests if the root user is used

exponential_backoff(fn, sleeptime_s_max=30 * 60)

Calls fn until it returns True, with an exponentially increasing wait time between calls

download(source, destination, force=False)

Downloads the file from source to destination

search(lines, pattern)

return all lines that match the pattern

grep(pattern, filename)

Very simple grep that returns the first matching line in a file.

is_gitbash()

returns True if you run in a Windows gitbash

is_powershell()

True if you run in powershell

is_cmd_exe()

return True if you run in a Windows CMD

path_expand(text)

returns a string with expanded variable.

convert_from_unicode(data)

converts unicode data to a string

yn_choice(message, default='y', tries=None)

asks for a yes/no question.

str_banner(txt=None, c='-', prefix='#', debug=True, label=None, color='BLUE', padding=False, figlet=False, font='big')

prints a banner of the form with a frame of # around the txt:

banner(txt=None, c='-', prefix='#', debug=True, label=None, color='BLUE', padding=False, figlet=False, font='big')

prints a banner of the form with a frame of # around the txt:

HEADING(txt=None, c='#', color='HEADER')

Prints a message to stdout with #### surrounding it. This is useful for

backup_name(filename)

param filename

given a filename creates a backup name of the form

auto_create_version(class_name, version, filename='__init__.py')

creates a version number in the __init__.py file.

auto_create_requirements(requirements)

creates a requirement.txt file form the requirements in the list. If the file

copy_files(files_glob, source_dir, dest_dir)

copies the files to the destination

readfile(filename, mode='r')

returns the content of a file

writefile(filename, content)

writes the content into the file

writefd(filename, content, mode='w', flags=os.O_RDWR | os.O_CREAT, mask=384)

writes the content into the file and control permissions

sudo_readfile(filename, split=True, trim=False)

Reads the content of the file as sudo and returns the result

generate_password(length=8, lower=True, upper=True, number=True)

generates a simple password. We should not really use this in production.

cloudmesh-common.cloudmesh.common.util.collectionsAbc
cloudmesh-common.cloudmesh.common.util.tempdir(*args, **kwargs)

A contextmanager to work in an auto-removed temporary directory

Arguments are passed through to tempfile.mkdtemp

example:

>>> with tempdir() as path:
...    pass
cloudmesh-common.cloudmesh.common.util.check_root(terminate=True)

Tests if the root user is used

Parameters

terminate – if true exist the program

Returns

cloudmesh-common.cloudmesh.common.util.exponential_backoff(fn, sleeptime_s_max=30 * 60)

Calls fn until it returns True, with an exponentially increasing wait time between calls

cloudmesh-common.cloudmesh.common.util.download(source, destination, force=False)

Downloads the file from source to destination

Parameters
  • source – The http source

  • destination – The destination in the file system

  • force – If True the file will be downloaded even if it already exists

cloudmesh-common.cloudmesh.common.util.search(lines, pattern)

return all lines that match the pattern #TODO: we need an example

Parameters
  • lines

  • pattern

Returns

cloudmesh-common.cloudmesh.common.util.grep(pattern, filename)

Very simple grep that returns the first matching line in a file. String matching only, does not do REs as currently implemented.

cloudmesh-common.cloudmesh.common.util.is_gitbash()

returns True if you run in a Windows gitbash

Returns

True if gitbash

cloudmesh-common.cloudmesh.common.util.is_powershell()

True if you run in powershell

Returns

True if you run in powershell

cloudmesh-common.cloudmesh.common.util.is_cmd_exe()

return True if you run in a Windows CMD

Returns

True if you run in CMD

cloudmesh-common.cloudmesh.common.util.path_expand(text)

returns a string with expanded variable.

Parameters
  • text – the path to be expanded, which can include ~ and environment variables

  • text – string

cloudmesh-common.cloudmesh.common.util.convert_from_unicode(data)

converts unicode data to a string :param data: the data to convert :return:

cloudmesh-common.cloudmesh.common.util.yn_choice(message, default='y', tries=None)

asks for a yes/no question.

Parameters
  • tries – the number of tries

  • message – the message containing the question

  • default – the default answer

cloudmesh-common.cloudmesh.common.util.str_banner(txt=None, c='-', prefix='#', debug=True, label=None, color='BLUE', padding=False, figlet=False, font='big')

prints a banner of the form with a frame of # around the txt:

# --------------------------
# txt
# --------------------------
Parameters
  • color – prints in the given color

  • label – adds a label

  • debug – prints only if debug is true

  • txt (string) – a text message to be printed

  • c (character) – the character used instead of c

cloudmesh-common.cloudmesh.common.util.banner(txt=None, c='-', prefix='#', debug=True, label=None, color='BLUE', padding=False, figlet=False, font='big')

prints a banner of the form with a frame of # around the txt:

# --------------------------
# txt
# --------------------------
Parameters
  • color – prints in the given color

  • label – adds a label

  • debug – prints only if debug is true

  • txt (string) – a text message to be printed

  • c (character) – the character used instead of c

  • padding (bool) – ads additional comment line around the text so the banner is larger

cloudmesh-common.cloudmesh.common.util.HEADING(txt=None, c='#', color='HEADER')

Prints a message to stdout with #### surrounding it. This is useful for pytests to better distinguish them.

Parameters
  • c – uses the given char to wrap the header

  • txt (string) – a text message to be printed

cloudmesh-common.cloudmesh.common.util.backup_name(filename)
Parameters

filename (string) – given a filename creates a backup name of the form filename.bak.1. If the filename already exists the number will be increased as much as needed so the file does not exist in the given location. The filename can consists a path and is expanded with ~ and environment variables.

Return type

string

cloudmesh-common.cloudmesh.common.util.auto_create_version(class_name, version, filename='__init__.py')

creates a version number in the __init__.py file. it can be accessed with __version__ :param class_name: :param version: :param filename: :return:

cloudmesh-common.cloudmesh.common.util.auto_create_requirements(requirements)

creates a requirement.txt file form the requirements in the list. If the file exists, it get changed only if the requirements in the list are different from the existing file

Parameters

requirements – the requirements in a list

cloudmesh-common.cloudmesh.common.util.copy_files(files_glob, source_dir, dest_dir)

copies the files to the destination

Parameters
  • files_glob*.yaml

  • source_dir – source directory

  • dest_dir – destination directory

cloudmesh-common.cloudmesh.common.util.readfile(filename, mode='r')

returns the content of a file :param filename: the filename :return:

cloudmesh-common.cloudmesh.common.util.writefile(filename, content)

writes the content into the file :param filename: the filename :param content: teh content :return:

cloudmesh-common.cloudmesh.common.util.writefd(filename, content, mode='w', flags=os.O_RDWR | os.O_CREAT, mask=384)

writes the content into the file and control permissions :param filename: the full or relative path to the filename :param content: the content being written :param mode: the write mode (‘w’) or write bytes mode (‘wb’) :param flags: the os flags that determine the permissions for the file :param mask: the mask that the permissions will be applied to

cloudmesh-common.cloudmesh.common.util.sudo_readfile(filename, split=True, trim=False)

Reads the content of the file as sudo and returns the result

param filename

the filename

type filename

str

param split

uf true returns a list of lines

type split

bool

param trim

trim trailing whitespace. This is useful to prevent empty string entries when splitting by ‘

type trim

bool

return

the content

rtype

str or list

cloudmesh-common.cloudmesh.common.util.generate_password(length=8, lower=True, upper=True, number=True)

generates a simple password. We should not really use this in production. :param length: the length of the password :param lower: True of lower case characters are allowed :param upper: True if upper case characters are allowed :param number: True if numbers are allowed :return: