snakeoil.bash module

Functionality for reading bash like files

Please note that while this functionality can do variable interpolation, it strictly treats the source as non-executable code. It cannot parse subshells, variable additions, etc.

Its primary usage is for reading things like gentoo make.conf’s, or libtool .la files that are bash compatible, but non-executable.

exception snakeoil.bash.BashParseError(filename, line, errmsg=None)[source]

Bases: Exception

Exception thrown when a handle being parsed isn’t valid bash.

class snakeoil.bash.bash_parser(source, sourcing_command=None, env=None, infile=None)[source]

Bases: shlex

Fixed up shlex version for bash parsing.

Corrects corner cases in quote expansion and adds variable interpolation. While it’s a fair bit slower than stdlib shlex, it parses a more complete subset of bash syntax than stdlib shlex.

read_token()[source]
sourcehook(newfile)[source]

Hook called on a filename to be sourced.

var_expand(val)[source]
snakeoil.bash.iter_read_bash(bash_source, allow_inline_comments=True, allow_line_cont=False, enum_line=False)[source]

Iterate over a file honoring bash commenting rules and line continuations.

Note that it’s considered good behaviour to close filehandles, as such, either iterate fully through this, or use read_bash instead. Once the file object is no longer referenced the handle will be closed, but be proactive instead of relying on the garbage collector.

Parameters:
  • bash_source – either a file to read from or a string holding the filename to open.

  • allow_inline_comments – whether or not to prune characters after a # that isn’t at the start of a line.

  • allow_line_cont – whether or not to respect line continuations

Returns:

yields lines w/ commenting stripped out

snakeoil.bash.read_bash(*args, **kwargs)[source]

Read a file honoring bash commenting rules.

See iter_read_bash() for parameter details.

Returns a list of lines w/ comments stripped out.

snakeoil.bash.read_bash_dict(bash_source, vars_dict=None, sourcing_command=None)[source]

Read bash source, yielding a dict of vars.

Parameters:
  • bash_source – either a file to read from or a string holding the filename to open

  • vars_dict (dict or None) – initial ‘env’ for the sourcing. Is protected from modification.

  • sourcing_command (callable) – controls whether a source command exists. If one does and is encountered, then this func is called.

Raises:

BashParseError – thrown if invalid syntax is encountered.

Returns:

dict representing the resultant env if bash executed the source.

snakeoil.bash.read_dict(bash_source, splitter='=', source_isiter=False, allow_inline_comments=True, strip=False, filename=None, ignore_errors=False)[source]

Read key value pairs from a file, ignoring bash-style comments.

Parameters:
  • splitter – the string to split on. Can be None to default to str.split’s default

  • bash_source – either a file to read from, or a string holding the filename to open.

  • allow_inline_comments – whether or not to prune characters after a # that isn’t at the start of a line.

  • ignore_errors – parse errors are logged instead of raised

Raise:

BashParseError if there are parse errors found.