From 3f0adae9efc20437b5284b523861862b61314ce7 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Mon, 7 Aug 2023 19:38:51 +0200 Subject: Remove the need for "node_regex" --- lib/spack/spack/solver/asp.py | 29 ++++++++++++++++++----------- lib/spack/spack/spec.py | 4 +++- 2 files changed, 21 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/solver/asp.py b/lib/spack/spack/solver/asp.py index 21dd185df4..41b07eb4a8 100644 --- a/lib/spack/spack/solver/asp.py +++ b/lib/spack/spack/solver/asp.py @@ -13,7 +13,7 @@ import pprint import re import types import warnings -from typing import List +from typing import List, NamedTuple import archspec.cpu @@ -586,6 +586,11 @@ def bootstrap_clingo(): from clingo import parse_files +class NodeArgument(NamedTuple): + id: str + pkg: str + + def stringify(sym): """Stringify symbols from clingo models. @@ -597,6 +602,14 @@ def stringify(sym): if isinstance(sym, (list, tuple)): return tuple(stringify(a) for a in sym) + try: + if sym.name == "node": + return NodeArgument(id=stringify(sym.arguments[0]), pkg=stringify(sym.arguments[1])) + except RuntimeError: + # This happens when using clingo w/ CFFI and trying to access ".name" for symbols + # that are not functions + pass + if clingo_cffi: # Clingo w/ CFFI will throw an exception on failure try: @@ -2532,17 +2545,15 @@ class SpecBuilder: ) ) - node_regex = re.compile(r"node\(\d,\"(.*)\"\)") - @staticmethod - def main_node(*, pkg: str) -> str: + def main_node(*, pkg: str) -> NodeArgument: """Given a package name, returns the string representation of the root node in the ASP encoding. Args: pkg: name of a package """ - return f'node(0,"{pkg}")' + return NodeArgument(id="0", pkg=pkg) def __init__(self, specs, hash_lookup=None): self._specs = {} @@ -2557,17 +2568,13 @@ class SpecBuilder: self._hash_lookup = hash_lookup or {} @staticmethod - def extract_pkg(node: str) -> str: + def extract_pkg(node: NodeArgument) -> str: """Extracts the package name from a node fact, and returns it. Args: node: node from which the package name is to be extracted """ - m = SpecBuilder.node_regex.match(node) - if m is None: - raise spack.error.SpackError(f"cannot extract package information from '{node}'") - - return m.group(1) + return node.pkg def hash(self, node, h): if node not in self._specs: diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index 169fd88a5b..41c1c8a115 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -2984,7 +2984,9 @@ class Spec: name = providers[0] node = spack.solver.asp.SpecBuilder.main_node(pkg=name) - assert node in answer, f"cannot find {name} in the list of specs {','.join(answer.keys())}" + assert ( + node in answer + ), f"cannot find {name} in the list of specs {','.join([n.pkg for n in answer.keys()])}" concretized = answer[node] self._dup(concretized) -- cgit v1.2.3-60-g2f50