Skip to content

Commit

Permalink
Add compatibility with Poetry 1.6.0+ (#6)
Browse files Browse the repository at this point in the history
* chore: update deps

* chore: rename CONTRIBUTE to CONTRIBUTING

* chore: new lint rules

* chore: test against PDM 2.8.2

* chore: update README

* chore: add poetry to lockfile

* fix: move sync dry-run check just before writing yaml

* feat: new command `pdm sync-pre-commit`

* feat: add `poetry sync-pre-commit` command

* docs: update README with new options, fix configuration mismatch

* ci: release files in Github Release

* ci: remove manual Poetry install

* ci: use PDM scripts
  • Loading branch information
GabDug committed Aug 28, 2023
1 parent 2321f71 commit 3f0772e
Show file tree
Hide file tree
Showing 17 changed files with 890 additions and 298 deletions.
125 changes: 61 additions & 64 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
---
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python CI

# yamllint disable-line rule:truthy
on:
push:
branches: ["main"]
Expand All @@ -19,73 +21,68 @@ jobs:
matrix:
python-version: ["3.10", "3.11", "3.12"]
# Empty is latest, head is latest from GitHub
pdm-version: ["", "head", "2.7.4", "2.8.0", "2.8.1"]
pdm-version: ["", "head", "2.7.4", "2.8.0", "2.8.1", "2.8.2"]

steps:
- uses: actions/checkout@v3
- uses: pdm-project/setup-pdm@v3
name: Setup PDM
with:
cache: true
python-version: ${{ matrix.python-version }} # Version range or exact version of a Python version to use, the same as actions/setup-python
architecture: x64 # The target architecture (x86, x64) of the Python interpreter. the same as actions/setup-python
version: ${{ matrix.pdm-version }} # The version of PDM to install. Leave it as empty to use the latest version from PyPI, or 'head' to use the latest version from GitHub
prerelease: true # Allow prerelease versions of PDM to be installed
enable-pep582: false # Enable PEP 582 package loading globally
allow-python-prereleases: true # Allow prerelease versions of Python to be installed. For example if only 3.12-dev is available, 3.12 will fall back to 3.12-dev
- name: Set Cache Variables
id: set_variables
run: |
echo "PIP_CACHE=$(pip cache dir)" >> $GITHUB_OUTPUT
echo "PDM_CACHE=$(pdm config cache_dir)" >> $GITHUB_OUTPUT
- name: Cache PIP and PDM
uses: actions/cache@v2
with:
path: |
${{ steps.set_variables.outputs.PIP_CACHE }}
${{ steps.set_variables.outputs.PDM_CACHE }}
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ matrix.pdm-version }}
- uses: actions/checkout@v3
- uses: pdm-project/setup-pdm@v3
name: Setup PDM
with:
cache: true
python-version: ${{ matrix.python-version }} # Version range or exact version of a Python version to use, the same as actions/setup-python
architecture: x64 # The target architecture (x86, x64) of the Python interpreter. the same as actions/setup-python
version: ${{ matrix.pdm-version }} # The version of PDM to install. Leave it as empty to use the latest version from PyPI, or 'head' to use the latest version from GitHub
prerelease: true # Allow prerelease versions of PDM to be installed
enable-pep582: false # Enable PEP 582 package loading globally
allow-python-prereleases: true # Allow prerelease versions of Python to be installed. For example if only 3.12-dev is available, 3.12 will fall back to 3.12-dev
- name: Set Cache Variables
id: set_variables
run: |
echo "PIP_CACHE=$(pip cache dir)" >> $GITHUB_OUTPUT
echo "PDM_CACHE=$(pdm config cache_dir)" >> $GITHUB_OUTPUT
- name: Cache PIP and PDM
uses: actions/cache@v2
with:
path: |
${{ steps.set_variables.outputs.PIP_CACHE }}
${{ steps.set_variables.outputs.PDM_CACHE }}
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ matrix.pdm-version }}

- name: Install dependencies
run: |
pdm config venv.with_pip True
pdm install -G :all --dev
pdm venv activate in-project
source .venv/bin/activate
- name: Install dependencies
run: |
pdm config venv.with_pip True
pdm install -G :all --dev
pdm venv activate in-project
source .venv/bin/activate
# Get the pip command to run depending on matrix.pdm-version
# We force reinstall of pdm in the virtualenv
if [[ "${{ matrix.pdm-version }}" == "head" ]]; then
pip install "pdm @ git+https://github.com/pdm-project/pdm"
elif [[ "${{ matrix.pdm-version }}" == "" ]]; then
pip install pdm
else
pip install pdm==${{ matrix.pdm-version }}
fi
# Get the pip command to run depending on matrix.pdm-version
# We force reinstall of pdm in the virtualenv
if [[ "${{ matrix.pdm-version }}" == "head" ]]; then
pip install "pdm @ git+https://github.com/pdm-project/pdm"
elif [[ "${{ matrix.pdm-version }}" == "" ]]; then
pip install pdm
else
pip install pdm==${{ matrix.pdm-version }}
fi
# XXX Force install of Poetry in the virtualenv
pip install "poetry @ git+https://github.com/python-poetry/poetry@master"
- name: Test with pytest
run: |
pdm run test-cov
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
flags: unittests

