summaryrefslogtreecommitdiff
path: root/lib/spack/spack/spec.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/spack/spack/spec.py')
-rw-r--r--lib/spack/spack/spec.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py
index 46400859d3..88516470fb 100644
--- a/lib/spack/spack/spec.py
+++ b/lib/spack/spack/spec.py
@@ -2893,6 +2893,40 @@ class Spec(object):
if not_existing:
raise vt.UnknownVariantError(spec, not_existing)
+ def update_variant_validate(self, variant_name, values):
+ """If it is not already there, adds the variant named
+ `variant_name` to the spec `spec` based on the definition
+ contained in the package metadata. Validates the variant and
+ values before returning.
+
+ Used to add values to a variant without being sensitive to the
+ variant being single or multi-valued. If the variant already
+ exists on the spec it is assumed to be multi-valued and the
+ values are appended.
+
+ Args:
+ variant_name: the name of the variant to add or append to
+ values: the value or values (as a tuple) to add/append
+ to the variant
+ """
+ if not isinstance(values, tuple):
+ values = (values,)
+
+ pkg_variant = self.package_class.variants[variant_name]
+
+ for value in values:
+ if self.variants.get(variant_name):
+ msg = ("Cannot append a value to a single-valued "
+ "variant with an already set value")
+ assert pkg_variant.multi, msg
+ self.variants[variant_name].append(value)
+ else:
+ variant = pkg_variant.make_variant(value)
+ self.variants[variant_name] = variant
+
+ pkg_variant.validate_or_raise(
+ self.variants[variant_name], self.package)
+
def constrain(self, other, deps=True):
"""Merge the constraints of other with self.