1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
from __future__ import annotations
import os
import pathlib
import pickle
import sys
from datetime import datetime
__all__ = [
"ConfigurationMixIn", # Deprecated
"find_default_config_files",
"find_pylintrc", # Deprecated
"Option", # Deprecated
"OptionsManagerMixIn", # Deprecated
"OptionParser", # Deprecated
"OptionsProviderMixIn", # Deprecated
"UnsupportedAction", # Deprecated
"PYLINTRC",
"USER_HOME",
]
from pylint.config.arguments_provider import UnsupportedAction
from pylint.config.configuration_mixin import ConfigurationMixIn
from pylint.config.environment_variable import PYLINTRC
from pylint.config.find_default_config_files import (
find_default_config_files,
find_pylintrc,
)
from pylint.config.option import Option
from pylint.config.option_manager_mixin import OptionsManagerMixIn
from pylint.config.option_parser import OptionParser
from pylint.config.options_provider_mixin import OptionsProviderMixIn
from pylint.constants import DEFAULT_PYLINT_HOME, OLD_DEFAULT_PYLINT_HOME, USER_HOME
from pylint.utils import LinterStats
if "PYLINTHOME" in os.environ:
PYLINT_HOME = os.environ["PYLINTHOME"]
else:
PYLINT_HOME = DEFAULT_PYLINT_HOME
# The spam prevention is due to pylint being used in parallel by
# pre-commit, and the message being spammy in this context
# Also if you work with old version of pylint that recreate 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 = os.path.join(
PYLINT_HOME,
datetime.now().strftime(prefix_spam_prevention + "_%Y-%m-%d.temp"),
)
old_home = os.path.join(USER_HOME, OLD_DEFAULT_PYLINT_HOME)
if os.path.exists(old_home) and not os.path.exists(spam_prevention_file):
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 os.path.exists(PYLINT_HOME):
for filename in os.listdir(PYLINT_HOME):
if prefix_spam_prevention in filename:
try:
os.remove(os.path.join(PYLINT_HOME, filename))
except OSError:
pass
# Create spam prevention file for today
try:
pathlib.Path(PYLINT_HOME).mkdir(parents=True, exist_ok=True)
with open(spam_prevention_file, "w", encoding="utf8") as f:
f.write("")
except Exception as exc: # 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_pdata_path(base_name: str, recurs: int) -> pathlib.Path:
base_name = base_name.replace(os.sep, "_")
return pathlib.Path(PYLINT_HOME) / f"{base_name}{recurs}.stats"
def load_results(base: str) -> LinterStats | None:
data_file = _get_pdata_path(base, 1)
try:
with open(data_file, "rb") as stream:
data = pickle.load(stream)
if not isinstance(data, LinterStats):
raise TypeError
return data
except Exception: # pylint: disable=broad-except
return None
def save_results(results: LinterStats, base: str) -> None:
if not os.path.exists(PYLINT_HOME):
try:
os.makedirs(PYLINT_HOME)
except OSError:
print(f"Unable to create directory {PYLINT_HOME}", file=sys.stderr)
data_file = _get_pdata_path(base, 1)
try:
with open(data_file, "wb") as stream:
pickle.dump(results, stream)
except OSError as ex:
print(f"Unable to create file {data_file}: {ex}", file=sys.stderr)
|