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-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
|
|
|
|
|
2020-05-03 09:09:23 +00:00
|
|
|
try:
|
|
|
|
content["tool"]["pylint"]
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
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()
|
2021-04-10 12:29:08 +00:00
|
|
|
parser.read(path, encoding="utf-8")
|
2020-05-03 09:09:23 +00:00
|
|
|
return any(section.startswith("pylint.") for section in parser.sections())
|
|
|
|
|
|
|
|
|
2021-10-25 12:42:45 +00:00
|
|
|
def find_default_config_files() -> Iterator[str]:
|
2020-05-03 09:09:23 +00:00
|
|
|
"""Find all possible config files."""
|
|
|
|
rc_names = ("pylintrc", ".pylintrc")
|
|
|
|
config_names = rc_names + ("pyproject.toml", "setup.cfg")
|
|
|
|
for config_name in config_names:
|
|
|
|
if os.path.isfile(config_name):
|
|
|
|
if config_name.endswith(".toml") and not _toml_has_config(config_name):
|
|
|
|
continue
|
|
|
|
if config_name.endswith(".cfg") and not _cfg_has_config(config_name):
|
|
|
|
continue
|
|
|
|
|
|
|
|
yield os.path.abspath(config_name)
|
|
|
|
|
|
|
|
if os.path.isfile("__init__.py"):
|
|
|
|
curdir = os.path.abspath(os.getcwd())
|
|
|
|
while os.path.isfile(os.path.join(curdir, "__init__.py")):
|
|
|
|
curdir = os.path.abspath(os.path.join(curdir, ".."))
|
|
|
|
for rc_name in rc_names:
|
|
|
|
rc_path = os.path.join(curdir, rc_name)
|
|
|
|
if os.path.isfile(rc_path):
|
|
|
|
yield rc_path
|
|
|
|
|
|
|
|
if "PYLINTRC" in os.environ and os.path.exists(os.environ["PYLINTRC"]):
|
|
|
|
if os.path.isfile(os.environ["PYLINTRC"]):
|
|
|
|
yield os.environ["PYLINTRC"]
|
|
|
|
else:
|
|
|
|
user_home = os.path.expanduser("~")
|
|
|
|
if user_home not in ("~", "/root"):
|
|
|
|
home_rc = os.path.join(user_home, ".pylintrc")
|
|
|
|
if os.path.isfile(home_rc):
|
|
|
|
yield home_rc
|
|
|
|
home_rc = os.path.join(user_home, ".config", "pylintrc")
|
|
|
|
if os.path.isfile(home_rc):
|
|
|
|
yield home_rc
|
|
|
|
|
|
|
|
if os.path.isfile("/etc/pylintrc"):
|
|
|
|
yield "/etc/pylintrc"
|
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():
|
|
|
|
if config_file.endswith("pylintrc"):
|
|
|
|
return config_file
|
|
|
|
return None
|