MFUTIL

mfutil (core)

Generic utility classes and functions.

class mfutil.BashWrapper(bash_cmd)[source]

Bash command/output wrapper.

code

Return the status code of the command as int (0 => ok).

stderr

Return the stderr output of the command stripped and utf8 decoded.

Returns:(string) stderr output (stripped and utf8 decoded).
stdout

Return the stdout output of the command stripped and utf8 decoded.

Returns:(string) stdout output (stripped and utf8 decoded).
exception mfutil.BashWrapperException(message, bash_wrapper=None)[source]

Specific exception class for BashWrapper objects.

class mfutil.BashWrapperOrRaise(bash_cmd, exception_class=<class 'mfutil.BashWrapperException'>, exception_msg='bad return code')[source]

BashWrapper subclass which raise an exception if status_code != 0.

exception mfutil.MFUtilException[source]

Just a custom exception object dedicated for mfutil package.

mfutil.add_inotify_watch(inotify, directory, ignores=[])[source]

Register recursively directories to watch.

Parameters:
  • inotify (inotify object) – object that owns the file descriptors
  • directory (string) – complete path of a directory to scan.
  • ignores (list of strings) – list of shell-style wildcards
  • to define which filenames/dirnames to ignores (see fnmatch)
mfutil.create_tmp_dirpath(tmp_dir=None, prefix='')[source]

Create and return a temporary directory inside a father tempory directory.

The dirname is made with get_unique_hexa_identifier() identifier so 32 hexa characters.

The father dirname can be provided by the user (or be a system one). He will be created if necessary. An exception can be raised if any problems at this side.

Note: the temporary directory is created.

Parameters:
  • tmp_dir (string) – user provided tmp directory (None to use the system temp dir).
  • prefix (string) – you can add here a prefix for dirnames (will be preprended before the 32 hexa characters).
Returns:

(string) complete path of a newly created temporary directory.

Raises:

MFUtilException if the temp directory can’t be created.

mfutil.get_ipv4_for_hostname(hostname, static_mappings={})[source]

Translate a host name to IPv4 address format.

The IPv4 address is returned as a string, such as ‘100.50.200.5’. If the host name is an IPv4 address itself it is returned unchanged.

You can provide a dictionnary with static mappings. Following mappings are added by default: ‘127.0.0.1’ => ‘127.0.0.1’ ‘localhost’ => ‘127.0.0.1’ ‘localhost.localdomain’ => ‘127.0.0.1’

Parameters:
  • hostname (string) – hostname.
  • static_mappings (dict) – dictionnary of static mappings ((hostname) string: (ip) string).
Returns:

(string) IPv4 address for the given hostname (None if any problem)

mfutil.get_recursive_mtime(directory, ignores=[])[source]

Get the latest mtime recursivly on a directory.

Parameters:
  • directory (string) – complete path of a directory to scan.
  • ignores (list of strings) – list of shell-style wildcards to define which filenames/dirnames to ignores (see fnmatch).
Returns:

(int) timestamp of the latest mtime on the directory.

mfutil.get_tmp_filepath(tmp_dir=None, prefix='')[source]

Return a tmp (complete) filepath.

The filename is made with get_unique_hexa_identifier() identifier so 32 hexa characters.

The dirname can be provided by the user (or be a system one). He will be created if necessary. An exception can be raised if any problems at this side.

Note: the file is not created or open at all. The function just returns a filename.

Parameters:
  • tmp_dir (string) – user provided tmp directory (None to use the system temp dir).
  • prefix (string) – you can add here a prefix for filenames (will be preprended before the 32 hexa characters).
Returns:

(string) tmp (complete) filepath.

Raises:

MFUtilException if the temp directory is not good or can’t – be created.

mfutil.get_unique_hexa_identifier()[source]

Return an unique hexa identifier on 32 bytes.

The idenfier is made only with 0123456789abcdef characters.

Returns:(string) unique hexa identifier.
mfutil.get_utc_unix_timestamp()[source]

Return the current unix UTC timestamp on all platforms.

It works even if the machine is configured in local time.

Returns:(int) a int corresponding to the current unix utc timestamp.
mfutil.hash_generator(*args)[source]

Generate a hash from a variable number of arguments as a safe string.

Note that pickle is used so arguments have to be serializable.

Parameters:*args – arguments to hash
mfutil.kill_process_and_children(pid)[source]

Kill recursively a complete tree of processes.

Given a pid, this method recursively kills the complete tree (children and children of each child…) of this process.

The SIGKILL signal is used.

Parameters:pid (int) – process PID to kill.
mfutil.mkdir_p(path, nodebug=False, nowarning=False)[source]

Make a directory recursively (clone of mkdir -p).

Thanks to http://stackoverflow.com/questions/600268/
mkdir-p-functionality-in-python .

Any exceptions are catched and a warning message is logged in case of problems.

If the directory already exists, True is returned with no debug or warning.

Parameters:
  • path (string) – complete path to create.
  • nodebug (boolean) – if True, no debug messages are logged.
  • nowarning (boolean) – if True, no message are logged in case of problems.
Returns:

True if the directory exists at the end.

Return type:

boolean

mfutil.mkdir_p_or_die(path, nodebug=False, exit_code=2)[source]

Make a directory recursively (clone of mkdir -p).

If the directory already exists, True is returned with no debug or warning.

Any exceptions are catched.

In case of problems, the program dies here with corresponding exit_code.

Parameters:
  • path (string) – complete path to create.
  • nodebug (boolean) – if True, no debug messages are logged.
  • exit_code (int) – os._exit() exit code.

