sampledoc

Common Utilities

Some helper functions, classes, etc., are useful to both the client and server. Some of these are used to maintain Python Compatibility, and should go in Bcfg2.Compat. Those that aren’t strictly for Python compatibility go in Bcfg2.Utils, which is documented below.

Miscellaneous useful utility functions, classes, etc., that are used by both client and server. Stuff that doesn’t fit anywhere else.

Bcfg2.Utils.ClassName[source]

This very simple descriptor class exists only to get the name of the owner class. This is used because, for historical reasons, we expect every server plugin and every client tool to have a name attribute that is in almost all cases the same as the __class__.__name__ attribute of the plugin object. This makes that more dynamic so that each plugin and tool isn’t repeating its own name.

class Bcfg2.Utils.Executor(timeout=None)[source]

Bases: object

A convenient way to run external commands with subprocess.Popen

Parameters:timeout (float) – Set a default timeout for all commands run by this Executor object
run(command, inputdata=None, shell=False, timeout=None)[source]

Run a command, given as a list, optionally giving it the specified input data.

Parameters:
  • command (list or string) – The command to run, as a list (preferred) or as a string. See subprocess.Popen for details.
  • inputdata (string) – Data to pass to the command on stdin
  • shell (bool) – Run the given command in a shell (not recommended)
  • timeout (float) – Kill the command if it runs longer than this many seconds. Set to 0 or -1 to explicitly override a default timeout.
Returns:

Bcfg2.Utils.ExecutorResult

class Bcfg2.Utils.ExecutorResult(stdout, stderr, retval)[source]

Bases: object

Returned as the result of a call to Bcfg2.Utils.Executor.run(). The result can be accessed via the instance variables, documented below, as a boolean (which is equivalent to Bcfg2.Utils.ExecutorResult.success), or as a tuple, which, for backwards compatibility, is equivalent to (result.retval, result.stdout.splitlines()).

error = None

A friendly error message

retval = None

The return value of the command.

success = None

Whether or not the command was successful. If the ExecutorResult is used as a boolean, success is returned.

class Bcfg2.Utils.PackedDigitRange(*ranges)[source]

Bases: object

Representation of a set of integer ranges. A range is described by a comma-delimited string of integers and ranges, e.g.:

1,10-12,15-20

Ranges are inclusive on both bounds, and may include 0. Negative numbers are not supported.

May be instantiated in one of two ways:

PackedDigitRange(<comma-delimited list of ranges>)

Or:

PackedDigitRange(<int_or_range>[, <int_or_range>[, ...]])

E.g., both of the following are valid:

PackedDigitRange("1-5,7, 10-12")
PackedDigitRange("1-5", 7, "10-12")
includes(other)[source]

Return True if other is included in this range. Functionally equivalent to other in range, which should be used instead.

Bcfg2.Utils.locked(fd)[source]

Acquire a lock on a file.

Parameters:fd (int) – The file descriptor to lock
Returns:bool - True if the file is already locked, False otherwise

Previous topic

Bcfg2 unit testing

Next topic

Versioning Bcfg2

This Page