From 74d36076c262d1d2de59227680d0543dd08a58c5 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Fri, 1 Oct 2021 01:03:39 +0200 Subject: 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() --- lib/spack/spack/spec.py | 77 +++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 51 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index dba4924144..fc8edea6aa 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -225,11 +225,6 @@ class ArchSpec(object): 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 @@ class ArchSpec(object): 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 - platform, operating_system, target = spec_fields - platform_tuple = _string_or_none(platform),\ - _string_or_none(operating_system), target + # 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)) - 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, operating_system, target = spec_fields + platform_tuple = ( + _string_or_none(platform), + _string_or_none(operating_system), + target + ) self.platform, self.os, self.target = platform_tuple @@ -265,11 +266,6 @@ class ArchSpec(object): 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 @@ class ArchSpec(object): 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 @@ class ArchSpec(object): @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 -- cgit v1.2.3-60-g2f50