Skip to content

Commit

Permalink
Merge pull request #27 from floatingpurr/1.0.0
Browse files Browse the repository at this point in the history
Release 1.0.0. Close #26
  • Loading branch information
floatingpurr authored May 25, 2023
2 parents 036a348 + 681bf73 commit 97df834
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 484 deletions.
31 changes: 9 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,34 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.6, 3.7, 3.8, 3.9, "3.10"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest, macOS-latest, windows-latest]

steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install poetry
run: pipx install poetry

- name: Set Up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: 'poetry'

- name: Display Python version
run: python -c "import sys; print(sys.version)"

- name: Install and setup Poetry
run: |
python -m pip install --upgrade pip
pip install poetry
# See https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows
# pip and pipenv are supported by setup-python
# poetry is not (https://github.com/actions/setup-python/issues/298)
# for poetry you need an ad-hoc solution
# see https://github.com/python-poetry/poetry/issues/2629 for the problem with win + python3.6
- name: Set up Poetry cache for Python dependencies
uses: actions/cache@v2
if: matrix.os != 'windows-latest'
with:
path: .venv
key: venv-${{ runner.os }}-${{ matrix.python-version }}-f-${{ hashFiles('**/poetry.lock') }}

- name: Install dependencies
run: poetry install

- name: Run tests with coverage
run: poetry run pytest -svv --cov=./ --cov-report=xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.10'
uses: codecov/codecov-action@v3
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11'
with:
name: sync with poetry # optional
fail_ci_if_error: true # optional (default = false)
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ repos:

