2.7. libmace.util

Swiss knife module.

$Repo: git://git.sugarlabs.org/alsroot/codelets.git$ $File: src/util.py$ $Data: 2011-11-29$

libmace.util.enforce(condition, error=None, *args)

Make an assertion in runtime.

In comparing with assert, it will all time present in the code. Just a bit of syntax sugar.

Parameters:
  • condition – the condition to assert; if not False then return, otherse raise an RuntimeError exception
  • error – error message to pass to RuntimeError object or Exception class to raise
  • args – optional ‘%’ arguments for the error
libmace.util.exception(*args)

Log about exception on low log level.

That might be useful for non-critial exception. Input arguments are the same as for logging.exception function.

Parameters:args – optional arguments to pass to logging function; the first argument might be a logging.Logger to use instead of using direct logging calls
libmace.util.assert_call(cmd, stdin=None, **kwargs)

Variant of call method with raising exception of errors.

Parameters:
  • cmd – commad to execute, might be string or argv list
  • stdin – text that will be used as an input for executed process
libmace.util.call(cmd, stdin=None, asserts=False, raw=False, error_cb=None, **kwargs)

Convenient wrapper around subprocess call.

Note, this function is intended for processes that output finite and not big amount of text.

Parameters:
  • cmd – commad to execute, might be string or argv list
  • stdin – text that will be used as an input for executed process
  • asserts – whether to raise RuntimeError on fail execution status
  • error_cb – call callback(stderr) on getting error exit status from the process
Returns:

None on errors, otherwise str value of stdout

libmace.util.rmtree(path, ignore_errors=True, **kwargs)

Remove directory with all its content.

Function will check if owner has permissions for removing directories (it makes sense for 0install implementaion caches).

Parameters:
  • path – path to the directory to remove
  • ignore_errors – ignore all errors while removing
Returns:

None on errors, otherwise str value of stdout

libmace.util.cptree(src, dst)

Efficient version of copying directories.

Function will try to make hard links for copying files at first and will fallback to regular copying overwise.

Parameters:
  • src – path to the source directory
  • dst – path to the new directory
libmace.util.new_file(path, mode=None)

Atomic new file creation.

Method will create temporaty file in the same directory as the specified one. When file object associated with this temporaty file will be closed, temporaty file will be renamed to the final destination.

Parameters:
  • path – path to save final file to
  • mode – mode for new file
Returns:

file object

libmace.util.get_frame(frame_no)

Return Python call stack frame.

The reason to have this wrapper is that this stack information is a private data and might depend on Python implementaion.

Parameters:frame_no – number of stack frame starting from caller’s stack position
Returns:frame object
libmace.util.utcnow()

Return local time in UTC.

Support testing workflow on multi processes level.

Returns:datetime.datetime.utcnow() value
class libmace.util.Option(description=None, default=None, short_option=None, type_cast=None, type_repr=None, action=None)

Configuration option.

Option object will be used as command-line argument and configuration file option. All these objects will be automatically collected from sugar_server.env module and from etc module from all services.

Parameters:
  • description – description string
  • default – default value for the option
  • short_option – value in for of -<char> to use as a short option for command-line parser
  • type_cast – function that will be uses to type cast to option type while setting option value
  • type_repr – function that will be uses to type cast from option type while converting option value to string
  • action – value for action argument of OptionParser.add_option()
unsorted_items

Collected by Option.seek() options in original order.

items

Collected by Option.seek() options by name.

sections

Collected by Option.seek() options by section.

long_option

Long command-line argument name.

value

Get option raw value.

static seek(section, mod=None)

Collect Option objects.

Function will populate Option.unsorted_items, Option.items and Option.sections values. Call this function before any usage of Option objects.

Parameters:
  • section – arbitrary name to group options per section
  • mod – mdoule object to search for Option objects; if omited, use caller’s module
static bind(parser, config_files=None, notice=None)

Initilize option usage.

Call this function after invoking Option.seek().

Parameters:
  • parser – if not None, OptionParser object to export, collected by Option.seek options, to
  • config_files – list of paths to files that will be used to read default option values; this value will initiate Option.config variable
  • notice – optional notice to print with arguments’ description
static merge(options, config_files=None)

Combine default values with command-line arguments and config files.

Call this function after invoking Option.bind().

Parameters:
  • options – the first value from a tuple returned by OptionParser.parse_args() function
  • config_files – list of paths to files that will be used to read default option values instead of reusing –config value
static export()

Current configuration in human readable form.

Returns:list of lines
class libmace.util.Command(description=None, cmd_format=None)

Service command.

Command is a way to have custom sub-commands in services. All these objects will be automatically collected from etc module from all services.

Parameters:
  • description – command description
  • cmd_format – part of description to explain additional command arguments
items

Collected by Command.seek() commands by name.

sections

Collected by Command.seek() commands by section.

static seek(section, mod=None)

Collect Command objects.

Function will populate Command.items and Command.sections values. Call this function before any usage of Command objects.

Parameters:
  • section – arbitrary name to group options per section
  • mod – mdoule object to search for Option objects; if omited, use caller’s module
static call(mod, name, *args, **kwargs)

Call the command.

Specfied module should contain a function with a name CMD_<command-name>(). All additional Command.call() arguments will be passed as-is to command implementaion function.

Parameters:
  • mod – module to search for command implementaion
  • name – command name
Returns:

what command implementaion returns

class libmace.util.TempFilePath

Auto removed temporary file.

Right after creating TempFilePath object, temporaty file will be created. On TempFilePath object deleting, this file will be removed. The key difference with tempfile.NamedTemporaryFile is that TempFilePath doesn’t keep open file descriptor with removing file right after closing it (though starting form Python 2.6, tempfile.NamedTemporaryFile supports delete argument).

class libmace.util.TempDir

Auto removed temporary directory.

Right after creating TempDir object, temporaty directory will be created. On TempDir object deleting, this directory will be removed.

persist()

Do not auto remove this directory.

Previous topic

2.6. libmace.printf

Next topic

3. How to contribute

This Page