Module mfutil.misc

Generic utility classes and functions.

Functions

def create_tmp_dirpath(tmp_dir=None, prefix='')

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.

Args

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.

def eval(expr, variables=None)

Evaluate (safely) a python expression (as a string).

The eval is done with simpleeval library.

Following functions are available (in expressions):

  • re_match: see match() function of re module
  • re_imatch: insensitive match() function of re module
  • fnmatch.fnmatch: fnmatch() function of fnmatch module

Args

expr : string
the python expression to eval.
variables : dict
if set, inject some variables/values in the expression.
def get_ipv4_for_hostname(hostname, static_mappings={})

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'

Args

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)

def get_recursive_mtime(directory, ignores=[])

Get the latest mtime recursivly on a directory.

Args

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.

def get_tmp_filepath(tmp_dir=None, prefix='')

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.

Args

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.

def get_unique_hexa_identifier()

Return an unique hexa identifier on 32 bytes.

The idenfier is made only with 0123456789abcdef characters.

Returns

(string) unique hexa identifier.

def get_utc_unix_timestamp()

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.

def hash_generator(*args)

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

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

Args

*args
arguments to hash
def kill_process_and_children(pid)

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.

Args

pid : int
process PID to kill.
def mkdir_p(path, nodebug=False, nowarning=False)

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.

Args

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

boolean
True if the directory exists at the end.
def mkdir_p_or_die(path, nodebug=False, exit_code=2)

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.

Args

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