common.utils

Attributes

T

P

Functions

git_commit_hash(→ str)

Get the commit hash of the git repository in which caller_file is located.

get_git_commit_hash(→ str)

Get the commit hash of the git repository in which caller_file is located.

rank0(→ collections.abc.Callable[P, T | None])

Rank 0 decorator: only call function on MPI rank 0.

capture_environment(→ dict[str, Any])

Capture environment metadata for reproducibility and write metadata files.

Module Contents

common.utils.T[source]
common.utils.P[source]
common.utils.git_commit_hash(caller_file: os.PathLike[str] | str) → str[source]

Get the commit hash of the git repository in which caller_file is located.

Parameters:

caller_file – Pass __file__ from calling module to capture its git commit

Returns:

commit hash of HEAD

common.utils.get_git_commit_hash(caller_file: os.PathLike[str] | str) → str[source]

Get the commit hash of the git repository in which caller_file is located.

Parameters:

caller_file – Pass __file__ from calling module to capture its git commit

Returns:

commit hash of HEAD

common.utils.rank0(func: collections.abc.Callable[P, T]) → collections.abc.Callable[P, T | None][source]

Rank 0 decorator: only call function on MPI rank 0.

common.utils.capture_environment(caller_file: str, output_dir: pathlib.Path, package_name: str | None = None) → dict[str, Any][source]

Capture environment metadata for reproducibility and write metadata files.

Writes:
  • environment.yaml: Metadata summary

  • pip_packages.txt: pip freeze output

  • spack.yaml, spack.lock: if running in spack environment

  • conda_environment.yml: if running in conda environment

Parameters:
  • caller_file – Pass __file__ from calling module to capture its git commit.

  • output_dir – Directory to write metadata files.

  • package_name –

    Name of the packing calling this function. Must be given such that the package version can be determined. For non-editable builds, the package name is also required to find the git commit hash, if (and only if) it is stored in __init__.py by setuptools_scm.

    Example setup of a package called example: - content of __init__.py:

    try:
        from nsx._version import __git_commit__ as _git_commit
        from nsx._version import __version__
    
        __git_commit__ = _git_commit.lstrip("g") if isinstance(_git_commit, str)                          else "unknown"
    except ImportError:
        __version__ = "unknown"
        __git_commit__ = "unknown"
    
    • add to pyproject.toml:

      [build-system]
      requires = ["setuptools>=64", "setuptools_scm[toml]>=6.2"]
      build-backend = "setuptools.build_meta"
      
      [project]
      dynamic = ["version"]
      # ...
      
      [tool.setuptools_scm]
      write_to = "example/_version.py"   # replace `example` by package name
      write_to_template = '''# Generated by setuptools_scm
      __version__ = "{version}"
      __git_commit__ = "{scm_version.node}"
      '''
      

Returns:

Environment metadata dictionary (for logging/inspection) with keys

  • timestamp

  • platform

  • hostname

  • python:

    • version

    • PYTHONPATH

    • venv

  • package:

    • git_commit

    • version

    • name (if passed as argument)

  • threads

    • OMP_NUM_THREADS

    • OPENBLAS_NUM_THREADS

    • MKL_NUM_THREADS

    • BLIS_NUM_THREADS

    • VECLIB_MAXIMUM_THREADS

  • petsc: (if installed)

    • version

    • arch

  • dolfinx: (if installed)

    • version

    • commit

  • mpi_size: (if mpi4py installed)

  • spack: (if running in spack environment)

    • version

    • env_name

    • env_path

  • conda: (if running in conda environment)

    • env_name