diff --git a/crates/README.md b/crates/README.md index 23e8f3cd2..ca398296c 100644 --- a/crates/README.md +++ b/crates/README.md @@ -4,6 +4,10 @@ Utilities for interacting with Python version numbers and specifiers. +## [pep508-rs](./pep508-rs) + +Utilities for interacting with [PEP 508](https://peps.python.org/pep-0508/) dependency specifiers. + ## [puffin-cli](./puffin-cli) Command-line interface for the Puffin package manager. diff --git a/crates/pep508-rs/.github/workflows/release.yml b/crates/pep508-rs/.github/workflows/release.yml new file mode 100644 index 000000000..bbff151d7 --- /dev/null +++ b/crates/pep508-rs/.github/workflows/release.yml @@ -0,0 +1,177 @@ +name: Release + +on: + push: + tags: + - v* + + +jobs: + crates-io: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@stable + - name: Push to crates.io + env: + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} + run: cargo publish + + macos: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.9 + architecture: x64 + - uses: dtolnay/rust-toolchain@stable + - name: Build wheels - x86_64 + uses: PyO3/maturin-action@v1 + with: + target: x86_64 + args: --release --strip --out dist --sdist + env: + RUSTFLAGS: "-C link-arg=-undefined -C link-arg=dynamic_lookup" + - name: Build wheels - universal2 + uses: PyO3/maturin-action@v1 + with: + args: --release --strip --target --target universal2-apple-darwin --out dist + env: + RUSTFLAGS: "-C link-arg=-undefined -C link-arg=dynamic_lookup" + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + windows: + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.9 + - uses: dtolnay/rust-toolchain@stable + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + args: --release --strip --out dist + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + linux: + runs-on: ubuntu-latest + strategy: + matrix: + target: [ x86_64 ] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.9 + architecture: x64 + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + args: --release --strip --out dist + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + linux-cross: + runs-on: ubuntu-latest + strategy: + matrix: + target: [ aarch64, armv7, s390x, ppc64le, ppc64 ] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.9 + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + rust-toolchain: nightly + target: ${{ matrix.target }} + args: --release --strip --out dist + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + musllinux: + runs-on: ubuntu-latest + strategy: + matrix: + target: + - x86_64-unknown-linux-musl + - i686-unknown-linux-musl + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.9 + architecture: x64 + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + manylinux: musllinux_1_2 + args: --release --strip --out dist + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + musllinux-cross: + runs-on: ubuntu-latest + strategy: + matrix: + platform: + - target: aarch64-unknown-linux-musl + arch: aarch64 + - target: armv7-unknown-linux-musleabihf + arch: armv7 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.9 + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + rust-toolchain: nightly + target: ${{ matrix.platform.target }} + manylinux: musllinux_1_2 + args: --release --strip --out dist + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + release: + name: Release + runs-on: ubuntu-latest + needs: [ macos, windows, linux, linux-cross, musllinux, musllinux-cross ] + environment: release + permissions: + id-token: write + steps: + - uses: actions/download-artifact@v3 + with: + name: wheels + - name: Publish to PyPI + uses: PyO3/maturin-action@v1 + with: + command: upload + args: --skip-existing * diff --git a/crates/pep508-rs/.github/workflows/test.yml b/crates/pep508-rs/.github/workflows/test.yml new file mode 100644 index 000000000..817e676cc --- /dev/null +++ b/crates/pep508-rs/.github/workflows/test.yml @@ -0,0 +1,38 @@ +name: test + +on: [ push, pull_request ] + +env: + CARGO_TERM_COLOR: always + +jobs: + test: + strategy: + matrix: + os: [ ubuntu-latest, windows-latest, macos-latest ] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - run: rustup toolchain install stable --profile minimal + - uses: Swatinem/rust-cache@v2 + - name: Run tests + run: cargo test + + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@stable + with: + components: clippy, rustfmt + - uses: Swatinem/rust-cache@v2 + - name: Ruff + run: pipx run ruff check --format github . + - name: black + run: pipx run black --check . + - name: Rustfmt + run: cargo fmt --all -- --check + - name: Clippy (pure rust) + run: cargo clippy --tests -- -D warnings + - name: Clippy (pyo3) + run: cargo clippy --tests --all-features -- -D warnings diff --git a/crates/pep508-rs/.gitignore b/crates/pep508-rs/.gitignore new file mode 100644 index 000000000..41721ef44 --- /dev/null +++ b/crates/pep508-rs/.gitignore @@ -0,0 +1,6 @@ +.ipynb_checkpoints +.venv +/pypi_analysis/pipy_requires_dist.ndjson +/pypi_analysis/pypi_all.ndjson +__pycache__ +target \ No newline at end of file diff --git a/crates/pep508-rs/Cargo.lock b/crates/pep508-rs/Cargo.lock new file mode 100644 index 000000000..ff39cd9eb --- /dev/null +++ b/crates/pep508-rs/Cargo.lock @@ -0,0 +1,655 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + +[[package]] +name = "arc-swap" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "form_urlencoded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "hashbrown" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" + +[[package]] +name = "idna" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad227c3af19d4914570ad36d30409928b75967c298feb9ea1969db3a610bb14e" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "indoc" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" + +[[package]] +name = "indoc" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8" + +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.148" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" + +[[package]] +name = "lock_api" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memchr" +version = "2.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" + +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pep440_rs" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05bf2c44c4cd12f03b2c3ca095f3aa21f44e43c16021c332e511884719705be" +dependencies = [ + "lazy_static", + "pyo3", + "regex", + "serde", + "unicode-width", +] + +[[package]] +name = "pep508_rs" +version = "0.2.3" +dependencies = [ + "anyhow", + "indoc 2.0.4", + "log", + "once_cell", + "pep440_rs", + "pyo3", + "pyo3-log", + "regex", + "serde", + "serde_json", + "testing_logger", + "thiserror", + "toml", + "tracing", + "unicode-width", + "url", +] + +[[package]] +name = "percent-encoding" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + +[[package]] +name = "proc-macro2" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pyo3" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e681a6cfdc4adcc93b4d3cf993749a4552018ee0a9b65fc0ccfad74352c72a38" +dependencies = [ + "cfg-if", + "indoc 1.0.9", + "libc", + "memoffset", + "parking_lot", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "076c73d0bc438f7a4ef6fdd0c3bb4732149136abd952b110ac93e4edb13a6ba5" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e53cee42e77ebe256066ba8aa77eff722b3bb91f3419177cf4cd0f304d3284d9" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-log" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f47b0777feb17f61eea78667d61103758b243a871edc09a7786500a50467b605" +dependencies = [ + "arc-swap", + "log", + "pyo3", +] + +[[package]] +name = "pyo3-macros" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfeb4c99597e136528c6dd7d5e3de5434d1ceaf487436a3f03b2d56b6fc9efd1" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "947dc12175c254889edc0c02e399476c2f652b4b9ebd123aa655c224de259536" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" + +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", +] + +[[package]] +name = "serde_json" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_spanned" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" +dependencies = [ + "serde", +] + +[[package]] +name = "smallvec" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target-lexicon" +version = "0.12.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a" + +[[package]] +name = "testing_logger" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d92b727cb45d33ae956f7f46b966b25f1bc712092aeef9dba5ac798fc89f720" +dependencies = [ + "log", +] + +[[package]] +name = "thiserror" +version = "1.0.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", +] + +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "toml" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc1433177506450fe920e46a4f9812d0c211f5dd556da10e731a0a3dfa151f0" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca676d9ba1a322c1b64eb8045a5ec5c0cfb0c9d08e15e9ff622589ad5221c8fe" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + +[[package]] +name = "tracing" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +dependencies = [ + "cfg-if", + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", +] + +[[package]] +name = "tracing-core" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "unicode-bidi" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + +[[package]] +name = "unindent" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" + +[[package]] +name = "url" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "winnow" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc" +dependencies = [ + "memchr", +] diff --git a/crates/pep508-rs/Cargo.toml b/crates/pep508-rs/Cargo.toml new file mode 100644 index 000000000..f24820fe7 --- /dev/null +++ b/crates/pep508-rs/Cargo.toml @@ -0,0 +1,49 @@ +[package] +name = "pep508_rs" +version = "0.2.3" +description = "A library for python dependency specifiers, better known as PEP 508" +edition = "2021" +include = ["/src", "Changelog.md", "License-Apache", "License-BSD", "Readme.md", "pyproject.toml"] +# Same license as pypa/packaging where the tests are from +license = "Apache-2.0 OR BSD-2-Clause" +readme = "Readme.md" +repository = "https://github.com/konstin/pep508_rs" + +[dependencies] +anyhow = { version = "1.0.75", optional = true } +once_cell = "1.18.0" +pep440_rs = "0.3.11" +pyo3 = { version = "0.19.2", optional = true, features = ["abi3", "extension-module"] } +pyo3-log = { version = "0.8.3", optional = true } +regex = { version = "1.9.5", default-features = false, features = ["std"] } +serde = { version = "1.0.188", features = ["derive"], optional = true } +serde_json = { version = "1.0.107", optional = true } +thiserror = "1.0.49" +toml = { version = "0.8.1", optional = true } +tracing = { version = "0.1.37", features = ["log"] } +unicode-width = "0.1.11" +url = { version = "2.4.1", features = ["serde"] } + +[dev-dependencies] +indoc = "2.0.4" +log = "0.4.20" +testing_logger = "0.1.1" +serde_json = "1.0.107" + +[features] +pyo3 = ["dep:pyo3", "pep440_rs/pyo3", "pyo3-log"] +serde = ["dep:serde", "pep440_rs/serde"] +modern = ["serde", "toml", "anyhow"] +default = [] + +[lib] +name = "pep508_rs" +crate-type = ["cdylib", "rlib"] + +[profile.release] +debug = true + +[profile.maturin] +inherits = "release" +strip = true + diff --git a/crates/pep508-rs/License-Apache b/crates/pep508-rs/License-Apache new file mode 100644 index 000000000..d9a10c0d8 --- /dev/null +++ b/crates/pep508-rs/License-Apache @@ -0,0 +1,176 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/crates/pep508-rs/License-BSD b/crates/pep508-rs/License-BSD new file mode 100644 index 000000000..76f5704dd --- /dev/null +++ b/crates/pep508-rs/License-BSD @@ -0,0 +1,22 @@ +Copyright (c) 2023 konstin + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/crates/pep508-rs/Readme.md b/crates/pep508-rs/Readme.md new file mode 100644 index 000000000..c8948947a --- /dev/null +++ b/crates/pep508-rs/Readme.md @@ -0,0 +1,68 @@ +# Dependency specifiers (PEP 508) in Rust + +[![Crates.io](https://img.shields.io/crates/v/pep508_rs.svg?logo=rust&style=flat-square)](https://crates.io/crates/pep508_rs) +[![PyPI](https://img.shields.io/pypi/v/pep508_rs.svg?logo=python&style=flat-square)](https://pypi.org/project/pep508_rs) + +A library for python [dependency specifiers](https://packaging.python.org/en/latest/specifications/dependency-specifiers/), better known as [PEP 508](https://peps.python.org/pep-0508/). + +## Usage + +**In Rust** + +```rust +use std::str::FromStr; +use pep508_rs::Requirement; + +let marker = r#"requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8""#; +let dependency_specification = Requirement::from_str(marker).unwrap(); +assert_eq!(dependency_specification.name, "requests"); +assert_eq!(dependency_specification.extras, Some(vec!["security".to_string(), "tests".to_string()])); +``` + +**In Python** + +```python +from pep508_rs import Requirement + +requests = Requirement( + 'requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"' +) +assert requests.name == "requests" +assert requests.extras == ["security", "tests"] +assert [str(i) for i in requests.version_or_url] == [">= 2.8.1", "== 2.8.*"] +``` + +Python bindings are built with [maturin](https://github.com/PyO3/maturin), but you can also use the normal `pip install .` + +`Version` and `VersionSpecifier` from [pep440_rs](https://github.com/konstin/pep440-rs) are reexported to avoid type mismatches. + +## Markers + +Markers allow you to install dependencies only in specific environments (python version, operating system, architecture, etc.) or when a specific feature is activated. E.g. you can say `importlib-metadata ; python_version < "3.8"` or `itsdangerous (>=1.1.0) ; extra == 'security'`. Unfortunately, the marker grammar has some oversights (e.g. ) and the design of comparisons (PEP 440 comparisons with lexicographic fallback) leads to confusing outcomes. This implementation tries to carefully validate everything and emit warnings whenever bogus comparisons with unintended semantics are made. + +In python, warnings are by default sent to the normal python logging infrastructure: + +```python +from pep508_rs import Requirement, MarkerEnvironment + +env = MarkerEnvironment.current() +assert not Requirement("numpy; extra == 'science'").evaluate_markers(env, []) +assert Requirement("numpy; extra == 'science'").evaluate_markers(env, ["science"]) +assert not Requirement( + "numpy; extra == 'science' and extra == 'arrays'" +).evaluate_markers(env, ["science"]) +assert Requirement( + "numpy; extra == 'science' or extra == 'arrays'" +).evaluate_markers(env, ["science"]) +``` + + +```python +from pep508_rs import Requirement, MarkerEnvironment + +env = MarkerEnvironment.current() +Requirement("numpy; python_version >= '3.9.'").evaluate_markers(env, []) +# This will log: +# "Expected PEP 440 version to compare with python_version, found '3.9.', " +# "evaluating to false: Version `3.9.` doesn't match PEP 440 rules" +``` diff --git a/crates/pep508-rs/poetry.lock b/crates/pep508-rs/poetry.lock new file mode 100644 index 000000000..7248ef805 --- /dev/null +++ b/crates/pep508-rs/poetry.lock @@ -0,0 +1,580 @@ +# This file is automatically @generated by Poetry 1.5.0 and should not be changed by hand. + +[[package]] +name = "appnope" +version = "0.1.3" +description = "Disable App Nap on macOS >= 10.9" +optional = false +python-versions = "*" +files = [ + {file = "appnope-0.1.3-py2.py3-none-any.whl", hash = "sha256:265a455292d0bd8a72453494fa24df5a11eb18373a60c7c0430889f22548605e"}, + {file = "appnope-0.1.3.tar.gz", hash = "sha256:02bd91c4de869fbb1e1c50aafc4098827a7a54ab2f39d9dcba6c9547ed920e24"}, +] + +[[package]] +name = "backcall" +version = "0.2.0" +description = "Specifications for callback functions passed in to an API" +optional = false +python-versions = "*" +files = [ + {file = "backcall-0.2.0-py2.py3-none-any.whl", hash = "sha256:fbbce6a29f263178a1f7915c1940bde0ec2b2a967566fe1c65c1dfb7422bd255"}, + {file = "backcall-0.2.0.tar.gz", hash = "sha256:5cbdbf27be5e7cfadb448baf0aa95508f91f2bbc6c6437cd9cd06e2a4c215e1e"}, +] + +[[package]] +name = "black" +version = "23.3.0" +description = "The uncompromising code formatter." +optional = false +python-versions = ">=3.7" +files = [ + {file = "black-23.3.0-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:0945e13506be58bf7db93ee5853243eb368ace1c08a24c65ce108986eac65915"}, + {file = "black-23.3.0-cp310-cp310-macosx_10_16_universal2.whl", hash = "sha256:67de8d0c209eb5b330cce2469503de11bca4085880d62f1628bd9972cc3366b9"}, + {file = "black-23.3.0-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:7c3eb7cea23904399866c55826b31c1f55bbcd3890ce22ff70466b907b6775c2"}, + {file = "black-23.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:32daa9783106c28815d05b724238e30718f34155653d4d6e125dc7daec8e260c"}, + {file = "black-23.3.0-cp310-cp310-win_amd64.whl", hash = "sha256:35d1381d7a22cc5b2be2f72c7dfdae4072a3336060635718cc7e1ede24221d6c"}, + {file = "black-23.3.0-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:a8a968125d0a6a404842fa1bf0b349a568634f856aa08ffaff40ae0dfa52e7c6"}, + {file = "black-23.3.0-cp311-cp311-macosx_10_16_universal2.whl", hash = "sha256:c7ab5790333c448903c4b721b59c0d80b11fe5e9803d8703e84dcb8da56fec1b"}, + {file = "black-23.3.0-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:a6f6886c9869d4daae2d1715ce34a19bbc4b95006d20ed785ca00fa03cba312d"}, + {file = "black-23.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6f3c333ea1dd6771b2d3777482429864f8e258899f6ff05826c3a4fcc5ce3f70"}, + {file = "black-23.3.0-cp311-cp311-win_amd64.whl", hash = "sha256:11c410f71b876f961d1de77b9699ad19f939094c3a677323f43d7a29855fe326"}, + {file = "black-23.3.0-cp37-cp37m-macosx_10_16_x86_64.whl", hash = "sha256:1d06691f1eb8de91cd1b322f21e3bfc9efe0c7ca1f0e1eb1db44ea367dff656b"}, + {file = "black-23.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:50cb33cac881766a5cd9913e10ff75b1e8eb71babf4c7104f2e9c52da1fb7de2"}, + {file = "black-23.3.0-cp37-cp37m-win_amd64.whl", hash = "sha256:e114420bf26b90d4b9daa597351337762b63039752bdf72bf361364c1aa05925"}, + {file = "black-23.3.0-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:48f9d345675bb7fbc3dd85821b12487e1b9a75242028adad0333ce36ed2a6d27"}, + {file = "black-23.3.0-cp38-cp38-macosx_10_16_universal2.whl", hash = "sha256:714290490c18fb0126baa0fca0a54ee795f7502b44177e1ce7624ba1c00f2331"}, + {file = "black-23.3.0-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:064101748afa12ad2291c2b91c960be28b817c0c7eaa35bec09cc63aa56493c5"}, + {file = "black-23.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:562bd3a70495facf56814293149e51aa1be9931567474993c7942ff7d3533961"}, + {file = "black-23.3.0-cp38-cp38-win_amd64.whl", hash = "sha256:e198cf27888ad6f4ff331ca1c48ffc038848ea9f031a3b40ba36aced7e22f2c8"}, + {file = "black-23.3.0-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:3238f2aacf827d18d26db07524e44741233ae09a584273aa059066d644ca7b30"}, + {file = "black-23.3.0-cp39-cp39-macosx_10_16_universal2.whl", hash = "sha256:f0bd2f4a58d6666500542b26354978218a9babcdc972722f4bf90779524515f3"}, + {file = "black-23.3.0-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:92c543f6854c28a3c7f39f4d9b7694f9a6eb9d3c5e2ece488c327b6e7ea9b266"}, + {file = "black-23.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3a150542a204124ed00683f0db1f5cf1c2aaaa9cc3495b7a3b5976fb136090ab"}, + {file = "black-23.3.0-cp39-cp39-win_amd64.whl", hash = "sha256:6b39abdfb402002b8a7d030ccc85cf5afff64ee90fa4c5aebc531e3ad0175ddb"}, + {file = "black-23.3.0-py3-none-any.whl", hash = "sha256:ec751418022185b0c1bb7d7736e6933d40bbb14c14a0abcf9123d1b159f98dd4"}, + {file = "black-23.3.0.tar.gz", hash = "sha256:1c7b8d606e728a41ea1ccbd7264677e494e87cf630e399262ced92d4a8dac940"}, +] + +[package.dependencies] +click = ">=8.0.0" +ipython = {version = ">=7.8.0", optional = true, markers = "extra == \"jupyter\""} +mypy-extensions = ">=0.4.3" +packaging = ">=22.0" +pathspec = ">=0.9.0" +platformdirs = ">=2" +tokenize-rt = {version = ">=3.2.0", optional = true, markers = "extra == \"jupyter\""} +tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} +typed-ast = {version = ">=1.4.2", markers = "python_version < \"3.8\" and implementation_name == \"cpython\""} +typing-extensions = {version = ">=3.10.0.0", markers = "python_version < \"3.10\""} + +[package.extras] +colorama = ["colorama (>=0.4.3)"] +d = ["aiohttp (>=3.7.4)"] +jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] +uvloop = ["uvloop (>=0.15.2)"] + +[[package]] +name = "click" +version = "8.1.3" +description = "Composable command line interface toolkit" +optional = false +python-versions = ">=3.7" +files = [ + {file = "click-8.1.3-py3-none-any.whl", hash = "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"}, + {file = "click-8.1.3.tar.gz", hash = "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e"}, +] + +[package.dependencies] +colorama = {version = "*", markers = "platform_system == \"Windows\""} +importlib-metadata = {version = "*", markers = "python_version < \"3.8\""} + +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "decorator" +version = "5.1.1" +description = "Decorators for Humans" +optional = false +python-versions = ">=3.5" +files = [ + {file = "decorator-5.1.1-py3-none-any.whl", hash = "sha256:b8c3f85900b9dc423225913c5aace94729fe1fa9763b38939a95226f02d37186"}, + {file = "decorator-5.1.1.tar.gz", hash = "sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330"}, +] + +[[package]] +name = "exceptiongroup" +version = "1.1.1" +description = "Backport of PEP 654 (exception groups)" +optional = false +python-versions = ">=3.7" +files = [ + {file = "exceptiongroup-1.1.1-py3-none-any.whl", hash = "sha256:232c37c63e4f682982c8b6459f33a8981039e5fb8756b2074364e5055c498c9e"}, + {file = "exceptiongroup-1.1.1.tar.gz", hash = "sha256:d484c3090ba2889ae2928419117447a14daf3c1231d5e30d0aae34f354f01785"}, +] + +[package.extras] +test = ["pytest (>=6)"] + +[[package]] +name = "importlib-metadata" +version = "6.6.0" +description = "Read metadata from Python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "importlib_metadata-6.6.0-py3-none-any.whl", hash = "sha256:43dd286a2cd8995d5eaef7fee2066340423b818ed3fd70adf0bad5f1fac53fed"}, + {file = "importlib_metadata-6.6.0.tar.gz", hash = "sha256:92501cdf9cc66ebd3e612f1b4f0c0765dfa42f0fa38ffb319b6bd84dd675d705"}, +] + +[package.dependencies] +typing-extensions = {version = ">=3.6.4", markers = "python_version < \"3.8\""} +zipp = ">=0.5" + +[package.extras] +docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-lint"] +perf = ["ipython"] +testing = ["flake8 (<5)", "flufl.flake8", "importlib-resources (>=1.3)", "packaging", "pyfakefs", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)", "pytest-perf (>=0.9.2)"] + +[[package]] +name = "iniconfig" +version = "2.0.0" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.7" +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + +[[package]] +name = "ipython" +version = "7.34.0" +description = "IPython: Productive Interactive Computing" +optional = false +python-versions = ">=3.7" +files = [ + {file = "ipython-7.34.0-py3-none-any.whl", hash = "sha256:c175d2440a1caff76116eb719d40538fbb316e214eda85c5515c303aacbfb23e"}, + {file = "ipython-7.34.0.tar.gz", hash = "sha256:af3bdb46aa292bce5615b1b2ebc76c2080c5f77f54bda2ec72461317273e7cd6"}, +] + +[package.dependencies] +appnope = {version = "*", markers = "sys_platform == \"darwin\""} +backcall = "*" +colorama = {version = "*", markers = "sys_platform == \"win32\""} +decorator = "*" +jedi = ">=0.16" +matplotlib-inline = "*" +pexpect = {version = ">4.3", markers = "sys_platform != \"win32\""} +pickleshare = "*" +prompt-toolkit = ">=2.0.0,<3.0.0 || >3.0.0,<3.0.1 || >3.0.1,<3.1.0" +pygments = "*" +setuptools = ">=18.5" +traitlets = ">=4.2" + +[package.extras] +all = ["Sphinx (>=1.3)", "ipykernel", "ipyparallel", "ipywidgets", "nbconvert", "nbformat", "nose (>=0.10.1)", "notebook", "numpy (>=1.17)", "pygments", "qtconsole", "requests", "testpath"] +doc = ["Sphinx (>=1.3)"] +kernel = ["ipykernel"] +nbconvert = ["nbconvert"] +nbformat = ["nbformat"] +notebook = ["ipywidgets", "notebook"] +parallel = ["ipyparallel"] +qtconsole = ["qtconsole"] +test = ["ipykernel", "nbformat", "nose (>=0.10.1)", "numpy (>=1.17)", "pygments", "requests", "testpath"] + +[[package]] +name = "jedi" +version = "0.18.2" +description = "An autocompletion tool for Python that can be used for text editors." +optional = false +python-versions = ">=3.6" +files = [ + {file = "jedi-0.18.2-py2.py3-none-any.whl", hash = "sha256:203c1fd9d969ab8f2119ec0a3342e0b49910045abe6af0a3ae83a5764d54639e"}, + {file = "jedi-0.18.2.tar.gz", hash = "sha256:bae794c30d07f6d910d32a7048af09b5a39ed740918da923c6b780790ebac612"}, +] + +[package.dependencies] +parso = ">=0.8.0,<0.9.0" + +[package.extras] +docs = ["Jinja2 (==2.11.3)", "MarkupSafe (==1.1.1)", "Pygments (==2.8.1)", "alabaster (==0.7.12)", "babel (==2.9.1)", "chardet (==4.0.0)", "commonmark (==0.8.1)", "docutils (==0.17.1)", "future (==0.18.2)", "idna (==2.10)", "imagesize (==1.2.0)", "mock (==1.0.1)", "packaging (==20.9)", "pyparsing (==2.4.7)", "pytz (==2021.1)", "readthedocs-sphinx-ext (==2.1.4)", "recommonmark (==0.5.0)", "requests (==2.25.1)", "six (==1.15.0)", "snowballstemmer (==2.1.0)", "sphinx (==1.8.5)", "sphinx-rtd-theme (==0.4.3)", "sphinxcontrib-serializinghtml (==1.1.4)", "sphinxcontrib-websupport (==1.2.4)", "urllib3 (==1.26.4)"] +qa = ["flake8 (==3.8.3)", "mypy (==0.782)"] +testing = ["Django (<3.1)", "attrs", "colorama", "docopt", "pytest (<7.0.0)"] + +[[package]] +name = "matplotlib-inline" +version = "0.1.6" +description = "Inline Matplotlib backend for Jupyter" +optional = false +python-versions = ">=3.5" +files = [ + {file = "matplotlib-inline-0.1.6.tar.gz", hash = "sha256:f887e5f10ba98e8d2b150ddcf4702c1e5f8b3a20005eb0f74bfdbd360ee6f304"}, + {file = "matplotlib_inline-0.1.6-py3-none-any.whl", hash = "sha256:f1f41aab5328aa5aaea9b16d083b128102f8712542f819fe7e6a420ff581b311"}, +] + +[package.dependencies] +traitlets = "*" + +[[package]] +name = "maturin" +version = "1.0.1" +description = "Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "maturin-1.0.1-py3-none-linux_armv6l.whl", hash = "sha256:10097e2602330c0b9db16d7dfd002476f5e5cf99df58ba2f3abc6de64a69e9a6"}, + {file = "maturin-1.0.1-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:9ecebccb111c9c870fb2f5eee17518fe106f676227bb16f204a51e7a162aceec"}, + {file = "maturin-1.0.1-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:b39f9a42b3c8242e3f3ab990bd03ba989c6c07e4de9e21fcf877a2418119d445"}, + {file = "maturin-1.0.1-py3-none-manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686.whl", hash = "sha256:c0b1efa47f8b7d15bc5945159764ce57316f9d1bfb7c8caa07cebdd41318359b"}, + {file = "maturin-1.0.1-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl", hash = "sha256:d392ec0578d9e6f03914837cef7bbb264d5708807e0b48176b6ff0b50083ba7c"}, + {file = "maturin-1.0.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl", hash = "sha256:d271b24febbfc020561984b1acdfc39b132df21f4e42d7af0fe274ea738c8000"}, + {file = "maturin-1.0.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.musllinux_1_1_armv7l.whl", hash = "sha256:8d88d1595d7514c27df96d5f4fe3dc5f24288528a746439403f27c3b448fca16"}, + {file = "maturin-1.0.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.musllinux_1_1_ppc64le.whl", hash = "sha256:04c0279dd0d6ccd317018bd1a43f52cbda715822537ae1a68015c9171f18b2fd"}, + {file = "maturin-1.0.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:787bb56c80eda482ece2dd4788d479dbd0e74d981b2e2c538228365c19290fb7"}, + {file = "maturin-1.0.1-py3-none-win32.whl", hash = "sha256:6d9b4ff7c2d501e91886b859296f5c0478fc08bc7d537a72f98a69d51ff4f519"}, + {file = "maturin-1.0.1-py3-none-win_amd64.whl", hash = "sha256:2907b345186a83db4bbe5571830509b3031784d08958b32d2ffa7857bd473725"}, + {file = "maturin-1.0.1-py3-none-win_arm64.whl", hash = "sha256:6b020b9abbd1e9fef468c171216dc4be053834b5bf638075264ee090a993b0b0"}, + {file = "maturin-1.0.1.tar.gz", hash = "sha256:71fdb2dbbd5bcc60bd91ddcbe34dba9f04cc53c2add089a95a79d0d8fc8337b8"}, +] + +[package.dependencies] +tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} + +[package.extras] +patchelf = ["patchelf"] +zig = ["ziglang (>=0.10.0,<0.11.0)"] + +[[package]] +name = "mypy-extensions" +version = "1.0.0" +description = "Type system extensions for programs checked with the mypy type checker." +optional = false +python-versions = ">=3.5" +files = [ + {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"}, + {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, +] + +[[package]] +name = "packaging" +version = "23.1" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "packaging-23.1-py3-none-any.whl", hash = "sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61"}, + {file = "packaging-23.1.tar.gz", hash = "sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f"}, +] + +[[package]] +name = "parso" +version = "0.8.3" +description = "A Python Parser" +optional = false +python-versions = ">=3.6" +files = [ + {file = "parso-0.8.3-py2.py3-none-any.whl", hash = "sha256:c001d4636cd3aecdaf33cbb40aebb59b094be2a74c556778ef5576c175e19e75"}, + {file = "parso-0.8.3.tar.gz", hash = "sha256:8c07be290bb59f03588915921e29e8a50002acaf2cdc5fa0e0114f91709fafa0"}, +] + +[package.extras] +qa = ["flake8 (==3.8.3)", "mypy (==0.782)"] +testing = ["docopt", "pytest (<6.0.0)"] + +[[package]] +name = "pathspec" +version = "0.11.1" +description = "Utility library for gitignore style pattern matching of file paths." +optional = false +python-versions = ">=3.7" +files = [ + {file = "pathspec-0.11.1-py3-none-any.whl", hash = "sha256:d8af70af76652554bd134c22b3e8a1cc46ed7d91edcdd721ef1a0c51a84a5293"}, + {file = "pathspec-0.11.1.tar.gz", hash = "sha256:2798de800fa92780e33acca925945e9a19a133b715067cf165b8866c15a31687"}, +] + +[[package]] +name = "pexpect" +version = "4.8.0" +description = "Pexpect allows easy control of interactive console applications." +optional = false +python-versions = "*" +files = [ + {file = "pexpect-4.8.0-py2.py3-none-any.whl", hash = "sha256:0b48a55dcb3c05f3329815901ea4fc1537514d6ba867a152b581d69ae3710937"}, + {file = "pexpect-4.8.0.tar.gz", hash = "sha256:fc65a43959d153d0114afe13997d439c22823a27cefceb5ff35c2178c6784c0c"}, +] + +[package.dependencies] +ptyprocess = ">=0.5" + +[[package]] +name = "pickleshare" +version = "0.7.5" +description = "Tiny 'shelve'-like database with concurrency support" +optional = false +python-versions = "*" +files = [ + {file = "pickleshare-0.7.5-py2.py3-none-any.whl", hash = "sha256:9649af414d74d4df115d5d718f82acb59c9d418196b7b4290ed47a12ce62df56"}, + {file = "pickleshare-0.7.5.tar.gz", hash = "sha256:87683d47965c1da65cdacaf31c8441d12b8044cdec9aca500cd78fc2c683afca"}, +] + +[[package]] +name = "platformdirs" +version = "3.5.1" +description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." +optional = false +python-versions = ">=3.7" +files = [ + {file = "platformdirs-3.5.1-py3-none-any.whl", hash = "sha256:e2378146f1964972c03c085bb5662ae80b2b8c06226c54b2ff4aa9483e8a13a5"}, + {file = "platformdirs-3.5.1.tar.gz", hash = "sha256:412dae91f52a6f84830f39a8078cecd0e866cb72294a5c66808e74d5e88d251f"}, +] + +[package.dependencies] +typing-extensions = {version = ">=4.5", markers = "python_version < \"3.8\""} + +[package.extras] +docs = ["furo (>=2023.3.27)", "proselint (>=0.13)", "sphinx (>=6.2.1)", "sphinx-autodoc-typehints (>=1.23,!=1.23.4)"] +test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.3.1)", "pytest-cov (>=4)", "pytest-mock (>=3.10)"] + +[[package]] +name = "pluggy" +version = "1.0.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.6" +files = [ + {file = "pluggy-1.0.0-py2.py3-none-any.whl", hash = "sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3"}, + {file = "pluggy-1.0.0.tar.gz", hash = "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159"}, +] + +[package.dependencies] +importlib-metadata = {version = ">=0.12", markers = "python_version < \"3.8\""} + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["pytest", "pytest-benchmark"] + +[[package]] +name = "prompt-toolkit" +version = "3.0.38" +description = "Library for building powerful interactive command lines in Python" +optional = false +python-versions = ">=3.7.0" +files = [ + {file = "prompt_toolkit-3.0.38-py3-none-any.whl", hash = "sha256:45ea77a2f7c60418850331366c81cf6b5b9cf4c7fd34616f733c5427e6abbb1f"}, + {file = "prompt_toolkit-3.0.38.tar.gz", hash = "sha256:23ac5d50538a9a38c8bde05fecb47d0b403ecd0662857a86f886f798563d5b9b"}, +] + +[package.dependencies] +wcwidth = "*" + +[[package]] +name = "ptyprocess" +version = "0.7.0" +description = "Run a subprocess in a pseudo terminal" +optional = false +python-versions = "*" +files = [ + {file = "ptyprocess-0.7.0-py2.py3-none-any.whl", hash = "sha256:4b41f3967fce3af57cc7e94b888626c18bf37a083e3651ca8feeb66d492fef35"}, + {file = "ptyprocess-0.7.0.tar.gz", hash = "sha256:5c5d0a3b48ceee0b48485e0c26037c0acd7d29765ca3fbb5cb3831d347423220"}, +] + +[[package]] +name = "pygments" +version = "2.15.1" +description = "Pygments is a syntax highlighting package written in Python." +optional = false +python-versions = ">=3.7" +files = [ + {file = "Pygments-2.15.1-py3-none-any.whl", hash = "sha256:db2db3deb4b4179f399a09054b023b6a586b76499d36965813c71aa8ed7b5fd1"}, + {file = "Pygments-2.15.1.tar.gz", hash = "sha256:8ace4d3c1dd481894b2005f560ead0f9f19ee64fe983366be1a21e171d12775c"}, +] + +[package.extras] +plugins = ["importlib-metadata"] + +[[package]] +name = "pytest" +version = "7.3.1" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.7" +files = [ + {file = "pytest-7.3.1-py3-none-any.whl", hash = "sha256:3799fa815351fea3a5e96ac7e503a96fa51cc9942c3753cda7651b93c1cfa362"}, + {file = "pytest-7.3.1.tar.gz", hash = "sha256:434afafd78b1d78ed0addf160ad2b77a30d35d4bdf8af234fe621919d9ed15e3"}, +] + +[package.dependencies] +colorama = {version = "*", markers = "sys_platform == \"win32\""} +exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""} +importlib-metadata = {version = ">=0.12", markers = "python_version < \"3.8\""} +iniconfig = "*" +packaging = "*" +pluggy = ">=0.12,<2.0" +tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} + +[package.extras] +testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] + +[[package]] +name = "ruff" +version = "0.0.270" +description = "An extremely fast Python linter, written in Rust." +optional = false +python-versions = ">=3.7" +files = [ + {file = "ruff-0.0.270-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:f74c4d550f7b8e808455ac77bbce38daafc458434815ba0bc21ae4bdb276509b"}, + {file = "ruff-0.0.270-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:643de865fd35cb76c4f0739aea5afe7b8e4d40d623df7e9e6ea99054e5cead0a"}, + {file = "ruff-0.0.270-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:eca02e709b3308eb7255b5f74e779be23b5980fca3862eae28bb23069cd61ae4"}, + {file = "ruff-0.0.270-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:3ed3b198768d2b3a2300fb18f730cd39948a5cc36ba29ae9d4639a11040880be"}, + {file = "ruff-0.0.270-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:739495d2dbde87cf4e3110c8d27bc20febf93112539a968a4e02c26f0deccd1d"}, + {file = "ruff-0.0.270-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:08188f8351f4c0b6216e8463df0a76eb57894ca59a3da65e4ed205db980fd3ae"}, + {file = "ruff-0.0.270-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:0827b074635d37984fc98d99316bfab5c8b1231bb83e60dacc83bd92883eedb4"}, + {file = "ruff-0.0.270-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:0d61ae4841313f6eeb8292dc349bef27b4ce426e62c36e80ceedc3824e408734"}, + {file = "ruff-0.0.270-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0eb412f20e77529a01fb94d578b19dcb8331b56f93632aa0cce4a2ea27b7aeba"}, + {file = "ruff-0.0.270-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:b775e2c5fc869359daf8c8b8aa0fd67240201ab2e8d536d14a0edf279af18786"}, + {file = "ruff-0.0.270-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:21f00e47ab2308617c44435c8dfd9e2e03897461c9e647ec942deb2a235b4cfd"}, + {file = "ruff-0.0.270-py3-none-musllinux_1_2_i686.whl", hash = "sha256:0bbfbf6fd2436165566ca85f6e57be03ed2f0a994faf40180cfbb3604c9232ef"}, + {file = "ruff-0.0.270-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:8af391ef81f7be960be10886a3c1aac0b298bde7cb9a86ec2b05faeb2081ce6b"}, + {file = "ruff-0.0.270-py3-none-win32.whl", hash = "sha256:b4c037fe2f75bcd9aed0c89c7c507cb7fa59abae2bd4c8b6fc331a28178655a4"}, + {file = "ruff-0.0.270-py3-none-win_amd64.whl", hash = "sha256:0012f9b7dc137ab7f1f0355e3c4ca49b562baf6c9fa1180948deeb6648c52957"}, + {file = "ruff-0.0.270-py3-none-win_arm64.whl", hash = "sha256:9613456b0b375766244c25045e353bc8890c856431cd97893c97b10cc93bd28d"}, + {file = "ruff-0.0.270.tar.gz", hash = "sha256:95db07b7850b30ebf32b27fe98bc39e0ab99db3985edbbf0754d399eb2f0e690"}, +] + +[[package]] +name = "setuptools" +version = "67.8.0" +description = "Easily download, build, install, upgrade, and uninstall Python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "setuptools-67.8.0-py3-none-any.whl", hash = "sha256:5df61bf30bb10c6f756eb19e7c9f3b473051f48db77fddbe06ff2ca307df9a6f"}, + {file = "setuptools-67.8.0.tar.gz", hash = "sha256:62642358adc77ffa87233bc4d2354c4b2682d214048f500964dbe760ccedf102"}, +] + +[package.extras] +docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-favicon", "sphinx-hoverxref (<2)", "sphinx-inline-tabs", "sphinx-lint", "sphinx-notfound-page (==0.8.3)", "sphinx-reredirects", "sphinxcontrib-towncrier"] +testing = ["build[virtualenv]", "filelock (>=3.4.0)", "flake8-2020", "ini2toml[lite] (>=0.9)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pip (>=19.1)", "pip-run (>=8.8)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-mypy (>=0.9.1)", "pytest-perf", "pytest-ruff", "pytest-timeout", "pytest-xdist", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"] +testing-integration = ["build[virtualenv]", "filelock (>=3.4.0)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pytest", "pytest-enabler", "pytest-xdist", "tomli", "virtualenv (>=13.0.0)", "wheel"] + +[[package]] +name = "tokenize-rt" +version = "5.0.0" +description = "A wrapper around the stdlib `tokenize` which roundtrips." +optional = false +python-versions = ">=3.7" +files = [ + {file = "tokenize_rt-5.0.0-py2.py3-none-any.whl", hash = "sha256:c67772c662c6b3dc65edf66808577968fb10badfc2042e3027196bed4daf9e5a"}, + {file = "tokenize_rt-5.0.0.tar.gz", hash = "sha256:3160bc0c3e8491312d0485171dea861fc160a240f5f5766b72a1165408d10740"}, +] + +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] + +[[package]] +name = "traitlets" +version = "5.9.0" +description = "Traitlets Python configuration system" +optional = false +python-versions = ">=3.7" +files = [ + {file = "traitlets-5.9.0-py3-none-any.whl", hash = "sha256:9e6ec080259b9a5940c797d58b613b5e31441c2257b87c2e795c5228ae80d2d8"}, + {file = "traitlets-5.9.0.tar.gz", hash = "sha256:f6cde21a9c68cf756af02035f72d5a723bf607e862e7be33ece505abf4a3bad9"}, +] + +[package.extras] +docs = ["myst-parser", "pydata-sphinx-theme", "sphinx"] +test = ["argcomplete (>=2.0)", "pre-commit", "pytest", "pytest-mock"] + +[[package]] +name = "typed-ast" +version = "1.5.4" +description = "a fork of Python 2 and 3 ast modules with type comment support" +optional = false +python-versions = ">=3.6" +files = [ + {file = "typed_ast-1.5.4-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:669dd0c4167f6f2cd9f57041e03c3c2ebf9063d0757dc89f79ba1daa2bfca9d4"}, + {file = "typed_ast-1.5.4-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:211260621ab1cd7324e0798d6be953d00b74e0428382991adfddb352252f1d62"}, + {file = "typed_ast-1.5.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:267e3f78697a6c00c689c03db4876dd1efdfea2f251a5ad6555e82a26847b4ac"}, + {file = "typed_ast-1.5.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:c542eeda69212fa10a7ada75e668876fdec5f856cd3d06829e6aa64ad17c8dfe"}, + {file = "typed_ast-1.5.4-cp310-cp310-win_amd64.whl", hash = "sha256:a9916d2bb8865f973824fb47436fa45e1ebf2efd920f2b9f99342cb7fab93f72"}, + {file = "typed_ast-1.5.4-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:79b1e0869db7c830ba6a981d58711c88b6677506e648496b1f64ac7d15633aec"}, + {file = "typed_ast-1.5.4-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a94d55d142c9265f4ea46fab70977a1944ecae359ae867397757d836ea5a3f47"}, + {file = "typed_ast-1.5.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:183afdf0ec5b1b211724dfef3d2cad2d767cbefac291f24d69b00546c1837fb6"}, + {file = "typed_ast-1.5.4-cp36-cp36m-win_amd64.whl", hash = "sha256:639c5f0b21776605dd6c9dbe592d5228f021404dafd377e2b7ac046b0349b1a1"}, + {file = "typed_ast-1.5.4-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:cf4afcfac006ece570e32d6fa90ab74a17245b83dfd6655a6f68568098345ff6"}, + {file = "typed_ast-1.5.4-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ed855bbe3eb3715fca349c80174cfcfd699c2f9de574d40527b8429acae23a66"}, + {file = "typed_ast-1.5.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:6778e1b2f81dfc7bc58e4b259363b83d2e509a65198e85d5700dfae4c6c8ff1c"}, + {file = "typed_ast-1.5.4-cp37-cp37m-win_amd64.whl", hash = "sha256:0261195c2062caf107831e92a76764c81227dae162c4f75192c0d489faf751a2"}, + {file = "typed_ast-1.5.4-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:2efae9db7a8c05ad5547d522e7dbe62c83d838d3906a3716d1478b6c1d61388d"}, + {file = "typed_ast-1.5.4-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:7d5d014b7daa8b0bf2eaef684295acae12b036d79f54178b92a2b6a56f92278f"}, + {file = "typed_ast-1.5.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:370788a63915e82fd6f212865a596a0fefcbb7d408bbbb13dea723d971ed8bdc"}, + {file = "typed_ast-1.5.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:4e964b4ff86550a7a7d56345c7864b18f403f5bd7380edf44a3c1fb4ee7ac6c6"}, + {file = "typed_ast-1.5.4-cp38-cp38-win_amd64.whl", hash = "sha256:683407d92dc953c8a7347119596f0b0e6c55eb98ebebd9b23437501b28dcbb8e"}, + {file = "typed_ast-1.5.4-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:4879da6c9b73443f97e731b617184a596ac1235fe91f98d279a7af36c796da35"}, + {file = "typed_ast-1.5.4-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:3e123d878ba170397916557d31c8f589951e353cc95fb7f24f6bb69adc1a8a97"}, + {file = "typed_ast-1.5.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ebd9d7f80ccf7a82ac5f88c521115cc55d84e35bf8b446fcd7836eb6b98929a3"}, + {file = "typed_ast-1.5.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:98f80dee3c03455e92796b58b98ff6ca0b2a6f652120c263efdba4d6c5e58f72"}, + {file = "typed_ast-1.5.4-cp39-cp39-win_amd64.whl", hash = "sha256:0fdbcf2fef0ca421a3f5912555804296f0b0960f0418c440f5d6d3abb549f3e1"}, + {file = "typed_ast-1.5.4.tar.gz", hash = "sha256:39e21ceb7388e4bb37f4c679d72707ed46c2fbf2a5609b8b8ebc4b067d977df2"}, +] + +[[package]] +name = "typing-extensions" +version = "4.6.2" +description = "Backported and Experimental Type Hints for Python 3.7+" +optional = false +python-versions = ">=3.7" +files = [ + {file = "typing_extensions-4.6.2-py3-none-any.whl", hash = "sha256:3a8b36f13dd5fdc5d1b16fe317f5668545de77fa0b8e02006381fd49d731ab98"}, + {file = "typing_extensions-4.6.2.tar.gz", hash = "sha256:06006244c70ac8ee83fa8282cb188f697b8db25bc8b4df07be1873c43897060c"}, +] + +[[package]] +name = "wcwidth" +version = "0.2.6" +description = "Measures the displayed width of unicode strings in a terminal" +optional = false +python-versions = "*" +files = [ + {file = "wcwidth-0.2.6-py2.py3-none-any.whl", hash = "sha256:795b138f6875577cd91bba52baf9e445cd5118fd32723b460e30a0af30ea230e"}, + {file = "wcwidth-0.2.6.tar.gz", hash = "sha256:a5220780a404dbe3353789870978e472cfe477761f06ee55077256e509b156d0"}, +] + +[[package]] +name = "zipp" +version = "3.15.0" +description = "Backport of pathlib-compatible object wrapper for zip files" +optional = false +python-versions = ">=3.7" +files = [ + {file = "zipp-3.15.0-py3-none-any.whl", hash = "sha256:48904fc76a60e542af151aded95726c1a5c34ed43ab4134b597665c86d7ad556"}, + {file = "zipp-3.15.0.tar.gz", hash = "sha256:112929ad649da941c23de50f356a2b5570c954b65150642bccdd66bf194d224b"}, +] + +[package.extras] +docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-lint"] +testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more-itertools", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)"] + +[metadata] +lock-version = "2.0" +python-versions = ">=3.7" +content-hash = "f64653737d40fcf3c7b9f9fea186e98322fea9e7ee1889b8f8ee181570b26e17" diff --git a/crates/pep508-rs/pyproject.toml b/crates/pep508-rs/pyproject.toml new file mode 100644 index 000000000..ec9ee608b --- /dev/null +++ b/crates/pep508-rs/pyproject.toml @@ -0,0 +1,32 @@ +[project] +name = "pep508_rs" +version = "0.2.2" +description = "A library for python dependency specifiers, better known as PEP 508" +readme = "Readme.md" + +[tool.poetry] +name = "pep508_rs" +version = "0.1.1" +description = "" +authors = ["konstin "] +readme = "Readme.md" + +[tool.poetry.dependencies] +python = ">=3.7" + +[tool.poetry.group.dev.dependencies] +black = { extras = ["jupyter"], version = "^23.1.0" } +maturin = "^1.0.0" +pytest = "^7.2.0" +ruff = "^0.0.270" + +[tool.maturin] +features = ["pyo3"] + +[tool.pytest.ini_options] +minversion = "7.2.0" +addopts = "--tb=short" + +[build-system] +requires = ["maturin>=1.0,<2.0"] +build-backend = "maturin" diff --git a/crates/pep508-rs/src/lib.rs b/crates/pep508-rs/src/lib.rs new file mode 100644 index 000000000..d1d50e5cd --- /dev/null +++ b/crates/pep508-rs/src/lib.rs @@ -0,0 +1,1420 @@ +//! A library for python [dependency specifiers](https://packaging.python.org/en/latest/specifications/dependency-specifiers/) +//! better known as [PEP 508](https://peps.python.org/pep-0508/) +//! +//! ## Usage +//! +//! ``` +//! use std::str::FromStr; +//! use pep508_rs::Requirement; +//! +//! let marker = r#"requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8""#; +//! let dependency_specification = Requirement::from_str(marker).unwrap(); +//! assert_eq!(dependency_specification.name, "requests"); +//! assert_eq!(dependency_specification.extras, Some(vec!["security".to_string(), "tests".to_string()])); +//! ``` + +#![deny(missing_docs)] + +mod marker; +#[cfg(feature = "modern")] +pub mod modern; + +pub use marker::{ + MarkerEnvironment, MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, + MarkerValueString, MarkerValueVersion, MarkerWarningKind, StringVersion, +}; +#[cfg(feature = "pyo3")] +use pep440_rs::PyVersion; +use pep440_rs::{Version, VersionSpecifier, VersionSpecifiers}; +#[cfg(feature = "pyo3")] +use pyo3::{ + basic::CompareOp, create_exception, exceptions::PyNotImplementedError, pyclass, pymethods, + pymodule, types::PyModule, IntoPy, PyObject, PyResult, Python, +}; +#[cfg(feature = "serde")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +#[cfg(feature = "pyo3")] +use std::collections::hash_map::DefaultHasher; +use std::collections::HashSet; +use std::fmt::{Display, Formatter}; +#[cfg(feature = "pyo3")] +use std::hash::{Hash, Hasher}; +use std::str::{Chars, FromStr}; +use thiserror::Error; +use unicode_width::UnicodeWidthStr; +use url::Url; + +/// Error with a span attached. Not that those aren't `String` but `Vec` indices. +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Pep508Error { + /// Either we have an error string from our parser or an upstream error from `url` + pub message: Pep508ErrorSource, + /// Span start index + pub start: usize, + /// Span length + pub len: usize, + /// The input string so we can print it underlined + pub input: String, +} + +/// Either we have an error string from our parser or an upstream error from `url` +#[derive(Debug, Error, Clone, Eq, PartialEq)] +pub enum Pep508ErrorSource { + /// An error from our parser + String(String), + /// A url parsing error + #[error(transparent)] + UrlError(#[from] url::ParseError), +} + +impl Display for Pep508ErrorSource { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Pep508ErrorSource::String(string) => string.fmt(f), + Pep508ErrorSource::UrlError(parse_err) => parse_err.fmt(f), + } + } +} + +impl Display for Pep508Error { + /// Pretty formatting with underline + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + // We can use char indices here since it's a Vec + let start_offset = self + .input + .chars() + .take(self.start) + .collect::() + .width(); + let underline_len = if self.start == self.input.len() { + // We also allow 0 here for convenience + assert!( + self.len <= 1, + "Can only go one past the input not {}", + self.len + ); + 1 + } else { + self.input + .chars() + .skip(self.start) + .take(self.len) + .collect::() + .width() + }; + write!( + f, + "{}\n{}\n{}{}", + self.message, + self.input, + " ".repeat(start_offset), + "^".repeat(underline_len) + ) + } +} + +/// We need this to allow e.g. anyhow's `.context()` +impl std::error::Error for Pep508Error {} + +#[cfg(feature = "pyo3")] +create_exception!( + pep508, + PyPep508Error, + pyo3::exceptions::PyValueError, + "A PEP 508 parser error with span information" +); + +/// A PEP 508 dependency specification +#[cfg_attr(feature = "pyo3", pyclass(module = "pep508"))] +#[derive(Hash, Debug, Clone, Eq, PartialEq)] +pub struct Requirement { + /// The distribution name such as `numpy` in + /// `requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"` + pub name: String, + /// The list of extras such as `security`, `tests` in + /// `requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"` + pub extras: Option>, + /// The version specifier such as `>= 2.8.1`, `== 2.8.*` in + /// `requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"` + /// or a url + pub version_or_url: Option, + /// The markers such as `python_version > "3.8"` in + /// `requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"`. + /// Those are a nested and/or tree + pub marker: Option, +} + +impl Display for Requirement { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.name)?; + if let Some(extras) = &self.extras { + write!(f, "[{}]", extras.join(","))?; + } + if let Some(version_or_url) = &self.version_or_url { + match version_or_url { + VersionOrUrl::VersionSpecifier(version_specifier) => { + let version_specifier: Vec = + version_specifier.iter().map(ToString::to_string).collect(); + write!(f, " {}", version_specifier.join(", "))?; + } + VersionOrUrl::Url(url) => { + // We add the space for markers later if necessary + write!(f, " @ {}", url)?; + } + } + } + if let Some(marker) = &self.marker { + write!(f, " ; {}", marker)?; + } + Ok(()) + } +} + +/// https://github.com/serde-rs/serde/issues/908#issuecomment-298027413 +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for Requirement { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + FromStr::from_str(&s).map_err(de::Error::custom) + } +} + +/// https://github.com/serde-rs/serde/issues/1316#issue-332908452 +#[cfg(feature = "serde")] +impl Serialize for Requirement { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.collect_str(self) + } +} + +#[cfg(feature = "pyo3")] +#[pymethods] +impl Requirement { + /// The distribution name such as `numpy` in + /// `requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"` + #[getter] + pub fn name(&self) -> String { + self.name.clone() + } + + /// The list of extras such as `security`, `tests` in + /// `requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"` + #[getter] + pub fn extras(&self) -> Option> { + self.extras.clone() + } + + /// The marker expression such as `python_version > "3.8"` in + /// `requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"` + #[getter] + pub fn marker(&self) -> Option { + self.marker.as_ref().map(|m| m.to_string()) + } + + /// Parses a PEP 440 string + #[new] + pub fn py_new(requirement: &str) -> PyResult { + Self::from_str(requirement).map_err(|err| PyPep508Error::new_err(err.to_string())) + } + + #[getter] + fn version_or_url(&self, py: Python<'_>) -> PyObject { + match &self.version_or_url { + None => py.None(), + Some(VersionOrUrl::VersionSpecifier(version_specifier)) => version_specifier + .iter() + .map(|x| x.clone().into_py(py)) + .collect::>() + .into_py(py), + Some(VersionOrUrl::Url(url)) => url.to_string().into_py(py), + } + } + + fn __str__(&self) -> String { + self.to_string() + } + + fn __repr__(&self) -> String { + format!(r#""{}""#, self) + } + + fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult { + let err = PyNotImplementedError::new_err("Requirement only supports equality comparisons"); + match op { + CompareOp::Lt => Err(err), + CompareOp::Le => Err(err), + CompareOp::Eq => Ok(self == other), + CompareOp::Ne => Ok(self != other), + CompareOp::Gt => Err(err), + CompareOp::Ge => Err(err), + } + } + + fn __hash__(&self) -> u64 { + let mut hasher = DefaultHasher::new(); + self.hash(&mut hasher); + hasher.finish() + } + + /// Returns whether the markers apply for the given environment + #[pyo3(name = "evaluate_markers")] + pub fn py_evaluate_markers(&self, env: &MarkerEnvironment, extras: Vec) -> bool { + self.evaluate_markers(env, extras) + } + + /// Returns whether the requirement would be satisfied, independent of environment markers, i.e. + /// if there is potentially an environment that could activate this requirement. + /// + /// Note that unlike [Self::evaluate_markers] this does not perform any checks for bogus + /// expressions but will simply return true. As caller you should separately perform a check + /// with an environment and forward all warnings. + #[pyo3(name = "evaluate_extras_and_python_version")] + pub fn py_evaluate_extras_and_python_version( + &self, + extras: HashSet, + python_versions: Vec, + ) -> bool { + self.evaluate_extras_and_python_version( + extras, + python_versions + .into_iter() + .map(|py_version| py_version.0) + .collect(), + ) + } + + /// Returns whether the markers apply for the given environment + #[pyo3(name = "evaluate_markers_and_report")] + pub fn py_evaluate_markers_and_report( + &self, + env: &MarkerEnvironment, + extras: Vec, + ) -> (bool, Vec<(MarkerWarningKind, String, String)>) { + self.evaluate_markers_and_report(env, extras) + } +} + +impl Requirement { + /// Returns whether the markers apply for the given environment + pub fn evaluate_markers(&self, env: &MarkerEnvironment, extras: Vec) -> bool { + if let Some(marker) = &self.marker { + marker.evaluate( + env, + &extras.iter().map(String::as_str).collect::>(), + ) + } else { + true + } + } + + /// Returns whether the requirement would be satisfied, independent of environment markers, i.e. + /// if there is potentially an environment that could activate this requirement. + /// + /// Note that unlike [Self::evaluate_markers] this does not perform any checks for bogus + /// expressions but will simply return true. As caller you should separately perform a check + /// with an environment and forward all warnings. + pub fn evaluate_extras_and_python_version( + &self, + extras: HashSet, + python_versions: Vec, + ) -> bool { + if let Some(marker) = &self.marker { + marker.evaluate_extras_and_python_version(&extras, &python_versions) + } else { + true + } + } + + /// Returns whether the markers apply for the given environment + pub fn evaluate_markers_and_report( + &self, + env: &MarkerEnvironment, + extras: Vec, + ) -> (bool, Vec<(MarkerWarningKind, String, String)>) { + if let Some(marker) = &self.marker { + marker.evaluate_collect_warnings( + env, + &extras.iter().map(|x| x.as_str()).collect::>(), + ) + } else { + (true, Vec::new()) + } + } +} + +impl FromStr for Requirement { + type Err = Pep508Error; + + /// Parse a [Dependency Specifier](https://packaging.python.org/en/latest/specifications/dependency-specifiers/) + fn from_str(input: &str) -> Result { + parse(&mut CharIter::new(input)) + } +} + +impl Requirement { + /// Parse a [Dependency Specifier](https://packaging.python.org/en/latest/specifications/dependency-specifiers/) + pub fn parse(input: &mut CharIter) -> Result { + parse(input) + } +} + +/// The actual version specifier or url to install +#[derive(Debug, Clone, Eq, Hash, PartialEq)] +pub enum VersionOrUrl { + /// A PEP 440 version specifier set + VersionSpecifier(VersionSpecifiers), + /// A installable URL + Url(Url), +} + +/// A `Vec` and an index inside of it. Like [String], but with utf-8 aware indexing +pub struct CharIter<'a> { + input: &'a str, + chars: Chars<'a>, + /// char-based (not byte-based) position + pos: usize, +} + +impl<'a> CharIter<'a> { + /// Convert from `&str` + pub fn new(input: &'a str) -> Self { + Self { + input, + chars: input.chars(), + pos: 0, + } + } + + fn copy_chars(&self) -> String { + self.input.to_string() + } + + fn peek(&self) -> Option<(usize, char)> { + self.chars.clone().next().map(|char| (self.pos, char)) + } + + fn eat(&mut self, token: char) -> Option { + let (start_pos, peek_char) = self.peek()?; + if peek_char == token { + self.next(); + Some(start_pos) + } else { + None + } + } + + fn next(&mut self) -> Option<(usize, char)> { + let next = (self.pos, self.chars.next()?); + self.pos += 1; + Some(next) + } + + fn peek_char(&self) -> Option { + self.chars.clone().next() + } + + fn get_pos(&self) -> usize { + self.pos + } + + fn peek_while(&mut self, condition: impl Fn(char) -> bool) -> (String, usize, usize) { + let peeker = self.chars.clone(); + let start = self.get_pos(); + let mut len = 0; + let substring = peeker + .take_while(|c| { + if condition(*c) { + len += 1; + true + } else { + false + } + }) + .collect::(); + (substring, start, len) + } + + fn take_while(&mut self, condition: impl Fn(char) -> bool) -> (String, usize, usize) { + // no pretty, but works + let mut substring = String::new(); + let start = self.get_pos(); + let mut len = 0; + while let Some(char) = self.peek_char() { + if !condition(char) { + break; + } else { + substring.push(char); + self.next(); + len += 1; + } + } + (substring, start, len) + } + + fn next_expect_char(&mut self, expected: char, span_start: usize) -> Result<(), Pep508Error> { + match self.next() { + None => Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected '{}', found end of dependency specification", + expected + )), + start: span_start, + len: 1, + input: self.copy_chars(), + }), + Some((_, value)) if value == expected => Ok(()), + Some((pos, other)) => Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected '{}', found '{}'", + expected, other + )), + start: pos, + len: 1, + input: self.copy_chars(), + }), + } + } + + fn eat_whitespace(&mut self) { + while let Some(char) = self.peek_char() { + if char.is_whitespace() { + self.next(); + } else { + return; + } + } + } +} + +fn parse_name(chars: &mut CharIter) -> Result { + // https://peps.python.org/pep-0508/#names + // ^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$ with re.IGNORECASE + let mut name = String::new(); + if let Some((index, char)) = chars.next() { + if matches!(char, 'A'..='Z' | 'a'..='z' | '0'..='9') { + name.push(char); + } else { + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected package name starting with an alphanumeric character, found '{}'", + char + )), + start: index, + len: 1, + input: chars.copy_chars(), + }); + } + } else { + return Err(Pep508Error { + message: Pep508ErrorSource::String("Empty field is not allowed for PEP508".to_string()), + start: 0, + len: 1, + input: chars.copy_chars(), + }); + } + + loop { + match chars.peek() { + Some((index, char @ ('A'..='Z' | 'a'..='z' | '0'..='9' | '.' | '-' | '_'))) => { + name.push(char); + chars.next(); + // [.-_] can't be the final character + if chars.peek().is_none() && matches!(char, '.' | '-' | '_') { + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Package name must end with an alphanumeric character, not '{}'", + char + )), + start: index, + len: 1, + input: chars.copy_chars(), + }); + } + } + Some(_) | None => return Ok(name), + } + } +} + +/// parses extras in the `[extra1,extra2] format` +fn parse_extras(chars: &mut CharIter) -> Result>, Pep508Error> { + let bracket_pos = match chars.eat('[') { + Some(pos) => pos, + None => return Ok(None), + }; + let mut extras = Vec::new(); + + loop { + // wsp* before the identifier + chars.eat_whitespace(); + let mut buffer = String::new(); + let early_eof_error = Pep508Error { + message: Pep508ErrorSource::String( + "Missing closing bracket (expected ']', found end of dependency specification)" + .to_string(), + ), + start: bracket_pos, + len: 1, + input: chars.copy_chars(), + }; + + // First char of the identifier + match chars.next() { + // letterOrDigit + Some((_, alphanumeric @ ('a'..='z' | 'A'..='Z' | '0'..='9'))) => { + buffer.push(alphanumeric) + } + Some((pos, other)) => { + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected an alphanumeric character starting the extra name, found '{}'", + other + )), + start: pos, + len: 1, + input: chars.copy_chars(), + }) + } + None => return Err(early_eof_error), + } + // Parse from the second char of the identifier + // We handle the illegal character case below + // identifier_end = letterOrDigit | (('-' | '_' | '.' )* letterOrDigit) + // identifier_end* + buffer.push_str( + &chars + .take_while( + |char| matches!(char, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.'), + ) + .0, + ); + match chars.peek() { + Some((pos, char)) if char != ',' && char != ']' && !char.is_whitespace() => { + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Invalid character in extras name, expected an alphanumeric character, '-', '_', '.', ',' or ']', found '{}'", char + )), + start: pos, + len: 1, + input: chars.copy_chars(), + }) + + } + _=>{} + }; + // wsp* after the identifier + chars.eat_whitespace(); + // end or next identifier? + match chars.next() { + Some((_, ',')) => { + extras.push(buffer); + } + Some((_, ']')) => { + extras.push(buffer); + break; + } + Some((pos, other)) => { + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected either ',' (separating extras) or ']' (ending the extras section), found '{other}'" + )), + start: pos, + len: 1, + input: chars.copy_chars(), + }) + } + None => return Err(early_eof_error), + } + } + + Ok(Some(extras)) +} + +fn parse_url(chars: &mut CharIter) -> Result { + // wsp* + chars.eat_whitespace(); + // + let (url, start, len) = chars.take_while(|char| !char.is_whitespace()); + if url.is_empty() { + return Err(Pep508Error { + message: Pep508ErrorSource::String("Expected URL".to_string()), + start, + len, + input: chars.copy_chars(), + }); + } + let url = Url::parse(&url).map_err(|err| Pep508Error { + message: Pep508ErrorSource::UrlError(err), + start, + len, + input: chars.copy_chars(), + })?; + Ok(VersionOrUrl::Url(url)) +} + +/// PEP 440 wrapper +fn parse_specifier( + chars: &mut CharIter, + buffer: &str, + start: usize, + end: usize, +) -> Result { + VersionSpecifier::from_str(buffer).map_err(|err| Pep508Error { + message: Pep508ErrorSource::String(err), + start, + len: end - start, + input: chars.copy_chars(), + }) +} + +/// Such as `>=1.19,<2.0`, either delimited by the end of the specifier or a `;` for the marker part +/// +/// ```text +/// version_one (wsp* ',' version_one)* +/// ``` +fn parse_version_specifier(chars: &mut CharIter) -> Result, Pep508Error> { + let mut start = chars.get_pos(); + let mut specifiers = Vec::new(); + let mut buffer = String::new(); + let requirement_kind = loop { + match chars.peek() { + Some((end, ',')) => { + let specifier = parse_specifier(chars, &buffer, start, end)?; + specifiers.push(specifier); + buffer.clear(); + chars.next(); + start = end + 1; + } + Some((_, ';')) | None => { + let end = chars.get_pos(); + let specifier = parse_specifier(chars, &buffer, start, end)?; + specifiers.push(specifier); + break Some(VersionOrUrl::VersionSpecifier( + specifiers.into_iter().collect(), + )); + } + Some((_, char)) => { + buffer.push(char); + chars.next(); + } + } + }; + Ok(requirement_kind) +} + +/// Such as `(>=1.19,<2.0)` +/// +/// ```text +/// '(' version_one (wsp* ',' version_one)* ')' +/// ``` +fn parse_version_specifier_parentheses( + chars: &mut CharIter, +) -> Result, Pep508Error> { + let brace_pos = chars.get_pos(); + chars.next(); + // Makes for slightly better error underline + chars.eat_whitespace(); + let mut start = chars.get_pos(); + let mut specifiers = Vec::new(); + let mut buffer = String::new(); + let requirement_kind = loop { + match chars.next() { + Some((end, ',')) => { + let specifier = + parse_specifier(chars, &buffer, start, end)?; + specifiers.push(specifier); + buffer.clear(); + start = end + 1; + } + Some((end, ')')) => { + let specifier = parse_specifier(chars, &buffer, start, end)?; + specifiers.push(specifier); + break Some(VersionOrUrl::VersionSpecifier(specifiers.into_iter().collect())); + }, + Some((_, char)) => buffer.push(char), + None => return Err(Pep508Error { + message: Pep508ErrorSource::String("Missing closing parenthesis (expected ')', found end of dependency specification)".to_string()), + start: brace_pos, + len: 1, + input: chars.copy_chars(), + }), + } + }; + Ok(requirement_kind) +} + +/// Parse a [dependency specifier](https://packaging.python.org/en/latest/specifications/dependency-specifiers) +fn parse(chars: &mut CharIter) -> Result { + // Technically, the grammar is: + // ```text + // name_req = name wsp* extras? wsp* versionspec? wsp* quoted_marker? + // url_req = name wsp* extras? wsp* urlspec wsp+ quoted_marker? + // specification = wsp* ( url_req | name_req ) wsp* + // ``` + // So we can merge this into: + // ```text + // specification = wsp* name wsp* extras? wsp* (('@' wsp* url_req) | ('(' versionspec ')') | (versionspec)) wsp* (';' wsp* marker)? wsp* + // ``` + // Where the extras start with '[' if any, then we have '@', '(' or one of the version comparison + // operators. Markers start with ';' if any + // wsp* + chars.eat_whitespace(); + // name + let name = parse_name(chars)?; + // wsp* + chars.eat_whitespace(); + // extras? + let extras = parse_extras(chars)?; + // wsp* + chars.eat_whitespace(); + + // ( url_req | name_req )? + let requirement_kind = match chars.peek_char() { + Some('@') => { + chars.next(); + Some(parse_url(chars)?) + } + Some('(') => parse_version_specifier_parentheses(chars)?, + Some('<' | '=' | '>' | '~' | '!') => parse_version_specifier(chars)?, + Some(';') | None => None, + Some(other) => { + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected one of `@`, `(`, `<`, `=`, `>`, `~`, `!`, `;`, found `{}`", + other + )), + start: chars.get_pos(), + len: 1, + input: chars.copy_chars(), + }) + } + }; + + // wsp* + chars.eat_whitespace(); + // quoted_marker? + let marker = if chars.peek_char() == Some(';') { + // Skip past the semicolon + chars.next(); + Some(marker::parse_markers_impl(chars)?) + } else { + None + }; + // wsp* + chars.eat_whitespace(); + if let Some((pos, char)) = chars.next() { + return Err(Pep508Error { + message: Pep508ErrorSource::String(if marker.is_none() { + format!(r#"Expected end of input or ';', found '{}'"#, char) + } else { + format!(r#"Expected end of input, found '{}'"#, char) + }), + start: pos, + len: 1, + input: chars.copy_chars(), + }); + } + + Ok(Requirement { + name, + extras, + version_or_url: requirement_kind, + marker, + }) +} + +/// A library for [dependency specifiers](https://packaging.python.org/en/latest/specifications/dependency-specifiers/) +/// as originally specified in [PEP 508](https://peps.python.org/pep-0508/) +/// +/// This has `Version` and `VersionSpecifier` included. That is because +/// `pep440_rs.Version("1.2.3") != pep508_rs.Requirement("numpy==1.2.3").version_or_url` as the +/// `Version`s come from two different binaries and can therefore never be equal. +#[cfg(feature = "pyo3")] +#[pymodule] +#[pyo3(name = "pep508_rs")] +pub fn python_module(py: Python<'_>, m: &PyModule) -> PyResult<()> { + // Allowed to fail if we embed this module in another + #[allow(unused_must_use)] + { + pyo3_log::try_init(); + } + + m.add_class::()?; + m.add_class::()?; + + m.add_class::()?; + m.add_class::()?; + m.add("Pep508Error", py.get_type::())?; + Ok(()) +} + +/// Half of these tests are copied from https://github.com/pypa/packaging/pull/624 +#[cfg(test)] +mod tests { + use crate::marker::{ + parse_markers_impl, MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, + MarkerValueString, MarkerValueVersion, + }; + use crate::{CharIter, Requirement, VersionOrUrl}; + use indoc::indoc; + use pep440_rs::{Operator, Version, VersionSpecifier}; + use std::str::FromStr; + use url::Url; + + fn assert_err(input: &str, error: &str) { + assert_eq!(Requirement::from_str(input).unwrap_err().to_string(), error); + } + + #[test] + fn error_empty() { + assert_err( + "", + indoc! {" + Empty field is not allowed for PEP508 + + ^" + }, + ); + } + + #[test] + fn error_start() { + assert_err( + "_name", + indoc! {" + Expected package name starting with an alphanumeric character, found '_' + _name + ^" + }, + ); + } + + #[test] + fn error_end() { + assert_err( + "name_", + indoc! {" + Package name must end with an alphanumeric character, not '_' + name_ + ^" + }, + ); + } + + #[test] + fn basic_examples() { + let input = r#"requests[security,tests] >=2.8.1, ==2.8.* ; python_version < '2.7'"#; + let requests = Requirement::from_str(input).unwrap(); + assert_eq!(input, requests.to_string()); + let expected = Requirement { + name: "requests".to_string(), + extras: Some(vec!["security".to_string(), "tests".to_string()]), + version_or_url: Some(VersionOrUrl::VersionSpecifier( + [ + VersionSpecifier::new( + Operator::GreaterThanEqual, + Version { + epoch: 0, + release: vec![2, 8, 1], + pre: None, + post: None, + dev: None, + local: None, + }, + false, + ) + .unwrap(), + VersionSpecifier::new( + Operator::Equal, + Version { + epoch: 0, + release: vec![2, 8], + pre: None, + post: None, + dev: None, + local: None, + }, + true, + ) + .unwrap(), + ] + .into_iter() + .collect(), + )), + marker: Some(MarkerTree::Expression(MarkerExpression { + l_value: MarkerValue::MarkerEnvVersion(MarkerValueVersion::PythonVersion), + operator: MarkerOperator::LessThan, + r_value: MarkerValue::QuotedString("2.7".to_string()), + })), + }; + assert_eq!(requests, expected); + } + + #[test] + fn parenthesized_single() { + let numpy = Requirement::from_str("numpy ( >=1.19 )").unwrap(); + assert_eq!(numpy.name, "numpy"); + } + + #[test] + fn parenthesized_double() { + let numpy = Requirement::from_str("numpy ( >=1.19, <2.0 )").unwrap(); + assert_eq!(numpy.name, "numpy"); + } + + #[test] + fn versions_single() { + let numpy = Requirement::from_str("numpy >=1.19 ").unwrap(); + assert_eq!(numpy.name, "numpy"); + } + + #[test] + fn versions_double() { + let numpy = Requirement::from_str("numpy >=1.19, <2.0 ").unwrap(); + assert_eq!(numpy.name, "numpy"); + } + + #[test] + fn error_extras_eof1() { + assert_err( + "black[", + indoc! {" + Missing closing bracket (expected ']', found end of dependency specification) + black[ + ^" + }, + ); + } + + #[test] + fn error_extras_eof2() { + assert_err( + "black[d", + indoc! {" + Missing closing bracket (expected ']', found end of dependency specification) + black[d + ^" + }, + ); + } + + #[test] + fn error_extras_eof3() { + assert_err( + "black[d,", + indoc! {" + Missing closing bracket (expected ']', found end of dependency specification) + black[d, + ^" + }, + ); + } + + #[test] + fn error_extras_illegal_start1() { + assert_err( + "black[ö]", + indoc! {" + Expected an alphanumeric character starting the extra name, found 'ö' + black[ö] + ^" + }, + ); + } + + #[test] + fn error_extras_illegal_start2() { + assert_err( + "black[_d]", + indoc! {" + Expected an alphanumeric character starting the extra name, found '_' + black[_d] + ^" + }, + ); + } + #[test] + fn error_extras_illegal_character() { + assert_err( + "black[jüpyter]", + indoc! {" + Invalid character in extras name, expected an alphanumeric character, '-', '_', '.', ',' or ']', found 'ü' + black[jüpyter] + ^" + }, + ); + } + + #[test] + fn error_extras1() { + let numpy = Requirement::from_str("black[d]").unwrap(); + assert_eq!(numpy.extras, Some(vec!["d".to_string()])); + } + + #[test] + fn error_extras2() { + let numpy = Requirement::from_str("black[d,jupyter]").unwrap(); + assert_eq!( + numpy.extras, + Some(vec!["d".to_string(), "jupyter".to_string()]) + ); + } + + #[test] + fn error_parenthesized_pep440() { + assert_err( + "numpy ( ><1.19 )", + indoc! {" + Version specifier `><1.19 ` doesn't match PEP 440 rules + numpy ( ><1.19 ) + ^^^^^^^" + }, + ); + } + + #[test] + fn error_parenthesized_parenthesis() { + assert_err( + "numpy ( >=1.19 ", + indoc! {" + Missing closing parenthesis (expected ')', found end of dependency specification) + numpy ( >=1.19 + ^" + }, + ); + } + + #[test] + fn error_whats_that() { + assert_err( + "numpy % 1.16", + indoc! {" + Expected one of `@`, `(`, `<`, `=`, `>`, `~`, `!`, `;`, found `%` + numpy % 1.16 + ^" + }, + ); + } + + #[test] + fn url() { + let pip_url = + Requirement::from_str("pip @ https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686") + .unwrap(); + let url = "https://github.com/pypa/pip/archive/1.3.1.zip#sha1=da9234ee9982d4bbb3c72346a6de940a148ea686"; + let expected = Requirement { + name: "pip".to_string(), + extras: None, + marker: None, + version_or_url: Some(VersionOrUrl::Url(Url::parse(url).unwrap())), + }; + assert_eq!(pip_url, expected); + } + + #[test] + fn test_marker_parsing() { + let marker = r#"python_version == "2.7" and (sys_platform == "win32" or (os_name == "linux" and implementation_name == 'cpython'))"#; + let actual = parse_markers_impl(&mut CharIter::new(marker)).unwrap(); + let expected = MarkerTree::And(vec![ + MarkerTree::Expression(MarkerExpression { + l_value: MarkerValue::MarkerEnvVersion(MarkerValueVersion::PythonVersion), + operator: MarkerOperator::Equal, + r_value: MarkerValue::QuotedString("2.7".to_string()), + }), + MarkerTree::Or(vec![ + MarkerTree::Expression(MarkerExpression { + l_value: MarkerValue::MarkerEnvString(MarkerValueString::SysPlatform), + operator: MarkerOperator::Equal, + r_value: MarkerValue::QuotedString("win32".to_string()), + }), + MarkerTree::And(vec![ + MarkerTree::Expression(MarkerExpression { + l_value: MarkerValue::MarkerEnvString(MarkerValueString::OsName), + operator: MarkerOperator::Equal, + r_value: MarkerValue::QuotedString("linux".to_string()), + }), + MarkerTree::Expression(MarkerExpression { + l_value: MarkerValue::MarkerEnvString( + MarkerValueString::ImplementationName, + ), + operator: MarkerOperator::Equal, + r_value: MarkerValue::QuotedString("cpython".to_string()), + }), + ]), + ]), + ]); + assert_eq!(expected, actual); + } + + #[test] + fn name_and_marker() { + Requirement::from_str(r#"numpy; sys_platform == "win32" or (os_name == "linux" and implementation_name == 'cpython')"#).unwrap(); + } + + #[test] + fn error_marker_incomplete1() { + assert_err( + r#"numpy; sys_platform"#, + indoc! {" + Expected a valid marker operator (such as '>=' or 'not in'), found '' + numpy; sys_platform + ^" + }, + ); + } + + #[test] + fn error_marker_incomplete2() { + assert_err( + r#"numpy; sys_platform == "#, + indoc! {" + Expected marker value, found end of dependency specification + numpy; sys_platform == + ^" + }, + ); + } + + #[test] + fn error_marker_incomplete3() { + assert_err( + r#"numpy; sys_platform == "win32" or "#, + indoc! {r#" + Expected marker value, found end of dependency specification + numpy; sys_platform == "win32" or + ^"#}, + ); + } + + #[test] + fn error_marker_incomplete4() { + assert_err( + r#"numpy; sys_platform == "win32" or (os_name == "linux""#, + indoc! {r#" + Expected ')', found end of dependency specification + numpy; sys_platform == "win32" or (os_name == "linux" + ^"#}, + ); + } + + #[test] + fn error_marker_incomplete5() { + assert_err( + r#"numpy; sys_platform == "win32" or (os_name == "linux" and "#, + indoc! {r#" + Expected marker value, found end of dependency specification + numpy; sys_platform == "win32" or (os_name == "linux" and + ^"#}, + ); + } + + #[test] + fn error_pep440() { + assert_err( + r#"numpy >=1.1.*"#, + indoc! {" + Operator >= must not be used in version ending with a star + numpy >=1.1.* + ^^^^^^^" + }, + ); + } + + #[test] + fn error_no_name() { + assert_err( + r#"==0.0"#, + indoc! {" + Expected package name starting with an alphanumeric character, found '=' + ==0.0 + ^" + }, + ); + } + + #[test] + fn error_no_comma_between_extras() { + assert_err( + r#"name[bar baz]"#, + indoc! {" + Expected either ',' (separating extras) or ']' (ending the extras section), found 'b' + name[bar baz] + ^" + }, + ); + } + + #[test] + fn error_extra_comma_after_extras() { + assert_err( + r#"name[bar, baz,]"#, + indoc! {" + Expected an alphanumeric character starting the extra name, found ']' + name[bar, baz,] + ^" + }, + ); + } + + #[test] + fn error_extras_not_closed() { + assert_err( + r#"name[bar, baz >= 1.0"#, + indoc! {" + Expected either ',' (separating extras) or ']' (ending the extras section), found '>' + name[bar, baz >= 1.0 + ^" + }, + ); + } + + #[test] + fn error_no_space_after_url() { + assert_err( + r#"name @ https://example.com/; extra == 'example'"#, + indoc! {" + Expected end of input or ';', found 'e' + name @ https://example.com/; extra == 'example' + ^" + }, + ); + } + + #[test] + fn error_name_at_nothing() { + assert_err( + r#"name @ "#, + indoc! {" + Expected URL + name @ + ^" + }, + ); + } + + #[test] + fn test_error_invalid_marker_key() { + assert_err( + r#"name; invalid_name"#, + indoc! {" + Expected a valid marker name, found 'invalid_name' + name; invalid_name + ^^^^^^^^^^^^" + }, + ); + } + + #[test] + fn error_markers_invalid_order() { + assert_err( + "name; '3.7' <= invalid_name", + indoc! {" + Expected a valid marker name, found 'invalid_name' + name; '3.7' <= invalid_name + ^^^^^^^^^^^^" + }, + ); + } + + #[test] + fn error_markers_notin() { + assert_err( + "name; '3.7' notin python_version", + indoc! {" + Expected a valid marker operator (such as '>=' or 'not in'), found 'notin' + name; '3.7' notin python_version + ^^^^^" + }, + ); + } + + #[test] + fn error_markers_inpython_version() { + assert_err( + "name; '3.6'inpython_version", + indoc! {" + Expected a valid marker operator (such as '>=' or 'not in'), found 'inpython_version' + name; '3.6'inpython_version + ^^^^^^^^^^^^^^^^" + }, + ); + } + + #[test] + fn error_markers_not_python_version() { + assert_err( + "name; '3.7' not python_version", + indoc! {" + Expected 'i', found 'p' + name; '3.7' not python_version + ^" + }, + ); + } + + #[test] + fn error_markers_invalid_operator() { + assert_err( + "name; '3.7' ~ python_version", + indoc! {" + Expected a valid marker operator (such as '>=' or 'not in'), found '~' + name; '3.7' ~ python_version + ^" + }, + ); + } + + #[test] + fn error_invalid_prerelease() { + assert_err( + "name==1.0.org1", + indoc! {" + Version specifier `==1.0.org1` doesn't match PEP 440 rules + name==1.0.org1 + ^^^^^^^^^^" + }, + ); + } + + #[test] + fn error_no_version_value() { + assert_err( + "name==", + indoc! {" + Version specifier `==` doesn't match PEP 440 rules + name== + ^^" + }, + ); + } + + #[test] + fn error_no_version_operator() { + assert_err( + "name 1.0", + indoc! {" + Expected one of `@`, `(`, `<`, `=`, `>`, `~`, `!`, `;`, found `1` + name 1.0 + ^" + }, + ); + } + + #[test] + fn error_random_char() { + assert_err( + "name >= 1.0 #", + indoc! {" + Version specifier `>= 1.0 #` doesn't match PEP 440 rules + name >= 1.0 # + ^^^^^^^^" + }, + ); + } +} diff --git a/crates/pep508-rs/src/marker.rs b/crates/pep508-rs/src/marker.rs new file mode 100644 index 000000000..423d16ced --- /dev/null +++ b/crates/pep508-rs/src/marker.rs @@ -0,0 +1,1577 @@ +//! PEP 508 markers implementations with validation and warnings +//! +//! Markers allow you to install dependencies only in specific environments (python version, +//! operating system, architecture, etc.) or when a specific feature is activated. E.g. you can +//! say `importlib-metadata ; python_version < "3.8"` or +//! `itsdangerous (>=1.1.0) ; extra == 'security'`. Unfortunately, the marker grammar has some +//! oversights (e.g. ) and +//! the design of comparisons (PEP 440 comparisons with lexicographic fallback) leads to confusing +//! outcomes. This implementation tries to carefully validate everything and emit warnings whenever +//! bogus comparisons with unintended semantics are made. + +use crate::{CharIter, Pep508Error, Pep508ErrorSource}; +use pep440_rs::{Version, VersionSpecifier}; +#[cfg(feature = "pyo3")] +use pyo3::{ + basic::CompareOp, exceptions::PyValueError, pyclass, pymethods, PyAny, PyResult, Python, +}; +#[cfg(feature = "serde")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +use std::collections::HashSet; +use std::fmt::{Display, Formatter}; +use std::ops::Deref; +use std::str::FromStr; +use tracing::warn; + +/// Ways in which marker evaluation can fail +#[cfg_attr(feature = "pyo3", pyclass(module = "pep508"))] +#[derive(Debug, Eq, Hash, Ord, PartialOrd, PartialEq, Clone, Copy)] +pub enum MarkerWarningKind { + /// Using an old name from PEP 345 instead of the modern equivalent + /// + DeprecatedMarkerName, + /// Doing an operation other than `==` and `!=` on a quoted string with `extra`, such as + /// `extra > "perf"` or `extra == os_name` + ExtraInvalidComparison, + /// Comparing a string valued marker and a string lexicographically, such as `"3.9" > "3.10"` + LexicographicComparison, + /// Comparing two markers, such as `os_name != sys_implementation` + MarkerMarkerComparison, + /// Failed to parse a PEP 440 version or version specifier, e.g. `>=1<2` + Pep440Error, + /// Comparing two strings, such as `"3.9" > "3.10"` + StringStringComparison, +} + +#[cfg(feature = "pyo3")] +#[pymethods] +impl MarkerWarningKind { + fn __hash__(&self) -> u8 { + *self as u8 + } + + fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool { + op.matches(self.cmp(other)) + } +} + +/// Those environment markers with a PEP 440 version as value such as `python_version` +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[allow(clippy::enum_variant_names)] +pub enum MarkerValueVersion { + /// `implementation_version` + ImplementationVersion, + /// `python_full_version` + PythonFullVersion, + /// `python_version` + PythonVersion, +} + +impl Display for MarkerValueVersion { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Self::ImplementationVersion => f.write_str("implementation_version"), + Self::PythonFullVersion => f.write_str("python_full_version"), + Self::PythonVersion => f.write_str("python_version"), + } + } +} + +/// Those environment markers with an arbitrary string as value such as `sys_platform` +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub enum MarkerValueString { + /// `implementation_name` + ImplementationName, + /// `os_name` + OsName, + /// /// Deprecated `os.name` from https://peps.python.org/pep-0345/#environment-markers + OsNameDeprecated, + /// `platform_machine` + PlatformMachine, + /// /// Deprecated `platform.machine` from https://peps.python.org/pep-0345/#environment-markers + PlatformMachineDeprecated, + /// `platform_python_implementation` + PlatformPythonImplementation, + /// /// Deprecated `platform.python_implementation` from https://peps.python.org/pep-0345/#environment-markers + PlatformPythonImplementationDeprecated, + /// `platform_release` + PlatformRelease, + /// `platform_system` + PlatformSystem, + /// `platform_version` + PlatformVersion, + /// /// Deprecated `platform.version` from https://peps.python.org/pep-0345/#environment-markers + PlatformVersionDeprecated, + /// `sys_platform` + SysPlatform, + /// /// Deprecated `sys.platform` from https://peps.python.org/pep-0345/#environment-markers + SysPlatformDeprecated, +} + +impl Display for MarkerValueString { + /// Normalizes deprecated names to the proper ones + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Self::ImplementationName => f.write_str("implementation_name"), + Self::OsName | Self::OsNameDeprecated => f.write_str("os_name"), + Self::PlatformMachine | Self::PlatformMachineDeprecated => { + f.write_str("platform_machine") + } + Self::PlatformPythonImplementation | Self::PlatformPythonImplementationDeprecated => { + f.write_str("platform_python_implementation") + } + Self::PlatformRelease => f.write_str("platform_release"), + Self::PlatformSystem => f.write_str("platform_system"), + Self::PlatformVersion | Self::PlatformVersionDeprecated => { + f.write_str("platform_version") + } + Self::SysPlatform | Self::SysPlatformDeprecated => f.write_str("sys_platform"), + } + } +} + +/// One of the predefined environment values +/// +/// +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub enum MarkerValue { + /// Those environment markers with a PEP 440 version as value such as `python_version` + MarkerEnvVersion(MarkerValueVersion), + /// Those environment markers with an arbitrary string as value such as `sys_platform` + MarkerEnvString(MarkerValueString), + /// `extra`. This one is special because it's a list and not env but user given + Extra, + /// Not a constant, but a user given quoted string with a value inside such as '3.8' or "windows" + QuotedString(String), +} + +impl MarkerValue { + fn string_value(value: String) -> Self { + Self::QuotedString(value) + } +} + +impl FromStr for MarkerValue { + type Err = String; + + /// This is specifically for the reserved values + fn from_str(s: &str) -> Result { + let value = match s { + "implementation_name" => Self::MarkerEnvString(MarkerValueString::ImplementationName), + "implementation_version" => { + Self::MarkerEnvVersion(MarkerValueVersion::ImplementationVersion) + } + "os_name" => Self::MarkerEnvString(MarkerValueString::OsName), + "os.name" => Self::MarkerEnvString(MarkerValueString::OsNameDeprecated), + "platform_machine" => Self::MarkerEnvString(MarkerValueString::PlatformMachine), + "platform.machine" => { + Self::MarkerEnvString(MarkerValueString::PlatformMachineDeprecated) + } + "platform_python_implementation" => { + Self::MarkerEnvString(MarkerValueString::PlatformPythonImplementation) + } + "platform.python_implementation" => { + Self::MarkerEnvString(MarkerValueString::PlatformPythonImplementationDeprecated) + } + "platform_release" => Self::MarkerEnvString(MarkerValueString::PlatformRelease), + "platform_system" => Self::MarkerEnvString(MarkerValueString::PlatformSystem), + "platform_version" => Self::MarkerEnvString(MarkerValueString::PlatformVersion), + "platform.version" => { + Self::MarkerEnvString(MarkerValueString::PlatformVersionDeprecated) + } + "python_full_version" => Self::MarkerEnvVersion(MarkerValueVersion::PythonFullVersion), + "python_version" => Self::MarkerEnvVersion(MarkerValueVersion::PythonVersion), + "sys_platform" => Self::MarkerEnvString(MarkerValueString::SysPlatform), + "sys.platform" => Self::MarkerEnvString(MarkerValueString::SysPlatformDeprecated), + "extra" => Self::Extra, + _ => return Err(format!("Invalid key: {}", s)), + }; + Ok(value) + } +} + +impl Display for MarkerValue { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Self::MarkerEnvVersion(marker_value_version) => marker_value_version.fmt(f), + Self::MarkerEnvString(marker_value_string) => marker_value_string.fmt(f), + Self::Extra => f.write_str("extra"), + Self::QuotedString(value) => write!(f, "'{}'", value), + } + } +} + +/// How to compare key and value, such as by `==`, `>` or `not in` +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub enum MarkerOperator { + /// `==` + Equal, + /// `!=` + NotEqual, + /// `>` + GreaterThan, + /// `>=` + GreaterEqual, + /// `<` + LessThan, + /// `<=` + LessEqual, + /// `~=` + TildeEqual, + /// `in` + In, + /// `not in` + NotIn, +} + +impl MarkerOperator { + /// Compare two versions, returning None for `in` and `not in` + fn to_pep440_operator(&self) -> Option { + match self { + MarkerOperator::Equal => Some(pep440_rs::Operator::Equal), + MarkerOperator::NotEqual => Some(pep440_rs::Operator::NotEqual), + MarkerOperator::GreaterThan => Some(pep440_rs::Operator::GreaterThan), + MarkerOperator::GreaterEqual => Some(pep440_rs::Operator::GreaterThanEqual), + MarkerOperator::LessThan => Some(pep440_rs::Operator::LessThan), + MarkerOperator::LessEqual => Some(pep440_rs::Operator::LessThanEqual), + MarkerOperator::TildeEqual => Some(pep440_rs::Operator::TildeEqual), + MarkerOperator::In => None, + MarkerOperator::NotIn => None, + } + } +} + +impl FromStr for MarkerOperator { + type Err = String; + + /// PEP 508 allows arbitrary whitespace between "not" and "in", and so do we + fn from_str(s: &str) -> Result { + let value = match s { + "==" => Self::Equal, + "!=" => Self::NotEqual, + ">" => Self::GreaterThan, + ">=" => Self::GreaterEqual, + "<" => Self::LessThan, + "<=" => Self::LessEqual, + "~=" => Self::TildeEqual, + "in" => Self::In, + not_space_in + if not_space_in + // start with not + .strip_prefix("not") + // ends with in + .and_then(|space_in| space_in.strip_suffix("in")) + // and has only whitespace in between + .map(|space| !space.is_empty() && space.trim().is_empty()) + .unwrap_or_default() => + { + Self::NotIn + } + other => return Err(format!("Invalid comparator: {}", other)), + }; + Ok(value) + } +} + +impl Display for MarkerOperator { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::Equal => "==", + Self::NotEqual => "!=", + Self::GreaterThan => ">", + Self::GreaterEqual => ">=", + Self::LessThan => "<", + Self::LessEqual => "<=", + Self::TildeEqual => "~=", + Self::In => "in", + Self::NotIn => "not in", + }) + } +} + +/// Helper type with a [Version] and its original text +#[cfg_attr(feature = "pyo3", pyclass(get_all, module = "pep508"))] +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct StringVersion { + /// Original unchanged string + pub string: String, + /// Parsed version + pub version: Version, +} + +impl FromStr for StringVersion { + type Err = String; + + fn from_str(s: &str) -> Result { + Ok(Self { + string: s.to_string(), + version: Version::from_str(s)?, + }) + } +} + +#[cfg(feature = "serde")] +impl Serialize for StringVersion { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&self.string) + } +} + +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for StringVersion { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let string = String::deserialize(deserializer)?; + Self::from_str(&string).map_err(de::Error::custom) + } +} + +impl Deref for StringVersion { + type Target = Version; + + fn deref(&self) -> &Self::Target { + &self.version + } +} + +/// The marker values for a python interpreter, normally the current one +/// +/// +/// +/// Some are `(String, Version)` because we have to support version comparison +#[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "pyo3", pyclass(get_all, module = "pep508"))] +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct MarkerEnvironment { + pub implementation_name: String, + pub implementation_version: StringVersion, + pub os_name: String, + pub platform_machine: String, + pub platform_python_implementation: String, + pub platform_release: String, + pub platform_system: String, + pub platform_version: String, + pub python_full_version: StringVersion, + pub python_version: StringVersion, + pub sys_platform: String, +} + +impl MarkerEnvironment { + /// Returns of the PEP 440 version typed value of the key in the current environment + fn get_version(&self, key: &MarkerValueVersion) -> &Version { + match key { + MarkerValueVersion::ImplementationVersion => &self.implementation_version.version, + MarkerValueVersion::PythonFullVersion => &self.python_full_version.version, + MarkerValueVersion::PythonVersion => &self.python_version.version, + } + } + + /// Returns of the stringly typed value of the key in the current environment + fn get_string(&self, key: &MarkerValueString) -> &str { + match key { + MarkerValueString::ImplementationName => &self.implementation_name, + MarkerValueString::OsName | MarkerValueString::OsNameDeprecated => &self.os_name, + MarkerValueString::PlatformMachine | MarkerValueString::PlatformMachineDeprecated => { + &self.platform_machine + } + MarkerValueString::PlatformPythonImplementation + | MarkerValueString::PlatformPythonImplementationDeprecated => { + &self.platform_python_implementation + } + MarkerValueString::PlatformRelease => &self.platform_release, + MarkerValueString::PlatformSystem => &self.platform_system, + MarkerValueString::PlatformVersion | MarkerValueString::PlatformVersionDeprecated => { + &self.platform_version + } + MarkerValueString::SysPlatform | MarkerValueString::SysPlatformDeprecated => { + &self.sys_platform + } + } + } +} + +#[cfg(feature = "pyo3")] +#[pymethods] +impl MarkerEnvironment { + /// Construct your own marker environment + #[new] + #[pyo3(signature = (*, + implementation_name, + implementation_version, + os_name, + platform_machine, + platform_python_implementation, + platform_release, + platform_system, + platform_version, + python_full_version, + python_version, + sys_platform + ))] + #[allow(clippy::too_many_arguments)] + fn py_new( + implementation_name: &str, + implementation_version: &str, + os_name: &str, + platform_machine: &str, + platform_python_implementation: &str, + platform_release: &str, + platform_system: &str, + platform_version: &str, + python_full_version: &str, + python_version: &str, + sys_platform: &str, + ) -> PyResult { + let implementation_version = + StringVersion::from_str(implementation_version).map_err(|err| { + PyValueError::new_err(format!( + "implementation_version is not a valid PEP440 version: {}", + err + )) + })?; + let python_full_version = StringVersion::from_str(python_full_version).map_err(|err| { + PyValueError::new_err(format!( + "python_full_version is not a valid PEP440 version: {}", + err + )) + })?; + let python_version = StringVersion::from_str(python_version).map_err(|err| { + PyValueError::new_err(format!( + "python_version is not a valid PEP440 version: {}", + err + )) + })?; + Ok(Self { + implementation_name: implementation_name.to_string(), + implementation_version, + os_name: os_name.to_string(), + platform_machine: platform_machine.to_string(), + platform_python_implementation: platform_python_implementation.to_string(), + platform_release: platform_release.to_string(), + platform_system: platform_system.to_string(), + platform_version: platform_version.to_string(), + python_full_version, + python_version, + sys_platform: sys_platform.to_string(), + }) + } + + /// Query the current python interpreter to get the correct marker value + #[staticmethod] + fn current(py: Python<'_>) -> PyResult { + let os = py.import("os")?; + let platform = py.import("platform")?; + let sys = py.import("sys")?; + let python_version_tuple: (String, String, String) = platform + .getattr("python_version_tuple")? + .call0()? + .extract()?; + + // See pseudocode at + // https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers + let name = sys.getattr("implementation")?.getattr("name")?.extract()?; + let info: &PyAny = sys.getattr("implementation")?.getattr("version")?; + let kind = info.getattr("releaselevel")?.extract::()?; + let implementation_version: String = format!( + "{}.{}.{}{}", + info.getattr("major")?.extract::()?, + info.getattr("minor")?.extract::()?, + info.getattr("micro")?.extract::()?, + if kind != "final" { + format!("{}{}", kind, info.getattr("serial")?.extract::()?) + } else { + "".to_string() + } + ); + let python_full_version: String = platform.getattr("python_version")?.call0()?.extract()?; + let python_version = format!("{}.{}", python_version_tuple.0, python_version_tuple.1); + + // This is not written down in PEP 508, but it's the only reasonable assumption to make + let implementation_version = + StringVersion::from_str(&implementation_version).map_err(|err| { + PyValueError::new_err(format!( + "Broken python implementation, implementation_version is not a valid PEP440 version: {}", + err + )) + })?; + let python_full_version = StringVersion::from_str(&python_full_version).map_err(|err| { + PyValueError::new_err(format!( + "Broken python implementation, python_full_version is not a valid PEP440 version: {}", + err + )) + })?; + let python_version = StringVersion::from_str(&python_version).map_err(|err| { + PyValueError::new_err(format!( + "Broken python implementation, python_version is not a valid PEP440 version: {}", + err + )) + })?; + Ok(Self { + implementation_name: name, + implementation_version, + os_name: os.getattr("name")?.extract()?, + platform_machine: platform.getattr("machine")?.call0()?.extract()?, + platform_python_implementation: platform + .getattr("python_implementation")? + .call0()? + .extract()?, + platform_release: platform.getattr("release")?.call0()?.extract()?, + platform_system: platform.getattr("system")?.call0()?.extract()?, + platform_version: platform.getattr("version")?.call0()?.extract()?, + python_full_version, + python_version, + sys_platform: sys.getattr("platform")?.extract()?, + }) + } +} + +/// Represents one clause such as `python_version > "3.8"` in the form +/// ```text +/// +/// ``` +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct MarkerExpression { + /// A name from the PEP508 list or a string + pub l_value: MarkerValue, + /// an operator, such as `>=` or `not in` + pub operator: MarkerOperator, + /// A name from the PEP508 list or a string + pub r_value: MarkerValue, +} + +impl MarkerExpression { + /// Evaluate a expression + fn evaluate( + &self, + env: &MarkerEnvironment, + extras: &[&str], + reporter: &mut impl FnMut(MarkerWarningKind, String, &MarkerExpression), + ) -> bool { + match &self.l_value { + // The only sound choice for this is ` ` + MarkerValue::MarkerEnvVersion(l_key) => { + let value = &self.r_value; + let (r_version, r_star) = if let MarkerValue::QuotedString(r_string) = &value { + match Version::from_str_star(r_string) { + Ok((version, star)) => (version, star), + Err(err) => { + reporter(MarkerWarningKind::Pep440Error, format!( + "Expected PEP 440 version to compare with {}, found {}, evaluating to false: {}", + l_key, self.r_value, err + ), self); + return false; + } + } + } else { + reporter(MarkerWarningKind::Pep440Error, format!( + "Expected double quoted PEP 440 version to compare with {}, found {}, evaluating to false", + l_key, self.r_value + ), self); + return false; + }; + + let operator = match self.operator.to_pep440_operator() { + None => { + reporter(MarkerWarningKind::Pep440Error, format!( + "Expected PEP 440 version operator to compare {} with '{}', found '{}', evaluating to false", + l_key, r_version, self.operator + ), self); + return false; + } + Some(operator) => operator, + }; + + let specifier = match VersionSpecifier::new(operator, r_version, r_star) { + Ok(specifier) => specifier, + Err(err) => { + reporter( + MarkerWarningKind::Pep440Error, + format!("Invalid operator/version combination: {}", err), + self, + ); + return false; + } + }; + + let l_version = env.get_version(l_key); + specifier.contains(l_version) + } + // This is half the same block as above inverted + MarkerValue::MarkerEnvString(l_key) => { + let r_string = match &self.r_value { + MarkerValue::Extra + | MarkerValue::MarkerEnvVersion(_) + | MarkerValue::MarkerEnvString(_) => { + reporter(MarkerWarningKind::MarkerMarkerComparison, "Comparing two markers with each other doesn't make any sense, evaluating to false".to_string(), self); + return false; + } + MarkerValue::QuotedString(r_string) => r_string, + }; + + let l_string = env.get_string(l_key); + self.compare_strings(l_string, r_string, reporter) + } + // `extra == '...'` + MarkerValue::Extra => { + let r_value_string = match &self.r_value { + MarkerValue::MarkerEnvVersion(_) + | MarkerValue::MarkerEnvString(_) + | MarkerValue::Extra => { + reporter(MarkerWarningKind::ExtraInvalidComparison, "Comparing extra with something other than a quoted string is wrong, evaluating to false".to_string(), self); + return false; + } + MarkerValue::QuotedString(r_value_string) => r_value_string, + }; + self.marker_compare(r_value_string, extras, reporter) + } + // This is either MarkerEnvVersion, MarkerEnvString or Extra inverted + MarkerValue::QuotedString(l_string) => { + match &self.r_value { + // The only sound choice for this is ` ` + MarkerValue::MarkerEnvVersion(r_key) => { + let l_version = match Version::from_str(l_string) { + Ok(l_version) => l_version, + Err(err) => { + reporter(MarkerWarningKind::Pep440Error, format!( + "Expected double quoted PEP 440 version to compare with {}, found {}, evaluating to false: {}", + l_string, self.r_value, err + ), self); + return false; + } + }; + let r_version = env.get_version(r_key); + + let operator = match self.operator.to_pep440_operator() { + None => { + reporter(MarkerWarningKind::Pep440Error, format!( + "Expected PEP 440 version operator to compare '{}' with {}, found '{}', evaluating to false", + l_string, r_key, self.operator + ), self); + return false; + } + Some(operator) => operator, + }; + + let specifier = + match VersionSpecifier::new(operator, r_version.clone(), false) { + Ok(specifier) => specifier, + Err(err) => { + reporter( + MarkerWarningKind::Pep440Error, + format!("Invalid operator/version combination: {}", err), + self, + ); + return false; + } + }; + + specifier.contains(&l_version) + } + // This is half the same block as above inverted + MarkerValue::MarkerEnvString(r_key) => { + let r_string = env.get_string(r_key); + self.compare_strings(l_string, r_string, reporter) + } + // `'...' == extra` + MarkerValue::Extra => self.marker_compare(l_string, extras, reporter), + // `'...' == '...'`, doesn't make much sense + MarkerValue::QuotedString(_) => { + // Not even pypa/packaging 22.0 supports this + // https://github.com/pypa/packaging/issues/632 + reporter(MarkerWarningKind::StringStringComparison, format!( + "Comparing two quoted strings with each other doesn't make sense: {}, evaluating to false", + self + ), self); + false + } + } + } + } + } + + /// Evaluates only the extras and python version part of the markers. We use this during + /// dependency resolution when we want to have packages for all possible environments but + /// already know the extras and the possible python versions (from `requires-python`) + /// + /// This considers only expression in the from `extra == '...'`, `'...' == extra`, + /// `python_version '...'` and + /// `'...' python_version`. + /// + /// Note that unlike [Self::evaluate] this does not perform any checks for bogus expressions but + /// will simply return true. + /// + /// ```rust + /// # use std::collections::HashSet; + /// # use std::str::FromStr; + /// # use pep508_rs::{MarkerTree, Pep508Error}; + /// # use pep440_rs::Version; + /// + /// # fn main() -> Result<(), Pep508Error> { + /// let marker_tree = MarkerTree::from_str(r#"("linux" in sys_platform) and extra == 'day'"#)?; + /// let versions: Vec = (8..12).map(|minor| Version::from_release(vec![3, minor])).collect(); + /// assert!(marker_tree.evaluate_extras_and_python_version(&["day".to_string()].into(), &versions)); + /// assert!(!marker_tree.evaluate_extras_and_python_version(&["night".to_string()].into(), &versions)); + /// + /// let marker_tree = MarkerTree::from_str(r#"extra == 'day' and python_version < '3.11' and '3.10' <= python_version"#)?; + /// assert!(!marker_tree.evaluate_extras_and_python_version(&["day".to_string()].into(), &vec![Version::from_release(vec![3, 9])])); + /// assert!(marker_tree.evaluate_extras_and_python_version(&["day".to_string()].into(), &vec![Version::from_release(vec![3, 10])])); + /// assert!(!marker_tree.evaluate_extras_and_python_version(&["day".to_string()].into(), &vec![Version::from_release(vec![3, 11])])); + /// # Ok(()) + /// # } + /// ``` + fn evaluate_extras_and_python_version( + &self, + extras: &HashSet, + python_versions: &[Version], + ) -> bool { + match (&self.l_value, &self.operator, &self.r_value) { + // `extra == '...'` + (MarkerValue::Extra, MarkerOperator::Equal, MarkerValue::QuotedString(r_string)) => { + extras.contains(r_string) + } + // `'...' == extra` + (MarkerValue::QuotedString(l_string), MarkerOperator::Equal, MarkerValue::Extra) => { + extras.contains(l_string) + } + // `extra != '...'` + (MarkerValue::Extra, MarkerOperator::NotEqual, MarkerValue::QuotedString(r_string)) => { + !extras.contains(r_string) + } + // `'...' != extra` + (MarkerValue::QuotedString(l_string), MarkerOperator::NotEqual, MarkerValue::Extra) => { + !extras.contains(l_string) + } + ( + MarkerValue::MarkerEnvVersion(MarkerValueVersion::PythonVersion), + operator, + MarkerValue::QuotedString(r_string), + ) => { + // ignore all errors block + (|| { + // The right hand side is allowed to contain a star, e.g. `python_version == '3.*'` + let (r_version, r_star) = Version::from_str_star(r_string).ok()?; + let operator = operator.to_pep440_operator()?; + // operator and right hand side make the specifier + let specifier = VersionSpecifier::new(operator, r_version, r_star).ok()?; + + let compatible = python_versions + .iter() + .any(|l_version| specifier.contains(l_version)); + Some(compatible) + })() + .unwrap_or(true) + } + ( + MarkerValue::QuotedString(l_string), + operator, + MarkerValue::MarkerEnvVersion(MarkerValueVersion::PythonVersion), + ) => { + // ignore all errors block + (|| { + // Not star allowed here, `'3.*' == python_version` is not a valid PEP 440 + // comparison + let l_version = Version::from_str(l_string).ok()?; + let operator = operator.to_pep440_operator()?; + + let compatible = python_versions.iter().any(|r_version| { + // operator and right hand side make the specifier and in this case the + // right hand is `python_version` so changes every iteration + match VersionSpecifier::new(operator.clone(), r_version.clone(), false) { + Ok(specifier) => specifier.contains(&l_version), + Err(_) => true, + } + }); + + Some(compatible) + })() + .unwrap_or(true) + } + _ => true, + } + } + + /// Compare strings by PEP 508 logic, with warnings + fn compare_strings( + &self, + l_string: &str, + r_string: &str, + reporter: &mut impl FnMut(MarkerWarningKind, String, &MarkerExpression), + ) -> bool { + match self.operator { + MarkerOperator::Equal => l_string == r_string, + MarkerOperator::NotEqual => l_string != r_string, + MarkerOperator::GreaterThan => { + reporter( + MarkerWarningKind::LexicographicComparison, + format!("Comparing {} and {} lexicographically", l_string, r_string), + self, + ); + l_string > r_string + } + MarkerOperator::GreaterEqual => { + reporter( + MarkerWarningKind::LexicographicComparison, + format!("Comparing {} and {} lexicographically", l_string, r_string), + self, + ); + l_string >= r_string + } + MarkerOperator::LessThan => { + reporter( + MarkerWarningKind::LexicographicComparison, + format!("Comparing {} and {} lexicographically", l_string, r_string), + self, + ); + l_string < r_string + } + MarkerOperator::LessEqual => { + reporter( + MarkerWarningKind::LexicographicComparison, + format!("Comparing {} and {} lexicographically", l_string, r_string), + self, + ); + l_string <= r_string + } + MarkerOperator::TildeEqual => { + reporter( + MarkerWarningKind::LexicographicComparison, + format!("Can't compare {} and {} with `~=`", l_string, r_string), + self, + ); + false + } + MarkerOperator::In => r_string.contains(l_string), + MarkerOperator::NotIn => !r_string.contains(l_string), + } + } + + // The `marker '...'` comparison + fn marker_compare( + &self, + value: &str, + extras: &[&str], + reporter: &mut impl FnMut(MarkerWarningKind, String, &MarkerExpression), + ) -> bool { + match self.operator { + // TODO: normalize extras + MarkerOperator::Equal => extras.contains(&value), + MarkerOperator::NotEqual => !extras.contains(&value), + _ => { + reporter(MarkerWarningKind::ExtraInvalidComparison, "Comparing extra with something other than equal (`==`) or unequal (`!=`) is wrong, evaluating to false".to_string(), self); + false + } + } + } +} + +impl FromStr for MarkerExpression { + type Err = Pep508Error; + + fn from_str(s: &str) -> Result { + let mut chars = CharIter::new(s); + let expression = parse_marker_key_op_value(&mut chars)?; + chars.eat_whitespace(); + if let Some((pos, unexpected)) = chars.next() { + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Unexpected character '{}', expected end of input", + unexpected + )), + start: pos, + len: chars.chars.clone().count(), + input: chars.copy_chars(), + }); + } + Ok(expression) + } +} + +impl Display for MarkerExpression { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{} {} {}", self.l_value, self.operator, self.r_value) + } +} + +/// Represents one of the nested marker expressions with and/or/parentheses +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub enum MarkerTree { + /// A simple expression such as `python_version > "3.8"` + Expression(MarkerExpression), + /// An and between nested expressions, such as + /// `python_version > "3.8" and implementation_name == 'cpython'` + And(Vec), + /// An or between nested expressions, such as + /// `python_version > "3.8" or implementation_name == 'cpython'` + Or(Vec), +} + +impl FromStr for MarkerTree { + type Err = Pep508Error; + + fn from_str(markers: &str) -> Result { + parse_markers(markers) + } +} + +impl MarkerTree { + /// Does this marker apply in the given environment? + pub fn evaluate(&self, env: &MarkerEnvironment, extras: &[&str]) -> bool { + let mut reporter = |_kind, message, _marker_expression: &MarkerExpression| { + warn!("{}", message); + }; + self.report_deprecated_options(&mut reporter); + match self { + MarkerTree::Expression(expression) => expression.evaluate(env, extras, &mut reporter), + MarkerTree::And(expressions) => expressions + .iter() + .all(|x| x.evaluate_reporter_impl(env, extras, &mut reporter)), + MarkerTree::Or(expressions) => expressions + .iter() + .any(|x| x.evaluate_reporter_impl(env, extras, &mut reporter)), + } + } + + /// Same as [Self::evaluate], but instead of using logging to warn, you can pass your own + /// handler for warnings + pub fn evaluate_reporter( + &self, + env: &MarkerEnvironment, + extras: &[&str], + reporter: &mut impl FnMut(MarkerWarningKind, String, &MarkerExpression), + ) -> bool { + self.report_deprecated_options(reporter); + self.evaluate_reporter_impl(env, extras, reporter) + } + + fn evaluate_reporter_impl( + &self, + env: &MarkerEnvironment, + extras: &[&str], + reporter: &mut impl FnMut(MarkerWarningKind, String, &MarkerExpression), + ) -> bool { + match self { + MarkerTree::Expression(expression) => expression.evaluate(env, extras, reporter), + MarkerTree::And(expressions) => expressions + .iter() + .all(|x| x.evaluate_reporter_impl(env, extras, reporter)), + MarkerTree::Or(expressions) => expressions + .iter() + .any(|x| x.evaluate_reporter_impl(env, extras, reporter)), + } + } + + /// Checks if the requirement should be activated with the given set of active extras and a set + /// of possible python versions (from `requires-python`) without evaluating the remaining + /// environment markers, i.e. if there is potentially an environment that could activate this + /// requirement. + /// + /// Note that unlike [Self::evaluate] this does not perform any checks for bogus expressions but + /// will simply return true. As caller you should separately perform a check with an environment + /// and forward all warnings. + pub fn evaluate_extras_and_python_version( + &self, + extras: &HashSet, + python_versions: &[Version], + ) -> bool { + match self { + MarkerTree::Expression(expression) => { + expression.evaluate_extras_and_python_version(extras, python_versions) + } + MarkerTree::And(expressions) => expressions + .iter() + .all(|x| x.evaluate_extras_and_python_version(extras, python_versions)), + MarkerTree::Or(expressions) => expressions + .iter() + .any(|x| x.evaluate_extras_and_python_version(extras, python_versions)), + } + } + + /// Same as [Self::evaluate], but instead of using logging to warn, you get a Vec with all + /// warnings collected + pub fn evaluate_collect_warnings( + &self, + env: &MarkerEnvironment, + extras: &[&str], + ) -> (bool, Vec<(MarkerWarningKind, String, String)>) { + let mut warnings = Vec::new(); + let mut reporter = |kind, warning, marker: &MarkerExpression| { + warnings.push((kind, warning, marker.to_string())) + }; + self.report_deprecated_options(&mut reporter); + let result = self.evaluate_reporter_impl(env, extras, &mut reporter); + (result, warnings) + } + + /// Report the deprecated marker from + fn report_deprecated_options( + &self, + reporter: &mut impl FnMut(MarkerWarningKind, String, &MarkerExpression), + ) { + match self { + MarkerTree::Expression(expression) => { + for value in [&expression.l_value, &expression.r_value] { + match value { + MarkerValue::MarkerEnvString(MarkerValueString::OsNameDeprecated) => { + reporter( + MarkerWarningKind::DeprecatedMarkerName, + "os.name is deprecated in favor of os_name".to_string(), + expression, + ); + } + MarkerValue::MarkerEnvString( + MarkerValueString::PlatformMachineDeprecated, + ) => { + reporter( + MarkerWarningKind::DeprecatedMarkerName, + "platform.machine is deprecated in favor of platform_machine" + .to_string(), + expression, + ); + } + MarkerValue::MarkerEnvString( + MarkerValueString::PlatformPythonImplementationDeprecated, + ) => { + reporter( + MarkerWarningKind::DeprecatedMarkerName, + "platform.python_implementation is deprecated in favor of platform_python_implementation".to_string(), + expression, + ); + } + MarkerValue::MarkerEnvString( + MarkerValueString::PlatformVersionDeprecated, + ) => { + reporter( + MarkerWarningKind::DeprecatedMarkerName, + "platform.version is deprecated in favor of platform_version" + .to_string(), + expression, + ); + } + MarkerValue::MarkerEnvString(MarkerValueString::SysPlatformDeprecated) => { + reporter( + MarkerWarningKind::DeprecatedMarkerName, + "sys.platform is deprecated in favor of sys_platform".to_string(), + expression, + ); + } + _ => {} + } + } + } + MarkerTree::And(expressions) => { + for expression in expressions { + expression.report_deprecated_options(reporter) + } + } + MarkerTree::Or(expressions) => { + for expression in expressions { + expression.report_deprecated_options(reporter) + } + } + } + } +} + +impl Display for MarkerTree { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let format_inner = |expression: &MarkerTree| { + if matches!(expression, MarkerTree::Expression(_)) { + format!("{}", expression) + } else { + format!("({})", expression) + } + }; + match self { + MarkerTree::Expression(expression) => write!(f, "{}", expression), + MarkerTree::And(and_list) => f.write_str( + &and_list + .iter() + .map(format_inner) + .collect::>() + .join(" and "), + ), + MarkerTree::Or(or_list) => f.write_str( + &or_list + .iter() + .map(format_inner) + .collect::>() + .join(" or "), + ), + } + } +} + +/// ```text +/// version_cmp = wsp* <'<=' | '<' | '!=' | '==' | '>=' | '>' | '~=' | '==='> +/// marker_op = version_cmp | (wsp* 'in') | (wsp* 'not' wsp+ 'in') +/// ``` +fn parse_marker_operator(chars: &mut CharIter) -> Result { + let (operator, start, len) = + chars.take_while(|char| !char.is_whitespace() && char != '\'' && char != '"'); + if operator == "not" { + // 'not' wsp+ 'in' + match chars.next() { + None => { + return Err(Pep508Error { + message: Pep508ErrorSource::String( + "Expected whitespace after 'not', found end of input".to_string(), + ), + start: chars.get_pos(), + len: 1, + input: chars.copy_chars(), + }) + } + Some((_, whitespace)) if whitespace.is_whitespace() => {} + Some((pos, other)) => { + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected whitespace after 'not', found '{}'", + other + )), + start: pos, + len: 1, + input: chars.copy_chars(), + }) + } + }; + chars.eat_whitespace(); + chars.next_expect_char('i', chars.get_pos())?; + chars.next_expect_char('n', chars.get_pos())?; + return Ok(MarkerOperator::NotIn); + } + MarkerOperator::from_str(&operator).map_err(|_| Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected a valid marker operator (such as '>=' or 'not in'), found '{}'", + operator + )), + start, + len, + input: chars.copy_chars(), + }) +} + +/// Either a single or double quoted string or one of 'python_version', 'python_full_version', +/// 'os_name', 'sys_platform', 'platform_release', 'platform_system', 'platform_version', +/// 'platform_machine', 'platform_python_implementation', 'implementation_name', +/// 'implementation_version', 'extra' +fn parse_marker_value(chars: &mut CharIter) -> Result { + // > User supplied constants are always encoded as strings with either ' or " quote marks. Note + // > that backslash escapes are not defined, but existing implementations do support them. They + // > are not included in this specification because they add complexity and there is no observable + // > need for them today. Similarly we do not define non-ASCII character support: all the runtime + // > variables we are referencing are expected to be ASCII-only. + match chars.peek() { + None => Err(Pep508Error { + message: Pep508ErrorSource::String( + "Expected marker value, found end of dependency specification".to_string(), + ), + start: chars.get_pos(), + len: 1, + input: chars.copy_chars(), + }), + // It can be a string ... + Some((start_pos, quotation_mark @ ('"' | '\''))) => { + chars.next(); + let (value, _, _) = chars.take_while(|c| c != quotation_mark); + chars.next_expect_char(quotation_mark, start_pos)?; + Ok(MarkerValue::string_value(value)) + } + // ... or it can be a keyword + Some(_) => { + let (key, start, len) = chars.take_while(|char| { + !char.is_whitespace() && !['>', '=', '<', '!', '~', ')'].contains(&char) + }); + MarkerValue::from_str(&key).map_err(|_| Pep508Error { + message: Pep508ErrorSource::String(format!( + "Expected a valid marker name, found '{}'", + key + )), + start, + len, + input: chars.copy_chars(), + }) + } + } +} + +/// ```text +/// marker_var:l marker_op:o marker_var:r +/// ``` +fn parse_marker_key_op_value(chars: &mut CharIter) -> Result { + chars.eat_whitespace(); + let lvalue = parse_marker_value(chars)?; + chars.eat_whitespace(); + // "not in" and "in" must be preceded by whitespace. We must already have matched a whitespace + // when we're here because other `parse_marker_key` would have pulled the characters in and + // errored + let operator = parse_marker_operator(chars)?; + chars.eat_whitespace(); + let rvalue = parse_marker_value(chars)?; + Ok(MarkerExpression { + l_value: lvalue, + operator, + r_value: rvalue, + }) +} + +/// ```text +/// marker_expr = marker_var:l marker_op:o marker_var:r -> (o, l, r) +/// | wsp* '(' marker:m wsp* ')' -> m +/// ``` +fn parse_marker_expr(chars: &mut CharIter) -> Result { + chars.eat_whitespace(); + if let Some(start_pos) = chars.eat('(') { + let marker = parse_marker_or(chars)?; + chars.next_expect_char(')', start_pos)?; + Ok(marker) + } else { + Ok(MarkerTree::Expression(parse_marker_key_op_value(chars)?)) + } +} + +/// ```text +/// marker_and = marker_expr:l wsp* 'and' marker_expr:r -> ('and', l, r) +/// | marker_expr:m -> m +/// ``` +fn parse_marker_and(chars: &mut CharIter) -> Result { + parse_marker_op(chars, "and", MarkerTree::And, parse_marker_expr) +} + +/// ```text +/// marker_or = marker_and:l wsp* 'or' marker_and:r -> ('or', l, r) +/// | marker_and:m -> m +/// ``` +fn parse_marker_or(chars: &mut CharIter) -> Result { + parse_marker_op(chars, "or", MarkerTree::Or, parse_marker_and) +} + +/// Parses both `marker_and` and `marker_or` +fn parse_marker_op( + chars: &mut CharIter, + op: &str, + op_constructor: fn(Vec) -> MarkerTree, + parse_inner: fn(&mut CharIter) -> Result, +) -> Result { + // marker_and or marker_expr + let first_element = parse_inner(chars)?; + // wsp* + chars.eat_whitespace(); + // Check if we're done here instead of invoking the whole vec allocating loop + if matches!(chars.peek_char(), None | Some(')')) { + return Ok(first_element); + } + + let mut expressions = Vec::with_capacity(1); + expressions.push(first_element); + loop { + // wsp* + chars.eat_whitespace(); + // ('or' marker_and) or ('and' marker_or) + let (maybe_op, _start, _len) = chars.peek_while(|c| !c.is_whitespace()); + match maybe_op { + value if value == op => { + chars.take_while(|c| !c.is_whitespace()); + let expression = parse_inner(chars)?; + expressions.push(expression); + } + _ => { + // Build minimal trees + return if expressions.len() == 1 { + Ok(expressions.remove(0)) + } else { + Ok(op_constructor(expressions)) + }; + } + } + } +} + +/// ```text +/// marker = marker_or +/// ``` +pub(crate) fn parse_markers_impl(chars: &mut CharIter) -> Result { + let marker = parse_marker_or(chars)?; + chars.eat_whitespace(); + if let Some((pos, unexpected)) = chars.next() { + // If we're here, both parse_marker_or and parse_marker_and returned because the next + // character was neither "and" nor "or" + return Err(Pep508Error { + message: Pep508ErrorSource::String(format!( + "Unexpected character '{}', expected 'and', 'or' or end of input", + unexpected + )), + start: pos, + len: chars.chars.clone().count(), + input: chars.copy_chars(), + }); + }; + Ok(marker) +} + +/// Parses markers such as `python_version < '3.8'` or +/// `python_version == "3.10" and (sys_platform == "win32" or (os_name == "linux" and implementation_name == 'cpython'))` +fn parse_markers(markers: &str) -> Result { + let mut chars = CharIter::new(markers); + parse_markers_impl(&mut chars) +} + +#[cfg(test)] +mod test { + use crate::marker::{MarkerEnvironment, StringVersion}; + use crate::{MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, MarkerValueString}; + use indoc::indoc; + use log::Level; + use std::str::FromStr; + + fn assert_err(input: &str, error: &str) { + assert_eq!(MarkerTree::from_str(input).unwrap_err().to_string(), error); + } + + fn env37() -> MarkerEnvironment { + let v37 = StringVersion::from_str("3.7").unwrap(); + + MarkerEnvironment { + implementation_name: "".to_string(), + implementation_version: v37.clone(), + os_name: "linux".to_string(), + platform_machine: "".to_string(), + platform_python_implementation: "".to_string(), + platform_release: "".to_string(), + platform_system: "".to_string(), + platform_version: "".to_string(), + python_full_version: v37.clone(), + python_version: v37, + sys_platform: "linux".to_string(), + } + } + + /// Copied from + #[test] + fn test_marker_equivalence() { + let values = [ + (r#"python_version == '2.7'"#, r#"python_version == "2.7""#), + (r#"python_version == "2.7""#, r#"python_version == "2.7""#), + ( + r#"python_version == "2.7" and os_name == "posix""#, + r#"python_version == "2.7" and os_name == "posix""#, + ), + ( + r#"python_version == "2.7" or os_name == "posix""#, + r#"python_version == "2.7" or os_name == "posix""#, + ), + ( + r#"python_version == "2.7" and os_name == "posix" or sys_platform == "win32""#, + r#"python_version == "2.7" and os_name == "posix" or sys_platform == "win32""#, + ), + (r#"(python_version == "2.7")"#, r#"python_version == "2.7""#), + ( + r#"(python_version == "2.7" and sys_platform == "win32")"#, + r#"python_version == "2.7" and sys_platform == "win32""#, + ), + ( + r#"python_version == "2.7" and (sys_platform == "win32" or sys_platform == "linux")"#, + r#"python_version == "2.7" and (sys_platform == "win32" or sys_platform == "linux")"#, + ), + ]; + for (a, b) in values { + assert_eq!( + MarkerTree::from_str(a).unwrap(), + MarkerTree::from_str(b).unwrap(), + "{} {}", + a, + b + ); + } + } + + #[test] + fn test_marker_evaluation() { + let v27 = StringVersion::from_str("2.7").unwrap(); + let env27 = MarkerEnvironment { + implementation_name: "".to_string(), + implementation_version: v27.clone(), + os_name: "linux".to_string(), + platform_machine: "".to_string(), + platform_python_implementation: "".to_string(), + platform_release: "".to_string(), + platform_system: "".to_string(), + platform_version: "".to_string(), + python_full_version: v27.clone(), + python_version: v27, + sys_platform: "linux".to_string(), + }; + let env37 = env37(); + let marker1 = MarkerTree::from_str("python_version == '2.7'").unwrap(); + let marker2 = MarkerTree::from_str( + "os_name == \"linux\" or python_version == \"3.7\" and sys_platform == \"win32\"", + ) + .unwrap(); + let marker3 = MarkerTree::from_str( + "python_version == \"2.7\" and (sys_platform == \"win32\" or sys_platform == \"linux\")", + ).unwrap(); + assert!(marker1.evaluate(&env27, &[])); + assert!(!marker1.evaluate(&env37, &[])); + assert!(marker2.evaluate(&env27, &[])); + assert!(marker2.evaluate(&env37, &[])); + assert!(marker3.evaluate(&env27, &[])); + assert!(!marker3.evaluate(&env37, &[])); + } + + #[test] + fn warnings() { + let env37 = env37(); + testing_logger::setup(); + let compare_keys = MarkerTree::from_str("platform_version == sys_platform").unwrap(); + compare_keys.evaluate(&env37, &[]); + testing_logger::validate(|captured_logs| { + assert_eq!( + captured_logs[0].body, + "Comparing two markers with each other doesn't make any sense, evaluating to false" + ); + assert_eq!(captured_logs[0].level, Level::Warn); + assert_eq!(captured_logs.len(), 1); + }); + let non_pep440 = MarkerTree::from_str("python_version >= '3.9.'").unwrap(); + non_pep440.evaluate(&env37, &[]); + testing_logger::validate(|captured_logs| { + assert_eq!( + captured_logs[0].body, + "Expected PEP 440 version to compare with python_version, found '3.9.', evaluating to false: Version `3.9.` doesn't match PEP 440 rules" + ); + assert_eq!(captured_logs[0].level, Level::Warn); + assert_eq!(captured_logs.len(), 1); + }); + let string_string = MarkerTree::from_str("'b' >= 'a'").unwrap(); + string_string.evaluate(&env37, &[]); + testing_logger::validate(|captured_logs| { + assert_eq!( + captured_logs[0].body, + "Comparing two quoted strings with each other doesn't make sense: 'b' >= 'a', evaluating to false" + ); + assert_eq!(captured_logs[0].level, Level::Warn); + assert_eq!(captured_logs.len(), 1); + }); + let string_string = MarkerTree::from_str(r#"os.name == 'posix' and platform.machine == 'x86_64' and platform.python_implementation == 'CPython' and 'Ubuntu' in platform.version and sys.platform == 'linux'"#).unwrap(); + string_string.evaluate(&env37, &[]); + testing_logger::validate(|captured_logs| { + let messages: Vec<_> = captured_logs + .iter() + .map(|message| { + assert_eq!(message.level, Level::Warn); + &message.body + }) + .collect(); + let expected = [ + "os.name is deprecated in favor of os_name", + "platform.machine is deprecated in favor of platform_machine", + "platform.python_implementation is deprecated in favor of platform_python_implementation", + "platform.version is deprecated in favor of platform_version", + "sys.platform is deprecated in favor of sys_platform" + ]; + assert_eq!(messages, &expected); + }); + } + + #[test] + fn test_not_in() { + MarkerTree::from_str("'posix' not in os_name").unwrap(); + } + + #[test] + fn test_marker_version_star() { + let env37 = env37(); + let (result, warnings) = MarkerTree::from_str("python_version == '3.7.*'") + .unwrap() + .evaluate_collect_warnings(&env37, &[]); + assert_eq!(warnings, &[]); + assert!(result); + } + + #[test] + fn test_tilde_equal() { + let env37 = env37(); + let (result, warnings) = MarkerTree::from_str("python_version ~= '3.7'") + .unwrap() + .evaluate_collect_warnings(&env37, &[]); + assert_eq!(warnings, &[]); + assert!(result); + } + + #[test] + fn test_closing_parentheses() { + MarkerTree::from_str(r#"( "linux" in sys_platform) and extra == 'all'"#).unwrap(); + } + + #[test] + fn wrong_quotes_dot_star() { + assert_err( + r#"python_version == "3.8".* and python_version >= "3.8""#, + indoc! {r#" + Unexpected character '.', expected 'and', 'or' or end of input + python_version == "3.8".* and python_version >= "3.8" + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"# + }, + ); + assert_err( + r#"python_version == "3.8".*"#, + indoc! {r#" + Unexpected character '.', expected 'and', 'or' or end of input + python_version == "3.8".* + ^"# + }, + ); + } + + #[test] + fn test_marker_expression() { + assert_eq!( + MarkerExpression::from_str(r#"os_name == "nt""#).unwrap(), + MarkerExpression { + l_value: MarkerValue::MarkerEnvString(MarkerValueString::OsName), + operator: MarkerOperator::Equal, + r_value: MarkerValue::QuotedString("nt".to_string()), + } + ); + } + + #[test] + fn test_marker_expression_to_long() { + assert_eq!( + MarkerExpression::from_str(r#"os_name == "nt" and python_version >= "3.8""#) + .unwrap_err() + .to_string(), + indoc! {r#" + Unexpected character 'a', expected end of input + os_name == "nt" and python_version >= "3.8" + ^^^^^^^^^^^^^^^^^^^^^^^^^^"# + }, + ); + } + + #[cfg(feature = "serde")] + #[test] + fn test_marker_environment_from_json() { + let _env: MarkerEnvironment = serde_json::from_str( + r##"{ + "implementation_name": "cpython", + "implementation_version": "3.7.13", + "os_name": "posix", + "platform_machine": "x86_64", + "platform_python_implementation": "CPython", + "platform_release": "5.4.188+", + "platform_system": "Linux", + "platform_version": "#1 SMP Sun Apr 24 10:03:06 PDT 2022", + "python_full_version": "3.7.13", + "python_version": "3.7", + "sys_platform": "linux" + }"##, + ) + .unwrap(); + } +} diff --git a/crates/pep508-rs/src/modern.rs b/crates/pep508-rs/src/modern.rs new file mode 100644 index 000000000..fadcd5edf --- /dev/null +++ b/crates/pep508-rs/src/modern.rs @@ -0,0 +1,362 @@ +//! WIP Draft for a poetry/cargo like, modern dependency specification +//! +//! This still needs +//! * Better VersionSpecifier (e.g. allowing `^1.19`) and it's sentry integration +//! * PEP 440/PEP 508 translation +//! * a json schema + +#![cfg(feature = "modern")] + +use crate::MarkerValue::QuotedString; +use crate::{MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, Requirement, VersionOrUrl}; +use anyhow::{bail, format_err, Context}; +use once_cell::sync::Lazy; +use pep440_rs::{Operator, Pep440Error, Version, VersionSpecifier, VersionSpecifiers}; +use regex::Regex; +use serde::{de, Deserialize, Deserializer, Serialize}; +use std::collections::HashMap; +use std::str::FromStr; +use url::Url; + +/// Shared fields for version/git/file/path/url dependencies (`optional`, `extras`, `markers`) +#[derive(Eq, PartialEq, Debug, Clone, Deserialize, Serialize)] +pub struct RequirementModernCommon { + /// Whether this is an optional dependency. This is inverted from PEP 508 extras where the + /// requirements has the extras attached, as here the extras has a table where each extra + /// says which optional dependencies it activates + #[serde(default)] + pub optional: bool, + /// The list of extras + pub extras: Option>, + /// The list of markers . + /// Note that this will not accept extras. + /// + /// TODO: Deserialize into `MarkerTree` that does not accept the extras key + pub markers: Option, +} + +/// Instead of only PEP 440 specifier, you can also set a single version (exact) or TODO use +/// the semver caret +#[derive(Eq, PartialEq, Debug, Clone, Serialize)] +pub enum VersionSpecifierModern { + /// e.g. `4.12.1-beta.1` + Version(Version), + /// e.g. `== 4.12.1-beta.1` or `>=3.8,<4.0` + VersionSpecifier(VersionSpecifiers), +} + +impl VersionSpecifierModern { + /// `4.12.1-beta.1` -> `== 4.12.1-beta.1` + /// `== 4.12.1-beta.1` -> `== 4.12.1-beta.1` + /// `>=3.8,<4.0` -> `>=3.8,<4.0` + /// TODO: `^1.19` -> `>=1.19,<2.0` + pub fn to_pep508_specifier(&self) -> VersionSpecifiers { + match self { + // unwrapping is safe here because we're using Operator::Equal + VersionSpecifierModern::Version(version) => { + [VersionSpecifier::new(Operator::Equal, version.clone(), false).unwrap()] + .into_iter() + .collect() + } + VersionSpecifierModern::VersionSpecifier(version_specifiers) => { + version_specifiers.clone() + } + } + } +} + +impl FromStr for VersionSpecifierModern { + /// TODO: Modern needs it's own error type + type Err = Pep440Error; + + /// dispatching between just a version and a version specifier set + fn from_str(s: &str) -> Result { + // If it starts with + if s.trim_start().starts_with(|x: char| x.is_ascii_digit()) { + Ok(Self::Version(Version::from_str(s).map_err(|err| { + // TODO: Fix this in pep440_rs + Pep440Error { + message: err, + line: s.to_string(), + start: 0, + width: 1, + } + })?)) + } else if s.starts_with('^') { + todo!("TODO caret operator is not supported yet"); + } else { + Ok(Self::VersionSpecifier(VersionSpecifiers::from_str(s)?)) + } + } +} + +/// https://github.com/serde-rs/serde/issues/908#issuecomment-298027413 +impl<'de> Deserialize<'de> for VersionSpecifierModern { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + FromStr::from_str(&s).map_err(de::Error::custom) + } +} + +/// WIP Draft for a poetry/cargo like, modern dependency specification +#[derive(Eq, PartialEq, Debug, Clone, Deserialize, Serialize)] +#[serde(untagged)] +pub enum RequirementModern { + /// e.g. `numpy = "1.24.1"` + Dependency(VersionSpecifierModern), + /// e.g. `numpy = { version = "1.24.1" }` or `django-anymail = { version = "1.24.1", extras = ["sendgrid"], optional = true }` + LongDependency { + /// e.g. `1.2.3.beta1` + version: VersionSpecifierModern, + #[serde(flatten)] + #[allow(missing_docs)] + common: RequirementModernCommon, + }, + /// e.g. `tqdm = { git = "https://github.com/tqdm/tqdm", rev = "0bb91857eca0d4aea08f66cf1c8949abe0cd6b7a" }` + GitDependency { + /// URL of the git repository e.g. `https://github.com/tqdm/tqdm` + git: Url, + /// The git branch to use + branch: Option, + /// The git revision to use. Can be the short revision (`0bb9185`) or the long revision + /// (`0bb91857eca0d4aea08f66cf1c8949abe0cd6b7a`) + rev: Option, + #[serde(flatten)] + #[allow(missing_docs)] + common: RequirementModernCommon, + }, + /// e.g. `tqdm = { file = "tqdm-4.65.0-py3-none-any.whl" }` + FileDependency { + /// Path to a source distribution (e.g. `tqdm-4.65.0.tar.gz`) or wheel (e.g. `tqdm-4.65.0-py3-none-any.whl`) + file: String, + #[serde(flatten)] + #[allow(missing_docs)] + common: RequirementModernCommon, + }, + /// Path to a directory with source distributions and/or wheels e.g. + /// `scilib_core = { path = "build_wheels/scilib_core/" }`. + /// + /// Use this option if you e.g. have multiple platform platform dependent wheels or want to + /// have a fallback to a source distribution for you wheel. + PathDependency { + /// e.g. `dist/`, `target/wheels` or `vendored` + path: String, + #[serde(flatten)] + #[allow(missing_docs)] + common: RequirementModernCommon, + }, + /// e.g. `jax = { url = "https://storage.googleapis.com/jax-releases/cuda112/jaxlib-0.1.64+cuda112-cp39-none-manylinux2010_x86_64.whl" }` + UrlDependency { + /// URL to a source distribution or wheel. The file available there must be named + /// appropriately for a source distribution or a wheel. + url: Url, + #[serde(flatten)] + #[allow(missing_docs)] + common: RequirementModernCommon, + }, +} + +/// Adopted from the grammar at +static EXTRA_REGEX: Lazy = + Lazy::new(|| Regex::new(r"^[a-zA-Z0-9]([-_.]*[a-zA-Z0-9])*$").unwrap()); + +impl RequirementModern { + /// Check the things that serde doesn't check, namely that extra names are valid + pub fn check(&self) -> anyhow::Result<()> { + match self { + Self::LongDependency { common, .. } + | Self::GitDependency { common, .. } + | Self::FileDependency { common, .. } + | Self::PathDependency { common, .. } + | Self::UrlDependency { common, .. } => { + if let Some(extras) = &common.extras { + for extra in extras { + if !EXTRA_REGEX.is_match(extra) { + bail!("Not a valid extra name: '{}'", extra) + } + } + } + } + _ => {} + } + Ok(()) + } + + /// WIP Converts the modern format to PEP 508 + pub fn to_pep508( + &self, + name: &str, + extras: &HashMap>, + ) -> Result { + let default = RequirementModernCommon { + optional: false, + extras: None, + markers: None, + }; + + let common = match self { + RequirementModern::Dependency(..) => &default, + RequirementModern::LongDependency { common, .. } + | RequirementModern::GitDependency { common, .. } + | RequirementModern::FileDependency { common, .. } + | RequirementModern::PathDependency { common, .. } + | RequirementModern::UrlDependency { common, .. } => common, + }; + + let marker = if common.optional { + // invert the extras table from the modern format + // extra1 -> optional_dep1, optional_dep2, ... + // to the PEP 508 format + // optional_dep1; extra == "extra1" or extra == "extra2" + let dep_markers = extras + .iter() + .filter(|(_marker, dependencies)| dependencies.contains(&name.to_string())) + .map(|(marker, _dependencies)| { + MarkerTree::Expression(MarkerExpression { + l_value: MarkerValue::Extra, + operator: MarkerOperator::Equal, + r_value: QuotedString(marker.to_string()), + }) + }) + .collect(); + // any of these extras activates the dependency -> or clause + let dep_markers = MarkerTree::Or(dep_markers); + let joined_marker = if let Some(user_markers) = &common.markers { + let user_markers = MarkerTree::from_str(user_markers) + .context("TODO: parse this in serde already")?; + // but the dependency needs to be activated and match the other markers + // -> and clause + MarkerTree::And(vec![user_markers, dep_markers]) + } else { + dep_markers + }; + Some(joined_marker) + } else { + None + }; + + if let Some(extras) = &common.extras { + debug_assert!(extras.iter().all(|extra| EXTRA_REGEX.is_match(extra))); + } + + let version_or_url = match self { + RequirementModern::Dependency(version) => { + VersionOrUrl::VersionSpecifier(version.to_pep508_specifier()) + } + RequirementModern::LongDependency { version, .. } => { + VersionOrUrl::VersionSpecifier(version.to_pep508_specifier()) + } + RequirementModern::GitDependency { + git, branch, rev, .. + } => { + // TODO: Read https://peps.python.org/pep-0440/#direct-references properly + // set_scheme doesn't like us adding `git+` to https, therefore this hack + let mut url = + Url::parse(&format!("git+{}", git)).expect("TODO: Better url validation"); + match (branch, rev) { + (Some(_branch), Some(_rev)) => { + bail!("You can set both branch and rev (for {})", name) + } + (Some(branch), None) => url.set_path(&format!("{}@{}", url.path(), branch)), + (None, Some(rev)) => url.set_path(&format!("{}@{}", url.path(), rev)), + (None, None) => {} + } + + VersionOrUrl::Url(url) + } + RequirementModern::FileDependency { file, .. } => VersionOrUrl::Url( + Url::from_file_path(file) + .map_err(|()| format_err!("File must be absolute (for {})", name))?, + ), + RequirementModern::PathDependency { path, .. } => VersionOrUrl::Url( + Url::from_directory_path(path) + .map_err(|()| format_err!("Path must be absolute (for {})", name))?, + ), + RequirementModern::UrlDependency { url, .. } => VersionOrUrl::Url(url.clone()), + }; + + Ok(Requirement { + name: name.to_string(), + extras: common.extras.clone(), + version_or_url: Some(version_or_url), + marker, + }) + } +} + +#[cfg(test)] +mod test { + use crate::modern::{RequirementModern, VersionSpecifierModern}; + use crate::Requirement; + use indoc::indoc; + use pep440_rs::VersionSpecifiers; + use serde::Deserialize; + use std::collections::{BTreeMap, HashMap}; + + use std::str::FromStr; + + #[test] + fn test_basic() { + let deps: HashMap = + toml::from_str(r#"numpy = "==1.19""#).unwrap(); + assert_eq!( + deps["numpy"], + RequirementModern::Dependency(VersionSpecifierModern::VersionSpecifier( + VersionSpecifiers::from_str("==1.19").unwrap() + )) + ); + assert_eq!( + deps["numpy"].to_pep508("numpy", &HashMap::new()).unwrap(), + Requirement::from_str("numpy== 1.19").unwrap() + ); + } + + #[test] + fn test_conversion() { + #[derive(Deserialize)] + struct PyprojectToml { + // BTreeMap to keep the order + #[serde(rename = "modern-dependencies")] + modern_dependencies: BTreeMap, + extras: HashMap>, + } + + let pyproject_toml = indoc! {r#" + [modern-dependencies] + pydantic = "1.10.5" + numpy = ">=1.24.2, <2.0.0" + pandas = { version = ">=1.5.3, <2.0.0" } + flask = { version = "2.2.3 ", extras = ["dotenv"], optional = true } + tqdm = { git = "https://github.com/tqdm/tqdm", rev = "0bb91857eca0d4aea08f66cf1c8949abe0cd6b7a" } + jax = { url = "https://storage.googleapis.com/jax-releases/cuda112/jaxlib-0.1.64+cuda112-cp39-none-manylinux2010_x86_64.whl" } + zstandard = { file = "/home/ferris/wheels/zstandard/zstandard-0.20.0.tar.gz" } + h5py = { path = "/home/ferris/wheels/h5py/" } + + [extras] + internet = ["flask"] + "# + }; + + let deps: PyprojectToml = toml::from_str(pyproject_toml).unwrap(); + + let actual: Vec = deps + .modern_dependencies + .iter() + .map(|(name, spec)| spec.to_pep508(name, &deps.extras).unwrap().to_string()) + .collect(); + let expected: Vec = vec![ + "flask[dotenv] ==2.2.3 ; extra == 'internet'".to_string(), + "h5py @ file:///home/ferris/wheels/h5py/".to_string(), + "jax @ https://storage.googleapis.com/jax-releases/cuda112/jaxlib-0.1.64+cuda112-cp39-none-manylinux2010_x86_64.whl".to_string(), + "numpy >=1.24.2, <2.0.0".to_string(), + "pandas >=1.5.3, <2.0.0".to_string(), + "pydantic ==1.10.5".to_string(), + "tqdm @ git+https://github.com/tqdm/tqdm@0bb91857eca0d4aea08f66cf1c8949abe0cd6b7a".to_string(), + "zstandard @ file:///home/ferris/wheels/zstandard/zstandard-0.20.0.tar.gz".to_string() + ]; + assert_eq!(actual, expected) + } +} diff --git a/crates/pep508-rs/test/test_pep508.py b/crates/pep508-rs/test/test_pep508.py new file mode 100644 index 000000000..d86e9e565 --- /dev/null +++ b/crates/pep508-rs/test/test_pep508.py @@ -0,0 +1,97 @@ +from collections import namedtuple +from unittest import mock + +import pytest +from pep508_rs import Requirement, MarkerEnvironment, Pep508Error, VersionSpecifier + + +def test_pep508(): + req = Requirement("numpy; python_version >= '3.7'") + assert req.name == "numpy" + env = MarkerEnvironment.current() + assert req.evaluate_markers(env, []) + req2 = Requirement("numpy; python_version < '3.7'") + assert not req2.evaluate_markers(env, []) + + requests = Requirement( + 'requests [security,tests] >=2.8.1, ==2.8.* ; python_version > "3.8"' + ) + assert requests.name == "requests" + assert requests.extras == ["security", "tests"] + assert requests.version_or_url == [ + VersionSpecifier(">=2.8.1"), + VersionSpecifier("==2.8.*"), + ] + assert requests.marker == "python_version > '3.8'" + + +def test_marker(): + env = MarkerEnvironment.current() + assert not Requirement("numpy; extra == 'science'").evaluate_markers(env, []) + assert Requirement("numpy; extra == 'science'").evaluate_markers(env, ["science"]) + assert not Requirement( + "numpy; extra == 'science' and extra == 'arrays'" + ).evaluate_markers(env, ["science"]) + assert Requirement( + "numpy; extra == 'science' or extra == 'arrays'" + ).evaluate_markers(env, ["science"]) + + +class FakeVersionInfo( + namedtuple("FakeVersionInfo", ["major", "minor", "micro", "releaselevel", "serial"]) +): + pass + + +@pytest.mark.parametrize( + ("version", "version_str"), + [ + (FakeVersionInfo(3, 10, 11, "final", 0), "3.10.11"), + (FakeVersionInfo(3, 10, 11, "rc", 1), "3.10.11rc1"), + ], +) +def test_marker_values(version, version_str): + with mock.patch("sys.implementation.version", version): + env = MarkerEnvironment.current() + assert str(env.implementation_version.version) == version_str + + +def test_marker_values_current_platform(): + MarkerEnvironment.current() + + +def test_errors(): + with pytest.raises( + Pep508Error, + match="Expected an alphanumeric character starting the extra name, found 'ö'", + ): + Requirement("numpy[ö]; python_version < '3.7'") + + +def test_warnings(caplog): + env = MarkerEnvironment.current() + assert not Requirement("numpy; '3.6' < '3.7'").evaluate_markers(env, []) + assert caplog.messages == [ + "Comparing two quoted strings with each other doesn't make sense: " + "'3.6' < '3.7', evaluating to false" + ] + caplog.clear() + assert not Requirement("numpy; 'a' < 'b'").evaluate_markers(env, []) + assert caplog.messages == [ + "Comparing two quoted strings with each other doesn't make sense: " + "'a' < 'b', evaluating to false" + ] + caplog.clear() + Requirement("numpy; python_version >= '3.9.'").evaluate_markers(env, []) + assert caplog.messages == [ + "Expected PEP 440 version to compare with python_version, found '3.9.', " + "evaluating to false: Version `3.9.` doesn't match PEP 440 rules" + ] + caplog.clear() + # pickleshare 0.7.5 + Requirement("numpy; python_version in '2.6 2.7 3.2 3.3'").evaluate_markers(env, []) + assert caplog.messages == [ + "Expected PEP 440 version to compare with python_version, " + "found '2.6 2.7 3.2 3.3', " + "evaluating to false: Version `2.6 2.7 3.2 3.3` doesn't match PEP 440 rules" + ]