spack.util package


spack.util.compression module

spack.util.compression.decompressor_for(path, extension=None)

Get the appropriate decompressor for a path.


Get the archive extension for a path.


Get the part of a path that does not include its compressed type extension.

spack.util.crypto module

class spack.util.crypto.Checker(hexdigest, **kwargs)

Bases: object

A checker checks files against one particular hex digest. It will automatically determine what hashing algorithm to used based on the length of the digest it’s initialized with. e.g., if the digest is 32 hex characters long this will use md5.

Example: know your tarball should hash to ‘abc123’. You want to check files against this. You would use this class like so:

hexdigest = 'abc123'
checker = Checker(hexdigest)
success = checker.check('downloaded.tar.gz')

After the call to check, the actual checksum is available in checker.sum, in case it’s needed for error output.

You can trade read performance and memory usage by adjusting the block_size optional arg. By default it’s a 1MB (2**20 bytes) buffer.


Read the file with the specified name and check its checksum against self.hexdigest. Return True if they match, False otherwise. Actual checksum is stored in self.sum.


Get the name of the hash function this Checker is using.


Number of bits required to represent an integer in binary.

spack.util.crypto.checksum(hashlib_algo, filename, **kwargs)

Returns a hex digest of the filename generated using an algorithm from hashlib.

spack.util.crypto.hashes = {'sha1': <built-in function openssl_sha1>, 'sha224': <built-in function openssl_sha224>, 'sha384': <built-in function openssl_sha384>, 'sha256': <built-in function openssl_sha256>, 'sha512': <built-in function openssl_sha512>, 'md5': <built-in function openssl_md5>}

Index for looking up hasher for a digest.

spack.util.crypto.prefix_bits(byte_array, bits)

Return the first <bits> bits of a byte array as an integer.

spack.util.debug module

Debug signal handler: prints a stack trace and enters interpreter.

register_interrupt_handler() enables a ctrl-C handler that prints a stack trace and drops the user into an interpreter.

spack.util.debug.debug_handler(sig, frame)

Interrupt running process, and provide a python prompt for interactive debugging.


Print traceback and enter an interpreter on Ctrl-C

spack.util.environment module


Dump the current environment out to a file.


Predicate that given a path returns True if it is a system path, False otherwise.

Parameters:path (str) – path to a directory
Returns:True or False
spack.util.environment.path_put_first(var_name, directories)

Puts the provided directories first in the path, adding them if they’re not already there.

spack.util.environment.path_set(var_name, directories)

spack.util.executable module

class spack.util.executable.Executable(name)

Bases: object

Class representing a program that can be run on the command line.


Add a default argument to the command.

add_default_env(key, value)

Set an environment variable when the command is run.

  • key – The environment variable to set
  • value – The value to set it to

The command-line string.

Returns:The executable and default arguments
Return type:str

The executable name.

Returns:The basename of the executable
Return type:str

The path to the executable.

Returns:The path to the executable
Return type:str
spack.util.executable.which(*args, **kwargs)

Finds an executable in the path like command-line which.

If given multiple executables, returns the first one that is found. If no executables are found, returns None.


*args (str) – One or more executables to search for

Keyword Arguments:
  • path (list() or str) – The path to search. Defaults to PATH
  • required (bool) – If set to True, raise an error if executable not found

The first executable that is found in the path

Return type:


exception spack.util.executable.ProcessError(message, long_message=None)

Bases: spack.error.SpackError

ProcessErrors are raised when Executables exit with an error code.

spack.util.gpg module

class spack.util.gpg.Gpg

Bases: object

classmethod create(**kwargs)
classmethod export_keys(location, *keys)
static gpg()
classmethod list(trusted, signing)
classmethod sign(key, file, output, clearsign=False)
classmethod signing_keys()
classmethod trust(keyfile)
classmethod untrust(signing, *keys)
classmethod verify(signature, file)

spack.util.log_parse module


Get error context from a log file.

Parameters:log_events (list of LogEvent) – list of events created by ctest_log_parser.parse()
Returns:context from the build log with errors highlighted
Return type:str

Parses the log file for lines containing errors, and prints them out with line numbers and context. Errors are highlighted with ‘>>’ and with red highlighting (if color is enabled).