# isort - sorting imports
- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort

Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ E.g., starting from the following files:
name = "black"
version = "21.12b0"
description = "The uncompromising code formatter."
category = "dev"
optional = false
python-versions = ">=3.6.2"
```
Expand Down Expand Up @@ -78,7 +77,6 @@ Excerpt from a `.pre-commit-config.yaml` using an example of this hook:
### Args

```
--all Scan all dependencies in poetry.lock (main and dev)
--skip [SKIP ...] Packages to skip
--config CONFIG Path to a custom .pre-commit-config.yaml file
--db PACKAGE_LIST Path to a custom package list (json)
Expand Down Expand Up @@ -111,9 +109,9 @@ Supported packages out-of-the-box are listed in
- mypy
- pyupgrade
From version `0.4.0`, you can create your very own package list, passing a
custom json file with the arg `--db`. Such a file specifies how to map a package
to the corresponding repo, following this pattern:
You can create your very own package list, passing a custom json file with the
arg `--db`. Such a file specifies how to map a package to the corresponding
repo, following this pattern:
```json
{
Expand All @@ -131,7 +129,8 @@ leading `v`, you need to specify `"v${rev}"` as a `"<revision_template>"`. Use
`"${rev}"` if both the package version and the repo `rev` follow the same
pattern.

PRs extending [`db.py`](sync_with_poetry/db.py) are welcome.
Please, do not open PRs to extend [`db.py`](sync_with_poetry/db.py) anymore. Use
your personal package list instead.

## Contributing

Expand Down
725 changes: 380 additions & 345 deletions poetry.lock

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
[tool.poetry]
authors = ["Andrea <floatingpurr@users.noreply.github.com>"]
description = ""
authors = ["Andrea Mignone <andrea.mignone@virgilio.it>"]
description = "Sync .pre-commit-config.yaml repos starting from poetry.lock"
name = "sync_with_poetry"
version = "0.4.0"
version = "1.0.0"

[tool.poetry.dependencies]
python = "^3.6.1"

PyYAML = "^6.0"
tomlkit = "^0.7.2"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
black = {version = "^22.8.0", python = "^3.6.2"}
coverage = [
{extras = [
"toml",
], version = "^6.0", python = ">=3.6,<3.7"},
{extras = [
"toml",
], version = ">=6.5", python = ">=3.7"},
]
flake8 = "^5.0.4"
mypy = "^0.971"
pre-commit = {version = "^2.15.0", python = "^3.6.1"}
pytest = "^6.2"
pytest-cov = "^3.0.0"
pytest-cov = "^4.0.0"
types-PyYAML = "^6.0.1"

[tool.poetry.scripts]
Expand Down Expand Up @@ -59,4 +67,4 @@ src_paths = ["sync_with_poetry", "tests"]

[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core>=1.0.0"]
requires = ["poetry-core"]
2 changes: 1 addition & 1 deletion sync_with_poetry/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.4.0"
__version__ = "1.0.0"
59 changes: 18 additions & 41 deletions sync_with_poetry/swp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,30 @@
REV_LINE_RE = re.compile(r'^(\s+)rev:(\s*)([\'"]?)([^\s#]+)(.*)(\r?\n)$')


class PreCommitRepo(object):
"""A simple class representing the version (i.e., rev) of a repo
to sync in .pre-commit-config.yaml"""

def __init__(self, name: str, repo: str, rev: str) -> None:
self.name = name
self.repo = repo
self.rev = rev


class PoetryItems(object):
"""A class to get and filter poetry.lock packages to sync in .pre-commit-config.yaml"""

def __init__(
self,
poetry_list: AoT,
all: bool = False,
skip: List[str] = [],
db: Dict[str, Dict[str, str]] = DEPENDENCY_MAPPING,
) -> None:
"""Create a PoetryItems collection
Args:
poetry_list (list): a list of packages coming from poetry.lock
all (Optional[bool], optional): Set to True to consider all dependencies.
Set to False for dev dependencies only.
Defaults to False.
skip (Optional[list], optional): A list of packages to skip. Such packages won't
be synchronized in .pre-commit-config.yaml.
Defaults to [].
db (Dict[str, Dict[str, str]], optional): A package-repo mapping. Defaults to DEPENDENCY_MAPPING.
"""

self._poetry_list = []

self._poetry_lock = {}
for package in poetry_list:

# skip
# if all == False and this package is not a dev dependency
# or
# if the package is in the skip list
if ((not all) and package["category"] != "dev") or package["name"] in skip:
if package["name"] in skip:
continue

dependency_mapping = db.get(package["name"], None)
Expand All @@ -66,25 +48,23 @@ def __init__(
rev = Template(dependency_mapping["rev"]).substitute(
rev=package["version"]
)
self._poetry_list.append(PreCommitRepo(name, repo, rev))
self._poetry_lock[repo] = {"name": name, "rev": rev}

def get_by_repo(self, repo: str) -> Optional[PreCommitRepo]:
"""Get a PreCommitRepo gives its url
def get_by_repo(self, repo: str) -> Optional[Dict[str, str]]:
"""Get a PreCommitRepo given its url
Args:
repo (str): The repo url
Returns:
Optional[PreCommitRepo]: a PreCommitRepo instance
Optional[Dict[str, str]]: a dictionary representing a repo data (name and version)
e.g., {'name': 'black', 'rev': '22.8.0'}
"""
return next(
(package for package in self._poetry_list if package.repo == repo), None
)
return self._poetry_lock.get(repo)


def sync_repos(
filename: str,
all: bool = False,
skip: List[str] = [],
config: str = YAML_FILE,
db: Dict[str, Dict[str, str]] = DEPENDENCY_MAPPING,
Expand All @@ -96,7 +76,7 @@ def sync_repos(
content = toml.read()

assert isinstance(content["package"], AoT)
poetry_items = PoetryItems(content["package"], all, skip, db)
poetry_items = PoetryItems(content["package"], skip, db)

with open(config, "r") as stream:
pre_commit_data = yaml.safe_load(stream)
Expand All @@ -121,15 +101,13 @@ def sync_repos(

assert match is not None

if pre_commit_repo.rev == match[4].replace('"', "").replace("'", ""):
if pre_commit_repo["rev"] == match[4].replace('"', "").replace("'", ""):
continue

new_rev_s = yaml.dump({"rev": pre_commit_repo.rev}, default_style=match[3])
new_rev_s = yaml.dump({"rev": pre_commit_repo["rev"]}, default_style=match[3])
new_rev = new_rev_s.split(":", 1)[1].strip()
lines[idx] = f"{match[1]}rev:{match[2]}{new_rev}{match[5]}{match[6]}"
print(
f"[{pre_commit_repo.name}] {pre_commit_repo.repo}pyton -> rev: {pre_commit_repo.rev}"
)
print(f"[{pre_commit_repo['name']}] -> rev: {pre_commit_repo['rev']}")
retv |= 1

with open(config, "w", newline="") as f:
Expand All @@ -140,11 +118,11 @@ def sync_repos(
def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument("filenames", nargs="*")
parser.add_argument(
"--all",
action="store_true",
help="Scan all dependencies in poetry.lock (main and dev)",
)
# parser.add_argument(
# "--all",
# action="store_true",
# help="Scan all dependencies in poetry.lock (main and dev)",
# )
# See how to pass a list here: https://github.com/pre-commit/pre-commit/issues/971
parser.add_argument("--skip", nargs="*", default=[], help="Packages to skip")
parser.add_argument(
Expand All @@ -164,10 +142,9 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
else:
with open(args.db, "r") as f:
mapping = json.load(f)

retv = 0
for filename in args.filenames:
retv |= sync_repos(filename, args.all, args.skip, args.config, mapping)
retv |= sync_repos(filename, args.skip, args.config, mapping)
return retv


Expand Down
5 changes: 0 additions & 5 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,30 @@
'name = "mypy"\n'
'version = "0.910"\n'
'description = "Optional static typing for Python"\n'
'category = "dev"\n'
"optional = false\n"
'python-versions = ">=3.5"\n'
"[[package]]\n"
'name = "flake8"\n'
'version = "4.0.1"\n'
'description = "the modular source code checker: pep8 pyflakes and co"\n'
'category = "dev"\n'
"optional = false\n"
'python-versions = ">=3.6"\n'
"[[package]]\n"
'name = "black"\n'
'version = "21.11b1"\n'
'description = "The uncompromising code formatter."\n'
'category = "main"\n'
"optional = false\n"
'python-versions = ">=3.6.2"\n'
"[[package]]\n"
'name = "pytest"\n'
'version = "6.2.5"\n'
'description = "pytest: simple powerful testing with Python"\n'
'category = "dev"\n'
"optional = false\n"
'python-versions = ">=3.6"\n'
"[[package]]\n"
'name = "foobarbaz"\n'
'version = "1.0.1"\n'
'description = "a dummy package"\n'
'category = "dev"\n'
"optional = false\n"
'python-versions = ">=3.6"\n'
)
Expand Down
11 changes: 5 additions & 6 deletions tests/test_lock_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tomlkit

from sync_with_poetry.swp import PoetryItems, PreCommitRepo
from sync_with_poetry.swp import PoetryItems
from tests.helpers import LOCK_CONTENT


Expand All @@ -9,7 +9,7 @@ def test_poetry_items_creation() -> None:
content = tomlkit.loads(LOCK_CONTENT)
assert isinstance(content["package"], tomlkit.items.AoT)
p = PoetryItems(content["package"])
assert type(p._poetry_list) == list
assert type(p._poetry_lock) == dict


def test_poetry_items_metadata() -> None:
Expand All @@ -18,7 +18,6 @@ def test_poetry_items_metadata() -> None:
assert isinstance(content["package"], tomlkit.items.AoT)
p = PoetryItems(content["package"])
item = p.get_by_repo("https://github.com/pre-commit/mirrors-mypy")
assert type(item) == PreCommitRepo
assert item.name == "mypy"
assert item.repo == "https://github.com/pre-commit/mirrors-mypy"
assert item.rev == "v0.910"
assert type(item) == dict
assert item["name"] == "mypy"
assert item["rev"] == "v0.910"
2 changes: 1 addition & 1 deletion tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

def test_version() -> None:
"""Test version"""
assert __version__ == "0.4.0"
assert __version__ == "1.0.0"
Loading

0 comments on commit 97df834

Please sign in to comment.