Utils

Provides functionality that is needed in multiple modules. E.g. retrieving a environment variable or fixing file permisions.

utils.expandpath(path)

Expands ~ and environment variables.

Parameters:path (str) -- The path that will be expanded
Returns:str -- The expanded path
utils.expanduser(path)

Behaves like the os.path.expanduser() but uses get_user_env_var() to look up the substitution.

Parameters:path (str) -- The path that will be expanded
Returns:str -- The expanded path
utils.expandvars(path)

Behaves like the os.path.expandvars() but uses get_user_env_var() to look up the substitution.

Parameters:path (str) -- The path that will be expanded
Returns:str -- The expanded path
utils.find_exact_target(target)

Finds the exact target in the repository to link to.

This will search TARGET_FILES for files that match target.

Parameters:target (str) -- The filename that will be searched for
Raises:ValueError -- Multiple targets where found
Returns:str -- Relative path of found file. Returns None if no target found.
utils.find_files(filename, paths)

Finds existing files matching a specific name in a list of paths.

Parameters:
  • filename (str) -- The name of the file that will be searched for in paths
  • paths (list) -- A list of paths that will be searched for filename
Returns:

list -- A list of file paths that were found

utils.find_target(target, tags)

Finds the correct target version in the repository to link to.

This will search TARGET_FILES for files that match the naming schema <any string>%<target> and returns the file whose <any string> occurs first in tags. If no file is found the return value of find_exact_target() is returned.

Parameters:
  • target (str) -- The filename that will be searched for
  • tags (list) -- A list of tags that will be matched against the search result
Raises:

ValueError -- Multiple targets where found

Returns:

str -- Relative path of found file. Returns None if no target found.

utils.get_current_username()

Gets the current users username.

Gets the username of the user that started uberdot. If the program was started with sudo, it still returns the original username and not "root".

Returns:str -- The username of the current user
utils.get_date_time_now()

Returns a datetime string for the current moment in the format YYYY-MM-DD hh:mm:ss

Returns:str -- The datetime string
utils.get_dir_owner(filename)

Gets the owner of the directory of a (non existing) file.

If the the directory does not exist, this function will goes the directory tree up until it finds an existing directory and returns its owner instead. This is used to figure out which permission shall have if it is about to be created.

Parameters:filename (str) -- (Non existing) absolute path to file
Returns:A tuple containing the UID of the directory owner and the GID of the directory owner
utils.get_gid()

Get the GID of the user that started uberdot.

This gets the current users GID. If SUDO_GID is set (this means the process was started with sudo) SUDO_GID will be returned instead.

Returns:(int) -- GID of the user that started uberdot
utils.get_groupname(gid)

Gets the groupname of a given gid.

Returns:str -- The groupname of the gid
utils.get_timestamp_now()

Returns a timestamp string for the current moment

Returns:str -- The timestamp
utils.get_uid()

Get the UID of the user that started uberdot.

This gets the current users UID. If SUDO_UID is set (this means the process was started with sudo) SUDO_UID will be returned instead.

Returns:(int) -- UID of the user that started uberdot
utils.get_user_env_var(varname, fallback=None)

Lookup an environment variable.

If executed as root, this function will login as the original user and look up the variable there. This means that this function will fail to look up environment variables that are for example set by the user in his .bashrc, because they aren't set at the time they will be looked up. Otherwise this function will do a standart look up of the users environment variables which means that all variables that were set at the time uberdot was started can be accessed.

Parameters:
  • varname (str) -- Name of the variable to look up
  • fallback (str) -- A fallback value if varname does not exist
Raises:

PreconditionError -- The variable does not exist and no fallback value was set

Returns:

str -- The value of the variable

utils.get_username(uid)

Gets the username of a given uid.

Returns:str -- The username of the uid
utils.has_root_priveleges()

Checks if this programm has root priveleges.

Returns:bool -- True, if the current process has root priveleges
utils.import_profile_class(class_name)

Imports a profile class only by it's name.

Searches PROFILE_FILES for python modules and imports them temporarily. If the module has a class that is the same as class_name it returns it.

Parameters:

class_name (str) -- The name of the class that will be imported

Raises:
Returns:

class -- The class that was imported

utils.is_dynamic_file(target)

Returns if a given path is a dynamic file.

Parameters:target (str) -- The path to the file
Returns:bool -- True, if given path is a dynamicfile
utils.log(message)

Alias for logger.info() but creates a newline.

Using the log functions, the output will also be printed into a logfile if the user set the --log flag.

Parameters:message -- The message that will be logged
utils.log_debug(message)

Alias for logger.debug() but creates a newline and colorizes output.

Using the log functions, the output will also be printed into a logfile if the user set the --log flag.

Parameters:message (str) -- The message that will be printed.
utils.log_error(message)

Alias for logger.error() but creates a newline.

Using the log functions, the output will also be printed into a logfile if the user set the --log flag.

Parameters:message (str) -- The message that will be printed.
utils.log_operation(profile_name, message)

Logs/Prints out a message for a profile.

Using the log functions, the output will also be printed into a logfile if the user set the --log flag.

Parameters:
  • profile_name (str) -- The name of the profile that triggered the operation.
  • message (str) -- The message that will be logged
utils.log_success(message)

Alias for logger.info() but creates a newline and colorizes output.

Using the log functions, the output will also be printed into a logfile if the user set the --log flag.

Parameters:message (str) -- The message that will be printed.
utils.log_warning(message)

Alias for logger.warning() but creates a newline and colorizes output.

Using the log functions, the output will also be printed into a logfile if the user set the --log flag.

Parameters:message (str) -- The message that will be printed.
utils.md5(string)

Calculate the md5 hash for a given string or bytearray.

Parameters:string -- A string or bytearray that the md5 hash will be calculated for. Strings will be encoded before hashing.
Returns:The hexadecimal representation of the md5 hash
utils.normpath(path)

Normalizes path, expands ~ and environment vars, and converts it in an absolute path.

Parameters:path (str) -- The path that will be normalized
Returns:str -- The normalized path
utils.walk_dotfiles()

Walks through the TARGET_FILES and returns all files found.

This also takes the .dotignore-file into account.

Returns:(list) -- Contains tuples with the directory and the filename of every found file