Skip to content

Commit

Permalink
Make pre-commit an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Mar 30, 2021
1 parent e5990ae commit 2afeebf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
21 changes: 12 additions & 9 deletions repo_helper/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import os
import platform
import textwrap
from contextlib import suppress
from typing import Iterable, Optional

# 3rd party
Expand All @@ -38,7 +39,6 @@
from domdf_python_tools.paths import PathPlus, in_directory
from domdf_python_tools.typing import PathLike
from dulwich.errors import CommitError
from pre_commit.commands import install_uninstall # type: ignore
from southwark import assert_clean
from southwark.repo import Repo

Expand All @@ -47,10 +47,14 @@
"run_repo_helper",
]

# Disable logging from pre-commit install command
logging.getLogger(install_uninstall.__name__).addHandler(logging.NullHandler())
logging.getLogger(install_uninstall.__name__).propagate = False
logging.getLogger(install_uninstall.__name__).addFilter(lambda record: False)
with suppress(ImportError):
# 3rd party
from pre_commit.commands import install_uninstall # type: ignore

# Disable logging from pre-commit install command
logging.getLogger(install_uninstall.__name__).addHandler(logging.NullHandler())
logging.getLogger(install_uninstall.__name__).propagate = False
logging.getLogger(install_uninstall.__name__).addFilter(lambda record: False)


def commit_changed_files(
Expand All @@ -73,9 +77,6 @@ def commit_changed_files(
:returns: :py:obj:`True` if the changes were committed. :py:obj:`False` otherwise.
"""

# 3rd party
import pre_commit.main # type: ignore

# this package
from repo_helper.utils import commit_changes, sort_paths, stage_changes

Expand All @@ -86,7 +87,9 @@ def commit_changed_files(

# Ensure pre-commit hooks are installed
if enable_pre_commit and platform.system() == "Linux":
with in_directory(repo_path):
with in_directory(repo_path), suppress(ImportError):
# 3rd party
import pre_commit.main # type: ignore
pre_commit.main.main(["install"])

if staged_files:
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ isort>=5.0.0
jinja2>=2.11.2
natsort>=7.1.0
packaging>=20.4
pre-commit>=2.7.1
ruamel-yaml>=0.16.12
shippinglabel>=0.8.2
southwark>=0.7.1
Expand Down

0 comments on commit 2afeebf

Please sign in to comment.