Allow conditional possible values in variants (#29530)

Allow declaring possible values for variants with an associated condition. If the variant takes one of those values, the condition is imposed as a further constraint.

The idea of this PR is to implement part of the mechanisms needed for modeling [packages with multiple build-systems]( https://github.com/spack/seps/pull/3). After this PR the build-system directive can be implemented as:
```python
variant(
    'build-system',
    default='cmake',
    values=(
        'autotools',
        conditional('cmake', when='@X.Y:')
    ), 
    description='...',
)
```

Modifications:
- [x] Allow conditional possible values in variants
- [x] Add a unit-test for the feature
- [x] Add documentation
This commit is contained in:
Massimiliano Culpo
2022-04-05 02:37:57 +02:00
committed by GitHub
parent d64de54ebe
commit f2fc4ee9af
8 changed files with 214 additions and 13 deletions

View File

@@ -16,7 +16,7 @@
import six
from six import string_types
from llnl.util.compat import MutableMapping, zip_longest
from llnl.util.compat import MutableMapping, MutableSequence, zip_longest
# Ignore emacs backups when listing modules
ignore_modules = [r'^\.#', '~$']
@@ -976,3 +976,41 @@ def enum(**kwargs):
**kwargs: explicit dictionary of enums
"""
return type('Enum', (object,), kwargs)
class TypedMutableSequence(MutableSequence):
"""Base class that behaves like a list, just with a different type.
Client code can inherit from this base class:
class Foo(TypedMutableSequence):
pass
and later perform checks based on types:
if isinstance(l, Foo):
# do something
"""
def __init__(self, iterable):
self.data = list(iterable)
def __getitem__(self, item):
return self.data[item]
def __setitem__(self, key, value):
self.data[key] = value
def __delitem__(self, key):
del self.data[key]
def __len__(self):
return len(self.data)
def insert(self, index, item):
self.data.insert(index, item)
def __repr__(self):
return repr(self.data)
def __str__(self):
return str(self.data)