ArchSpec: minor cleanup of a few methods (#26376)

* Remove vestigial code to be compatible with Spack v0.9.X
* ArchSpec: reworked __repr__ to be more adherent to common Python idioms
* ArchSpec: simplified __init__.py and copy()
This commit is contained in:
Massimiliano Culpo 2021-10-01 01:03:39 +02:00 committed by GitHub
parent a75c86f178
commit 74d36076c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -225,11 +225,6 @@ def __init__(self, spec_or_platform_tuple=(None, None, None)):
Otherwise information on platform, OS and target should be Otherwise information on platform, OS and target should be
passed in either as a spec string or as a tuple. passed in either as a spec string or as a tuple.
""" """
# If another instance of ArchSpec was passed, duplicate it
if isinstance(spec_or_platform_tuple, ArchSpec):
self._dup(spec_or_platform_tuple)
return
# If the argument to __init__ is a spec string, parse it # If the argument to __init__ is a spec string, parse it
# and construct an ArchSpec # and construct an ArchSpec
def _string_or_none(s): def _string_or_none(s):
@ -237,21 +232,27 @@ def _string_or_none(s):
return str(s) return str(s)
return None return None
if isinstance(spec_or_platform_tuple, six.string_types): # If another instance of ArchSpec was passed, duplicate it
spec_fields = spec_or_platform_tuple.split("-") if isinstance(spec_or_platform_tuple, ArchSpec):
msg = "invalid arch spec [{0}]" other = spec_or_platform_tuple
assert len(spec_fields) == 3, msg.format(spec_or_platform_tuple) platform_tuple = other.platform, other.os, other.target
elif isinstance(spec_or_platform_tuple, (six.string_types, tuple)):
spec_fields = spec_or_platform_tuple
# Normalize the string to a tuple
if isinstance(spec_or_platform_tuple, six.string_types):
spec_fields = spec_or_platform_tuple.split("-")
if len(spec_fields) != 3:
msg = 'cannot construct an ArchSpec from {0!s}'
raise ValueError(msg.format(spec_or_platform_tuple))
platform, operating_system, target = spec_fields platform, operating_system, target = spec_fields
platform_tuple = _string_or_none(platform),\ platform_tuple = (
_string_or_none(operating_system), target _string_or_none(platform),
_string_or_none(operating_system),
if isinstance(spec_or_platform_tuple, tuple): target
platform, operating_system, target = spec_or_platform_tuple )
platform_tuple = _string_or_none(platform), \
_string_or_none(operating_system), target
msg = "invalid arch spec tuple [{0}]"
assert len(platform_tuple) == 3, msg.format(platform_tuple)
self.platform, self.os, self.target = platform_tuple self.platform, self.os, self.target = platform_tuple
@ -265,11 +266,6 @@ def _cmp_iter(self):
yield self.os yield self.os
yield self.target yield self.target
def _dup(self, other):
self.platform = other.platform
self.os = other.os
self.target = other.target
@property @property
def platform(self): def platform(self):
"""The platform of the architecture.""" """The platform of the architecture."""
@ -484,9 +480,7 @@ def constrain(self, other):
def copy(self): def copy(self):
"""Copy the current instance and returns the clone.""" """Copy the current instance and returns the clone."""
clone = ArchSpec.__new__(ArchSpec) return ArchSpec(self)
clone._dup(self)
return clone
@property @property
def concrete(self): def concrete(self):
@ -509,36 +503,17 @@ def to_dict(self):
@staticmethod @staticmethod
def from_dict(d): def from_dict(d):
"""Import an ArchSpec from raw YAML/JSON data. """Import an ArchSpec from raw YAML/JSON data"""
arch = d['arch']
This routine implements a measure of compatibility with older target = spack.target.Target.from_dict_or_value(arch['target'])
versions of Spack. Spack releases before 0.10 used a single return ArchSpec((arch['platform'], arch['platform_os'], target))
string with no OS or platform identifiers. We import old Spack
architectures with platform ``spack09``, OS ``unknown``, and the
old arch string as the target.
Specs from `0.10` or later have a more fleshed out architecture
descriptor with a platform, an OS, and a target.
"""
if not isinstance(d['arch'], dict):
return ArchSpec(('spack09', 'unknown', d['arch']))
d = d['arch']
operating_system = d.get('platform_os', None) or d['os']
target = spack.target.Target.from_dict_or_value(d['target'])
return ArchSpec((d['platform'], operating_system, target))
def __str__(self): def __str__(self):
return "%s-%s-%s" % (self.platform, self.os, self.target) return "%s-%s-%s" % (self.platform, self.os, self.target)
def __repr__(self): def __repr__(self):
# TODO: this needs to be changed (repr is meant to return valid fmt = 'ArchSpec(({0.platform!r}, {0.os!r}, {1!r}))'
# TODO: Python code to return an instance equivalent to the current return fmt.format(self, str(self.target))
# TODO: one).
return str(self)
def __contains__(self, string): def __contains__(self, string):
return string in str(self) or string in self.target return string in str(self) or string in self.target