Skip to content

Commit 08c32c3

Browse files
committed
Merge PyprojectBuildPack into PythonBuildPack
1 parent c13a796 commit 08c32c3

File tree

5 files changed

+60
-160
lines changed

5 files changed

+60
-160
lines changed

repo2docker/app.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
LegacyBinderDockerBuildPack,
3434
NixBuildPack,
3535
PipfileBuildPack,
36-
PyprojectBuildPack,
3736
PythonBuildPack,
3837
RBuildPack,
3938
)
@@ -101,7 +100,6 @@ def _default_log_level(self):
101100
RBuildPack,
102101
CondaBuildPack,
103102
PipfileBuildPack,
104-
PyprojectBuildPack,
105103
PythonBuildPack,
106104
],
107105
config=True,

repo2docker/buildpacks/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
from .legacy import LegacyBinderDockerBuildPack
66
from .nix import NixBuildPack
77
from .pipfile import PipfileBuildPack
8-
from .pyproject import PyprojectBuildPack
98
from .python import PythonBuildPack
109
from .r import RBuildPack

repo2docker/buildpacks/pyproject/__init__.py

Lines changed: 0 additions & 149 deletions
This file was deleted.

repo2docker/buildpacks/python/__init__.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
"""Generates Dockerfiles based on an input matrix based on Python."""
22

33
import os
4+
import re
45
from functools import lru_cache
56

7+
try:
8+
import tomllib
9+
except ImportError:
10+
import tomli as tomllib
11+
612
from ...utils import is_local_pip_requirement, open_guess_encoding
713
from ..conda import CondaBuildPack
814

@@ -34,6 +40,35 @@ def python_version(self):
3440
# get major.minor
3541
py_version = ".".join(py_version_info[:2])
3642
self._python_version = py_version
43+
44+
pyproject_toml = "pyproject.toml"
45+
if not self.binder_dir and os.path.exists(pyproject_toml):
46+
with open(pyproject_toml, "rb") as _pyproject_file:
47+
pyproject = tomllib.load(_pyproject_file)
48+
49+
if "project" in pyproject:
50+
if "requires-python" in pyproject["project"]:
51+
# This is the minumum version!
52+
raw_pyproject_minimum_version = pyproject["project"][
53+
"requires-python"
54+
]
55+
56+
match = re.compile(r"\d+(\.\d+)*").match(
57+
raw_pyproject_minimum_version
58+
)
59+
if match:
60+
pyproject_minimum_version = match.group()
61+
pyproject_minimum_version_info = (
62+
pyproject_minimum_version.split(".")
63+
)
64+
65+
if (py_version_info[0] < pyproject_minimum_version_info[0]) or (
66+
py_version_info[1] < pyproject_minimum_version_info[1]
67+
):
68+
raise RuntimeError(
69+
"runtime.txt version not supported by pyproject.toml."
70+
)
71+
3772
return self._python_version
3873

3974
def _get_pip_scripts(self):
@@ -78,9 +113,11 @@ def _should_preassemble_pip(self):
78113
If there are any local references, e.g. `-e .`,
79114
stage the whole repo prior to installation.
80115
"""
81-
if not os.path.exists("binder") and os.path.exists("setup.py"):
82-
# can't install from subset if we're using setup.py
83-
return False
116+
# can't install from subset
117+
for _configuration_file in ("pyproject.toml", "setup.py"):
118+
if not os.path.exists("binder") and os.path.exists(_configuration_file):
119+
return False
120+
84121
for name in ("requirements.txt", "requirements3.txt"):
85122
requirements_txt = self.binder_path(name)
86123
if not os.path.exists(requirements_txt):
@@ -119,26 +156,41 @@ def get_assemble_scripts(self):
119156
# and requirements3.txt (if it exists)
120157
# will be installed in the python 3 notebook server env.
121158
assemble_scripts = super().get_assemble_scripts()
122-
setup_py = "setup.py"
123159
# KERNEL_PYTHON_PREFIX is the env with the kernel,
124160
# whether it's distinct from the notebook or the same.
125161
pip = "${KERNEL_PYTHON_PREFIX}/bin/pip"
126162
if not self._should_preassemble_pip:
127163
assemble_scripts.extend(self._get_pip_scripts())
128164

129-
# setup.py exists *and* binder dir is not used
130-
if not self.binder_dir and os.path.exists(setup_py):
131-
assemble_scripts.append(("${NB_USER}", f"{pip} install --no-cache-dir ."))
165+
for _configuration_file in ("pyproject.toml", "setup.py"):
166+
if not self.binder_dir and os.path.exists(_configuration_file):
167+
assemble_scripts.append(
168+
("${NB_USER}", f"{pip} install --no-cache-dir .")
169+
)
170+
break
171+
132172
return assemble_scripts
133173

134174
def detect(self):
135175
"""Check if current repo should be built with the Python buildpack."""
136176
requirements_txt = self.binder_path("requirements.txt")
177+
pyproject_toml = "pyproject.toml"
137178
setup_py = "setup.py"
138179

139180
name = self.runtime[0]
140181
if name:
141182
return name == "python"
183+
if not self.binder_dir and os.path.exists(pyproject_toml):
184+
with open(pyproject_toml, "rb") as _pyproject_file:
185+
pyproject = tomllib.load(_pyproject_file)
186+
187+
if (
188+
("project" in pyproject)
189+
and ("build-system" in pyproject)
190+
and ("requires" in pyproject["build-system"])
191+
):
192+
return True
193+
142194
if not self.binder_dir and os.path.exists(setup_py):
143195
return True
144196
return os.path.exists(requirements_txt)

tests/pyproject/pyproject-toml/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ version = "1.0.0"
88
dependencies = [
99
"numpy",
1010
]
11-
requires-python = ">=3.12"
11+
requires-python = ">=3.10"
1212
authors = [
1313
{name = "Project Jupyter Contributors", email = "[email protected]"},
1414
]

0 commit comments

Comments
 (0)