Skip to content

Commit 42de356

Browse files
feat: build tool detected by macaron will be used now and build dependancy information supplied to those build tools will be better extracted
Signed-off-by: Abhinav Pradeep <abhinav.pradeep@oracle.com>
1 parent 84493c4 commit 42de356

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

src/macaron/build_spec_generator/common_spec/pypi_spec.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,20 @@ def get_default_build_commands(
6767

6868
match build_tool_name:
6969
case "pip":
70-
default_build_commands.append("python -m build".split())
70+
default_build_commands.append("python -m build --wheel -n".split())
7171
case "poetry":
72-
default_build_commands.append("poetry build".split())
72+
default_build_commands.append("pip install poetry && poetry build".split())
7373
case "flit":
74-
default_build_commands.append("flit build".split())
74+
# We might also want to deal with existence flit.ini, we can do so via
75+
# "python -m flit.tomlify"
76+
default_build_commands.append(
77+
'pip install flit && if test -f "flit.ini"; then python -m flit.tomlify; fi '
78+
"&& flit build".split()
79+
)
7580
case "hatch":
76-
default_build_commands.append("hatch build".split())
81+
default_build_commands.append("pip install hatch && hatch build".split())
7782
case "conda":
78-
default_build_commands.append("conda build".split())
83+
default_build_commands.append('echo("Not supported")'.split())
7984
case _:
8085
pass
8186

@@ -156,18 +161,35 @@ def resolve_fields(self, purl: PackageURL) -> None:
156161
try:
157162
with pypi_package_json.sourcecode():
158163
try:
164+
# Get the build time requirements from ["build-system", "requires"]
159165
pyproject_content = pypi_package_json.get_sourcecode_file_contents("pyproject.toml")
160166
content = tomli.loads(pyproject_content.decode("utf-8"))
161167
requires = json_extract(content, ["build-system", "requires"], list)
162168
if requires:
163169
build_requires_set.update(elem.replace(" ", "") for elem in requires)
170+
# If we have hatch as a build_tool, we will
171+
if "hatch" in self.data["build_tools"]:
172+
# Look for [tool.hatch.build.hooks.*]
173+
hatch_build_hooks = json_extract(content, ["tool", "hatch", "build", "hooks"], dict)
174+
if hatch_build_hooks:
175+
for _, section in hatch_build_hooks.items():
176+
dependencies = section.get("dependencies")
177+
if dependencies:
178+
build_requires_set.update(elem.replace(" ", "") for elem in dependencies)
164179
backend = json_extract(content, ["build-system", "build-backend"], str)
165180
if backend:
166181
build_backends_set.add(backend.replace(" ", ""))
167-
168182
python_version_constraint = json_extract(content, ["project", "requires-python"], str)
169183
if python_version_constraint:
170184
python_version_set.add(python_version_constraint.replace(" ", ""))
185+
# If we have flit as a build_tool, we will check if the legacy header [tool.flit.metadata] exists,
186+
# and if so, check to see if we can use its "requires-python".
187+
if "flit" in self.data["build_tools"]:
188+
flit_python_version_constraint = json_extract(
189+
content, ["tool", "flit", "metadata", "requires-python"], str
190+
)
191+
if flit_python_version_constraint:
192+
python_version_set.add(flit_python_version_constraint.replace(" ", ""))
171193
logger.debug(
172194
"After analyzing pyproject.toml from the sdist: build-requires: %s, build_backend: %s",
173195
build_requires_set,

src/macaron/build_spec_generator/dockerfile/pypi_dockerfile_output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def gen_dockerfile(buildspec: BaseBuildSpecDict) -> str:
8787
EOF
8888
8989
# Run the build
90-
RUN /deps/bin/python -m build --wheel -n
90+
RUN {"source /deps/bin/activate && " + " ".join(x for x in buildspec["build_commands"][0])}
9191
"""
9292

9393
return dedent(dockerfile_content)

tests/build_spec_generator/dockerfile/__snapshots__/test_pypi_dockerfile_output.ambr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
EOF
5151

5252
# Run the build
53-
RUN /deps/bin/python -m build --wheel -n
53+
RUN source /deps/bin/activate && python -m build
5454

5555
'''
5656
# ---

tests/integration/cases/pypi_markdown-it-py/expected_default.buildspec

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@
1717
],
1818
"build_commands": [
1919
[
20+
"pip",
21+
"install",
22+
"flit",
23+
"&&",
24+
"if",
25+
"test",
26+
"-f",
27+
"\"flit.ini\";",
28+
"then",
29+
"python",
30+
"-m",
31+
"flit.tomlify;",
32+
"fi",
33+
"&&",
2034
"flit",
2135
"build"
2236
]

tests/integration/cases/pypi_toga/expected_default.buildspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
[
2020
"python",
2121
"-m",
22-
"build"
22+
"build",
23+
"--wheel",
24+
"-n"
2325
]
2426
],
2527
"build_requires": {

0 commit comments

Comments
 (0)