:mod:`cloudmesh-common.cloudmesh.common.util` ============================================= .. py:module:: cloudmesh-common.cloudmesh.common.util Module Contents --------------- Functions ~~~~~~~~~ .. autoapisummary:: cloudmesh-common.cloudmesh.common.util.tempdir cloudmesh-common.cloudmesh.common.util.check_root cloudmesh-common.cloudmesh.common.util.exponential_backoff cloudmesh-common.cloudmesh.common.util.download cloudmesh-common.cloudmesh.common.util.search cloudmesh-common.cloudmesh.common.util.grep cloudmesh-common.cloudmesh.common.util.is_gitbash cloudmesh-common.cloudmesh.common.util.is_powershell cloudmesh-common.cloudmesh.common.util.is_cmd_exe cloudmesh-common.cloudmesh.common.util.path_expand cloudmesh-common.cloudmesh.common.util.convert_from_unicode cloudmesh-common.cloudmesh.common.util.yn_choice cloudmesh-common.cloudmesh.common.util.str_banner cloudmesh-common.cloudmesh.common.util.banner cloudmesh-common.cloudmesh.common.util.HEADING cloudmesh-common.cloudmesh.common.util.backup_name cloudmesh-common.cloudmesh.common.util.auto_create_version cloudmesh-common.cloudmesh.common.util.auto_create_requirements cloudmesh-common.cloudmesh.common.util.copy_files cloudmesh-common.cloudmesh.common.util.readfile cloudmesh-common.cloudmesh.common.util.writefile cloudmesh-common.cloudmesh.common.util.writefd cloudmesh-common.cloudmesh.common.util.sudo_readfile cloudmesh-common.cloudmesh.common.util.generate_password .. data:: collectionsAbc .. function:: 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 .. function:: check_root(terminate=True) Tests if the root user is used :param terminate: if true exist the program :return: .. function:: exponential_backoff(fn, sleeptime_s_max=30 * 60) Calls `fn` until it returns True, with an exponentially increasing wait time between calls .. function:: download(source, destination, force=False) Downloads the file from source to destination :param source: The http source :param destination: The destination in the file system :param force: If True the file will be downloaded even if it already exists .. function:: search(lines, pattern) return all lines that match the pattern #TODO: we need an example :param lines: :param pattern: :return: .. function:: 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. .. function:: is_gitbash() returns True if you run in a Windows gitbash :return: True if gitbash .. function:: is_powershell() True if you run in powershell :return: True if you run in powershell .. function:: is_cmd_exe() return True if you run in a Windows CMD :return: True if you run in CMD .. function:: path_expand(text) returns a string with expanded variable. :param text: the path to be expanded, which can include ~ and environment variables :param text: string .. function:: convert_from_unicode(data) converts unicode data to a string :param data: the data to convert :return: .. function:: yn_choice(message, default='y', tries=None) asks for a yes/no question. :param tries: the number of tries :param message: the message containing the question :param default: the default answer .. function:: 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 # -------------------------- :param color: prints in the given color :param label: adds a label :param debug: prints only if debug is true :param txt: a text message to be printed :type txt: string :param c: the character used instead of c :type c: character .. function:: 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 # -------------------------- :param color: prints in the given color :param label: adds a label :param debug: prints only if debug is true :param txt: a text message to be printed :type txt: string :param c: the character used instead of c :type c: character :param padding: ads additional comment line around the text so the banner is larger :type padding: bool .. function:: HEADING(txt=None, c='#', color='HEADER') Prints a message to stdout with #### surrounding it. This is useful for pytests to better distinguish them. :param c: uses the given char to wrap the header :param txt: a text message to be printed :type txt: string .. function:: backup_name(filename) :param filename: 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. :type filename: string :rtype: string .. function:: 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: .. function:: 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 :param requirements: the requirements in a list .. function:: copy_files(files_glob, source_dir, dest_dir) copies the files to the destination :param files_glob: `*.yaml` :param source_dir: source directory :param dest_dir: destination directory .. function:: readfile(filename, mode='r') returns the content of a file :param filename: the filename :return: .. function:: writefile(filename, content) writes the content into the file :param filename: the filename :param content: teh content :return: .. function:: 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 .. function:: 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 .. function:: 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: