From dc33223fcb1583bf31b89474ec299d45d8f80ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81rni=20Dagur?= Date: Mon, 1 Feb 2021 17:59:51 +0000 Subject: Packaging improvements (#34) * Use maturin for PEP 517 compliancy * Removing relative import in __init__.py * Bump version to 0.4.2 --- .github/workflows/ci.yml | 35 ++++++++++++------------- CHANGELOG.md | 6 +++++ Cargo.lock | 2 +- Cargo.toml | 20 +++++++++++--- README.md | 26 ++++++++++++++++++ adblock/__init__.py | 2 +- pyproject.toml | 13 +++++---- tests/test_metadata.py | 61 +++++++++++++++++++++++++++++++++++++++++++ tests/test_version_numbers.py | 40 ---------------------------- 9 files changed, 134 insertions(+), 71 deletions(-) create mode 100644 tests/test_metadata.py delete mode 100644 tests/test_version_numbers.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 68e1a10..806059d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,6 +4,8 @@ on: release: types: [created] push: + branches: + - master pull_request: schedule: # Runs every Thursday at 20:23 GMT to avoid bit rot @@ -69,7 +71,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install poetry + pip install poetry twine poetry install - name: Set Rust target environment variable @@ -84,15 +86,17 @@ jobs: override: true - name: Build Python package - if: matrix.architecture == 'x86' - run: poetry run maturin build --target ${{ env.ACTIONS_RUST_TARGET }} + run: poetry run maturin build --interpreter python${{matrix.python_version}} --target ${{ env.ACTIONS_RUST_TARGET }} - - name: Build Python package - if: matrix.architecture != 'x86' - run: poetry run maturin develop + - name: Check wheels with Twine + run: twine check target/wheels/* + shell: bash + + - name: Install Python package + run: poetry run pip install target/wheels/adblock*.whl + shell: bash - name: Run Python tests - if: matrix.architecture != 'x86' run: poetry run pytest -vv --color=yes python-publish: @@ -133,10 +137,10 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install poetry + pip install poetry twine poetry install - name: Build Python package - run: poetry run maturin build --release --no-sdist --strip --interpreter python${{matrix.python_version}} --target ${{ env.ACTIONS_RUST_TARGET }} + run: poetry run maturin build --release --strip --interpreter python${{matrix.python_version}} --target ${{ env.ACTIONS_RUST_TARGET }} - name: List wheels if: matrix.os == 'windows-latest' @@ -146,15 +150,9 @@ jobs: if: matrix.os != 'windows-latest' run: find ./target/wheels/ - # Note: Windows doesn't support glob - # https://stackoverflow.com/a/52481267/270334 - name: Install wheels - if: matrix.os == 'windows-latest' - run: pip install --find-links=target\wheels adblock - - - name: Install wheels - if: matrix.os != 'windows-latest' run: pip install target/wheels/adblock*.whl + shell: bash - name: Release uses: softprops/action-gh-release@v1 @@ -167,8 +165,9 @@ jobs: - name: PyPi publish if: github.event_name == 'release' && github.event.action == 'created' env: - MATURIN_PASSWORD: ${{ secrets.PYPI }} - run: poetry run maturin publish --interpreter python${{matrix.python_version}} --username __token__ + TWINE_PASSWORD: ${{ secrets.PYPI }} + run: twine upload --non-interactive --skip-existing --username __token__ target/wheels/* + shell: bash docs-publish: runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e603b8..9274c90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch --- +## 0.4.2 - (2021-02-01) +--- +### Fixes +* Remove relative import which caused problems in [#17](https://github.com/ArniDagur/python-adblock/issues/17). + + ## 0.4.1 - (2021-01-27) --- diff --git a/Cargo.lock b/Cargo.lock index 3fdec81..22454cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,7 +24,7 @@ dependencies = [ [[package]] name = "adblock" -version = "0.4.1" +version = "0.4.2" dependencies = [ "adblock 0.3.4", "pyo3", diff --git a/Cargo.toml b/Cargo.toml index a1947ae..f565d0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,22 @@ [package] -name = "adblock" publish = false -version = "0.4.1" -authors = ["Árni Dagur "] license = "MIT OR Apache-2.0" +readme = "README.md" +homepage = "https://github.com/ArniDagur/python-adblock" +repository = "https://github.com/ArniDagur/python-adblock" + +[package.metadata.maturin] +classifier = [ + "Programming Language :: Python", + "Programming Language :: Rust", + "License :: OSI Approved :: MIT License", + "License :: OSI Approved :: Apache Software License", +] +requires-python = ">=3.6" [profile.release] debug = true @@ -18,4 +30,4 @@ name = "adblock" crate-type = ["rlib", "cdylib"] [features] -default = ["pyo3/extension-module"] +default = ["pyo3/extension-module"] \ No newline at end of file diff --git a/README.md b/README.md index 6a42298..f5278e9 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,32 @@ python-adblock ========== Python wrapper for Brave's adblocking library, which is written in Rust. +### Building + +``` +maturin build --release +``` + +#### Build dependencies + +| Build Dependency | Versions | Arch Linux | Url | +|------------------|----------|------------|-----| +| Python | `>=3.6` | `python3` | - | +| Rust | `>=1.45` | `rust` | - | +| Maturin | `*` | `maturin` | https://github.com/PyO3/maturin | + +### Developing + +I use Poetry for development. To create and enter a virtual environment, do +``` +poetry install +poetry shell +``` +then, to install the `adblock` module into the virtual environment, do +``` +maturin develop +``` + ### Documentation Rust documentation for the latest `master` branch can be found at https://arnidagur.github.io/python-adblock/docs/adblock/index.html. diff --git a/adblock/__init__.py b/adblock/__init__.py index bb1b6e1..0cc1876 100644 --- a/adblock/__init__.py +++ b/adblock/__init__.py @@ -1,4 +1,4 @@ -from .adblock import __version__, Engine, FilterSet, BlockerResult, UrlSpecificResources +from adblock.adblock import __version__, Engine, FilterSet, BlockerResult, UrlSpecificResources __all__ = ("Engine", "FilterSet", "BlockerResult", "UrlSpecificResources") diff --git a/pyproject.toml b/pyproject.toml index 432e476..8bbdb76 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,18 +1,17 @@ [tool.poetry] name = "adblock" -version = "0.4.1" +version = "0.0.0" description = "Brave's adblocking in Python" authors = ["Árni Dagur "] -license = "MIT OR Apache-2.0" -readme = "README.md" -repository = "https://github.com/ArniDagur/python-adblock" -keywords = [] [tool.poetry.dependencies] -python = "^3.5" +python = "^3.6" [tool.poetry.dev-dependencies] maturin = "*" pytest = "*" toml = "*" -changelog-cli = "*" \ No newline at end of file + +[build-system] +requires = ["maturin"] +build-backend = "maturin" \ No newline at end of file diff --git a/tests/test_metadata.py b/tests/test_metadata.py new file mode 100644 index 0000000..1a0e6d8 --- /dev/null +++ b/tests/test_metadata.py @@ -0,0 +1,61 @@ +import re +import sys + +import toml +import adblock + + +def get_version_value_cargo(): + with open("Cargo.toml", encoding="utf-8") as f: + cargo_toml = toml.loads(f.read()) + return cargo_toml["package"]["version"] + + +def get_version_value_changelog(): + """ + Try to get the names of all classes that we added to the Python module + from Rust. As always, we unfortunately don't have access to the Rust AST + so we have to make do with regular expressions. + """ + versions = [] + with open("CHANGELOG.md", "r", encoding="utf-8") as f: + for line in f: + match = re.match( + r"## ([0-9]+\.[0-9]+\.[0-9]+) - \(20[0-9]+-[0-1][0-9]-[0-3][0-9]\)", + line.strip(), + ) + if match is not None: + versions.append(match.group(1)) + assert versions == sorted(versions, reverse=True) + return versions[0] + + +def test_version_numbers_all_same(): + """ + Makes sure that `Cargo.toml` and `CHANGELOG.md` contain the same version + number as the one attached to the `adblock` module. + """ + cargo_version = get_version_value_cargo() + changelog_version = get_version_value_changelog() + module_version = adblock.__version__ + + assert cargo_version == module_version + assert module_version == changelog_version + + +def get_current_python_version(): + return f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" + + +def test_required_python_version(): + """ + Make sure that the Python interpreter we're running this test suite on + falls into the required Python range. + """ + with open("Cargo.toml", encoding="utf-8") as f: + cargo_toml = toml.loads(f.read()) + + required_python = cargo_toml["package"]["metadata"]["maturin"]["requires-python"] + assert required_python.startswith(">=") + required_python = required_python[2:] + assert get_current_python_version() >= required_python diff --git a/tests/test_version_numbers.py b/tests/test_version_numbers.py deleted file mode 100644 index 77db2f5..0000000 --- a/tests/test_version_numbers.py +++ /dev/null @@ -1,40 +0,0 @@ -import subprocess - -import toml -import adblock - - -def get_version_value_poetry(): - with open("pyproject.toml", encoding="utf-8") as f: - pyproject_toml = toml.loads(f.read()) - return pyproject_toml["tool"]["poetry"]["version"] - - -def get_version_value_cargo(): - with open("Cargo.toml", encoding="utf-8") as f: - cargo_toml = toml.loads(f.read()) - return cargo_toml["package"]["version"] - - -def get_version_value_changelog(): - try: - proc = subprocess.Popen(["changelog", "current"], stdout=subprocess.PIPE) - except FileNotFoundError: - return None - assert proc.wait() == 0 - return proc.stdout.read().decode("utf-8").strip() - - -def test_version_numbers_all_same(): - """ - Makes sure that `pyproject.toml`, `Cargo.toml`, and `CHANGELOG.md` contain - the same version number as the one attached to the `adblock` module. - """ - cargo_version = get_version_value_cargo() - poetry_version = get_version_value_poetry() - changelog_version = get_version_value_changelog() - module_version = adblock.__version__ - - assert cargo_version == poetry_version - assert poetry_version == module_version - assert changelog_version is None or module_version == changelog_version -- cgit v1.2.3