spack.util.log_parse.parse_log_events(stream, context=6)

Extract interesting events from a log file as a list of LogEvent.

  • stream (str or fileobject) – build log name or file object
  • context (int) – lines of context to extract around each log event

two lists containig BuildError and

BuildWarning objects.

Return type:


This is a wrapper around ctest_log_parser.CTestLogParser that lazily constructs a single CTestLogParser object. This ensures that all the regex compilation is only done once.

spack.util.module_cmd module

This module contains routines related to the module command for accessing and parsing environment modules.

exception spack.util.module_cmd.ModuleError

Bases: exceptions.Exception

Raised the the module_cmd utility to indicate errors.


Inspects a TCL module for entries that indicate the absolute path at which the library supported by said module can be found.


Takes a module name and removes modules until it is possible to load that module. It then loads the provided module. Depends on the modulecmd implementation of modules used in cray and lmod.

spack.util.multiproc module

This implements a parallel map operation but it can accept more values than multiprocessing.Pool.apply() can. For example, apply() will fail to pickle functions if they’re passed indirectly as parameters.

spack.util.multiproc.parmap(f, X)
class spack.util.multiproc.Barrier(n, timeout=None)

Simple reusable semaphore barrier.

Python 2.6 doesn’t have multiprocessing barriers so we implement this.

See, p. 41.


spack.util.naming module


Convert a name from module style to class name style. Spack mostly follows PEP-8:

  • Module and package names use lowercase_with_underscores.
  • Class names use the CapWords convention.

Regular source code follows these convetions. Spack is a bit more liberal with its Package names and Compiler names:

  • They can contain ‘-‘ as well as ‘_’, but cannot start with ‘-‘.
  • They can start with numbers, e.g. “3proxy”.

This function converts from the module convention to the class convention by removing _ and - and converting surrounding lowercase text to CapWords. If mod_name starts with a number, the class name returned will be prepended with ‘_’ to make a valid Python identifier.


Given a Spack module name, returns the name by which it can be imported in Python.


Return whether mod_name is valid for use in Spack.


Return whether mod_name is a valid namespaced module name.


Raise an exception if mod_name is not a valid namespaced module name.


Raise an exception if mod_name is not valid.


Given a Python module name, return a list of all possible spack module names that could correspond to it.


Simplify package name to only lowercase, digits, and dashes.

Simplifies a name which may include uppercase letters, periods, underscores, and pluses. In general, we want our package names to only contain lowercase letters, digits, and dashes.

Parameters:name (str) – The original name of the package
Returns:The new name of the package
Return type:str
class spack.util.naming.NamespaceTrie(separator='.')

Bases: object

class Element(value)

Bases: object


True if there is a value set for the given namespace.


True if this namespace has no children in the trie.


True if the namespace has a value, or if it’s the prefix of one that does.

spack.util.path module

Utilities for managing paths in Spack.


Substitute placeholders into paths.

Spack allows paths in configs to have some placeholders, as follows:

  • $spack The Spack instance’s prefix
  • $user The current user’s username
  • $tempdir Default temporary directory returned by tempfile.gettempdir()

These are substituted case-insensitively into the path, and users can use either $var or ${var} syntax for the variables.


Substitute config vars, expand environment vars, expand user home, take abspath.

spack.util.pattern module

class spack.util.pattern.Args(*flags, **kwargs)

Bases: spack.util.pattern.Bunch

Subclass of Bunch to write argparse args more naturally.

class spack.util.pattern.Bunch(**kwargs)

Bases: object

Carries a bunch of named attributes (from Alex Martelli bunch)

spack.util.pattern.composite(interface=None, method_list=None, container=<type 'list'>)

Decorator implementing the GoF composite pattern.

  • interface (type) – class exposing the interface to which the composite object must conform. Only non-private and non-special methods will be taken into account
  • method_list (list of str) – names of methods that should be part of the composite
  • container (MutableSequence) – container for the composite object (default = list). Must fulfill the MutableSequence contract. The composite class will expose the container API to manage object composition

a class decorator that patches a class adding all the methods it needs to be a composite for a given interface.

