Skip to content

Commit

Permalink
fix: allow incomplete specs if undefined jinja2
Browse files Browse the repository at this point in the history
  • Loading branch information
beckermr committed Nov 25, 2024
1 parent aa7a2b6 commit b11c991
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions conda_build/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,44 +1590,67 @@ def ms_depends(self, typ="run"):
("r-base", self.config.variant["r_base"]),
]
)

def _parse_spec(spec, raise_errors=False):
# parsing of specs may fail due to undefined jinja2
# so we do not raise in that case and try to handle it
# by parsing just the name
try:
return MatchSpec(spec)
except AssertionError:
if raise_errors:
raise RuntimeError(f"Invalid package specification: {spec!r}")
else:
return _parse_spec(spec.split()[0], raise_errors=True)
except (AttributeError, ValueError) as e:
if raise_errors:
raise RuntimeError(
"Received dictionary as spec. Note that pip requirements are "
"not supported in conda-build meta.yaml. Error message: "
+ str(e)
)
else:
return _parse_spec(spec.split()[0], raise_errors=True)

specs = OrderedDict()
for spec in ensure_list(self.get_value("requirements/" + typ, [])):
if not spec:
continue
try:
ms = MatchSpec(spec)
except AssertionError:
raise RuntimeError(f"Invalid package specification: {spec!r}")
except (AttributeError, ValueError) as e:
raise RuntimeError(
"Received dictionary as spec. Note that pip requirements are "
"not supported in conda-build meta.yaml. Error message: " + str(e)
)

ms = _parse_spec(spec, raise_errors=len(self.undefined_jinja_vars) == 0)

if ms.name == self.name() and not (
typ == "build" and self.config.host_subdir != self.config.build_subdir
):
raise RuntimeError(f"{self.name()} cannot depend on itself")

# IDK what this does since AFAIK the inner continue applies only
# to the inner loop
for name, ver in name_ver_list:
if ms.name == name:
if self.noarch:
continue

for c in "=!@#$%^&*:;\"'\\|<>?/":
if c in ms.name:
sys.exit(
f"Error: bad character '{c}' in package name "
f"dependency '{ms.name}'"
)
parts = spec.split()
if len(parts) >= 2:
if parts[1] in {">", ">=", "=", "==", "!=", "<", "<="}:
msg = (
f"Error: bad character '{parts[1]}' in package version "
f"dependency '{ms.name}'"
)
if len(parts) >= 3:
msg += f"\nPerhaps you meant '{ms.name} {parts[1]}{parts[2]}'"
sys.exit(msg)
# only validate here if we have all jinja2 variables defined
if len(self.undefined_jinja_vars) == 0:
for c in "=!@#$%^&*:;\"'\\|<>?/":
if c in ms.name:
sys.exit(
f"Error: bad character '{c}' in package name "
f"dependency '{ms.name}'"
)
parts = spec.split()
if len(parts) >= 2:
if parts[1] in {">", ">=", "=", "==", "!=", "<", "<="}:
msg = (
f"Error: bad character '{parts[1]}' in package version "
f"dependency '{ms.name}'"
)
if len(parts) >= 3:
msg += (
f"\nPerhaps you meant '{ms.name} {parts[1]}{parts[2]}'"
)
sys.exit(msg)
specs[spec] = ms
return list(specs.values())

Expand Down

0 comments on commit b11c991

Please sign in to comment.