From c504304d3953d12bf6d21dc88bd68ebb899f96b8 Mon Sep 17 00:00:00 2001 From: Jordan Galby <67924449+Jordan474@users.noreply.github.com> Date: Wed, 15 Jan 2025 13:07:17 +0100 Subject: [PATCH] Add version attributes up_to_1, up_to_2, up_to_3 (#38410) Making them also available in spec format string --- lib/spack/docs/module_file_support.rst | 11 +++++------ lib/spack/spack/test/spec_semantics.py | 6 ++++++ lib/spack/spack/version/version_types.py | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/spack/docs/module_file_support.rst b/lib/spack/docs/module_file_support.rst index 46a647b93ab..7ed4cc5c3a5 100644 --- a/lib/spack/docs/module_file_support.rst +++ b/lib/spack/docs/module_file_support.rst @@ -456,14 +456,13 @@ For instance, the following config options, tcl: all: suffixes: - ^python@3: 'python{^python.version}' + ^python@3: 'python{^python.version.up_to_2}' ^openblas: 'openblas' -will add a ``python-3.12.1`` version string to any packages compiled with -Python matching the spec, ``python@3``. This is useful to know which -version of Python a set of Python extensions is associated with. Likewise, the -``openblas`` string is attached to any program that has openblas in the spec, -most likely via the ``+blas`` variant specification. +will add a ``python3.12`` to module names of packages compiled with Python 3.12, and similarly for +all specs depending on ``python@3``. This is useful to know which version of Python a set of Python +extensions is associated with. Likewise, the ``openblas`` string is attached to any program that +has openblas in the spec, most likely via the ``+blas`` variant specification. The most heavyweight solution to module naming is to change the entire naming convention for module files. This uses the projections format diff --git a/lib/spack/spack/test/spec_semantics.py b/lib/spack/spack/test/spec_semantics.py index 406a63518f9..df3c1ddfacb 100644 --- a/lib/spack/spack/test/spec_semantics.py +++ b/lib/spack/spack/test/spec_semantics.py @@ -869,6 +869,12 @@ def test_spec_formatting(self, default_mock_concretization): ("{namespace=namespace}", "namespace=", "namespace", lambda spec: spec), ("{compiler.name}", "", "name", lambda spec: spec.compiler), ("{compiler.version}", "", "version", lambda spec: spec.compiler), + ( + "{compiler.version.up_to_1}", + "", + "up_to_1", + lambda spec: spec.compiler.version.up_to(1), + ), ("{%compiler.name}", "%", "name", lambda spec: spec.compiler), ("{@compiler.version}", "@", "version", lambda spec: spec.compiler), ("{architecture.platform}", "", "platform", lambda spec: spec.architecture), diff --git a/lib/spack/spack/version/version_types.py b/lib/spack/spack/version/version_types.py index 8468e32245d..db89594c005 100644 --- a/lib/spack/spack/version/version_types.py +++ b/lib/spack/spack/version/version_types.py @@ -490,6 +490,21 @@ def up_to(self, index: int) -> "StandardVersion": """ return self[:index] + @property + def up_to_1(self): + """The version truncated to the first component.""" + return self.up_to(1) + + @property + def up_to_2(self): + """The version truncated to the first two components.""" + return self.up_to(2) + + @property + def up_to_3(self): + """The version truncated to the first three components.""" + return self.up_to(3) + _STANDARD_VERSION_TYPEMIN = StandardVersion("", ((), (ALPHA,)), ("",))