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
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
# and construct an ArchSpec
def _string_or_none(s):
@ -237,21 +232,27 @@ def _string_or_none(s):
return str(s)
return None
if isinstance(spec_or_platform_tuple, six.string_types):
spec_fields = spec_or_platform_tuple.split("-")
msg = "invalid arch spec [{0}]"
assert len(spec_fields) == 3, msg.format(spec_or_platform_tuple)
# If another instance of ArchSpec was passed, duplicate it
if isinstance(spec_or_platform_tuple, ArchSpec):
other = 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_tuple = _string_or_none(platform),\
_string_or_none(operating_system), target
if isinstance(spec_or_platform_tuple, tuple):
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)
platform_tuple = (
_string_or_none(platform),
_string_or_none(operating_system),
target
)
self.platform, self.os, self.target = platform_tuple
@ -265,11 +266,6 @@ def _cmp_iter(self):
yield self.os
yield self.target
def _dup(self, other):
self.platform = other.platform
self.os = other.os
self.target = other.target
@property
def platform(self):
"""The platform of the architecture."""
@ -484,9 +480,7 @@ def constrain(self, other):
def copy(self):
"""Copy the current instance and returns the clone."""
clone = ArchSpec.__new__(ArchSpec)
clone._dup(self)
return clone
return ArchSpec(self)
@property
def concrete(self):
@ -509,36 +503,17 @@ def to_dict(self):
@staticmethod
def from_dict(d):
"""Import an ArchSpec from raw YAML/JSON data.
This routine implements a measure of compatibility with older
versions of Spack. Spack releases before 0.10 used a single
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))
"""Import an ArchSpec from raw YAML/JSON data"""
arch = d['arch']
target = spack.target.Target.from_dict_or_value(arch['target'])
return ArchSpec((arch['platform'], arch['platform_os'], target))
def __str__(self):
return "%s-%s-%s" % (self.platform, self.os, self.target)
def __repr__(self):
# TODO: this needs to be changed (repr is meant to return valid
# TODO: Python code to return an instance equivalent to the current
# TODO: one).
return str(self)
fmt = 'ArchSpec(({0.platform!r}, {0.os!r}, {1!r}))'
return fmt.format(self, str(self.target))
def __contains__(self, string):
return string in str(self) or string in self.target