Skip to content

Commit

Permalink
Be more user-friendly by pointing to PyPA specifications instead of P…
Browse files Browse the repository at this point in the history
…EPs (#1146)
  • Loading branch information
jeanas authored Dec 14, 2023
1 parent 05c3b45 commit b66335c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
- Drop `setuptools` and `wheel` from the shared libraries. This results in less time consumption when the libraries are
automatically upgraded.
- Allow running `pip` with `pipx run`
- Support PEP 723 run requirements in `pipx run`.
- Support [inline script metadata](https://packaging.python.org/en/latest/specifications/inline-script-metadata/)
in `pipx run`.
- Imply `--include-apps` when running `pipx inject --include-deps`
- Add `--with-suffix` for `pipx inject` command
- `pipx install`: emit a warning when `--force` and `--python` were passed at the same time
Expand Down
5 changes: 3 additions & 2 deletions docs/comparisons.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ virtualenvs with their dependencies installed.

- Both [fades](https://github.com/PyAr/fades#how-to-mark-the-dependencies-to-be-installed) and
[pipx run](examples.md#pipx-run-examples) allow specifying a script's dependencies in specially formatted comments,
but the exact syntax differs (see also [PEP 722](https://peps.python.org/pep-0722/) which seeks to standardize such
syntax).
but the exact syntax differs. (pipx's syntax is standardized by a
[provisional specification](https://packaging.python.org/en/latest/specifications/inline-script-metadata/),
fades's syntax is not standardized.)
- Both tools automatically set up reusable virtualenvs containing the necessary dependencies.
- Both can download Python scripts/packages to execute from remote resources.
- fades can only run individual script files while pipx can also run packages.
Expand Down
8 changes: 4 additions & 4 deletions src/pipx/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,21 @@ def _http_get_request(url: str) -> str:
raise PipxError(str(e)) from e


# This regex comes from PEP 723
PEP723 = re.compile(r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$")
# This regex comes from the inline script metadata spec
INLINE_SCRIPT_METADATA = re.compile(r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$")


def _get_requirements_from_script(content: str) -> Optional[List[str]]:
"""
Supports PEP 723.
Supports inline script metadata.
"""

name = "pyproject"

# Windows is currently getting un-normalized line endings, so normalize
content = content.replace("\r\n", "\n")

matches = [m for m in PEP723.finditer(content) if m.group("type") == name]
matches = [m for m in INLINE_SCRIPT_METADATA.finditer(content) if m.group("type") == name]

if not matches:
return None
Expand Down

0 comments on commit b66335c

Please sign in to comment.