primer.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. import argparse
  6. import json
  7. import sys
  8. from pathlib import Path
  9. from pylint.testutils._primer import PackageToLint
  10. from pylint.testutils._primer.primer_command import PrimerCommand
  11. from pylint.testutils._primer.primer_compare_command import CompareCommand
  12. from pylint.testutils._primer.primer_prepare_command import PrepareCommand
  13. from pylint.testutils._primer.primer_run_command import RunCommand
  14. class Primer:
  15. """Main class to handle priming of packages."""
  16. def __init__(self, primer_directory: Path, json_path: Path) -> None:
  17. # Preparing arguments
  18. self.primer_directory = primer_directory
  19. self._argument_parser = argparse.ArgumentParser(prog="Pylint Primer")
  20. self._subparsers = self._argument_parser.add_subparsers(
  21. dest="command", required=True
  22. )
  23. # All arguments for the prepare parser
  24. prepare_parser = self._subparsers.add_parser("prepare")
  25. prepare_parser.add_argument(
  26. "--clone", help="Clone all packages.", action="store_true", default=False
  27. )
  28. prepare_parser.add_argument(
  29. "--check",
  30. help="Check consistencies and commits of all packages.",
  31. action="store_true",
  32. default=False,
  33. )
  34. prepare_parser.add_argument(
  35. "--make-commit-string",
  36. help="Get latest commit string.",
  37. action="store_true",
  38. default=False,
  39. )
  40. prepare_parser.add_argument(
  41. "--read-commit-string",
  42. help="Print latest commit string.",
  43. action="store_true",
  44. default=False,
  45. )
  46. # All arguments for the run parser
  47. run_parser = self._subparsers.add_parser("run")
  48. run_parser.add_argument(
  49. "--type", choices=["main", "pr"], required=True, help="Type of primer run."
  50. )
  51. # All arguments for the compare parser
  52. compare_parser = self._subparsers.add_parser("compare")
  53. compare_parser.add_argument(
  54. "--base-file",
  55. required=True,
  56. help="Location of output file of the base run.",
  57. )
  58. compare_parser.add_argument(
  59. "--new-file",
  60. required=True,
  61. help="Location of output file of the new run.",
  62. )
  63. compare_parser.add_argument(
  64. "--commit",
  65. required=True,
  66. help="Commit hash of the PR commit being checked.",
  67. )
  68. # Storing arguments
  69. self.config = self._argument_parser.parse_args()
  70. self.packages = self._get_packages_to_lint_from_json(json_path)
  71. """All packages to prime."""
  72. if self.config.command == "prepare":
  73. command_class: type[PrimerCommand] = PrepareCommand
  74. elif self.config.command == "run":
  75. command_class = RunCommand
  76. elif self.config.command == "compare":
  77. command_class = CompareCommand
  78. self.command = command_class(self.primer_directory, self.packages, self.config)
  79. def run(self) -> None:
  80. self.command.run()
  81. @staticmethod
  82. def _minimum_python_supported(package_data: dict[str, str]) -> bool:
  83. min_python_str = package_data.get("minimum_python", None)
  84. if not min_python_str:
  85. return True
  86. min_python_tuple = tuple(int(n) for n in min_python_str.split("."))
  87. return min_python_tuple <= sys.version_info[:2]
  88. @staticmethod
  89. def _get_packages_to_lint_from_json(json_path: Path) -> dict[str, PackageToLint]:
  90. with open(json_path, encoding="utf8") as f:
  91. return {
  92. name: PackageToLint(**package_data)
  93. for name, package_data in json.load(f).items()
  94. if Primer._minimum_python_supported(package_data)
  95. }