config_initialization.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. import sys
  6. from glob import glob
  7. from itertools import chain
  8. from pathlib import Path
  9. from typing import TYPE_CHECKING
  10. from pylint import reporters
  11. from pylint.config.config_file_parser import _ConfigurationFileParser
  12. from pylint.config.exceptions import _UnrecognizedOptionError
  13. from pylint.utils import utils
  14. if TYPE_CHECKING:
  15. from pylint.lint import PyLinter
  16. def _config_initialization(
  17. linter: PyLinter,
  18. args_list: list[str],
  19. reporter: reporters.BaseReporter | reporters.MultiReporter | None = None,
  20. config_file: None | str | Path = None,
  21. verbose_mode: bool = False,
  22. ) -> list[str]:
  23. """Parse all available options, read config files and command line arguments and
  24. set options accordingly.
  25. """
  26. config_file = Path(config_file) if config_file else None
  27. # Set the current module to the configuration file
  28. # to allow raising messages on the configuration file.
  29. linter.set_current_module(str(config_file) if config_file else "")
  30. # Read the configuration file
  31. config_file_parser = _ConfigurationFileParser(verbose_mode, linter)
  32. try:
  33. config_data, config_args = config_file_parser.parse_config_file(
  34. file_path=config_file
  35. )
  36. except OSError as ex:
  37. print(ex, file=sys.stderr)
  38. sys.exit(32)
  39. # Run init hook, if present, before loading plugins
  40. if "init-hook" in config_data:
  41. exec(utils._unquote(config_data["init-hook"])) # pylint: disable=exec-used
  42. # Load plugins if specified in the config file
  43. if "load-plugins" in config_data:
  44. linter.load_plugin_modules(utils._splitstrip(config_data["load-plugins"]))
  45. unrecognized_options_message = None
  46. # First we parse any options from a configuration file
  47. try:
  48. linter._parse_configuration_file(config_args)
  49. except _UnrecognizedOptionError as exc:
  50. unrecognized_options_message = ", ".join(exc.options)
  51. # Then, if a custom reporter is provided as argument, it may be overridden
  52. # by file parameters, so we re-set it here. We do this before command line
  53. # parsing, so it's still overridable by command line options
  54. if reporter:
  55. linter.set_reporter(reporter)
  56. # Set the current module to the command line
  57. # to allow raising messages on it
  58. linter.set_current_module("Command line")
  59. # Now we parse any options from the command line, so they can override
  60. # the configuration file
  61. parsed_args_list = linter._parse_command_line_configuration(args_list)
  62. # Remove the positional arguments separator from the list of arguments if it exists
  63. try:
  64. parsed_args_list.remove("--")
  65. except ValueError:
  66. pass
  67. # Check if there are any options that we do not recognize
  68. unrecognized_options: list[str] = []
  69. for opt in parsed_args_list:
  70. if opt.startswith("--"):
  71. unrecognized_options.append(opt[2:])
  72. elif opt.startswith("-"):
  73. unrecognized_options.append(opt[1:])
  74. if unrecognized_options:
  75. msg = ", ".join(unrecognized_options)
  76. try:
  77. linter._arg_parser.error(f"Unrecognized option found: {msg}")
  78. except SystemExit:
  79. sys.exit(32)
  80. # Now that config file and command line options have been loaded
  81. # with all disables, it is safe to emit messages
  82. if unrecognized_options_message is not None:
  83. linter.set_current_module(str(config_file) if config_file else "")
  84. linter.add_message(
  85. "unrecognized-option", args=unrecognized_options_message, line=0
  86. )
  87. linter._emit_stashed_messages()
  88. # Set the current module to configuration as we don't know where
  89. # the --load-plugins key is coming from
  90. linter.set_current_module("Command line or configuration file")
  91. # We have loaded configuration from config file and command line. Now, we can
  92. # load plugin specific configuration.
  93. linter.load_plugin_configuration()
  94. # Now that plugins are loaded, get list of all fail_on messages, and
  95. # enable them
  96. linter.enable_fail_on_messages()
  97. linter._parse_error_mode()
  98. # Link the base Namespace object on the current directory
  99. linter._directory_namespaces[Path(".").resolve()] = (linter.config, {})
  100. # parsed_args_list should now only be a list of inputs to lint.
  101. # All other options have been removed from the list.
  102. return list(
  103. chain.from_iterable(
  104. # NOTE: 'or [arg]' is needed in the case the input file or directory does
  105. # not exist and 'glob(arg)' cannot find anything. Without this we would
  106. # not be able to output the fatal import error for this module later on,
  107. # as it would get silently ignored.
  108. glob(arg, recursive=True) or [arg]
  109. for arg in parsed_args_list
  110. )
  111. )