Generic formatter for pretty printing InstallRequirements to the terminal in a less verbose way than using its ``__str__`` method.
(
ireq: InstallRequirement,
marker: Marker | None = None,
hashes: set[str] | None = None,
)
| 97 | |
| 98 | |
| 99 | def format_requirement( |
| 100 | ireq: InstallRequirement, |
| 101 | marker: Marker | None = None, |
| 102 | hashes: set[str] | None = None, |
| 103 | ) -> str: |
| 104 | """ |
| 105 | Generic formatter for pretty printing InstallRequirements to the terminal |
| 106 | in a less verbose way than using its ``__str__`` method. |
| 107 | """ |
| 108 | if ireq.editable: |
| 109 | line = f"-e {ireq.link.url}" |
| 110 | elif is_url_requirement(ireq): |
| 111 | line = _build_direct_reference_best_efforts(ireq) |
| 112 | else: |
| 113 | # Canonicalize the requirement name |
| 114 | # https://packaging.pypa.io/en/latest/utils.html#packaging.utils.canonicalize_name |
| 115 | req = copy.copy(ireq.req) |
| 116 | req.name = canonicalize_name(req.name) |
| 117 | line = str(req) |
| 118 | |
| 119 | if marker: |
| 120 | line = f"{line} ; {marker}" |
| 121 | |
| 122 | if hashes: |
| 123 | for hash_ in sorted(hashes): |
| 124 | line += f" \\\n --hash={hash_}" |
| 125 | |
| 126 | return line |
| 127 | |
| 128 | |
| 129 | def _build_direct_reference_best_efforts(ireq: InstallRequirement) -> str: |
searching dependent graphs…