Skip to content

Commit 48c7c84

Browse files
Fb add support pep440 (#281)
* Add support for PEP440
1 parent 4902a52 commit 48c7c84

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

grayskull/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def get_py_version_available(
7474
else:
7575
py_ver_enabled = {py_ver: True for py_ver in sup_python_ver}
7676
for op, major, minor in req_python:
77+
if op == "=":
78+
op = "=="
7779
if not minor:
7880
minor = 0
7981
for sup_py, is_enabled in py_ver_enabled.items():

grayskull/strategy/py_base.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ def get_setup_cfg(source_path: str) -> dict:
293293
setup_cfg = read_configuration(str(path_setup_cfg))
294294
setup_cfg = dict(setup_cfg)
295295
if setup_cfg.get("options", {}).get("python_requires"):
296-
setup_cfg["options"]["python_requires"] = str(
297-
setup_cfg["options"]["python_requires"]
296+
setup_cfg["options"]["python_requires"] = ensure_pep440(
297+
str(setup_cfg["options"]["python_requires"])
298298
)
299299
result = {}
300300
result.update(setup_cfg.get("options", {}))
@@ -677,3 +677,30 @@ def get_sdist_metadata(
677677
metadata["source"] = {"url": sdist_url, "sha256": sha256_checksum(path_pkg)}
678678

679679
return metadata
680+
681+
682+
def ensure_pep440_in_req_list(list_req: List[str]) -> List[str]:
683+
return [ensure_pep440(pkg) for pkg in list_req]
684+
685+
686+
def ensure_pep440(pkg: str) -> str:
687+
if not pkg:
688+
return pkg
689+
if pkg.strip().startswith("<{") or pkg.strip().startswith("{{"):
690+
return pkg
691+
split_pkg = pkg.strip().split(" ")
692+
if len(split_pkg) <= 1:
693+
return pkg
694+
constrain_pkg = "".join(split_pkg[1:])
695+
list_constrains = constrain_pkg.split(",")
696+
full_constrain = []
697+
for constrain in list_constrains:
698+
if "~=" in constrain:
699+
version = constrain.strip().replace("~=", "").strip()
700+
version_reduced = ".".join(version.split(".")[:-1])
701+
version_reduced += ".*"
702+
full_constrain.append(f">={version},=={version_reduced}")
703+
else:
704+
full_constrain.append(constrain.strip())
705+
all_constrains = ",".join(full_constrain)
706+
return f"{split_pkg[0]} {all_constrains}"

grayskull/strategy/pypi.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
RE_DEPS_NAME,
2222
clean_deps_for_conda_forge,
2323
discover_license,
24+
ensure_pep440,
25+
ensure_pep440_in_req_list,
2426
get_compilers,
2527
get_entry_points_from_sdist,
2628
get_extra_from_requires_dist,
@@ -85,8 +87,10 @@ def get_val(key):
8587
"entry_points": get_entry_points_from_sdist(sdist_metadata),
8688
"scripts": get_val("scripts"),
8789
"summary": get_val("summary"),
88-
"requires_python": pypi_metadata.get("requires_python")
89-
or sdist_metadata.get("python_requires"),
90+
"requires_python": ensure_pep440(
91+
pypi_metadata.get("requires_python")
92+
or sdist_metadata.get("python_requires")
93+
),
9094
"doc_url": get_val("doc_url"),
9195
"dev_url": get_val("dev_url"),
9296
"license": get_val("license"),
@@ -341,14 +345,17 @@ def get_metadata(recipe, config) -> dict:
341345
print_msg(f"License file: {Fore.LIGHTMAGENTA_EX}{license_file}")
342346

343347
all_requirements = extract_requirements(metadata, config, recipe)
348+
344349
if all_requirements.get("host"):
345350
all_requirements["host"] = solve_list_pkg_name(
346351
all_requirements["host"], PYPI_CONFIG
347352
)
353+
all_requirements["host"] = ensure_pep440_in_req_list(all_requirements["host"])
348354
if all_requirements.get("run"):
349355
all_requirements["run"] = solve_list_pkg_name(
350356
all_requirements["run"], PYPI_CONFIG
351357
)
358+
all_requirements["run"] = ensure_pep440_in_req_list(all_requirements["run"])
352359
if config.is_strict_cf:
353360
all_requirements["host"] = clean_deps_for_conda_forge(
354361
all_requirements["host"], config.py_cf_supported[0]

tests/test_pypi.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from grayskull.config import Configuration
1616
from grayskull.strategy.py_base import (
1717
clean_deps_for_conda_forge,
18+
ensure_pep440,
1819
generic_py_ver_to,
1920
get_compilers,
2021
get_entry_points_from_sdist,
@@ -839,3 +840,17 @@ def test_get_test_imports_clean_modules():
839840
)
840841
== ["_pytest", "_pytest._code"]
841842
)
843+
844+
845+
def test_ensure_pep440():
846+
assert ensure_pep440("pytest ~=5.3.2") == "pytest >=5.3.2,==5.3.*"
847+
848+
849+
def test_pep440_recipe():
850+
recipe = create_python_recipe("codalab=0.5.26", is_strict_cf=False)[0]
851+
assert recipe["requirements"]["host"] == ["pip", "python >=3.6,<3.7"]
852+
853+
854+
def test_pep440_in_recipe_pypi():
855+
recipe = create_python_recipe("kedro=0.17.6", is_strict_cf=False)[0]
856+
assert sorted(recipe["requirements"]["run"])[0] == "anyconfig >=0.10.0,==0.10.*"

0 commit comments

Comments
 (0)