| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Any, Literal |
| 4 |
|
| 5 |
import click |
| 6 |
from pip._internal.commands import create_command |
| 7 |
from pip._internal.utils.misc import redact_auth_from_url |
| 8 |
|
| 9 |
from piptools.locations import CACHE_DIR, DEFAULT_CONFIG_FILE_NAMES |
| 10 |
from piptools.utils import UNSAFE_PACKAGES, override_defaults_from_config_file |
| 11 |
|
| 12 |
BuildTargetT = Literal["sdist", "wheel", "editable"] |
| 13 |
ALL_BUILD_TARGETS: tuple[BuildTargetT, ...] = ( |
| 14 |
"editable", |
| 15 |
"sdist", |
| 16 |
"wheel", |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
def _get_default_option(option_name: str) -> Any: |
| 21 |
""" |
| 22 |
Get default value of the pip's option (including option from pip.conf) |
| 23 |
by a given option name. |
| 24 |
""" |
| 25 |
install_command = create_command("install") |
| 26 |
default_values = install_command.parser.get_default_values() |
| 27 |
return getattr(default_values, option_name) |
| 28 |
|
| 29 |
|
| 30 |
help_option_names = ("-h", "--help") |
| 31 |
|
| 32 |
# The options used by pip-compile and pip-sync are presented in no specific order. |
| 33 |
|
| 34 |
version = click.version_option(package_name="pip-tools") |
| 35 |
|
| 36 |
color = click.option( |
| 37 |
"--color/--no-color", |
| 38 |
default=None, |
| 39 |
help="Force output to be colorized or not, instead of auto-detecting color support", |
| 40 |
) |
| 41 |
|
| 42 |
verbose = click.option( |
| 43 |
"-v", |
| 44 |
"--verbose", |
| 45 |
count=True, |
| 46 |
help="Show more output", |
| 47 |
) |
| 48 |
quiet = click.option( |
| 49 |
"-q", |
| 50 |
"--quiet", |
| 51 |
count=True, |
| 52 |
help="Give less output", |
| 53 |
) |
| 54 |
|
| 55 |
dry_run = click.option( |
| 56 |
"-n", |
| 57 |
"--dry-run", |
| 58 |
is_flag=True, |
| 59 |
help="Only show what would happen, don't change anything", |
| 60 |
) |
| 61 |
|
| 62 |
pre = click.option( |
| 63 |
"-p", |
| 64 |
"--pre", |
| 65 |
is_flag=True, |
| 66 |
default=None, |
| 67 |
help="Allow resolving to prereleases (default is not)", |
| 68 |
) |
| 69 |
|
| 70 |
rebuild = click.option( |
| 71 |
"-r", |
| 72 |
"--rebuild", |
| 73 |
is_flag=True, |
| 74 |
help="Clear any caches upfront, rebuild from scratch", |
| 75 |
) |
| 76 |
|
| 77 |
extra = click.option( |
| 78 |
"--extra", |
| 79 |
"extras", |
| 80 |
multiple=True, |
| 81 |
help="Name of an extras_require group to install; may be used more than once", |
| 82 |
) |
| 83 |
|
| 84 |
all_extras = click.option( |
| 85 |
"--all-extras", |
| 86 |
is_flag=True, |
| 87 |
default=False, |
| 88 |
help="Install all extras_require groups", |
| 89 |
) |
| 90 |
|
| 91 |
find_links = click.option( |
| 92 |
"-f", |
| 93 |
"--find-links", |
| 94 |
multiple=True, |
| 95 |
help="Look for archives in this directory or on this HTML page; may be used more than once", |
| 96 |
) |
| 97 |
|
| 98 |
index_url = click.option( |
| 99 |
"-i", |
| 100 |
"--index-url", |
| 101 |
help="Change index URL (defaults to {index_url})".format( |
| 102 |
index_url=redact_auth_from_url(_get_default_option("index_url")) |
| 103 |
), |
| 104 |
) |
| 105 |
|
| 106 |
no_index = click.option( |
| 107 |
"--no-index", |
| 108 |
is_flag=True, |
| 109 |
help="Ignore package index (only looking at --find-links URLs instead).", |
| 110 |
) |
| 111 |
|
| 112 |
extra_index_url = click.option( |
| 113 |
"--extra-index-url", |
| 114 |
multiple=True, |
| 115 |
help="Add another index URL to search; may be used more than once", |
| 116 |
) |
| 117 |
|
| 118 |
cert = click.option("--cert", help="Path to alternate CA bundle.") |
| 119 |
|
| 120 |
client_cert = click.option( |
| 121 |
"--client-cert", |
| 122 |
help=( |
| 123 |
"Path to SSL client certificate, a single file containing " |
| 124 |
"the private key and the certificate in PEM format." |
| 125 |
), |
| 126 |
) |
| 127 |
|
| 128 |
trusted_host = click.option( |
| 129 |
"--trusted-host", |
| 130 |
multiple=True, |
| 131 |
help=( |
| 132 |
"Mark this host as trusted, even though it does not have " |
| 133 |
"valid or any HTTPS; may be used more than once" |
| 134 |
), |
| 135 |
) |
| 136 |
|
| 137 |
header = click.option( |
| 138 |
"--header/--no-header", |
| 139 |
is_flag=True, |
| 140 |
default=True, |
| 141 |
help="Add header to generated file", |
| 142 |
) |
| 143 |
|
| 144 |
emit_trusted_host = click.option( |
| 145 |
"--emit-trusted-host/--no-emit-trusted-host", |
| 146 |
is_flag=True, |
| 147 |
default=True, |
| 148 |
help="Add trusted host option to generated file", |
| 149 |
) |
| 150 |
|
| 151 |
annotate = click.option( |
| 152 |
"--annotate/--no-annotate", |
| 153 |
is_flag=True, |
| 154 |
default=True, |
| 155 |
help="Annotate results, indicating where dependencies come from", |
| 156 |
) |
| 157 |
|
| 158 |
annotation_style = click.option( |
| 159 |
"--annotation-style", |
| 160 |
type=click.Choice(("line", "split")), |
| 161 |
default="split", |
| 162 |
help="Choose the format of annotation comments", |
| 163 |
) |
| 164 |
|
| 165 |
upgrade = click.option( |
| 166 |
"-U", |
| 167 |
"--upgrade/--no-upgrade", |
| 168 |
is_flag=True, |
| 169 |
default=False, |
| 170 |
help="Try to upgrade all dependencies to their latest versions", |
| 171 |
) |
| 172 |
|
| 173 |
upgrade_package = click.option( |
| 174 |
"-P", |
| 175 |
"--upgrade-package", |
| 176 |
"upgrade_packages", |
| 177 |
nargs=1, |
| 178 |
multiple=True, |
| 179 |
help="Specify a particular package to upgrade; may be used more than once", |
| 180 |
) |
| 181 |
|
| 182 |
output_file = click.option( |
| 183 |
"-o", |
| 184 |
"--output-file", |
| 185 |
nargs=1, |
| 186 |
default=None, |
| 187 |
type=click.File("w+b", atomic=True, lazy=True), |
| 188 |
help=( |
| 189 |
"Output file name. Required if more than one input file is given. " |
| 190 |
"Will be derived from input file otherwise." |
| 191 |
), |
| 192 |
) |
| 193 |
|
| 194 |
newline = click.option( |
| 195 |
"--newline", |
| 196 |
type=click.Choice(("LF", "CRLF", "native", "preserve"), case_sensitive=False), |
| 197 |
default="preserve", |
| 198 |
help="Override the newline control characters used", |
| 199 |
) |
| 200 |
|
| 201 |
allow_unsafe = click.option( |
| 202 |
"--allow-unsafe/--no-allow-unsafe", |
| 203 |
is_flag=True, |
| 204 |
default=False, |
| 205 |
help=( |
| 206 |
"Pin packages considered unsafe: {}.\n\n" |
| 207 |
"WARNING: Future versions of pip-tools will enable this behavior by default. " |
| 208 |
"Use --no-allow-unsafe to keep the old behavior. It is recommended to pass the " |
| 209 |
"--allow-unsafe now to adapt to the upcoming change.".format( |
| 210 |
", ".join(sorted(UNSAFE_PACKAGES)) |
| 211 |
) |
| 212 |
), |
| 213 |
) |
| 214 |
|
| 215 |
strip_extras = click.option( |
| 216 |
"--strip-extras/--no-strip-extras", |
| 217 |
is_flag=True, |
| 218 |
default=None, |
| 219 |
help="Assure output file is constraints compatible, avoiding use of extras.", |
| 220 |
) |
| 221 |
|
| 222 |
generate_hashes = click.option( |
| 223 |
"--generate-hashes", |
| 224 |
is_flag=True, |
| 225 |
default=False, |
| 226 |
help="Generate pip 8 style hashes in the resulting requirements file.", |
| 227 |
) |
| 228 |
|
| 229 |
reuse_hashes = click.option( |
| 230 |
"--reuse-hashes/--no-reuse-hashes", |
| 231 |
is_flag=True, |
| 232 |
default=True, |
| 233 |
help=( |
| 234 |
"Improve the speed of --generate-hashes by reusing the hashes from an " |
| 235 |
"existing output file." |
| 236 |
), |
| 237 |
) |
| 238 |
|
| 239 |
max_rounds = click.option( |
| 240 |
"--max-rounds", |
| 241 |
default=10, |
| 242 |
help="Maximum number of rounds before resolving the requirements aborts.", |
| 243 |
) |
| 244 |
|
| 245 |
src_files = click.argument( |
| 246 |
"src_files", |
| 247 |
nargs=-1, |
| 248 |
type=click.Path(exists=True, allow_dash=True), |
| 249 |
) |
| 250 |
|
| 251 |
build_isolation = click.option( |
| 252 |
"--build-isolation/--no-build-isolation", |
| 253 |
is_flag=True, |
| 254 |
default=True, |
| 255 |
help=( |
| 256 |
"Enable isolation when building a modern source distribution. " |
| 257 |
"Build dependencies specified by PEP 518 must be already installed " |
| 258 |
"if build isolation is disabled." |
| 259 |
), |
| 260 |
) |
| 261 |
|
| 262 |
emit_find_links = click.option( |
| 263 |
"--emit-find-links/--no-emit-find-links", |
| 264 |
is_flag=True, |
| 265 |
default=True, |
| 266 |
help="Add the find-links option to generated file", |
| 267 |
) |
| 268 |
|
| 269 |
cache_dir = click.option( |
| 270 |
"--cache-dir", |
| 271 |
help="Store the cache data in DIRECTORY.", |
| 272 |
default=CACHE_DIR, |
| 273 |
envvar="PIP_TOOLS_CACHE_DIR", |
| 274 |
show_default=True, |
| 275 |
show_envvar=True, |
| 276 |
type=click.Path(file_okay=False, writable=True), |
| 277 |
) |
| 278 |
|
| 279 |
pip_args = click.option( |
| 280 |
"--pip-args", |
| 281 |
"pip_args_str", |
| 282 |
help="Arguments to pass directly to the pip command.", |
| 283 |
) |
| 284 |
|
| 285 |
resolver = click.option( |
| 286 |
"--resolver", |
| 287 |
"resolver_name", |
| 288 |
type=click.Choice(("legacy", "backtracking")), |
| 289 |
default="backtracking", |
| 290 |
envvar="PIP_TOOLS_RESOLVER", |
| 291 |
help="Choose the dependency resolver.", |
| 292 |
) |
| 293 |
|
| 294 |
emit_index_url = click.option( |
| 295 |
"--emit-index-url/--no-emit-index-url", |
| 296 |
is_flag=True, |
| 297 |
default=True, |
| 298 |
help="Add index URL to generated file", |
| 299 |
) |
| 300 |
|
| 301 |
emit_options = click.option( |
| 302 |
"--emit-options/--no-emit-options", |
| 303 |
is_flag=True, |
| 304 |
default=True, |
| 305 |
help="Add options to generated file", |
| 306 |
) |
| 307 |
|
| 308 |
unsafe_package = click.option( |
| 309 |
"--unsafe-package", |
| 310 |
multiple=True, |
| 311 |
help=( |
| 312 |
"Specify a package to consider unsafe; may be used more than once. " |
| 313 |
f"Replaces default unsafe packages: {', '.join(sorted(UNSAFE_PACKAGES))}" |
| 314 |
), |
| 315 |
) |
| 316 |
|
| 317 |
config = click.option( |
| 318 |
"--config", |
| 319 |
type=click.Path( |
| 320 |
exists=True, |
| 321 |
file_okay=True, |
| 322 |
dir_okay=False, |
| 323 |
readable=True, |
| 324 |
allow_dash=False, |
| 325 |
path_type=str, |
| 326 |
), |
| 327 |
help=( |
| 328 |
f"Read configuration from TOML file. By default, looks for the following " |
| 329 |
f"files in the given order: {', '.join(DEFAULT_CONFIG_FILE_NAMES)}." |
| 330 |
), |
| 331 |
is_eager=True, |
| 332 |
callback=override_defaults_from_config_file, |
| 333 |
) |
| 334 |
|
| 335 |
no_config = click.option( |
| 336 |
"--no-config", |
| 337 |
is_flag=True, |
| 338 |
default=False, |
| 339 |
help="Do not read any config file.", |
| 340 |
is_eager=True, |
| 341 |
) |
| 342 |
|
| 343 |
constraint = click.option( |
| 344 |
"-c", |
| 345 |
"--constraint", |
| 346 |
multiple=True, |
| 347 |
help="Constrain versions using the given constraints file; may be used more than once.", |
| 348 |
) |
| 349 |
|
| 350 |
ask = click.option( |
| 351 |
"-a", |
| 352 |
"--ask", |
| 353 |
is_flag=True, |
| 354 |
help="Show what would happen, then ask whether to continue", |
| 355 |
) |
| 356 |
|
| 357 |
force = click.option( |
| 358 |
"--force", is_flag=True, help="Proceed even if conflicts are found" |
| 359 |
) |
| 360 |
|
| 361 |
python_executable = click.option( |
| 362 |
"--python-executable", |
| 363 |
help="Custom python executable path if targeting an environment other than current.", |
| 364 |
) |
| 365 |
|
| 366 |
user = click.option( |
| 367 |
"--user", |
| 368 |
"user_only", |
| 369 |
is_flag=True, |
| 370 |
help="Restrict attention to user directory", |
| 371 |
) |
| 372 |
|
| 373 |
build_deps_for = click.option( |
| 374 |
"--build-deps-for", |
| 375 |
"build_deps_targets", |
| 376 |
multiple=True, |
| 377 |
type=click.Choice(ALL_BUILD_TARGETS), |
| 378 |
help="Name of a build target to extract dependencies for. " |
| 379 |
"Static dependencies declared in 'pyproject.toml::build-system.requires' will be included as " |
| 380 |
"well; may be used more than once.", |
| 381 |
) |
| 382 |
|
| 383 |
all_build_deps = click.option( |
| 384 |
"--all-build-deps", |
| 385 |
is_flag=True, |
| 386 |
default=False, |
| 387 |
help="Extract dependencies for all build targets. " |
| 388 |
"Static dependencies declared in 'pyproject.toml::build-system.requires' will be included as " |
| 389 |
"well.", |
| 390 |
) |
| 391 |
|
| 392 |
only_build_deps = click.option( |
| 393 |
"--only-build-deps", |
| 394 |
is_flag=True, |
| 395 |
default=False, |
| 396 |
help="Extract a package only if it is a build dependency.", |
| 397 |
) |