-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests; separate
cli
and core logic
- Loading branch information
1 parent
2adedd7
commit 130ece4
Showing
14 changed files
with
953 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Setup environment | ||
description: | | ||
Sets up the development environment for this repository. | ||
Notes: | ||
1. You have to first checkout the repository. | ||
2. To use the conda environment, you have to set `defaults.run.shell` to `bash -el {0}`. See [this page](https://github.com/marketplace/actions/setup-miniconda#important) for more details. | ||
inputs: | ||
python-version: | ||
description: 'Python version' | ||
required: true | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- name: Install Miniconda | ||
uses: conda-incubator/setup-miniconda@v3 | ||
with: | ||
auto-update-conda: true | ||
python-version: ${{ inputs.python-version }} | ||
activate-environment: 'pyimorg-${{ inputs.python-version }}' | ||
- name: Install Poetry | ||
uses: snok/install-poetry@v1 | ||
with: | ||
version: '1.4.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
name: Verify pre-release | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
push: | ||
branches: | ||
- master | ||
|
||
permissions: | ||
contents: write | ||
|
||
defaults: | ||
run: | ||
shell: bash -el {0} | ||
|
||
jobs: | ||
lint: | ||
name: Lint code | ||
strategy: | ||
matrix: | ||
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Git repository | ||
uses: actions/checkout@v4 | ||
- name: Setup environment | ||
uses: ./.github/actions/setup | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install repository | ||
run: | | ||
poetry lock --no-update | ||
poetry install --with dev | ||
- name: Check dependencies | ||
run: poetry run -- deptry . | ||
- name: Lint code | ||
run: poetry run -- ruff check . | ||
- name: Check type annotations | ||
run: poetry run -- pyright | ||
test: | ||
name: Test code | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, windows-latest] | ||
python-version: ['3.8', '3.12'] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Checkout Git repository | ||
uses: actions/checkout@v4 | ||
- uses: ./.github/actions/setup | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install repository | ||
run: | | ||
poetry lock --no-update | ||
poetry install --with dev | ||
- name: Pytest with code coverage | ||
run: poetry run -- pytest -n auto | ||
- name: Archive code coverage results | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: coverage-${{ matrix.os }}-${{ matrix.python-version }} | ||
path: coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1 @@ | ||
from __future__ import annotations | ||
|
||
from collections import defaultdict | ||
from collections.abc import Collection | ||
from itertools import groupby | ||
import json | ||
import re | ||
from urllib.request import urlopen | ||
|
||
import click | ||
import pandas as pd | ||
from pkg_resources import Requirement, parse_version | ||
|
||
__all__ = ['cli'] | ||
|
||
# Dummy requirement that cannot be satisfied | ||
BAD_REQUIREMENT = Requirement.parse('python<0,>0') | ||
|
||
def _get_requires_python_constraint(dist_data: dict[str, str]) -> Requirement | None: | ||
requires_python_data: str | None = dist_data.get('requires_python') | ||
if requires_python_data is None: | ||
return None | ||
|
||
return Requirement.parse(f'python{requires_python_data}') | ||
|
||
def _get_python_requirements(dist_data: dict[str, str]) -> Collection[Requirement]: | ||
requires_python_constraint = _get_requires_python_constraint(dist_data) | ||
|
||
python_version_data: str | None = dist_data.get('python_version') | ||
if python_version_data is None or python_version_data == 'source': | ||
return [] if requires_python_constraint is None else [requires_python_constraint] | ||
|
||
m = re.match(r'[a-z]{2}([0-9])([0-9]*)', python_version_data) | ||
if m is None: | ||
return [BAD_REQUIREMENT] if requires_python_constraint is None else [requires_python_constraint] | ||
|
||
major, minor = m.groups() | ||
if minor: | ||
bdist_constraint = Requirement.parse(f'python=={major}.{minor}.*') | ||
else: | ||
bdist_constraint = Requirement.parse(f'python=={major}.*') | ||
|
||
if requires_python_constraint is None: | ||
return [bdist_constraint] | ||
else: | ||
return [requires_python_constraint, bdist_constraint] | ||
|
||
@click.command() | ||
@click.argument('package_name', type=str) | ||
@click.option('--python', 'python_versions', metavar='<VERSIONS>', type=str, required=True, help='A comma-separated list of Python versions to check against') | ||
def cli(package_name: str, python_versions: str) -> None: | ||
"""Show the versions of a PyPI package that are compatible with each Python version.""" | ||
python_versions_lst = python_versions.split(',') | ||
output = defaultdict(lambda: defaultdict(lambda: '')) | ||
|
||
data = json.load(urlopen(f'https://pypi.org/pypi/{package_name}/json')) | ||
for package_version, release_data in data['releases'].items(): | ||
for dist_data in release_data: | ||
python_requirements = _get_python_requirements(dist_data) | ||
|
||
for python_version in python_versions_lst: | ||
if all(python_version in r for r in python_requirements): | ||
output[python_version][package_version] = 'Y' | ||
|
||
output_df = pd.DataFrame.from_dict(data=output, orient='index').fillna('') | ||
|
||
# We only care about the latest patch version for each minor version | ||
output_columns = tuple(str(c) for c in output_df.columns) | ||
latest_patches = [ | ||
max(columns, key=parse_version) | ||
for _, columns in groupby(output_columns, key=lambda s: parse_version(s).release[:2]) | ||
] | ||
|
||
formatted_output_df = output_df \ | ||
.loc[sorted(output_df.index, key=parse_version), sorted(latest_patches, key=parse_version)] \ | ||
.rename_axis(index='Python (↓)', columns=f'{package_name} (→)') | ||
|
||
click.echo(formatted_output_df) | ||
from .pycm import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from . import cli | ||
from .cli import cli | ||
|
||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from __future__ import annotations | ||
|
||
import click | ||
|
||
from .pycm import pycm | ||
|
||
__all__ = ['cli'] | ||
|
||
@click.command() | ||
@click.argument('package_name', type=str) | ||
@click.option('--python', 'python_versions', metavar='<VERSIONS>', type=str, required=True, help='A comma-separated list of Python versions to check against') | ||
def cli(package_name: str, python_versions: str) -> None: | ||
"""Show the versions of a PyPI package that are compatible with each Python version.""" | ||
output_df = pycm(package_name, python_versions.split(',')) | ||
click.echo(output_df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from __future__ import annotations | ||
|
||
from collections import defaultdict | ||
from collections.abc import Collection | ||
from itertools import groupby | ||
import json | ||
import re | ||
from urllib.request import urlopen | ||
|
||
from packaging.requirements import Requirement | ||
from packaging.version import Version | ||
import pandas as pd | ||
|
||
__all__ = ['pycm'] | ||
|
||
# Dummy requirement that cannot be satisfied | ||
BAD_REQUIREMENT = Requirement('python<0,>0') | ||
|
||
def _get_requires_python_constraint(dist_data: dict[str, str]) -> Requirement | None: | ||
requires_python_data: str | None = dist_data.get('requires_python') | ||
if requires_python_data is None: | ||
return None | ||
|
||
return Requirement(f'python{requires_python_data}') | ||
|
||
def _get_python_requirements(dist_data: dict[str, str]) -> Collection[Requirement]: | ||
requires_python_constraint = _get_requires_python_constraint(dist_data) | ||
|
||
python_version_data: str | None = dist_data.get('python_version') | ||
if python_version_data is None or python_version_data == 'source': | ||
return [] if requires_python_constraint is None else [requires_python_constraint] | ||
|
||
m = re.match(r'[a-z]{2}([0-9])([0-9]*)', python_version_data) | ||
if m is None: | ||
return [BAD_REQUIREMENT] if requires_python_constraint is None else [requires_python_constraint] | ||
|
||
major, minor = m.groups() | ||
if minor: | ||
bdist_constraint = Requirement(f'python=={major}.{minor}.*') | ||
else: | ||
bdist_constraint = Requirement(f'python=={major}.*') | ||
|
||
if requires_python_constraint is None: | ||
return [bdist_constraint] | ||
else: | ||
return [requires_python_constraint, bdist_constraint] | ||
|
||
def pycm(package_name: str, python_versions: list[str], *, groupby_patch: bool = True) -> pd.DataFrame: | ||
"""Show the versions of a PyPI package that are compatible with each Python version.""" | ||
output = defaultdict(lambda: defaultdict(lambda: '')) | ||
|
||
data = json.load(urlopen(f'https://pypi.org/pypi/{package_name}/json')) | ||
for package_version, release_data in data['releases'].items(): | ||
for dist_data in release_data: | ||
python_requirements = _get_python_requirements(dist_data) | ||
|
||
for python_version in python_versions: | ||
if all(python_version in r.specifier for r in python_requirements): | ||
output[python_version][package_version] = 'Y' | ||
|
||
output_df = pd.DataFrame.from_dict(data=output, orient='index').fillna('') | ||
|
||
|
||
if groupby_patch: | ||
# We only care about the latest patch version for each minor version | ||
output_columns = tuple(str(c) for c in output_df.columns) | ||
output_columns = [ | ||
max(columns, key=Version) | ||
for _, columns in groupby(output_columns, key=lambda s: Version(s).release[:2]) | ||
] | ||
else: | ||
output_columns = output_df.columns | ||
|
||
return output_df \ | ||
.loc[sorted(output_df.index, key=Version), sorted(output_columns, key=Version)] \ | ||
.rename_axis(index='Python (↓)', columns=f'{package_name} (→)') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import subprocess | ||
|
||
|
||
def test_help(): | ||
assert subprocess.call(['python', '-m', 'pycm', '--help']) == 0 |
Oops, something went wrong.