| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import io |
| 4 |
import os |
| 5 |
import re |
| 6 |
import sys |
| 7 |
from itertools import chain |
| 8 |
from typing import BinaryIO, Iterable, Iterator, cast |
| 9 |
|
| 10 |
from click import unstyle |
| 11 |
from click.core import Context |
| 12 |
from pip._internal.models.format_control import FormatControl |
| 13 |
from pip._internal.req.req_install import InstallRequirement |
| 14 |
from pip._vendor.packaging.markers import Marker |
| 15 |
from pip._vendor.packaging.utils import canonicalize_name |
| 16 |
|
| 17 |
from .logging import log |
| 18 |
from .utils import ( |
| 19 |
comment, |
| 20 |
dedup, |
| 21 |
format_requirement, |
| 22 |
get_compile_command, |
| 23 |
key_from_ireq, |
| 24 |
strip_extras, |
| 25 |
) |
| 26 |
|
| 27 |
MESSAGE_UNHASHED_PACKAGE = comment( |
| 28 |
"# WARNING: pip install will require the following package to be hashed." |
| 29 |
"\n# Consider using a hashable URL like " |
| 30 |
"https://github.com/jazzband/pip-tools/archive/SOMECOMMIT.zip" |
| 31 |
) |
| 32 |
|
| 33 |
MESSAGE_UNSAFE_PACKAGES_UNPINNED = comment( |
| 34 |
"# WARNING: The following packages were not pinned, but pip requires them to be" |
| 35 |
"\n# pinned when the requirements file includes hashes and the requirement is not" |
| 36 |
"\n# satisfied by a package already installed. " |
| 37 |
"Consider using the --allow-unsafe flag." |
| 38 |
) |
| 39 |
|
| 40 |
MESSAGE_UNSAFE_PACKAGES = comment( |
| 41 |
"# The following packages are considered to be unsafe in a requirements file:" |
| 42 |
) |
| 43 |
|
| 44 |
MESSAGE_UNINSTALLABLE = ( |
| 45 |
"The generated requirements file may be rejected by pip install. " |
| 46 |
"See # WARNING lines for details." |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
strip_comes_from_line_re = re.compile(r" \(line \d+\)$") |
| 51 |
|
| 52 |
|
| 53 |
def _comes_from_as_string(comes_from: str | InstallRequirement) -> str: |
| 54 |
if isinstance(comes_from, str): |
| 55 |
return strip_comes_from_line_re.sub("", comes_from) |
| 56 |
return cast(str, canonicalize_name(key_from_ireq(comes_from))) |
| 57 |
|
| 58 |
|
| 59 |
def annotation_style_split(required_by: set[str]) -> str: |
| 60 |
sorted_required_by = sorted(required_by) |
| 61 |
if len(sorted_required_by) == 1: |
| 62 |
source = sorted_required_by[0] |
| 63 |
annotation = "# via " + source |
| 64 |
else: |
| 65 |
annotation_lines = ["# via"] |
| 66 |
for source in sorted_required_by: |
| 67 |
annotation_lines.append(" # " + source) |
| 68 |
annotation = "\n".join(annotation_lines) |
| 69 |
return annotation |
| 70 |
|
| 71 |
|
| 72 |
def annotation_style_line(required_by: set[str]) -> str: |
| 73 |
return f"# via {', '.join(sorted(required_by))}" |
| 74 |
|
| 75 |
|
| 76 |
class OutputWriter: |
| 77 |
def __init__( |
| 78 |
self, |
| 79 |
dst_file: BinaryIO, |
| 80 |
click_ctx: Context, |
| 81 |
dry_run: bool, |
| 82 |
emit_header: bool, |
| 83 |
emit_index_url: bool, |
| 84 |
emit_trusted_host: bool, |
| 85 |
annotate: bool, |
| 86 |
annotation_style: str, |
| 87 |
strip_extras: bool, |
| 88 |
generate_hashes: bool, |
| 89 |
default_index_url: str, |
| 90 |
index_urls: Iterable[str], |
| 91 |
trusted_hosts: Iterable[str], |
| 92 |
format_control: FormatControl, |
| 93 |
linesep: str, |
| 94 |
allow_unsafe: bool, |
| 95 |
find_links: list[str], |
| 96 |
emit_find_links: bool, |
| 97 |
emit_options: bool, |
| 98 |
) -> None: |
| 99 |
self.dst_file = dst_file |
| 100 |
self.click_ctx = click_ctx |
| 101 |
self.dry_run = dry_run |
| 102 |
self.emit_header = emit_header |
| 103 |
self.emit_index_url = emit_index_url |
| 104 |
self.emit_trusted_host = emit_trusted_host |
| 105 |
self.annotate = annotate |
| 106 |
self.annotation_style = annotation_style |
| 107 |
self.strip_extras = strip_extras |
| 108 |
self.generate_hashes = generate_hashes |
| 109 |
self.default_index_url = default_index_url |
| 110 |
self.index_urls = index_urls |
| 111 |
self.trusted_hosts = trusted_hosts |
| 112 |
self.format_control = format_control |
| 113 |
self.linesep = linesep |
| 114 |
self.allow_unsafe = allow_unsafe |
| 115 |
self.find_links = find_links |
| 116 |
self.emit_find_links = emit_find_links |
| 117 |
self.emit_options = emit_options |
| 118 |
|
| 119 |
def _sort_key(self, ireq: InstallRequirement) -> tuple[bool, str]: |
| 120 |
return (not ireq.editable, key_from_ireq(ireq)) |
| 121 |
|
| 122 |
def write_header(self) -> Iterator[str]: |
| 123 |
if self.emit_header: |
| 124 |
yield comment("#") |
| 125 |
yield comment( |
| 126 |
"# This file is autogenerated by pip-compile with Python " |
| 127 |
f"{sys.version_info.major}.{sys.version_info.minor}" |
| 128 |
) |
| 129 |
yield comment("# by the following command:") |
| 130 |
yield comment("#") |
| 131 |
compile_command = os.environ.get( |
| 132 |
"CUSTOM_COMPILE_COMMAND" |
| 133 |
) or get_compile_command(self.click_ctx) |
| 134 |
yield comment(f"# {compile_command}") |
| 135 |
yield comment("#") |
| 136 |
|
| 137 |
def write_index_options(self) -> Iterator[str]: |
| 138 |
if self.emit_index_url: |
| 139 |
for index, index_url in enumerate(dedup(self.index_urls)): |
| 140 |
if index == 0 and index_url.rstrip("/") == self.default_index_url: |
| 141 |
continue |
| 142 |
flag = "--index-url" if index == 0 else "--extra-index-url" |
| 143 |
yield f"{flag} {index_url}" |
| 144 |
|
| 145 |
def write_trusted_hosts(self) -> Iterator[str]: |
| 146 |
if self.emit_trusted_host: |
| 147 |
for trusted_host in dedup(self.trusted_hosts): |
| 148 |
yield f"--trusted-host {trusted_host}" |
| 149 |
|
| 150 |
def write_format_controls(self) -> Iterator[str]: |
| 151 |
for nb in dedup(sorted(self.format_control.no_binary)): |
| 152 |
yield f"--no-binary {nb}" |
| 153 |
for ob in dedup(sorted(self.format_control.only_binary)): |
| 154 |
yield f"--only-binary {ob}" |
| 155 |
|
| 156 |
def write_find_links(self) -> Iterator[str]: |
| 157 |
if self.emit_find_links: |
| 158 |
for find_link in dedup(self.find_links): |
| 159 |
yield f"--find-links {find_link}" |
| 160 |
|
| 161 |
def write_flags(self) -> Iterator[str]: |
| 162 |
if not self.emit_options: |
| 163 |
return |
| 164 |
emitted = False |
| 165 |
for line in chain( |
| 166 |
self.write_index_options(), |
| 167 |
self.write_find_links(), |
| 168 |
self.write_trusted_hosts(), |
| 169 |
self.write_format_controls(), |
| 170 |
): |
| 171 |
emitted = True |
| 172 |
yield line |
| 173 |
if emitted: |
| 174 |
yield "" |
| 175 |
|
| 176 |
def _iter_lines( |
| 177 |
self, |
| 178 |
results: set[InstallRequirement], |
| 179 |
unsafe_requirements: set[InstallRequirement], |
| 180 |
unsafe_packages: set[str], |
| 181 |
markers: dict[str, Marker], |
| 182 |
hashes: dict[InstallRequirement, set[str]] | None = None, |
| 183 |
) -> Iterator[str]: |
| 184 |
# default values |
| 185 |
unsafe_packages = unsafe_packages if self.allow_unsafe else set() |
| 186 |
hashes = hashes or {} |
| 187 |
|
| 188 |
# Check for unhashed or unpinned packages if at least one package does have |
| 189 |
# hashes, which will trigger pip install's --require-hashes mode. |
| 190 |
warn_uninstallable = False |
| 191 |
has_hashes = hashes and any(hash for hash in hashes.values()) |
| 192 |
|
| 193 |
yielded = False |
| 194 |
|
| 195 |
for line in self.write_header(): |
| 196 |
yield line |
| 197 |
yielded = True |
| 198 |
for line in self.write_flags(): |
| 199 |
yield line |
| 200 |
yielded = True |
| 201 |
|
| 202 |
unsafe_requirements = unsafe_requirements or { |
| 203 |
r for r in results if r.name in unsafe_packages |
| 204 |
} |
| 205 |
packages = {r for r in results if r.name not in unsafe_packages} |
| 206 |
|
| 207 |
if packages: |
| 208 |
for ireq in sorted(packages, key=self._sort_key): |
| 209 |
if has_hashes and not hashes.get(ireq): |
| 210 |
yield MESSAGE_UNHASHED_PACKAGE |
| 211 |
warn_uninstallable = True |
| 212 |
line = self._format_requirement( |
| 213 |
ireq, markers.get(key_from_ireq(ireq)), hashes=hashes |
| 214 |
) |
| 215 |
yield line |
| 216 |
yielded = True |
| 217 |
|
| 218 |
if unsafe_requirements: |
| 219 |
yield "" |
| 220 |
yielded = True |
| 221 |
if has_hashes and not self.allow_unsafe: |
| 222 |
yield MESSAGE_UNSAFE_PACKAGES_UNPINNED |
| 223 |
warn_uninstallable = True |
| 224 |
else: |
| 225 |
yield MESSAGE_UNSAFE_PACKAGES |
| 226 |
|
| 227 |
for ireq in sorted(unsafe_requirements, key=self._sort_key): |
| 228 |
ireq_key = key_from_ireq(ireq) |
| 229 |
if not self.allow_unsafe: |
| 230 |
yield comment(f"# {ireq_key}") |
| 231 |
else: |
| 232 |
line = self._format_requirement( |
| 233 |
ireq, marker=markers.get(ireq_key), hashes=hashes |
| 234 |
) |
| 235 |
yield line |
| 236 |
|
| 237 |
# Yield even when there's no real content, so that blank files are written |
| 238 |
if not yielded: |
| 239 |
yield "" |
| 240 |
|
| 241 |
if warn_uninstallable: |
| 242 |
log.warning(MESSAGE_UNINSTALLABLE) |
| 243 |
|
| 244 |
def write( |
| 245 |
self, |
| 246 |
results: set[InstallRequirement], |
| 247 |
unsafe_requirements: set[InstallRequirement], |
| 248 |
unsafe_packages: set[str], |
| 249 |
markers: dict[str, Marker], |
| 250 |
hashes: dict[InstallRequirement, set[str]] | None, |
| 251 |
) -> None: |
| 252 |
if not self.dry_run: |
| 253 |
dst_file = io.TextIOWrapper( |
| 254 |
self.dst_file, |
| 255 |
encoding="utf8", |
| 256 |
newline=self.linesep, |
| 257 |
line_buffering=True, |
| 258 |
) |
| 259 |
try: |
| 260 |
for line in self._iter_lines( |
| 261 |
results, unsafe_requirements, unsafe_packages, markers, hashes |
| 262 |
): |
| 263 |
if self.dry_run: |
| 264 |
# Bypass the log level to always print this during a dry run |
| 265 |
log.log(line) |
| 266 |
else: |
| 267 |
log.info(line) |
| 268 |
dst_file.write(unstyle(line)) |
| 269 |
dst_file.write("\n") |
| 270 |
finally: |
| 271 |
if not self.dry_run: |
| 272 |
dst_file.detach() |
| 273 |
|
| 274 |
def _format_requirement( |
| 275 |
self, |
| 276 |
ireq: InstallRequirement, |
| 277 |
marker: Marker | None = None, |
| 278 |
hashes: dict[InstallRequirement, set[str]] | None = None, |
| 279 |
) -> str: |
| 280 |
ireq_hashes = (hashes if hashes is not None else {}).get(ireq) |
| 281 |
|
| 282 |
line = format_requirement(ireq, marker=marker, hashes=ireq_hashes) |
| 283 |
if self.strip_extras: |
| 284 |
line = strip_extras(line) |
| 285 |
|
| 286 |
if not self.annotate: |
| 287 |
return line |
| 288 |
|
| 289 |
# Annotate what packages or reqs-ins this package is required by |
| 290 |
required_by = set() |
| 291 |
if hasattr(ireq, "_source_ireqs"): |
| 292 |
required_by |= { |
| 293 |
_comes_from_as_string(src_ireq.comes_from) |
| 294 |
for src_ireq in ireq._source_ireqs |
| 295 |
if src_ireq.comes_from |
| 296 |
} |
| 297 |
|
| 298 |
# Filter out the origin install requirements for extras. |
| 299 |
# See https://github.com/jazzband/pip-tools/issues/2003 |
| 300 |
if ireq.comes_from and ( |
| 301 |
isinstance(ireq.comes_from, str) or ireq.comes_from.name != ireq.name |
| 302 |
): |
| 303 |
required_by.add(_comes_from_as_string(ireq.comes_from)) |
| 304 |
|
| 305 |
required_by |= set(getattr(ireq, "_required_by", set())) |
| 306 |
|
| 307 |
if required_by: |
| 308 |
if self.annotation_style == "split": |
| 309 |
annotation = annotation_style_split(required_by) |
| 310 |
sep = "\n " |
| 311 |
elif self.annotation_style == "line": |
| 312 |
annotation = annotation_style_line(required_by) |
| 313 |
sep = "\n " if ireq_hashes else " " |
| 314 |
else: # pragma: no cover |
| 315 |
raise ValueError("Invalid value for annotation style") |
| 316 |
if self.strip_extras: |
| 317 |
annotation = strip_extras(annotation) |
| 318 |
# 24 is one reasonable column size to use here, that we've used in the past |
| 319 |
lines = f"{line:24}{sep}{comment(annotation)}".splitlines() |
| 320 |
line = "\n".join(ln.rstrip() for ln in lines) |
| 321 |
|
| 322 |
return line |