run_other_pylint.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2019 The ungoogled-chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Run Pylint over any module"""
  6. import argparse
  7. import os
  8. import shutil
  9. from pathlib import Path
  10. from pylint import lint
  11. class ChangeDir:
  12. """
  13. Changes directory to path in with statement
  14. """
  15. def __init__(self, path):
  16. self._path = path
  17. self._orig_path = os.getcwd()
  18. def __enter__(self):
  19. os.chdir(str(self._path))
  20. def __exit__(self, *_):
  21. os.chdir(self._orig_path)
  22. def run_pylint(module_path, pylint_options, ignore_prefixes=tuple()):
  23. """Runs Pylint. Returns a boolean indicating success"""
  24. pylint_stats = Path('/run/user/{}/pylint_stats'.format(os.getuid()))
  25. if not pylint_stats.parent.is_dir(): #pylint: disable=no-member
  26. pylint_stats = Path('/run/shm/pylint_stats')
  27. os.environ['PYLINTHOME'] = str(pylint_stats)
  28. input_paths = list()
  29. if not module_path.exists():
  30. print('ERROR: Cannot find', module_path)
  31. exit(1)
  32. if module_path.is_dir():
  33. for path in module_path.rglob('*.py'):
  34. ignore_matched = False
  35. for prefix in ignore_prefixes:
  36. if path.parts[:len(prefix)] == prefix:
  37. ignore_matched = True
  38. break
  39. if ignore_matched:
  40. continue
  41. input_paths.append(str(path))
  42. else:
  43. input_paths.append(str(module_path))
  44. runner = lint.Run((*input_paths, *pylint_options), do_exit=False)
  45. if pylint_stats.is_dir():
  46. shutil.rmtree(str(pylint_stats))
  47. if runner.linter.msg_status != 0:
  48. print('WARNING: Non-zero exit status:', runner.linter.msg_status)
  49. return False
  50. return True
  51. def main():
  52. """CLI entrypoint"""
  53. parser = argparse.ArgumentParser(description='Run Pylint over arbitrary module')
  54. parser.add_argument('--hide-fixme', action='store_true', help='Hide "fixme" Pylint warnings.')
  55. parser.add_argument(
  56. '--show-locally-disabled',
  57. action='store_true',
  58. help='Show "locally-disabled" Pylint warnings.')
  59. parser.add_argument('module_path', type=Path, help='Path to the module to check')
  60. args = parser.parse_args()
  61. if not args.module_path.exists():
  62. print('ERROR: Module path "{}" does not exist'.format(args.module_path))
  63. exit(1)
  64. disables = [
  65. 'wrong-import-position',
  66. 'bad-continuation',
  67. ]
  68. if args.hide_fixme:
  69. disables.append('fixme')
  70. if not args.show_locally_disabled:
  71. disables.append('locally-disabled')
  72. pylint_options = [
  73. '--disable={}'.format(','.join(disables)),
  74. '--jobs=4',
  75. '--score=n',
  76. '--persistent=n',
  77. ]
  78. if not run_pylint(args.module_path, pylint_options):
  79. exit(1)
  80. exit(0)
  81. if __name__ == '__main__':
  82. main()