summaryrefslogtreecommitdiff
path: root/pylint/constants.py
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-05-05 17:22:22 +0200
committerGitHub <noreply@github.com>2022-05-05 17:22:22 +0200
commit04d54f3d179d378076e9dd465cbd8a35a32d659b (patch)
tree169ba2f42c81a4524e507a8c489dd6790cece84c /pylint/constants.py
parent12648fde32b7ed976666e5ff5b35171c0bd50150 (diff)
downloadpylint-git-04d54f3d179d378076e9dd465cbd8a35a32d659b.tar.gz
Move ``PYLINTHOME`` to constants and test it (#6509)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'pylint/constants.py')
-rw-r--r--pylint/constants.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/pylint/constants.py b/pylint/constants.py
index 90e2b734e..6986b8a4e 100644
--- a/pylint/constants.py
+++ b/pylint/constants.py
@@ -5,8 +5,10 @@
from __future__ import annotations
import os
+import pathlib
import platform
import sys
+from datetime import datetime
from typing import NamedTuple
import astroid
@@ -209,3 +211,58 @@ INCOMPATIBLE_WITH_USELESS_SUPPRESSION = frozenset(
TYPING_TYPE_CHECKS_GUARDS = frozenset({"typing.TYPE_CHECKING", "TYPE_CHECKING"})
+
+
+def _warn_about_old_home(pylint_home: pathlib.Path) -> None:
+ """Warn users about the old pylint home being deprecated.
+
+ The spam prevention mechanism is due to pylint being used in parallel by
+ pre-commit, and the message being spammy in this context
+ Also if you work with an old version of pylint that recreates the
+ old pylint home, you can get the old message for a long time.
+ """
+ prefix_spam_prevention = "pylint_warned_about_old_cache_already"
+ spam_prevention_file = pathlib.Path(pylint_home) / datetime.now().strftime(
+ prefix_spam_prevention + "_%Y-%m-%d.temp"
+ )
+ old_home = pathlib.Path(USER_HOME) / OLD_DEFAULT_PYLINT_HOME
+
+ if old_home.exists() and not spam_prevention_file.exists():
+ print(
+ f"PYLINTHOME is now '{pylint_home}' but obsolescent '{old_home}' is found; "
+ "you can safely remove the latter",
+ file=sys.stderr,
+ )
+
+ # Remove old spam prevention file
+ if pylint_home.exists():
+ for filename in pylint_home.iterdir():
+ if prefix_spam_prevention in str(filename):
+ try:
+ os.remove(pylint_home / filename)
+ except OSError: # pragma: no cover
+ pass
+
+ # Create spam prevention file for today
+ try:
+ pylint_home.mkdir(parents=True, exist_ok=True)
+ with open(spam_prevention_file, "w", encoding="utf8") as f:
+ f.write("")
+ except Exception as exc: # pragma: no cover # pylint: disable=broad-except
+ print(
+ "Can't write the file that was supposed to "
+ f"prevent 'pylint.d' deprecation spam in {pylint_home} because of {exc}."
+ )
+
+
+def _get_pylint_home() -> str:
+ """Return the pylint home."""
+ if "PYLINTHOME" in os.environ:
+ return os.environ["PYLINTHOME"]
+
+ _warn_about_old_home(pathlib.Path(DEFAULT_PYLINT_HOME))
+
+ return DEFAULT_PYLINT_HOME
+
+
+PYLINT_HOME = _get_pylint_home()