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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# 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 sys
import warnings
from collections.abc import Sequence
from pathlib import Path
from typing import Any, ClassVar
from pylint import config
from pylint.config._pylint_config import (
_handle_pylint_config_commands,
_register_generate_config_options,
)
from pylint.config.config_initialization import _config_initialization
from pylint.config.exceptions import ArgumentPreprocessingError
from pylint.config.utils import _preprocess_options
from pylint.constants import full_version
from pylint.lint.base_options import _make_run_options
from pylint.lint.pylinter import MANAGER, PyLinter
from pylint.reporters.base_reporter import BaseReporter
try:
import multiprocessing
from multiprocessing import synchronize # noqa pylint: disable=unused-import
except ImportError:
multiprocessing = None # type: ignore[assignment]
try:
from concurrent.futures import ProcessPoolExecutor
except ImportError:
ProcessPoolExecutor = None # type: ignore[assignment,misc]
def _query_cpu() -> int | None:
"""Try to determine number of CPUs allotted in a docker container.
This is based on discussion and copied from suggestions in
https://bugs.python.org/issue36054.
"""
cpu_quota, avail_cpu = None, None
if Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").is_file():
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", encoding="utf-8") as file:
# Not useful for AWS Batch based jobs as result is -1, but works on local linux systems
cpu_quota = int(file.read().rstrip())
if (
cpu_quota
and cpu_quota != -1
and Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us").is_file()
):
with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", encoding="utf-8") as file:
cpu_period = int(file.read().rstrip())
# Divide quota by period and you should get num of allotted CPU to the container, rounded down if fractional.
avail_cpu = int(cpu_quota / cpu_period)
elif Path("/sys/fs/cgroup/cpu/cpu.shares").is_file():
with open("/sys/fs/cgroup/cpu/cpu.shares", encoding="utf-8") as file:
cpu_shares = int(file.read().rstrip())
# For AWS, gives correct value * 1024.
avail_cpu = int(cpu_shares / 1024)
# In K8s Pods also a fraction of a single core could be available
# As multiprocessing is not able to run only a "fraction" of process
# assume we have 1 CPU available
if avail_cpu == 0:
avail_cpu = 1
return avail_cpu
def _cpu_count() -> int:
"""Use sched_affinity if available for virtualized or containerized
environments.
"""
cpu_share = _query_cpu()
cpu_count = None
sched_getaffinity = getattr(os, "sched_getaffinity", None)
# pylint: disable=not-callable,using-constant-test,useless-suppression
if sched_getaffinity:
cpu_count = len(sched_getaffinity(0))
elif multiprocessing:
cpu_count = multiprocessing.cpu_count()
else:
cpu_count = 1
if sys.platform == "win32":
# See also https://github.com/python/cpython/issues/94242
cpu_count = min(cpu_count, 56) # pragma: no cover
if cpu_share is not None:
return min(cpu_share, cpu_count)
return cpu_count
UNUSED_PARAM_SENTINEL = object()
class Run:
"""Helper class to use as main for pylint with 'run(*sys.argv[1:])'."""
LinterClass = PyLinter
option_groups = (
(
"Commands",
"Options which are actually commands. Options in this \
group are mutually exclusive.",
),
)
_is_pylint_config: ClassVar[bool] = False
"""Boolean whether or not this is a 'pylint-config' run.
Used by _PylintConfigRun to make the 'pylint-config' command work.
"""
# pylint: disable = too-many-statements, too-many-branches
def __init__(
self,
args: Sequence[str],
reporter: BaseReporter | None = None,
exit: bool = True, # pylint: disable=redefined-builtin
do_exit: Any = UNUSED_PARAM_SENTINEL,
) -> None:
# Immediately exit if user asks for version
if "--version" in args:
print(full_version)
sys.exit(0)
self._rcfile: str | None = None
self._output: str | None = None
self._plugins: list[str] = []
self.verbose: bool = False
# Pre-process certain options and remove them from args list
try:
args = _preprocess_options(self, args)
except ArgumentPreprocessingError as ex:
print(ex, file=sys.stderr)
sys.exit(32)
# Determine configuration file
if self._rcfile is None:
default_file = next(config.find_default_config_files(), None)
if default_file:
self._rcfile = str(default_file)
self.linter = linter = self.LinterClass(
_make_run_options(self),
option_groups=self.option_groups,
pylintrc=self._rcfile,
)
# register standard checkers
linter.load_default_plugins()
# load command line plugins
linter.load_plugin_modules(self._plugins)
linter.disable("I")
linter.enable("c-extension-no-member")
# Register the options needed for 'pylint-config'
# By not registering them by default they don't show up in the normal usage message
if self._is_pylint_config:
_register_generate_config_options(linter._arg_parser)
args = _config_initialization(
linter, args, reporter, config_file=self._rcfile, verbose_mode=self.verbose
)
# Handle the 'pylint-config' command
if self._is_pylint_config:
warnings.warn(
"NOTE: The 'pylint-config' command is experimental and usage can change",
UserWarning,
)
code = _handle_pylint_config_commands(linter)
if exit:
sys.exit(code)
return
# Display help messages if there are no files to lint
if not args:
print(linter.help())
sys.exit(32)
if linter.config.jobs < 0:
print(
f"Jobs number ({linter.config.jobs}) should be greater than or equal to 0",
file=sys.stderr,
)
sys.exit(32)
if linter.config.jobs > 1 or linter.config.jobs == 0:
if ProcessPoolExecutor is None:
print(
"concurrent.futures module is missing, fallback to single process",
file=sys.stderr,
)
linter.set_option("jobs", 1)
elif linter.config.jobs == 0:
linter.config.jobs = _cpu_count()
if self._output:
try:
with open(self._output, "w", encoding="utf-8") as output:
linter.reporter.out = output
linter.check(args)
score_value = linter.generate_reports()
except OSError as ex:
print(ex, file=sys.stderr)
sys.exit(32)
else:
linter.check(args)
score_value = linter.generate_reports()
if do_exit is not UNUSED_PARAM_SENTINEL:
warnings.warn(
"do_exit is deprecated and it is going to be removed in a future version.",
DeprecationWarning,
)
exit = do_exit
if linter.config.clear_cache_post_run:
MANAGER.clear_cache()
if exit:
if linter.config.exit_zero:
sys.exit(0)
elif linter.any_fail_on_issues():
# We need to make sure we return a failing exit code in this case.
# So we use self.linter.msg_status if that is non-zero, otherwise we just return 1.
sys.exit(self.linter.msg_status or 1)
elif score_value is not None:
if score_value >= linter.config.fail_under:
sys.exit(0)
else:
# We need to make sure we return a failing exit code in this case.
# So we use self.linter.msg_status if that is non-zero, otherwise we just return 1.
sys.exit(self.linter.msg_status or 1)
else:
sys.exit(self.linter.msg_status)
class _PylintConfigRun(Run):
"""A private wrapper for the 'pylint-config' command."""
_is_pylint_config: ClassVar[bool] = True
"""Boolean whether or not this is a 'pylint-config' run.
Used by _PylintConfigRun to make the 'pylint-config' command work.
"""
|