|
| 1 | +from pathlib import Path |
| 2 | +from typing import List, Sequence |
| 3 | + |
| 4 | +from packaging.utils import NormalizedName |
| 5 | + |
| 6 | +from .installed_dist import installed_distributions_as_pip_requirements |
| 7 | +from .pip import ( |
| 8 | + _dependencies_by_extra, # TODO _dependencies_by_extra should be elsewhere |
| 9 | + pip_dry_run_install_project, |
| 10 | +) |
| 11 | +from .project_name import get_project_name |
| 12 | +from .req_file_parser import OptionsLine, parse as parse_req_file |
| 13 | +from .req_merge import prepare_frozen_reqs_for_upgrade |
| 14 | +from .utils import ( |
| 15 | + HttpFetcher, |
| 16 | + get_temp_path_in_dir, |
| 17 | + make_requirements_path, |
| 18 | + make_requirements_paths, |
| 19 | + open_with_rollback, |
| 20 | + run_commands, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def lock( |
| 25 | + python: str, |
| 26 | + upgrade_all: bool, |
| 27 | + to_upgrade: List[str], |
| 28 | + extras: List[NormalizedName], |
| 29 | + project_root: Path, |
| 30 | + post_lock_commands: Sequence[str] = (), |
| 31 | +) -> None: |
| 32 | + # TODO error out if pip does not support install report |
| 33 | + project_name = get_project_name(python, project_root) |
| 34 | + requirements_in = project_root / "requirements.txt.in" |
| 35 | + # upgrade project and its dependencies, if needed |
| 36 | + constraints_path = get_temp_path_in_dir( |
| 37 | + dir=project_root, prefix="requirements.", suffix=".txt.df" |
| 38 | + ) |
| 39 | + with constraints_path.open(mode="w", encoding="utf-8") as constraints: |
| 40 | + for req_line in prepare_frozen_reqs_for_upgrade( |
| 41 | + make_requirements_paths(project_root, extras), |
| 42 | + requirements_in, |
| 43 | + upgrade_all, |
| 44 | + to_upgrade, |
| 45 | + ): |
| 46 | + print(req_line, file=constraints) |
| 47 | + installed_distributions = pip_dry_run_install_project( |
| 48 | + python, |
| 49 | + constraints_path, |
| 50 | + project_root, |
| 51 | + extras=extras, |
| 52 | + ) |
| 53 | + # freeze dependencies |
| 54 | + # TODO _dependencies_by_extra should be elsewhere |
| 55 | + frozen_reqs_by_extra, unneeded_reqs = _dependencies_by_extra( |
| 56 | + project_name, |
| 57 | + extras, |
| 58 | + installed_distributions, |
| 59 | + installed_distributions_as_pip_requirements(installed_distributions), |
| 60 | + ) |
| 61 | + assert not unneeded_reqs |
| 62 | + for extra, frozen_reqs in frozen_reqs_by_extra.items(): |
| 63 | + requirements_frozen_path = make_requirements_path(project_root, extra) |
| 64 | + with open_with_rollback(requirements_frozen_path) as f: |
| 65 | + print("# frozen requirements generated by pip-deepfreeze", file=f) |
| 66 | + # output pip options in main requirements only |
| 67 | + if not extra and requirements_in.exists(): |
| 68 | + # XXX can we avoid this second parse of requirements.txt.in? |
| 69 | + for parsed_req_line in parse_req_file( |
| 70 | + str(requirements_in), |
| 71 | + reqs_only=False, |
| 72 | + recurse=True, |
| 73 | + strict=True, |
| 74 | + http_fetcher=HttpFetcher(), |
| 75 | + ): |
| 76 | + if isinstance(parsed_req_line, OptionsLine): |
| 77 | + print(parsed_req_line.raw_line, file=f) |
| 78 | + # output frozen dependencies of project |
| 79 | + for req_line in frozen_reqs: |
| 80 | + print(req_line, file=f) |
| 81 | + # run post-lock commands |
| 82 | + run_commands(post_lock_commands, project_root, "post-lock") |
0 commit comments