-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
871242f
commit 71d31d9
Showing
20 changed files
with
886 additions
and
394 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
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,16 @@ | ||
from usethis import console | ||
from usethis._pre_commit.hooks import get_hook_entry | ||
from usethis._tool import PreCommitTool | ||
|
||
|
||
def add_deptry_root_dir() -> None: | ||
if not PreCommitTool().is_used(): | ||
return | ||
|
||
entry = get_hook_entry() | ||
if not entry.startswith("uv run --frozen deptry"): | ||
console.print( | ||
"☐ Reconfigure deptry in '.pre-commit-config.yaml' to run on the '/tests' directory.", | ||
style="blue", | ||
) | ||
return |
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
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,106 @@ | ||
from collections import Counter | ||
from pathlib import Path | ||
|
||
import ruamel.yaml | ||
from ruamel.yaml.util import load_yaml_guess_indent | ||
|
||
from usethis._pre_commit.config import PreCommitRepoConfig | ||
|
||
_HOOK_ORDER = [ | ||
"validate-pyproject", | ||
"ruff-format", | ||
"ruff-check", | ||
"deptry", | ||
] | ||
|
||
|
||
class DuplicatedHookNameError(ValueError): | ||
"""Raised when a hook name is duplicated in a pre-commit configuration file.""" | ||
|
||
|
||
def add_hook(config: PreCommitRepoConfig) -> None: | ||
path = Path.cwd() / ".pre-commit-config.yaml" | ||
|
||
with path.open(mode="r") as f: | ||
content, sequence_ind, offset_ind = load_yaml_guess_indent(f) | ||
|
||
yaml = ruamel.yaml.YAML(typ="rt") | ||
yaml.indent(mapping=sequence_ind, sequence=sequence_ind, offset=offset_ind) | ||
|
||
(hook_config,) = config.hooks | ||
hook_name = hook_config.id | ||
|
||
# Get an ordered list of the hooks already in the file | ||
existing_hooks = get_hook_names(path.parent) | ||
|
||
if not existing_hooks: | ||
raise NotImplementedError | ||
|
||
# Get the precendents, i.e. hooks occuring before the new hook | ||
try: | ||
hook_idx = _HOOK_ORDER.index(hook_name) | ||
except ValueError: | ||
raise NotImplementedError(f"Hook '{hook_name}' not recognized") | ||
precedents = _HOOK_ORDER[:hook_idx] | ||
|
||
# Find the last of the precedents in the existing hooks | ||
existings_precedents = [hook for hook in existing_hooks if hook in precedents] | ||
if existings_precedents: | ||
last_precedent = existings_precedents[-1] | ||
else: | ||
# Use the last existing hook | ||
last_precedent = existing_hooks[-1] | ||
|
||
# Insert the new hook after the last precedent repo | ||
# Do this by iterating over the repos and hooks, and inserting the new hook after | ||
# the last precedent | ||
new_repos = [] | ||
for repo in content["repos"]: | ||
new_repos.append(repo) | ||
for hook in repo["hooks"]: | ||
if hook["id"] == last_precedent: | ||
new_repos.append(config.model_dump(exclude_none=True)) | ||
content["repos"] = new_repos | ||
|
||
# Dump the new content | ||
yaml.dump(content, path) | ||
|
||
|
||
def remove_hook(name: str) -> None: | ||
path = Path.cwd() / ".pre-commit-config.yaml" | ||
|
||
with path.open(mode="r") as f: | ||
content, sequence_ind, offset_ind = load_yaml_guess_indent(f) | ||
|
||
yaml = ruamel.yaml.YAML(typ="rt") | ||
yaml.indent(mapping=sequence_ind, sequence=sequence_ind, offset=offset_ind) | ||
|
||
# search across the repos for any hooks with ID equal to name | ||
for repo in content["repos"]: | ||
for hook in repo["hooks"]: | ||
if hook["id"] == name: | ||
repo["hooks"].remove(hook) | ||
|
||
# if repo has no hooks, remove it | ||
if not repo["hooks"]: | ||
content["repos"].remove(repo) | ||
|
||
yaml.dump(content, path) | ||
|
||
|
||
def get_hook_names(path: Path) -> list[str]: | ||
yaml = ruamel.yaml.YAML() | ||
with (path / ".pre-commit-config.yaml").open(mode="r") as f: | ||
content = yaml.load(f) | ||
|
||
hook_names = [] | ||
for repo in content["repos"]: | ||
for hook in repo["hooks"]: | ||
hook_names.append(hook["id"]) | ||
|
||
# Need to validate there are no duplciates | ||
for name, count in Counter(hook_names).items(): | ||
if count > 1: | ||
raise DuplicatedHookNameError(f"Hook name '{name}' is duplicated") | ||
|
||
return hook_names |
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
Oops, something went wrong.