Remove the need for "node_regex"

This commit is contained in:
Massimiliano Culpo 2023-08-07 19:38:51 +02:00 committed by Todd Gamblin
parent 3b4d7bf119
commit 3f0adae9ef
2 changed files with 21 additions and 12 deletions

View file

@ -13,7 +13,7 @@
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 @@ def __init__(self, specs, hash_lookup=None):
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:

View file

@ -2984,7 +2984,9 @@ def _new_concretize(self, tests=False):
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)