summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@gmail.com>2023-08-07 19:38:51 +0200
committerTodd Gamblin <tgamblin@llnl.gov>2023-08-15 15:54:37 -0700
commit3f0adae9efc20437b5284b523861862b61314ce7 (patch)
tree6400b70bd26bb76426b41c9b83582c5cd88f8191 /lib
parent3b4d7bf1190b310d980dce53c73c0fe009fc1134 (diff)
downloadspack-3f0adae9efc20437b5284b523861862b61314ce7.tar.gz
spack-3f0adae9efc20437b5284b523861862b61314ce7.tar.bz2
spack-3f0adae9efc20437b5284b523861862b61314ce7.tar.xz
spack-3f0adae9efc20437b5284b523861862b61314ce7.zip
Remove the need for "node_regex"
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/solver/asp.py29
-rw-r--r--lib/spack/spack/spec.py4
2 files changed, 21 insertions, 12 deletions
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)