Skip to content

Commit

Permalink
WIP: Import extraction with Rust (#581)
Browse files Browse the repository at this point in the history
Adds Rust functionality to deptry. For now, the only part that is handled by Rust is the reading for .py files, parsing their AST and extracting the import statements. This PR also gives us a good starting point to potentially replace more parts of deptry with Rust.

List of changes:

- Adds maturin to build a Python/Rust hybrid project.
- Move from Poetry to PDM, so we have PEP-621 compatible metadata. Change backend to maturin.
- Adds a 'get_imports_from_py_files' that can be imported in Python as from deptry.rust import get_imports_from_py_files
- Minimal changes in the unit tests; most changes are an improvement in the 'column' of detected imports; earlier the column was 0 now it is 8.
- We use ABI to build and publish wheels. For an example of a CI/CD run, see here and for the files on test PyPi see [here](the files on test pypi)
- Rewrote the entire CI/CD pipeline


---------

Co-authored-by: Mathieu Kniewallner <mathieu.kniewallner@gmail.com>
  • Loading branch information
fpgmaas and mkniewallner committed Mar 13, 2024
1 parent d25dbde commit a13add1
Show file tree
Hide file tree
Showing 79 changed files with 2,234 additions and 805 deletions.
52 changes: 52 additions & 0 deletions .github/actions/setup-env/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: 'Setup Python and Rust Environment'
description: 'Set up Python and Rust environment for PDM projects with matrix support'

inputs:
python-version:
description: 'Python version for setup-python'
required: true
default: 3.11
target:
description: 'Target architecture for maturin'
required: true
default: 'x86_64'
python-target:
description: 'Target architecture for python installation'
required: true
default: 'x64'

runs:
using: 'composite'
steps:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
architecture: ${{ matrix.python-target }}

- name: Setup Rust toolchain
run: rustup component add clippy rustfmt
shell: bash

- uses: Swatinem/rust-cache@v2

- name: Install PDM and dependencies
run: |
python -m pip install pdm
python -m venv .venv
if [ "${{ runner.os }}" = "Windows" ]; then
source .venv/Scripts/Activate
else
source .venv/bin/activate
fi
python -m pip install --upgrade pip
pdm config python.use_venv true
pdm install
shell: bash

- name: Build and install the project using Maturin
uses: PyO3/maturin-action@v1
with:
target: ${{ inputs.target }}
command: develop
sccache: 'true'
49 changes: 0 additions & 49 deletions .github/actions/setup-poetry-env/action.yml

This file was deleted.

107 changes: 72 additions & 35 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,70 +1,104 @@
name: Main
name: CI

on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
quality:
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@v4

- uses: actions/cache@v4
- name: Set up the environment
uses: ./.github/actions/setup-env
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
python-version: '3.11'
target: 'x86_64'
python-target: 'x64'

- name: Run pre-commit
run: |
rustup component add rustfmt
pdm run pre-commit run -a --show-diff-on-failure
- name: Inspect dependencies with deptry
run: |
pdm run deptry python
linux:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
target: [x86_64, aarch64]
steps:
- name: Check out
uses: actions/checkout@v4

- name: Set up the environment
uses: ./.github/actions/setup-poetry-env
uses: ./.github/actions/setup-env
with:
python-version: ${{ matrix.python-version }}
target: ${{ matrix.target }}
python-target: 'x64'

- name: Run pre-commit
run: poetry run pre-commit run -a --show-diff-on-failure
- name: Check typing
run: pdm run mypy

- name: Inspect dependencies
run: poetry run deptry .
- name: Run tests
run: pdm run pytest tests --cov --cov-config=pyproject.toml --cov-report=xml

- name: Check Poetry lock file consistency
run: poetry lock --check
- name: Upload coverage reports to Codecov with GitHub Action on Python 3.11 and x86_64
uses: codecov/codecov-action@v3
if: ${{ matrix.python-version == '3.11' && matrix.target == 'x86_64' }}

tests-and-type-check:
runs-on: ${{ matrix.image }}
windows:
runs-on: windows-latest
strategy:
matrix:
os: [macOS, Ubuntu, Windows]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
include:
- os: macOS
image: macos-12
- os: Ubuntu
image: ubuntu-22.04
- os: Windows
image: windows-2022
fail-fast: false
defaults:
run:
shell: bash
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
target: [x64]
steps:
- name: Check out
uses: actions/checkout@v4

- name: Set up the environment
uses: ./.github/actions/setup-poetry-env
uses: ./.github/actions/setup-env
with:
python-version: ${{ matrix.python-version }}
target: ${{ matrix.target }}
python-target: ${{ matrix.target }}

- name: Run tests
run: poetry run pytest tests --cov --cov-config=pyproject.toml --cov-report=xml
run: pdm run pytest tests --cov --cov-config=pyproject.toml --cov-report=xml

- name: Check typing
run: poetry run mypy
macos:
runs-on: macos-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
target: [x86_64, aarch64]
steps:
- name: Check out
uses: actions/checkout@v4

- name: Upload coverage reports to Codecov with GitHub Action on Python 3.11 for Ubuntu
uses: codecov/codecov-action@v3
if: ${{ matrix.python-version == '3.11' && matrix.os == 'Ubuntu' }}
- name: Set up the environment
uses: ./.github/actions/setup-env
with:
python-version: ${{ matrix.python-version }}
target: ${{ matrix.target }}
python-target: ${{ matrix.target == 'aarch64' && 'arm64' || 'x64' }}

- name: Run tests
run: pdm run pytest tests --cov --cov-config=pyproject.toml --cov-report=xml

check-docs:
runs-on: ubuntu-latest
Expand All @@ -73,7 +107,10 @@ jobs:
uses: actions/checkout@v4

- name: Set up the environment
uses: ./.github/actions/setup-poetry-env
uses: ./.github/actions/setup-env
with:
python-version: '3.11'
target: 'x86_64'

- name: Check if documentation can be built
run: poetry run mkdocs build -s
run: pdm run mkdocs build -s
41 changes: 0 additions & 41 deletions .github/workflows/on-release-main.yml

This file was deleted.

Loading

0 comments on commit a13add1

Please sign in to comment.