run.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import codecs
  2. import os.path
  3. import sys
  4. import warnings
  5. from datetime import datetime
  6. from pathlib import Path
  7. from typing import TextIO
  8. from prospector import blender, postfilter, tools
  9. from prospector.compat import is_relative_to
  10. from prospector.config import ProspectorConfig
  11. from prospector.config import configuration as cfg
  12. from prospector.exceptions import FatalProspectorException
  13. from prospector.finder import FileFinder
  14. from prospector.formatters import FORMATTERS, Formatter
  15. from prospector.message import Location, Message
  16. from prospector.tools import DEPRECATED_TOOL_NAMES
  17. from prospector.tools.utils import CaptureOutput
  18. class Prospector:
  19. def __init__(self, config: ProspectorConfig):
  20. self.config = config
  21. self.summary = None
  22. self.messages = config.messages
  23. def process_messages(self, found_files, messages):
  24. if self.config.blending:
  25. messages = blender.blend(messages)
  26. if self.config.legacy_tool_names:
  27. updated = []
  28. new_names = {v: k for k, v in DEPRECATED_TOOL_NAMES.items()}
  29. for msg in messages:
  30. msg.source = new_names.get(msg.source, msg.source)
  31. updated.append(msg)
  32. messages = updated
  33. return postfilter.filter_messages(found_files.python_modules, messages)
  34. def execute(self):
  35. deprecated_names = self.config.replace_deprecated_tool_names()
  36. summary = {
  37. "started": datetime.now(),
  38. }
  39. summary.update(self.config.get_summary_information())
  40. paths = [Path(p) for p in self.config.paths]
  41. found_files = FileFinder(*paths, exclusion_filters=[self.config.make_exclusion_filter()])
  42. messages = []
  43. # see if any old tool names are run
  44. for deprecated_name in deprecated_names:
  45. loc = Location(self.config.workdir, None, None, None, None)
  46. new_name = DEPRECATED_TOOL_NAMES[deprecated_name]
  47. msg = (
  48. f"Tool {deprecated_name} has been renamed to {new_name}. "
  49. f"The old name {deprecated_name} is now deprecated and will be removed in Prospector 2.0. "
  50. f"Please update your prospector configuration."
  51. )
  52. message = Message(
  53. "prospector",
  54. "Deprecation",
  55. loc,
  56. message=msg,
  57. )
  58. messages.append(message)
  59. warnings.warn(msg, category=DeprecationWarning)
  60. # Run the tools
  61. for tool in self.config.get_tools(found_files):
  62. for name, cls in tools.TOOLS.items():
  63. if cls == tool.__class__:
  64. toolname = name
  65. break
  66. else:
  67. toolname = "Unknown"
  68. try:
  69. # Tools can output to stdout/stderr in unexpected places, for example,
  70. # pydocstyle emits warnings about __all__ and as pyroma exec's the setup.py
  71. # file, it will execute any print statements in that, etc etc...
  72. with CaptureOutput(hide=not self.config.direct_tool_stdout) as capture:
  73. messages += tool.run(found_files)
  74. if self.config.include_tool_stdout:
  75. loc = Location(self.config.workdir, None, None, None, None)
  76. if capture.get_hidden_stderr():
  77. msg = f"stderr from {toolname}:\n{capture.get_hidden_stderr()}"
  78. messages.append(Message(toolname, "hidden-output", loc, message=msg))
  79. if capture.get_hidden_stdout():
  80. msg = f"stdout from {toolname}:\n{capture.get_hidden_stdout()}"
  81. messages.append(Message(toolname, "hidden-output", loc, message=msg))
  82. except FatalProspectorException as fatal:
  83. sys.stderr.write(str(fatal))
  84. sys.exit(2)
  85. except Exception as ex: # pylint:disable=broad-except
  86. if self.config.die_on_tool_error:
  87. raise FatalProspectorException(f"Tool {toolname} failed to run.") from ex
  88. loc = Location(self.config.workdir, None, None, None, None)
  89. msg = (
  90. f"Tool {toolname} failed to run "
  91. f"(exception was raised, re-run prospector with -X to see the stacktrace)"
  92. )
  93. message = Message(
  94. toolname,
  95. "failure",
  96. loc,
  97. message=msg,
  98. )
  99. messages.append(message)
  100. messages = self.process_messages(found_files, messages)
  101. summary["message_count"] = len(messages)
  102. summary["completed"] = datetime.now()
  103. delta = summary["completed"] - summary["started"]
  104. summary["time_taken"] = "%0.2f" % delta.total_seconds()
  105. external_config = []
  106. for tool, configured_by in self.config.configured_by.items():
  107. if configured_by is not None:
  108. external_config.append((tool, configured_by))
  109. if len(external_config) > 0:
  110. summary["external_config"] = ", ".join(["%s: %s" % info for info in external_config])
  111. self.summary = summary
  112. self.messages = self.messages + messages
  113. def get_summary(self):
  114. return self.summary
  115. def get_messages(self):
  116. return self.messages
  117. def print_messages(self):
  118. output_reports = self.config.get_output_report()
  119. for report in output_reports:
  120. output_format, output_files = report
  121. self.summary["formatter"] = output_format
  122. relative_to = None
  123. # use relative paths by default unless explicitly told otherwise (with a --absolute-paths flag)
  124. # or if some paths passed to prospector are not relative to the CWD
  125. if not self.config.absolute_paths and all(
  126. is_relative_to(p, self.config.workdir) for p in self.config.paths
  127. ):
  128. relative_to = self.config.workdir
  129. formatter = FORMATTERS[output_format](self.summary, self.messages, self.config.profile, relative_to)
  130. if not output_files and not self.config.quiet:
  131. self.write_to(formatter, sys.stdout)
  132. for output_file in output_files:
  133. with codecs.open(output_file, "w+") as target:
  134. self.write_to(formatter, target)
  135. def write_to(self, formatter: Formatter, target: TextIO):
  136. # Produce the output
  137. target.write(
  138. formatter.render(
  139. summary=not self.config.messages_only,
  140. messages=not self.config.summary_only,
  141. profile=self.config.show_profile,
  142. )
  143. )
  144. target.write("\n")
  145. def get_parser():
  146. """
  147. This is a helper method to return an argparse parser, to
  148. be used with the Sphinx argparse plugin for documentation.
  149. """
  150. manager = cfg.build_manager()
  151. source = cfg.build_command_line_source(prog="prospector", description=None)
  152. return source.build_parser(manager.settings, None)
  153. def main():
  154. # Get our configuration
  155. config = ProspectorConfig()
  156. paths = config.paths
  157. if len(paths) > 1 and not all(os.path.isfile(path) for path in paths):
  158. sys.stderr.write("\nIn multi-path mode, all inputs must be files, " "not directories.\n\n")
  159. get_parser().print_usage()
  160. sys.exit(2)
  161. # Make it so
  162. prospector = Prospector(config)
  163. prospector.execute()
  164. prospector.print_messages()
  165. if config.exit_with_zero_on_success():
  166. # if we ran successfully, and the user wants us to, then we'll
  167. # exit cleanly
  168. sys.exit(0)
  169. # otherwise, finding messages is grounds for exiting with an error
  170. # code, to make it easier for bash scripts and similar situations
  171. # to know if any errors have been found.
  172. if len(prospector.get_messages()) > 0:
  173. sys.exit(1)
  174. sys.exit(0)
  175. if __name__ == "__main__":
  176. main()