Skip to content

Commit

Permalink
FEAT: autoupdate new pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Aug 24, 2023
1 parent ae858ec commit 423d095
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ module = ["ruamel.*"]
ignore_missing_imports = true
module = ["nbformat.*"]

[[tool.mypy.overrides]]
ignore_missing_imports = true
module = ["pre_commit.commands.autoupdate.*"]


[tool.pyright]
exclude = [
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ install_requires =
ini2toml
nbformat
pip-tools
pre-commit
PyYAML
ruamel.yaml # better YAML dumping
tomlkit
Expand Down
2 changes: 0 additions & 2 deletions src/repoma/check_dev_files/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import List, Set

from ruamel.yaml.comments import CommentedMap
from ruamel.yaml.scalarstring import DoubleQuotedScalarString
from tomlkit.items import Array, Table

from repoma.errors import PrecommitError
Expand Down Expand Up @@ -268,7 +267,6 @@ def _update_precommit_hook() -> None:
return
expected_hook = CommentedMap(
repo="https://github.com/astral-sh/ruff-pre-commit",
rev=DoubleQuotedScalarString(""),
hooks=[CommentedMap(id="ruff", args=["--fix"])],
)
update_single_hook_precommit_repo(expected_hook)
Expand Down
37 changes: 27 additions & 10 deletions src/repoma/utilities/precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import os.path
import re
import socket
from functools import lru_cache
from pathlib import Path
from textwrap import dedent
from typing import Any, List, Optional, Tuple, Type, TypeVar, Union

import attrs
import yaml
from attrs import define, field
from pre_commit.commands.autoupdate import autoupdate as precommit_autoupdate
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml.scalarstring import DoubleQuotedScalarString, PlainScalarString
from ruamel.yaml.scalarstring import PlainScalarString

from repoma.errors import PrecommitError

Expand Down Expand Up @@ -86,21 +88,22 @@ def update_single_hook_precommit_repo(expected_repo_def: CommentedMap) -> None:
hook_id: str = expected_repo_def["hooks"][0]["id"]
if idx_and_repo is None:
if "rev" not in expected_repo_def:
expected_repo_def.insert(1, "rev", DoubleQuotedScalarString(""))
expected_repo_def.insert(1, "rev", "PLEASE-UPDATE")
idx = _determine_expected_repo_index(config, hook_id)
repos.insert(idx, expected_repo_def)
repos.yaml_set_comment_before_after_key(
idx if idx + 1 == len(repos) else idx + 1,
before="\n",
)
yaml_parser.dump(config, CONFIG_PATH.precommit)
msg = dedent(f"""
Added {hook_id} hook to {CONFIG_PATH.precommit}. Please run
pre-commit autoupdate --repo {repo_url}
to update to the latest release of this pre-commit repository.
""").strip()
if has_internet_connection():
precommit_autoupdate(
CONFIG_PATH.precommit,
freeze=False,
repos=[repo_url],
tags_only=True,
)
msg = f"Added {hook_id} hook to {CONFIG_PATH.precommit}."
raise PrecommitError(msg)
idx, existing_hook = idx_and_repo
if not _is_equivalent_repo(existing_hook, expected_repo_def):
Expand Down Expand Up @@ -295,3 +298,17 @@ def fromdict(definition: dict, typ: Type[_T]) -> _T:
return PrecommitConfig(**definition) # type: ignore[return-value]
msg = f"No implementation for type {typ.__name__}"
raise NotImplementedError(msg)


@lru_cache(maxsize=None)
def has_internet_connection(
host: str = "8.8.8.8", port: int = 53, timeout: float = 0.5
) -> bool:
try:
# cspell:ignore setdefaulttimeout
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
except OSError:
return False
else:
return True

0 comments on commit 423d095

Please sign in to comment.