rituals.util package

rituals.util – Helper modules.

rituals.util.add_dir2pypath(path)[source]

Add given directory to PYTHONPATH, e.g. for pylint.

rituals.util.search_file_upwards(name, base=None)[source]

Search for a file named name from cwd or given directory to root. Return None if nothing’s found.

Submodules

rituals.util.antglob module

Recursive globbing with ant-style syntax.

class rituals.util.antglob.FileSet(root, patterns)[source]

Bases: object

Ant-style file and directory matching.

Produces an iterator of all of the files that match the provided patterns. Note that directory matches must end with a slash, and if they’re exclusions, they won’t be scanned (which prunes anything in that directory that would otherwise match).

Directory specifiers:
** matches zero or more directories. / path separator.
File specifiers:
  • glob style wildcard.

[chars] inclusive character sets. [^chars] exclusive character sets.

Examples

/*.py recursively match all python files. foo//.py recursively match all python files in the ‘foo’ directory. *.py match all the python files in the current directory. */.txt match all the text files in top-level directories. foo/**/* all files under directory ‘foo’. / top-level directories. foo/ the directory ‘foo’ itself. */foo/ any directory named ‘foo’. **/.* hidden files. **/.*/ hidden directories.

included(path, is_dir=False)[source]

Check patterns in order, last match that includes or excludes path wins. Return None on undecided.

walk(**kwargs)[source]

Like os.walk and taking the same keyword arguments, but generating paths relative to the root.

Starts in the fileset’s root and filters based on its patterns. If with_root=True is passed in, the generated paths include the root path.

rituals.util.antglob.includes(pattern)[source]

A single inclusive glob pattern.

rituals.util.antglob.excludes(pattern)[source]

A single exclusive glob pattern.

rituals.util.filesys module

File system helpers.

rituals.util.filesys.pretty_path(path, _home_re=<_sre.SRE_Pattern object>)[source]

Prettify path for humans, and make it Unicode.

rituals.util.filesys.pushd(*args, **kwds)[source]

A context that enters a given directory and restores the old state on exit.

The original directory is returned as the context variable.

rituals.util.filesys.url_as_file(*args, **kwds)[source]

Context manager that GETs a given url and provides it as a local file.

The file is in a closed state upon entering the context, and removed when leaving it, if still there.

To give the file name a specific extension, use ext; the extension can optionally include a separating dot, otherwise it will be added.

Parameters:
  • url (str) – URL to retrieve.
  • ext (str, optional) – Extension for the generated filename.
Yields:

str – The path to a temporary file with the content of the URL.

Raises:

requests.RequestException – Base exception of requests, see its docs for more detailed ones.

Example

>>> import io, re, json
>>> with url_as_file('https://api.github.com/meta', ext='json') as meta:
...     meta, json.load(io.open(meta, encoding='ascii'))['hooks']
(u'/tmp/www-api.github.com-Ba5OhD.json', [u'192.30.252.0/22'])

rituals.util.notify module

Log notification messages to console.

rituals.util.notify.banner(msg)[source]

Emit a banner just like Invoke’s run(…, echo=True).

rituals.util.notify.error(msg)[source]

Emit an error message to stderr.

rituals.util.notify.failure(msg)[source]

Emit a fatal message and exit.

rituals.util.notify.info(msg)[source]

Emit a normal message.

rituals.util.notify.warning(msg)[source]

Emit a warning message.

rituals.util.shell module

Shell command calls.

rituals.util.shell.capture(cmd, **kw)[source]

Run a command and return its stripped captured output.

rituals.util.shell.run(cmd, **kw)[source]

Run a command and flush its output.

rituals.util.which module

Find the full path to commands.

which(command, path=None, verbose=0, exts=None)
Return the full path to the first match of the given command on the path.
whichall(command, path=None, verbose=0, exts=None)
Return a list of full paths to all matches of the given command on the path.
whichgen(command, path=None, verbose=0, exts=None)
Return a generator which will yield full paths to all matches of the given command on the path.

By default the PATH environment variable is searched (as well as, on Windows, the AppPaths key in the registry), but a specific ‘path’ list to search may be specified as well. On Windows, the PATHEXT environment variable is applied as appropriate.

If “verbose” is true then a tuple of the form
(<fullpath>, <matched-where-description>)

is returned for each match. The latter element is a textual description of where the match was found. For example:

from PATH element 0 from HKLMSOFTWARE…perl.exe
exception rituals.util.which.WhichError[source]

Bases: exceptions.Exception

Executable not found by which module.

rituals.util.which.which(command, path=None, verbose=0, exts=None)[source]

Return the full path to the first match of the given command on the path.

“command” is a the name of the executable to search for. “path” is an optional alternate path list to search. The default it

to use the PATH environment variable.
“verbose”, if true, will cause a 2-tuple to be returned. The second
element is a textual description of where the match was found.
“exts” optionally allows one to specify a list of extensions to use
instead of the standard list for this system. This can effectively be used as an optimization to, for example, avoid stat’s of “foo.vbs” when searching for “foo” and you know it is not a VisualBasic script but “.vbs” is on PATHEXT. This option is only supported on Windows.

If no match is found for the command, a WhichError is raised.

rituals.util.which.whichall(command, path=None, verbose=0, exts=None)[source]

Return a list of full paths to all matches of the given command on the path.

“command” is a the name of the executable to search for. “path” is an optional alternate path list to search. The default it

to use the PATH environment variable.
“verbose”, if true, will cause a 2-tuple to be returned for each
match. The second element is a textual description of where the match was found.
“exts” optionally allows one to specify a list of extensions to use
instead of the standard list for this system. This can effectively be used as an optimization to, for example, avoid stat’s of “foo.vbs” when searching for “foo” and you know it is not a VisualBasic script but “.vbs” is on PATHEXT. This option is only supported on Windows.
rituals.util.which.whichgen(command, path=None, verbose=0, exts=None)[source]

Return a generator of full paths to the given command.

“command” is a the name of the executable to search for. “path” is an optional alternate path list to search. The default it

to use the PATH environment variable.
“verbose”, if true, will cause a 2-tuple to be returned for each
match. The second element is a textual description of where the match was found.
“exts” optionally allows one to specify a list of extensions to use
instead of the standard list for this system. This can effectively be used as an optimization to, for example, avoid stat’s of “foo.vbs” when searching for “foo” and you know it is not a VisualBasic script but “.vbs” is on PATHEXT. This option is only supported on Windows.

This method returns a generator which yields either full paths to the given command or, if verbose, tuples of the form (<path to command>, <where path found>).