Skip to content

Commit f0b3c60

Browse files
authored
Use pathlib to parse info/ paths in a stricter way (#36)
* use pathlib to parse tar paths * fix skips * add test
1 parent d8a2cec commit f0b3c60

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

conda_forge_metadata/artifact_info/info_json.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import tarfile
55
import warnings
6+
from pathlib import Path
67
from typing import Any, Generator, Tuple
78

89
from ruamel import yaml
@@ -114,27 +115,32 @@ def info_json_from_tar_generator(
114115
# e.g. linux-64/clangxx_osx-64-16.0.6-h027b494_6.conda
115116
YAML.allow_duplicate_keys = True
116117
for tar, member in tar_tuples:
117-
if member.name.endswith("index.json"):
118+
path = Path(member.name)
119+
if len(path.parts) > 1 and path.parts[0] == "info":
120+
path = Path(*path.parts[1:])
121+
if path.parts and path.parts[0] in ("test", "licenses"):
122+
continue
123+
if path.name == "index.json":
118124
index = json.loads(_extract_read(tar, member, default="{}"))
119125
data["name"] = index.get("name", "")
120126
data["version"] = index.get("version", "")
121127
data["index"] = index
122-
elif member.name.endswith("about.json"):
128+
elif path.name == "about.json":
123129
data["about"] = json.loads(_extract_read(tar, member, default="{}"))
124-
elif member.name.endswith("conda_build_config.yaml"):
130+
elif path.name == "conda_build_config.yaml":
125131
data["conda_build_config"] = YAML.load(
126132
_extract_read(tar, member, default="{}")
127133
)
128-
elif member.name.endswith("files"):
134+
elif path.name == "files":
129135
files = _extract_read(tar, member, default="").splitlines()
130136
if skip_files_suffixes:
131137
files = [
132138
f for f in files if not f.lower().endswith(skip_files_suffixes)
133139
]
134140
data["files"] = files
135-
elif member.name.endswith("meta.yaml.template"):
141+
elif path.name == "meta.yaml.template":
136142
data["raw_recipe"] = _extract_read(tar, member, default="")
137-
elif member.name.endswith("meta.yaml"):
143+
elif path.name == "meta.yaml":
138144
x = _extract_read(tar, member, default="{}")
139145
if ("{{" in x or "{%" in x) and not data["raw_recipe"]:
140146
data["raw_recipe"] = x

tests/test_info_json.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ def test_info_json_conda(backend: str):
6464
assert "site-packages/abipy/__init__.py" in info["files"]
6565

6666

67+
@pytest.mark.parametrize("backend", info_json.VALID_BACKENDS)
68+
def test_info_json_conda_unlucky_test_file(backend: str):
69+
"""
70+
See https://github.com/regro/conda-forge-metadata/pull/36
71+
72+
This artifact has test/xxxx/something_index.json,
73+
which tripped the original info/ parsing logic.
74+
"""
75+
info = info_json.get_artifact_info_as_json(
76+
"conda-forge",
77+
"noarch",
78+
"nodeenv-1.9.1-pyhd8ed1ab_0.conda",
79+
backend=backend,
80+
)
81+
assert info is not None
82+
assert info["metadata_version"] == 1
83+
assert info["name"] == "nodeenv"
84+
assert info["version"] == "1.9.1"
85+
assert info["index"]["name"] == "nodeenv"
86+
assert info["index"]["version"] == "1.9.1"
87+
assert info["index"]["build"] == "pyhd8ed1ab_0"
88+
assert info["index"]["subdir"] == "noarch"
89+
90+
6791
@pytest.mark.parametrize("backend", info_json.VALID_BACKENDS)
6892
def test_missing_conda_build_tar_bz2(backend: str):
6993
if backend == "streamed":

0 commit comments

Comments
 (0)