Remove the need for "node_regex"
This commit is contained in:
parent
3b4d7bf119
commit
3f0adae9ef
@ -13,7 +13,7 @@
|
|||||||
import re
|
import re
|
||||||
import types
|
import types
|
||||||
import warnings
|
import warnings
|
||||||
from typing import List
|
from typing import List, NamedTuple
|
||||||
|
|
||||||
import archspec.cpu
|
import archspec.cpu
|
||||||
|
|
||||||
@ -586,6 +586,11 @@ def bootstrap_clingo():
|
|||||||
from clingo import parse_files
|
from clingo import parse_files
|
||||||
|
|
||||||
|
|
||||||
|
class NodeArgument(NamedTuple):
|
||||||
|
id: str
|
||||||
|
pkg: str
|
||||||
|
|
||||||
|
|
||||||
def stringify(sym):
|
def stringify(sym):
|
||||||
"""Stringify symbols from clingo models.
|
"""Stringify symbols from clingo models.
|
||||||
|
|
||||||
@ -597,6 +602,14 @@ def stringify(sym):
|
|||||||
if isinstance(sym, (list, tuple)):
|
if isinstance(sym, (list, tuple)):
|
||||||
return tuple(stringify(a) for a in sym)
|
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:
|
if clingo_cffi:
|
||||||
# Clingo w/ CFFI will throw an exception on failure
|
# Clingo w/ CFFI will throw an exception on failure
|
||||||
try:
|
try:
|
||||||
@ -2532,17 +2545,15 @@ class SpecBuilder:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
node_regex = re.compile(r"node\(\d,\"(.*)\"\)")
|
|
||||||
|
|
||||||
@staticmethod
|
@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
|
"""Given a package name, returns the string representation of the root node in
|
||||||
the ASP encoding.
|
the ASP encoding.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
pkg: name of a package
|
pkg: name of a package
|
||||||
"""
|
"""
|
||||||
return f'node(0,"{pkg}")'
|
return NodeArgument(id="0", pkg=pkg)
|
||||||
|
|
||||||
def __init__(self, specs, hash_lookup=None):
|
def __init__(self, specs, hash_lookup=None):
|
||||||
self._specs = {}
|
self._specs = {}
|
||||||
@ -2557,17 +2568,13 @@ def __init__(self, specs, hash_lookup=None):
|
|||||||
self._hash_lookup = hash_lookup or {}
|
self._hash_lookup = hash_lookup or {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def extract_pkg(node: str) -> str:
|
def extract_pkg(node: NodeArgument) -> str:
|
||||||
"""Extracts the package name from a node fact, and returns it.
|
"""Extracts the package name from a node fact, and returns it.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
node: node from which the package name is to be extracted
|
node: node from which the package name is to be extracted
|
||||||
"""
|
"""
|
||||||
m = SpecBuilder.node_regex.match(node)
|
return node.pkg
|
||||||
if m is None:
|
|
||||||
raise spack.error.SpackError(f"cannot extract package information from '{node}'")
|
|
||||||
|
|
||||||
return m.group(1)
|
|
||||||
|
|
||||||
def hash(self, node, h):
|
def hash(self, node, h):
|
||||||
if node not in self._specs:
|
if node not in self._specs:
|
||||||
|
@ -2984,7 +2984,9 @@ def _new_concretize(self, tests=False):
|
|||||||
name = providers[0]
|
name = providers[0]
|
||||||
|
|
||||||
node = spack.solver.asp.SpecBuilder.main_node(pkg=name)
|
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]
|
concretized = answer[node]
|
||||||
self._dup(concretized)
|
self._dup(concretized)
|
||||||
|
Loading…
Reference in New Issue
Block a user