2
0

linters.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2020 Red Hat, Inc.
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. import os
  16. import re
  17. import subprocess
  18. import sys
  19. from typing import List, Mapping, Optional
  20. # TODO: Empty this list!
  21. SKIP_FILES = (
  22. '030', '040', '041', '044', '045', '055', '056', '057', '065', '093',
  23. '096', '118', '124', '132', '136', '139', '147', '148', '149',
  24. '151', '152', '155', '163', '165', '194', '196', '202',
  25. '203', '205', '206', '207', '208', '210', '211', '212', '213', '216',
  26. '218', '219', '224', '228', '234', '235', '236', '237', '238',
  27. '240', '242', '245', '246', '248', '255', '256', '257', '258', '260',
  28. '262', '264', '266', '274', '277', '280', '281', '295', '296', '298',
  29. '299', '302', '303', '304', '307',
  30. 'nbd-fault-injector.py', 'qcow2.py', 'qcow2_format.py', 'qed.py'
  31. )
  32. def is_python_file(filename):
  33. if not os.path.isfile(filename):
  34. return False
  35. if filename.endswith('.py'):
  36. return True
  37. with open(filename, encoding='utf-8') as f:
  38. try:
  39. first_line = f.readline()
  40. return re.match('^#!.*python', first_line) is not None
  41. except UnicodeDecodeError: # Ignore binary files
  42. return False
  43. def get_test_files() -> List[str]:
  44. named_tests = [f'tests/{entry}' for entry in os.listdir('tests')]
  45. check_tests = set(os.listdir('.') + named_tests) - set(SKIP_FILES)
  46. return list(filter(is_python_file, check_tests))
  47. def run_linter(
  48. tool: str,
  49. args: List[str],
  50. env: Optional[Mapping[str, str]] = None,
  51. suppress_output: bool = False,
  52. ) -> None:
  53. """
  54. Run a python-based linting tool.
  55. :param suppress_output: If True, suppress all stdout/stderr output.
  56. :raise CalledProcessError: If the linter process exits with failure.
  57. """
  58. subprocess.run(
  59. (sys.executable, '-m', tool, *args),
  60. env=env,
  61. check=True,
  62. stdout=subprocess.PIPE if suppress_output else None,
  63. stderr=subprocess.STDOUT if suppress_output else None,
  64. universal_newlines=True,
  65. )
  66. def main() -> None:
  67. """
  68. Used by the Python CI system as an entry point to run these linters.
  69. """
  70. def show_usage() -> None:
  71. print(f"Usage: {sys.argv[0]} < --mypy | --pylint >", file=sys.stderr)
  72. sys.exit(1)
  73. if len(sys.argv) != 2:
  74. show_usage()
  75. files = get_test_files()
  76. if sys.argv[1] == '--pylint':
  77. run_linter('pylint', files)
  78. elif sys.argv[1] == '--mypy':
  79. # mypy bug #9852; disable incremental checking as a workaround.
  80. args = ['--no-incremental'] + files
  81. run_linter('mypy', args)
  82. else:
  83. print(f"Unrecognized argument: '{sys.argv[1]}'", file=sys.stderr)
  84. show_usage()
  85. if __name__ == '__main__':
  86. main()