mfutil.net

utility functions around network.

mfutil.net.get_domainname(use_resolv_conf=True, resolv_conf_file='/etc/resolv.conf')[source]

Get the domain name of the server.

The domain name does not include the hostname.

We try first with the getfqdn method then with the resolv.conf file because
the domain can be found here.
Parameters:
  • use_resolv_conf (boolean) – if set to True, we use the resolv.conf file.
  • resolv_conf_file (string) – full path of the resolv.conf file (useful for unit testing).
Returns:

the domain name of the server (or None if we can’t find it)

Return type:

string

mfutil.net.get_full_hostname(use_resolv_conf=True, resolv_conf_file='/etc/resolv.conf')[source]

Return the “full” hostname of the server.

“full” hostname means “with network domain” appended.

Parameters:
  • use_resolv_conf (boolean) – if set to True, we use the resolv.conf file to find the domain name.
  • resolv_conf_file (string) – full path of the resolv.conf file (useful for unit testing).
Returns:

the “full” hostname of the server.

Return type:

string

mfutil.net.get_ip_for_hostname(hostname, ignore_hostnames_list=['AUTO'])[source]

Get the IP of a given hostname.

Parameters:
  • hostname (string) – hostname to find.
  • ignore_hostnames_list (list of strings) – special hostname values which won’t be lookup ip (if the given hostname is in this list, this function will return the given hostname without any modification).
Returns:

(string) IP of the given hostname (or None if we can’t find it).

mfutil.net.get_real_ip()[source]

Try to find and return the real IP of the server.

We try to avoid to return 127.0.0.1 by examining network interfaces.

Returns:the real IP of the server.
Return type:string
mfutil.net.get_simple_hostname()[source]

Return the “simple” hostname of the server.

“simple” hostname means “without network domain”, so without any dot in the hostname.

Returns:the “simple” hostname of the server.
Return type:string
mfutil.net.ping_tcp_port(host, port, timeout=5)[source]

Ping a TCP host/port with a configurable timeout.

It’s not really a ping but a little connection attempt to see if the port is open and listened. The timeout is useful when there is a kind of firewall between.

No Exception are raised in any case.

Parameters:
  • host (string) – the hostname/ip to ping.
  • port (int) – the TCP port to ping.
  • timeout (int) – timeout in seconds.
Returns:

True if the port is open and listened.

Return type:

boolean

mfutil.plugins

utility classes and functions for managing metwork plugins.

exception mfutil.plugins.MFUtilPluginAlreadyInstalled[source]

Exception class raised when a plugin is already installed.

exception mfutil.plugins.MFUtilPluginBaseNotInitialized[source]

Exception class raised when the plugin base is not initialized.

exception mfutil.plugins.MFUtilPluginCantBuild(message, bash_wrapper=None)[source]

Exception class raised when we can’t build a plugin.

exception mfutil.plugins.MFUtilPluginCantInit(message, bash_wrapper=None)[source]

Exception class raised when we can’t init the plugin base.

exception mfutil.plugins.MFUtilPluginCantInstall(message, bash_wrapper=None)[source]

Exception class raised when we can’t install a plugin.

exception mfutil.plugins.MFUtilPluginCantUninstall(message, bash_wrapper=None)[source]

Exception class raised when we can’t uninstall a plugin.

exception mfutil.plugins.MFUtilPluginFileNotFound[source]

Exception class raised when we can’t find the plugin file.

exception mfutil.plugins.MFUtilPluginInvalid[source]

Exception class raised when the plugin is invalid.

exception mfutil.plugins.MFUtilPluginNotInstalled[source]

Exception class raised when a plugin is not installed.

mfutil.plugins.get_plugins_base_dir()[source]

Return the default plugins base directory path.

This value correspond to: ${RUNTIME_HOME}/var/plugins value.

Returns:the default plugins base directory path.
Return type:(string)
mfutil.plugins.init_plugins_base(plugins_base_dir=None)[source]

Initialize the plugins base.

Parameters:plugins_base_dir (string) – alternate plugins base directory (useful for unit tests).
Raises:MFUtilPluginCantInit – if we can’t init the plugin base.
mfutil.plugins.is_plugins_base_initialized(plugins_base_dir=None)[source]

Return True is the plugins base is already initialized for the module.

You can pass a plugins_base_dir as argument but you don’t have to do that, except for unit testing.

The plugins base dir is stored by default in : ${MODULE_RUNTIME_HOME}/var/plugins

Parameters:plugins_base_dir (string) – alternate plugins base directory.
Returns:True if the base is initialized.
Return type:boolean

mfutil.cli

utility functions to build CLI.

mfutil.cli.echo_bold(message)[source]

Write a message in bold (if supported).

Parameters:message (string) – message to write in bold.
mfutil.cli.echo_clean()[source]

Clean waiting status.

mfutil.cli.echo_nok(message='')[source]

Write [ERROR] with colors if supported a little optional message.

Parameters:message (string) – little optional message.
mfutil.cli.echo_ok(message='')[source]

Write [OK] with colors if supported a little optional message.

Parameters:message (string) – little optional message.
mfutil.cli.echo_running(message=None)[source]

Write [RUNNING] with colors if supported.

You can pass an optional message which will be rendered before [RUNNING] on the same line.

Parameters:message (string) – little optional message.
mfutil.cli.echo_warning(message='')[source]

Write [WARNING] with colors if supported a little optional message.

Parameters:message (string) – little optional message.