spack.util.prefix module

This file contains utilities for managing the installation prefix of a package.

class spack.util.prefix.Prefix

Bases: str

This class represents an installation prefix, but provides useful attributes for referring to directories inside the prefix.

Attributes of this object are created on the fly when you request them, so any of the following is valid:

>>> prefix = Prefix('/usr')
>>> prefix.bin
>>> prefix.lib64

Prefix objects behave identically to strings. In fact, they subclass str. So operators like + are legal:

print('foobar ' + prefix)

This prints foobar /usr. All of this is meant to make custom installs easy.

spack.util.spack_json module

Simple wrapper around JSON to guarantee consistent use of load/dump.


Spack JSON needs to be ordered to support specs.

spack.util.spack_json.dump(data, stream=None)

Dump JSON with a reasonable amount of indentation and separation.

exception spack.util.spack_json.SpackJSONError(msg, json_error)

Bases: spack.error.SpackError

Raised when there are issues with JSON parsing.

spack.util.spack_yaml module

Enhanced YAML parsing for Spack.

  • load() preserves YAML Marks on returned objects – this allows us to access file and line information later.
  • Our load methods use ``OrderedDict class instead of YAML’s default unorderd dict.
spack.util.spack_yaml.load(*args, **kwargs)

Load but modify the loader instance so that it will add __line__ atrributes to the returned object.

spack.util.spack_yaml.dump(*args, **kwargs)
exception spack.util.spack_yaml.SpackYAMLError(msg, yaml_error)

Bases: spack.error.SpackError

Raised when there are issues with YAML parsing.

spack.util.string module

spack.util.string.comma_list(sequence, article='')

spack.util.web module

class spack.util.web.LinkParser

Bases: HTMLParser.HTMLParser

This parser just takes an HTML page and strips out the hrefs on the links. Good enough for a really simple spider.

handle_starttag(tag, attrs)
exception spack.util.web.NoNetworkConnectionError(message, url)

Bases: spack.util.web.SpackWebError

Raised when an operation can’t get an internet connection.

class spack.util.web.NonDaemonPool(processes=None, initializer=None, initargs=(), maxtasksperchild=None)

Bases: multiprocessing.pool.Pool

Pool that uses non-daemon processes


alias of NonDaemonProcess

class spack.util.web.NonDaemonProcess(group=None, target=None, name=None, args=(), kwargs={})

Bases: multiprocessing.process.Process

Process tha allows sub-processes, so pools can have sub-pools.

exception spack.util.web.SpackWebError(message, long_message=None)

Bases: spack.error.SpackError

Superclass for Spack web spidering errors.

exception spack.util.web.VersionFetchError(message, long_message=None)

Bases: spack.util.web.SpackWebError

Raised when we can’t determine a URL to fetch a package.

spack.util.web.find_versions_of_archive(archive_urls, list_url=None, list_depth=0)

Scrape web pages for new versions of a tarball.


archive_urls – URL or sequence of URLs for different versions of a package. Typically these are just the tarballs from the package file itself. By default, this searches the parent directories of archives.

Keyword Arguments:
  • list_url – URL for a listing of archives. Spack wills scrape these pages for download links that look like the archive URL.
  • list_depth – Max depth to follow links on list_url pages. Default 0.
spack.util.web.get_checksums_for_versions(url_dict, name, first_stage_function=None, keep_stage=False)

Fetches and checksums archives from URLs.

This function is called by both spack checksum and spack create. The first_stage_function argument allows the caller to inspect the first downloaded archive, e.g., to determine the build system.

  • url_dict (dict) – A dictionary of the form: version -> URL
  • name (str) – The name of the package
  • first_stage_function (callable) – function that takes a Stage and a URL; this is run on the stage of the first URL downloaded
  • keep_stage (bool) – whether to keep staging area when command completes

A multi-line string containing versions and corresponding hashes

Return type:


spack.util.web.spider(root_url, depth=0)

Gets web pages from a root URL.

If depth is specified (e.g., depth=2), then this will also follow up to <depth> levels of links from the root.

This will spawn processes to fetch the children, for much improved performance over a sequential fetch.

Module contents