- name: Test with pytest
run: |
pdm run pytest --junitxml=junit/test-results.xml --cov --cov-report=xml --cov-report=html
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
flags: unittests

- name: Type check with mypy
run: |
pdm run mypy src
# XXX Mypy on tests as well
- name: Lint with ruff
run: |
pdm run ruff src tests --format=github --exit-non-zero-on-fix
- name: Build with pdm
run: |
pdm build
# Do not upload to PyPI, here we only want to check that the build works
# XXX Check valid wheels
# XXX Report coverage/junit?
- name: Type check with mypy
run: |
pdm run lint-mypy
- name: Lint with ruff
run: |
pdm run lint-ruff --format=github --exit-non-zero-on-fix
- name: Build with pdm
run: |
pdm build
# Do not upload to PyPI, here we only want to check that the build works
# XXX Check valid wheels?
13 changes: 9 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ jobs:
id-token: write

steps:
- uses: actions/checkout@v3
- uses: pdm-project/setup-pdm@v3
- name: Publish package distributions to PyPI
run: pdm publish
- uses: actions/checkout@v3
- uses: pdm-project/setup-pdm@v3
- name: Publish package distributions to PyPI
run: pdm publish
- name: Upload files to Github Release
uses: NBTX/upload-release-assets@v1
with:
upload_url: ${{ github.event.release.upload_url }}
targets: dist/*
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
default_language_version:
python: python3.11
repos:
Expand All @@ -18,7 +19,7 @@ repos:
hooks:
- id: black

- rev: v0.0.280
- rev: v0.0.286
repo: https://github.com/charliermarsh/ruff-pre-commit
hooks:
- id: ruff
Expand Down
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pdm 2.8.1
python 3.11.4
pdm 2.8.2
python 3.11.5
8 changes: 8 additions & 0 deletions CONTRIBUTE.md → CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All contributions are welcome!

## New features and bug fixes

If you are not sure what to do, check the README roadmap for ideas.

If you have something in mind, please try to open an issue to discuss it first.

## Repository to package mapping

Contributions to the mapping of repos are welcomed, if the projects have enough usage to be worth it.

If not, you can still manually add them to your `pyproject.toml` file.
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ PDM and Poetry plugin to sync your pre-commit versions with your lockfile and au
- 🔁 Sync pre-commit versions with your lockfile
- ⏩ Run every time you run the lockfile is updated, not as a pre-commit hook
- 🔄 Install pre-commit hooks automatically, no need to run `pre-commit install` manually
- 💫 Preserve your pre-commit config file formatting

## Supported versions

- Python 3.10+
- PDM 2.7.4+
- Poetry 1.6.0+ (currently in development)
- Poetry 1.6.0+

## Installation

Expand All @@ -47,7 +48,13 @@ plugins = [
### For Poetry
Install like any other Poetry plugin, but beware that it's still in development!
Install [like any other Poetry plugin](https://python-poetry.org/docs/master/plugins/#using-plugins), e.g.:
```bash
poetry self plugin add "sync-pre-commit-lock[poetry]"
```
> Only Poetry 1.6.0+ is supported.
## Configuration
Expand All @@ -60,11 +67,11 @@ Here is the default configuration:
# Run `pre-commit install` automatically if applicable
automaticall-install-hooks = true
# Should we sync your pre-commit versions with your lockfile (when running lock, add, update, remove, etc.)?
disable-sync-from_lock = false
disable-sync-from-lock = false
# Packages to ignore when syncing from lock
ignore = []
# Name of the pre-commit config file to sync with
pre-commit-config-file = "pre-commit-config.yaml"
pre-commit-config-file = ".pre-commit-config.yaml"
# Additional mapping of URLs to python packages
# Default is empty, but will merge with the default mapping
# "rev" indicates the format of the Git tags
Expand All @@ -76,9 +83,23 @@ dependency-mapping = {"package-name"= {"repo"= "https://github.com/example/packa
## Usage
Once installed, and optionally configured, the plugin usage should be transparent, and trigger when you run applicable PDM or Poetry commands.
Once installed, and optionally configured, the plugin usage should be transparent, and trigger when you run applicable PDM or Poetry commands, like `pdm lock`, or `poetry lock`.
> There should be a message in the output, when the sync or install or pre-commit is triggered.
You can manually trigger the sync with the CLI command:
```bash
pdm sync-pre-commit
```
or
```bash
poetry sync-pre-commit
```
There should be a message in the output, when the sync or install or pre-commit is triggered.
Both commands support `--dry-run` and verbosity options.
## Improvement ideas
Expand All @@ -89,13 +110,14 @@ Feel free to open an issue or a PR if you have any idea, or if you want to help!
- [ ] Upload build artifacts on GitHub release
- [ ] Add a changelog
- [ ] Add "E2E" tests
- [ ] Add PDM scripts for dev and CI
- [X] Add PDM scripts for dev and CI
- [ ] Add docs
### Features or fixes
- [ ] Create a more verbose command
- [ ] Add support for other lockfiles / project managers (pipenv, flit, hatch, etc.)
- [X] Add a PDM/Poetry CLI command to sync manually
- [ ] Add a dedicated CLI command to manage sync/install
- [ ] Expose a pre-commit hook to sync the lockfile
- [ ] Support nested params for some repos? Like mypy types
Expand Down

0 comments on commit 3f0772e

Please sign in to comment.