wizard.util

Miscellaneous utility functions and classes.

Classes

class wizard.util.ChangeDirectory(dir)

Context for temporarily changing the working directory.

>>> with ChangeDirectory("/tmp"):
...    print os.getcwd()
/tmp
class wizard.util.PipeToLess

Context for printing output to a pager. Use this if output is expected to be long.

class wizard.util.IgnoreKeyboardInterrupts

Context for temporarily ignoring keyboard interrupts. Use this if aborting would cause more harm than finishing the job.

class wizard.util.Counter

Object for counting different values when you don’t know what they are a priori. Supports index access and iteration.

>>> counter = Counter()
>>> counter.count("foo")
>>> print counter["foo"]
1
count(value)

Increments count for value.

keys()

Returns the keys of counters.

max()

Returns the max counter value seen.

sum()

Returns the sum of all counter values.

class wizard.util.LockDirectory(lockfile, expiry=3600)

Context for locking a directory.

Functions

wizard.util.dictmap(f, d)

A map function for dictionaries. Only changes values.

>>> dictmap(lambda x: x + 2, {'a': 1, 'b': 2})
{'a': 3, 'b': 4}
wizard.util.dictkmap(f, d)

A map function for dictionaries that passes key and value.

>>> dictkmap(lambda x, y: x + y, {1: 4, 3: 4})
{1: 5, 3: 7}
wizard.util.chdir(dir)

Changes a directory, but has special exceptions for certain classes of errors.

wizard.util.get_exception_name(output)

Reads the traceback from a Python program and grabs the fully qualified exception name.

wizard.util.get_dir_uid(dir)

Finds the uid of the person who owns this directory.

wizard.util.get_revision()

Returns the commit ID of the current Wizard install.

wizard.util.get_operator_git()

Returns Real Name <username@mit.edu> suitable for use in Git Something-by: string. Throws NoOperatorInfo if no operator information is available.

wizard.util.set_operator_env()

Sets GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL environment variables if applicable. Does nothing if no information is available

wizard.util.set_author_env()

Sets GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL environment variables if applicable. Does nothing if wizard.user.passwd() fails.

wizard.util.set_git_env()

Sets all appropriate environment variables for Git commits.

Returns strings for placing in Git log info about Wizard.

Moves a file/dir to a backup location.

Unlink a file, but don’t complain if it doesn’t exist.

wizard.util.fetch(host, path, subpath, post=None)
wizard.util.mixed_newlines(filename)

Returns True if filename has mixed newlines.

wizard.util.random_key(length=30)

Generates a random alphanumeric key of length size.

wizard.util.makedirs(path)

Create a directory path (a la mkdir -p or os.makedirs), but don’t complain if it already exists.

wizard.util.disk_usage(dir=None, excluded_dir='.git')

Recursively determines the disk usage of a directory, excluding .git directories. Value is in bytes. If dir is omitted, the current working directory is assumed.

wizard.util.boolish(val)

Parse the contents of an environment variable as a boolean. This recognizes more values as False than bool() would.

>>> boolish("0")
False
>>> boolish("no")
False
>>> boolish("1")
True
wizard.util.init_wizard_dir()

Generates a .wizard directory and initializes it with some common files. This operation is idempotent.

wizard.util.truncate(version)

Truncates the Scripts specific version number.

Exceptions

exception wizard.util.NoOperatorInfo

No information could be found about the operator from Kerberos.

exception wizard.util.DirectoryLockedError(dir)
exception wizard.util.NoSuchDirectoryError

Bases: exceptions.IOError

exception wizard.util.PermissionsError

Bases: exceptions.IOError

exception wizard.util.DNSError(host)

Bases: socket.gaierror

Table Of Contents

Previous topic

wizard.user

This Page