Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compilers to R recipes #424

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions grayskull/strategy/cran.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import requests
from bs4 import BeautifulSoup
from souschef.jinja_expression import set_global_jinja_var

from grayskull.cli.stdout import print_msg
from grayskull.config import Configuration
Expand Down Expand Up @@ -51,6 +52,9 @@ def fetch_data(recipe, config, sections=None):
metadata_section = metadata.get(sec)
if metadata_section:
recipe[sec] = metadata_section
if metadata.get("need_compiler", False):
set_global_jinja_var(recipe, "posix", 'm2-" if win else "')
set_global_jinja_var(recipe, "native", 'm2w64-" if win else "')
return recipe


Expand Down Expand Up @@ -259,15 +263,7 @@ def get_cran_metadata(config: Configuration, cran_url: str):
_, pkg_version, pkg_url = get_cran_index(cran_url, pkg_name, pkg_version)
print_msg(pkg_name)
print_msg(pkg_version)
tarball_name = pkg_url.rsplit("/", 1)[-1]
print_msg(pkg_url)
response = requests.get(pkg_url)
response.raise_for_status()
download_file = os.path.join(
str(mkdtemp(f"grayskull-cran-metadata-{config.name}-")), tarball_name
)
with open(download_file, "wb") as f:
f.write(response.content)
download_file = download_cran_pkg(config, pkg_url)
metadata = get_archive_metadata(download_file)
r_recipe_end_comment = "\n".join(
[f"# {line}" for line in metadata["orig_lines"] if line]
Expand Down Expand Up @@ -295,7 +291,7 @@ def get_cran_metadata(config: Configuration, cran_url: str):
imports.append("r-base")
imports.sort() # this is not a requirement in conda but good for readability

return {
dict_metadata = {
"package": {
"name": "r-{{ name }}",
"version": "{{ version }}",
Expand All @@ -306,11 +302,13 @@ def get_cran_metadata(config: Configuration, cran_url: str):
},
"build": {
"number": 0,
"merge_build_host": True,
"script": "R CMD INSTALL --build .",
"entry_points": metadata.get("entry_points"),
"rpaths": ["lib/R/lib/", "lib/"],
},
"requirements": {
"build": [],
"host": deepcopy(imports),
"run": deepcopy(imports),
},
Expand All @@ -329,4 +327,33 @@ def get_cran_metadata(config: Configuration, cran_url: str):
"license": match_license(metadata.get("License", "")).get("licenseId")
or metadata.get("License", ""),
},
}, r_recipe_end_comment
}
if metadata.get("NeedsCompilation", "no").lower() == "yes":
dict_metadata["need_compiler"] = True
dict_metadata["requirements"]["build"].extend(
[
"cross-r-base {{ r_base }} # [build_platform != target_platform]",
"autoconf # [unix]",
"{{ compiler('c') }} # [unix]",
"{{ compiler('m2w64_c') }} # [win]",
"{{ compiler('cxx') }} # [unix]",
"{{ compiler('m2w64_cxx') }} # [win]",
"posix # [win]",
]
)
if not dict_metadata["requirements"]["build"]:
del dict_metadata["requirements"]["build"]
return dict_metadata, r_recipe_end_comment


def download_cran_pkg(config, pkg_url):
tarball_name = pkg_url.rsplit("/", 1)[-1]
print_msg(pkg_url)
response = requests.get(pkg_url)
response.raise_for_status()
download_file = os.path.join(
str(mkdtemp(f"grayskull-cran-metadata-{config.name}-")), tarball_name
)
with open(download_file, "wb") as f:
f.write(response.content)
return download_file
5 changes: 1 addition & 4 deletions tests/cli/test_cli_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ def mock_is_pkg_available(pkg):
# The CRAN test is not passing. I don't understand why!
@pytest.mark.parametrize(
"index, name, version",
[
# ("pypi", "pytest", "5.3.2"),
("cran", "future", "1.26.1")
],
[("pypi", "pytest", "5.3.2"), ("cran", "future", "1.26.1")],
)
def test_part_reload_recipe(tmpdir, index, name, version):
recipe = GrayskullFactory.create_recipe(index, Configuration(name, version))
Expand Down
38 changes: 38 additions & 0 deletions tests/test_cran.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import pytest

from grayskull.config import Configuration
from grayskull.strategy.cran import (
get_cran_metadata,
scrap_cran_archive_page_for_package_folder_url,
scrap_cran_pkg_folder_page_for_full_url,
scrap_main_page_cran_find_latest_package,
Expand Down Expand Up @@ -65,3 +67,39 @@ def test_scrap_cran_pkg_folder_page_for_full_url(mock_get_webpage, webpage_magic
scrap_cran_pkg_folder_page_for_full_url("CRAN_URL", "PKG_NAME", "1.0.0")
== "CRAN_URL/PKG_NAME_URL"
)


@patch("grayskull.strategy.cran.get_archive_metadata")
@patch("grayskull.strategy.cran.sha256_checksum")
@patch("grayskull.strategy.cran.download_cran_pkg")
@patch("grayskull.strategy.cran.get_cran_index")
def test_get_cran_metadata_need_compilation(
mock_get_cran_index,
mock_download_cran_pkg,
mock_sha256,
mock_get_archive_metadata,
tmp_path,
):
mock_get_cran_index.return_value = ("rpkg", "1.0.0", "http://foobar")
mock_sha256.return_value = 123456
mock_download_cran_pkg.return_value = str(tmp_path / "rpkg-1.0.0.tar.gz")
mock_get_archive_metadata.return_value = {
"orig_lines": ["foo", "bar"],
"License": "MIT",
"NeedsCompilation": "yes",
"URL": "PKG-URL",
}
cfg = Configuration(name="rpkg", version="1.0.0")
result_metadata, r_recipe_comment = get_cran_metadata(
cfg, "https://cran.r-project.org"
)
assert r_recipe_comment == "# foo\n# bar"
assert result_metadata["requirements"]["build"] == [
"cross-r-base {{ r_base }} # [build_platform != target_platform]",
"autoconf # [unix]",
"{{ compiler('c') }} # [unix]",
"{{ compiler('m2w64_c') }} # [win]",
"{{ compiler('cxx') }} # [unix]",
"{{ compiler('m2w64_cxx') }} # [win]",
"posix # [win]",
]