Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a912b13
Add optional param from_dir to format_requirement
AndydeCleyre Jun 30, 2022
c90d969
Reuse fragment_string to simplify _build_direct_reference_best_efforts
AndydeCleyre Jun 30, 2022
11ffba6
Add working_dir context manager and corresponding test
AndydeCleyre Jun 30, 2022
65c489c
Add abs_ireq for normalizing ireqs, and related tests
AndydeCleyre Jun 30, 2022
987d90b
Update test to expect and accept more Windows file URIs
AndydeCleyre Jun 30, 2022
6713b60
Add writer test for annotation accuracy regarding source ireqs
AndydeCleyre Jun 30, 2022
0f2cf1d
Add optional param from_dir to _comes_from_as_string
AndydeCleyre Jun 30, 2022
f2350e1
Add optional from_dir param to parse_requirements
AndydeCleyre Jun 30, 2022
8edf988
Add flag for compile: --write-relative-to-output
AndydeCleyre Jun 30, 2022
bc00d62
Add test_local_editable_vcs_package
AndydeCleyre Jun 30, 2022
a9784a1
Add flag to sync: --read-relative-to-input
AndydeCleyre Jun 30, 2022
996f21c
Absolute-ize src_file paths and more safely determine output file paths
AndydeCleyre Jun 30, 2022
a92729f
Exit earlier when no output file is specified for multiple input files
AndydeCleyre Jun 30, 2022
f46a2d5
Add flag for compile: --read-relative-to-input
AndydeCleyre Jun 30, 2022
7e55700
Make annotation req file paths relative to the output file
AndydeCleyre Jun 30, 2022
4004f96
Add test_annotation_relative_paths for #1107
AndydeCleyre Jun 30, 2022
7ef7b98
Add test_format_requirement_annotation_impossible_relative_path
AndydeCleyre Jun 30, 2022
48d05fd
Reinject any lost url fragments during parse_requirements
AndydeCleyre Jun 30, 2022
6084450
Include extras syntax in direct references we construct
AndydeCleyre Jun 30, 2022
8117c1d
Avoid choking on a relative path without scheme prefix, with fragment
AndydeCleyre Jun 30, 2022
ece9e14
When copying ireqs, copy extras from a newly provided link
AndydeCleyre Jun 30, 2022
3345abc
Improve consistency of output regarding fragments and extras
AndydeCleyre Jun 30, 2022
eee3e8a
Add test_url_package_with_extras and fix it for backtracking
AndydeCleyre Jun 30, 2022
4ed481d
Remove fragment_string parameter omit_extras
AndydeCleyre Jun 30, 2022
2d27659
Add test_local_file_uri_with_extras
AndydeCleyre Jun 30, 2022
88eb70d
Add test_local_file_path_package for non-URI paths
AndydeCleyre Jun 30, 2022
6468e37
Use consistently canonicalized ireq name when writing direct reference
AndydeCleyre Jun 30, 2022
aa3ee0f
Construct relative req lines to match what pip install understands
AndydeCleyre Jun 30, 2022
f853ba9
Merge branch 'master' into feature/relpaths-post-6.8.0
ssbarnea Oct 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add optional param from_dir to format_requirement
If passed, it will attempt to relative-ize the resulting URL (if possible).

Add helper: fragment_string
  • Loading branch information
AndydeCleyre committed Oct 3, 2022
commit a912b13faa15501bbf7adaf35ea354ce555a059c
50 changes: 45 additions & 5 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from pip._internal.req import InstallRequirement
from pip._internal.req.constructors import install_req_from_line
from pip._internal.utils.misc import redact_auth_from_url
from pip._internal.utils.urls import path_to_url
from pip._internal.vcs import is_url
from pip._vendor.packaging.markers import Marker
from pip._vendor.packaging.specifiers import SpecifierSet
Expand Down Expand Up @@ -109,25 +110,64 @@ def is_url_requirement(ireq: InstallRequirement) -> bool:
return bool(ireq.original_link)


def fragment_string(ireq: InstallRequirement, omit_egg: bool = False) -> str:
"""
Return a string like "#egg=pkgname&subdirectory=folder", or "".
"""
if ireq.link is None or not ireq.link._parsed_url.fragment:
return ""
fragment = f"#{ireq.link._parsed_url.fragment.replace(os.path.sep, '/')}"
if omit_egg:
fragment = re.sub(r"[#&]egg=[^#&]+", "", fragment).lstrip("#&")
if fragment:
fragment = f"#{fragment}"
return fragment


def format_requirement(
ireq: InstallRequirement,
marker: Optional[Marker] = None,
hashes: Optional[Set[str]] = None,
from_dir: Optional[str] = None,
) -> str:
"""
Generic formatter for pretty printing InstallRequirements to the terminal
in a less verbose way than using its `__str__` method.
"""
if ireq.editable:
line = f"-e {ireq.link.url}"
elif is_url_requirement(ireq):
line = _build_direct_reference_best_efforts(ireq)
else:
if not is_url_requirement(ireq):
# Canonicalize the requirement name
# https://packaging.pypa.io/en/latest/utils.html#packaging.utils.canonicalize_name
req = copy.copy(ireq.req)
req.name = canonicalize_name(req.name)
line = str(req)
elif not ireq.link.is_file:
line = (
f"-e {ireq.link.url}"
if ireq.editable
else _build_direct_reference_best_efforts(ireq)
)
# pip doesn't support relative paths in git+file scheme urls,
# for which ireq.link.is_file == False
elif not from_dir:
line = (
f"-e {path_to_url(ireq.local_file_path)}"
if ireq.editable
else _build_direct_reference_best_efforts(ireq)
)
else:
try:
path_url = "file:" + os.path.relpath(
ireq.local_file_path, from_dir
).replace(os.path.sep, "/")
except ValueError:
# On Windows, a relative path is not always possible (no common ancestor)
line = (
f"-e {path_to_url(ireq.local_file_path)}"
if ireq.editable
else _build_direct_reference_best_efforts(ireq)
)
else:
line = f"{'-e ' if ireq.editable else ''}{path_url}{fragment_string(ireq)}"

if marker:
line = f"{line} ; {marker}"
Expand Down