Skip to content

Commit

Permalink
Add option to include build-system dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
apljungquist committed Dec 11, 2022
1 parent eb48193 commit 3ae3810
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
32 changes: 27 additions & 5 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import sys
import tempfile
from typing import IO, Any, BinaryIO, cast
from typing import IO, Any, BinaryIO, List, Optional, Set, Tuple, Union, cast

import click
from build import BuildBackendException
import pep517
from build import BuildBackendException, ProjectBuilder
from build.util import project_wheel_metadata
from click.utils import LazyFile, safecall
from pip._internal.commands import create_command
Expand Down Expand Up @@ -38,6 +40,14 @@
METADATA_FILENAMES = frozenset({"setup.py", "setup.cfg", "pyproject.toml"})


def _build_system_requires(src_dir: str) -> Set[str]:
result = ProjectBuilder(
src_dir, runner=pep517.quiet_subprocess_runner
).build_system_requires
assert isinstance(result, set)
return result


def _get_default_option(option_name: str) -> Any:
"""
Get default value of the pip's option (including option from pip.conf)
Expand Down Expand Up @@ -291,6 +301,12 @@ def _determine_linesep(
help="Specify a package to consider unsafe; may be used more than once. "
f"Replaces default unsafe packages: {', '.join(sorted(UNSAFE_PACKAGES))}",
)
@click.option(
"--build-system-requires/--no-build-system-requires",
is_flag=True,
default=False,
help="Pin also build requirements",
)
def cli(
ctx: click.Context,
verbose: int,
Expand Down Expand Up @@ -328,6 +344,7 @@ def cli(
emit_index_url: bool,
emit_options: bool,
unsafe_package: tuple[str, ...],
build_system_requires: bool,
) -> None:
"""
Compiles requirements.txt from requirements.in, pyproject.toml, setup.cfg,
Expand Down Expand Up @@ -477,10 +494,8 @@ def cli(
elif is_setup_file:
setup_file_found = True
try:
metadata = project_wheel_metadata(
os.path.dirname(os.path.abspath(src_file)),
isolated=build_isolation,
)
src_dir = os.path.dirname(os.path.abspath(src_file))
metadata = project_wheel_metadata(src_dir)
except BuildBackendException as e:
log.error(str(e))
log.error(f"Failed to parse {os.path.abspath(src_file)}")
Expand All @@ -497,6 +512,13 @@ def cli(
msg = "--extra has no effect when used with --all-extras"
raise click.BadParameter(msg)
extras = tuple(metadata.get_all("Provides-Extra"))
if build_system_requires:
constraints.extend(
[
install_req_from_line(req, comes_from=comes_from)
for req in _build_system_requires(src_dir)
]
)
else:
constraints.extend(
parse_requirements(
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ install_requires =
build
click >= 8
pip >= 22.2
pep517
# indirect dependencies
setuptools # typically needed when pip-tools invokes setup.py
wheel # pip plugin needed by pip-tools
Expand Down

0 comments on commit 3ae3810

Please sign in to comment.