2020-05-03 09:09:23 +00:00
|
|
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2021-07-01 10:47:58 +00:00
|
|
|
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
|
2022-03-01 15:04:49 +00:00
|
|
|
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
|
2020-05-03 09:09:23 +00:00
|
|
|
|
|
|
|
import configparser
|
|
|
|
import os
|
2022-03-10 12:43:20 +00:00
|
|
|
import sys
|
2022-03-31 09:15:07 +00:00
|
|
|
import warnings
|
2022-03-30 11:29:13 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Iterator, Optional, Union
|
2020-05-03 09:09:23 +00:00
|
|
|
|
2022-03-10 12:43:20 +00:00
|
|
|
if sys.version_info >= (3, 11):
|
|
|
|
import tomllib
|
|
|
|
else:
|
|
|
|
import tomli as tomllib
|
2020-05-03 09:09:23 +00:00
|
|
|
|
2022-04-01 18:34:43 +00:00
|
|
|
RC_NAMES = (Path("pylintrc"), Path(".pylintrc"))
|
|
|
|
CONFIG_NAMES = RC_NAMES + (Path("pyproject.toml"), Path("setup.cfg"))
|
|
|
|
|
2020-05-03 09:09:23 +00:00
|
|
|
|
2022-03-30 11:29:13 +00:00
|
|
|
def _toml_has_config(path: Union[Path, str]) -> bool:
|
2022-03-10 12:43:20 +00:00
|
|
|
with open(path, mode="rb") as toml_handle:
|
2021-03-07 12:40:11 +00:00
|
|
|
try:
|
2022-03-10 12:43:20 +00:00
|
|
|
content = tomllib.load(toml_handle)
|
|
|
|
except tomllib.TOMLDecodeError as error:
|
2021-05-03 18:07:40 +00:00
|
|
|
print(f"Failed to load '{path}': {error}")
|
2021-03-07 12:40:11 +00:00
|
|
|
return False
|
2022-03-30 11:32:02 +00:00
|
|
|
return "pylint" in content.get("tool", [])
|
2020-05-03 09:09:23 +00:00
|
|
|
|
|
|
|
|
2022-03-30 11:29:13 +00:00
|
|
|
def _cfg_has_config(path: Union[Path, str]) -> bool:
|
2020-05-03 09:09:23 +00:00
|
|
|
parser = configparser.ConfigParser()
|
2022-03-30 11:50:06 +00:00
|
|
|
try:
|
|
|
|
parser.read(path, encoding="utf-8")
|
|
|
|
except configparser.Error:
|
|
|
|
return False
|
2020-05-03 09:09:23 +00:00
|
|
|
return any(section.startswith("pylint.") for section in parser.sections())
|
|
|
|
|
|
|
|
|
2022-04-01 18:34:43 +00:00
|
|
|
def find_default_config_files() -> Iterator[Path]:
|
2020-05-03 09:09:23 +00:00
|
|
|
"""Find all possible config files."""
|
2022-04-01 18:34:43 +00:00
|
|
|
for config_name in CONFIG_NAMES:
|
|
|
|
if config_name.is_file():
|
|
|
|
if config_name.suffix == ".toml" and not _toml_has_config(config_name):
|
2020-05-03 09:09:23 +00:00
|
|
|
continue
|
2022-04-01 18:34:43 +00:00
|
|
|
if config_name.suffix == ".cfg" and not _cfg_has_config(config_name):
|
2020-05-03 09:09:23 +00:00
|
|
|
continue
|
|
|
|
|
2022-04-01 18:34:43 +00:00
|
|
|
yield config_name.resolve()
|
2020-05-03 09:09:23 +00:00
|
|
|
|
2022-04-01 18:34:43 +00:00
|
|
|
if Path("__init__.py").is_file():
|
|
|
|
curdir = Path(os.getcwd()).resolve()
|
|
|
|
while (curdir / "__init__.py").is_file():
|
|
|
|
curdir = curdir.parent
|
|
|
|
for rc_name in RC_NAMES:
|
|
|
|
rc_path = curdir / rc_name
|
|
|
|
if rc_path.is_file():
|
|
|
|
yield rc_path.resolve()
|
2020-05-03 09:09:23 +00:00
|
|
|
|
2022-04-01 18:34:43 +00:00
|
|
|
if "PYLINTRC" in os.environ and Path(os.environ["PYLINTRC"]).exists():
|
|
|
|
if Path(os.environ["PYLINTRC"]).is_file():
|
|
|
|
yield Path(os.environ["PYLINTRC"]).resolve()
|
2020-05-03 09:09:23 +00:00
|
|
|
else:
|
2022-04-01 18:34:43 +00:00
|
|
|
user_home = Path.home()
|
|
|
|
if str(user_home) not in ("~", "/root"):
|
|
|
|
home_rc = user_home / ".pylintrc"
|
|
|
|
if home_rc.is_file():
|
|
|
|
yield home_rc.resolve()
|
|
|
|
home_rc = user_home / ".config" / "pylintrc"
|
|
|
|
if home_rc.is_file():
|
|
|
|
yield home_rc.resolve()
|
2020-05-03 09:09:23 +00:00
|
|
|
|
|
|
|
if os.path.isfile("/etc/pylintrc"):
|
2022-04-01 18:34:43 +00:00
|
|
|
yield Path("/etc/pylintrc").resolve()
|
2021-10-25 12:42:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find_pylintrc() -> Optional[str]:
|
2022-02-10 18:30:15 +00:00
|
|
|
"""Search the pylint rc file and return its path if it finds it, else return None."""
|
2022-03-31 09:15:07 +00:00
|
|
|
# pylint: disable-next=fixme
|
|
|
|
# TODO: Remove this function in 3.0
|
|
|
|
warnings.warn(
|
|
|
|
"find_pylintrc and the PYLINTRC constant have been deprecated. "
|
|
|
|
"Use find_default_config_files if you want access to pylint's configuration file "
|
|
|
|
"finding logic.",
|
|
|
|
DeprecationWarning,
|
|
|
|
)
|
2021-10-25 12:42:45 +00:00
|
|
|
for config_file in find_default_config_files():
|
2022-04-01 18:34:43 +00:00
|
|
|
if str(config_file).endswith("pylintrc"):
|
|
|
|
return str(config_file)
|
2021-10-25 12:42:45 +00:00
|
|
|
return None
|