epylint.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4
  2. # -*- vim:fenc=utf-8:ft=python:et:sw=4:ts=4:sts=4
  3. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  4. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  5. # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
  6. """Emacs and Flymake compatible Pylint.
  7. This script is for integration with Emacs and is compatible with Flymake mode.
  8. epylint walks out of python packages before invoking pylint. This avoids
  9. reporting import errors that occur when a module within a package uses the
  10. absolute import path to get another module within this package.
  11. For example:
  12. - Suppose a package is structured as
  13. a/__init__.py
  14. a/b/x.py
  15. a/c/y.py
  16. - Then if y.py imports x as "from a.b import x" the following produces pylint
  17. errors
  18. cd a/c; pylint y.py
  19. - The following obviously doesn't
  20. pylint a/c/y.py
  21. - As this script will be invoked by Emacs within the directory of the file
  22. we are checking we need to go out of it to avoid these false positives.
  23. You may also use py_run to run pylint with desired options and get back (or not)
  24. its output.
  25. """
  26. from __future__ import annotations
  27. import os
  28. import shlex
  29. import sys
  30. import warnings
  31. from collections.abc import Sequence
  32. from io import StringIO
  33. from subprocess import PIPE, Popen
  34. from typing import NoReturn, TextIO, overload
  35. if sys.version_info >= (3, 8):
  36. from typing import Literal
  37. else:
  38. from typing_extensions import Literal
  39. def _get_env() -> dict[str, str]:
  40. """Extracts the environment PYTHONPATH and appends the current 'sys.path'
  41. to it.
  42. """
  43. env = dict(os.environ)
  44. env["PYTHONPATH"] = os.pathsep.join(sys.path)
  45. return env
  46. def lint(filename: str, options: Sequence[str] = ()) -> int:
  47. """Pylint the given file.
  48. When run from Emacs we will be in the directory of a file, and passed its
  49. filename. If this file is part of a package and is trying to import other
  50. modules from within its own package or another package rooted in a directory
  51. below it, pylint will classify it as a failed import.
  52. To get around this, we traverse down the directory tree to find the root of
  53. the package this module is in. We then invoke pylint from this directory.
  54. Finally, we must correct the filenames in the output generated by pylint so
  55. Emacs doesn't become confused (it will expect just the original filename,
  56. while pylint may extend it with extra directories if we've traversed down
  57. the tree)
  58. """
  59. # traverse downwards until we are out of a python package
  60. full_path = os.path.abspath(filename)
  61. parent_path = os.path.dirname(full_path)
  62. child_path = os.path.basename(full_path)
  63. while parent_path != "/" and os.path.exists(
  64. os.path.join(parent_path, "__init__.py")
  65. ):
  66. child_path = os.path.join(os.path.basename(parent_path), child_path)
  67. parent_path = os.path.dirname(parent_path)
  68. # Start pylint
  69. # Ensure we use the python and pylint associated with the running epylint
  70. run_cmd = "import sys; from pylint.lint import Run; Run(sys.argv[1:])"
  71. cmd = (
  72. [sys.executable, "-c", run_cmd]
  73. + [
  74. "--msg-template",
  75. "{path}:{line}: {category} ({msg_id}, {symbol}, {obj}) {msg}",
  76. "-r",
  77. "n",
  78. child_path,
  79. ]
  80. + list(options)
  81. )
  82. with Popen(
  83. cmd, stdout=PIPE, cwd=parent_path, env=_get_env(), universal_newlines=True
  84. ) as process:
  85. for line in process.stdout: # type: ignore[union-attr]
  86. # remove pylintrc warning
  87. if line.startswith("No config file found"):
  88. continue
  89. # modify the file name that's put out to reverse the path traversal we made
  90. parts = line.split(":")
  91. if parts and parts[0] == child_path:
  92. line = ":".join([filename] + parts[1:])
  93. print(line, end=" ")
  94. process.wait()
  95. return process.returncode
  96. @overload
  97. def py_run(
  98. command_options: str = ...,
  99. return_std: Literal[False] = ...,
  100. stdout: TextIO | int | None = ...,
  101. stderr: TextIO | int | None = ...,
  102. ) -> None:
  103. ...
  104. @overload
  105. def py_run(
  106. command_options: str,
  107. return_std: Literal[True],
  108. stdout: TextIO | int | None = ...,
  109. stderr: TextIO | int | None = ...,
  110. ) -> tuple[StringIO, StringIO]:
  111. ...
  112. def py_run(
  113. command_options: str = "",
  114. return_std: bool = False,
  115. stdout: TextIO | int | None = None,
  116. stderr: TextIO | int | None = None,
  117. ) -> tuple[StringIO, StringIO] | None:
  118. """Run pylint from python.
  119. ``command_options`` is a string containing ``pylint`` command line options;
  120. ``return_std`` (boolean) indicates return of created standard output
  121. and error (see below);
  122. ``stdout`` and ``stderr`` are 'file-like' objects in which standard output
  123. could be written.
  124. Calling agent is responsible for stdout/err management (creation, close).
  125. Default standard output and error are those from sys,
  126. or standalone ones (``subprocess.PIPE``) are used
  127. if they are not set and ``return_std``.
  128. If ``return_std`` is set to ``True``, this function returns a 2-uple
  129. containing standard output and error related to created process,
  130. as follows: ``(stdout, stderr)``.
  131. To silently run Pylint on a module, and get its standard output and error:
  132. >>> (pylint_stdout, pylint_stderr) = py_run( 'module_name.py', True)
  133. """
  134. warnings.warn(
  135. "'epylint' will be removed in pylint 3.0, use https://github.com/emacsorphanage/pylint instead.",
  136. DeprecationWarning,
  137. stacklevel=2,
  138. )
  139. # Detect if we use Python as executable or not, else default to `python`
  140. executable = sys.executable if "python" in sys.executable else "python"
  141. # Create command line to call pylint
  142. epylint_part = [executable, "-c", "from pylint import epylint;epylint.Run()"]
  143. options = shlex.split(command_options, posix=not sys.platform.startswith("win"))
  144. cli = epylint_part + options
  145. # Providing standard output and/or error if not set
  146. if stdout is None:
  147. stdout = PIPE if return_std else sys.stdout
  148. if stderr is None:
  149. stderr = PIPE if return_std else sys.stderr
  150. # Call pylint in a sub-process
  151. with Popen(
  152. cli,
  153. shell=False,
  154. stdout=stdout,
  155. stderr=stderr,
  156. env=_get_env(),
  157. universal_newlines=True,
  158. ) as process:
  159. proc_stdout, proc_stderr = process.communicate()
  160. # Return standard output and error
  161. if return_std:
  162. return StringIO(proc_stdout), StringIO(proc_stderr)
  163. return None
  164. def Run(argv: Sequence[str] | None = None) -> NoReturn:
  165. warnings.warn(
  166. "'epylint' will be removed in pylint 3.0, use https://github.com/emacsorphanage/pylint instead.",
  167. DeprecationWarning,
  168. stacklevel=2,
  169. )
  170. if not argv and len(sys.argv) == 1:
  171. print(f"Usage: {sys.argv[0]} <filename> [options]")
  172. sys.exit(1)
  173. argv = argv or sys.argv[1:]
  174. if not os.path.exists(argv[0]):
  175. print(f"{argv[0]} does not exist")
  176. sys.exit(1)
  177. else:
  178. sys.exit(lint(argv[0], argv[1:]))
  179. if __name__ == "__main__":
  180. Run()