prfilter 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python3
  2. import collections
  3. import json
  4. import pathlib
  5. import subprocess
  6. import sys
  7. DEFAULT_REF = 'origin/master'
  8. THE_BIG_LIST_OF_NAUGHTY_FILES = list(map(pathlib.Path, [
  9. #
  10. # Problems with files listed here get lighter treatment: they are blocking
  11. # only when they are a part of the current pull request.
  12. #
  13. # for pylint
  14. 'Documentation/conf.py',
  15. 'LibOS/shim/test/apps/ltp/contrib/conf_lint.py',
  16. 'LibOS/shim/test/apps/ltp/contrib/conf_merge.py',
  17. 'LibOS/shim/test/apps/ltp/contrib/conf_missing.py',
  18. 'LibOS/shim/test/apps/ltp/contrib/conf_remove_must_pass.py',
  19. 'LibOS/shim/test/apps/ltp/contrib/has_own_main.py',
  20. 'LibOS/shim/test/apps/ltp/contrib/report.py',
  21. 'LibOS/shim/test/apps/ltp/runltp_xml.py',
  22. 'LibOS/shim/test/apps/python-scipy-insecure/scripts/test-numpy.py',
  23. 'LibOS/shim/test/apps/python-scipy-insecure/scripts/test-scipy.py',
  24. 'LibOS/shim/test/apps/python-simple/scripts/benchrun.py',
  25. 'LibOS/shim/test/apps/python-simple/scripts/dummy-web-server.py',
  26. 'LibOS/shim/test/apps/python-simple/scripts/fibonacci.py',
  27. 'LibOS/shim/test/apps/python-simple/scripts/helloworld.py',
  28. 'LibOS/shim/test/apps/python-simple/scripts/test-http.py',
  29. 'LibOS/shim/test/fs/test_fs.py',
  30. 'LibOS/shim/test/regression/test_libos.py',
  31. 'Pal/regression/test_pal.py',
  32. 'Pal/src/host/Linux-SGX/sgx-driver/link-intel-driver.py',
  33. 'Pal/src/host/Linux/pal-gdb.py',
  34. 'Scripts/regression.py',
  35. 'Tools',
  36. # for shellcheck
  37. 'LibOS/shim/test/apps/bash/scripts/bash_test.sh',
  38. 'LibOS/shim/test/apps/common_tools/benchmark-http.sh',
  39. 'LibOS/shim/test/apps/python-simple/run-tests.sh',
  40. 'LibOS/shim/test/native',
  41. 'Pal/src/host/Linux-SGX/debugger/gdb',
  42. 'Pal/src/host/Linux-SGX/sgx-driver/load.sh',
  43. 'Scripts/list-all-graphene.sh',
  44. 'Scripts/memusg',
  45. '.ci/run-pylint',
  46. '.ci/run-shellcheck',
  47. ]))
  48. def get_diff_ranges(ref=DEFAULT_REF):
  49. '''Get chunks affected by a merge request
  50. Args:
  51. ref (str): a reference to diff the HEAD against (default: origin/master)
  52. Returns:
  53. dict: a dict with filenames in keys and list of `(start, end)` ranges in
  54. values (start is inclusive, end is not, wrt :py:func:`range`)
  55. '''
  56. files = collections.defaultdict(list)
  57. data = subprocess.check_output(['git', 'diff', '-U0', ref, 'HEAD']).decode()
  58. path = None
  59. for line in data.split('\n'):
  60. if line.startswith('+++ '):
  61. path = (None if line == '+++ /dev/null'
  62. else line.split('/', maxsplit=1)[-1])
  63. continue
  64. if line.startswith('@@ '):
  65. # @@ -8,0 +9 @@ [class name or previous line or whatever]
  66. if path is None: # /dev/null
  67. continue
  68. _, _, plus, *_ = line.split()
  69. start, length, *_ = *(int(i) for i in plus[1:].split(',')), 1
  70. if not length:
  71. # remove-only chunk
  72. continue
  73. files[path].append((start, start + length))
  74. return files
  75. class Diff:
  76. '''A quick and dirty diff evaluator
  77. >>> diff = Diff()
  78. >>> (message['file'], message['line']) in diff
  79. True # or False
  80. >>> diff.message_is_important(message)
  81. True # or False
  82. The default diff is to the ``origin/master`` ref.
  83. '''
  84. # pylint: disable=too-few-public-methods
  85. def __init__(self, ref=DEFAULT_REF):
  86. self._files = get_diff_ranges(ref)
  87. def __contains__(self, pathline):
  88. path, line = pathline
  89. try:
  90. return any(start <= line < end for start, end in self._files[path])
  91. except KeyError:
  92. return False
  93. def message_is_important(self, message):
  94. path = pathlib.Path(message['path'])
  95. for i in THE_BIG_LIST_OF_NAUGHTY_FILES:
  96. try:
  97. path.relative_to(i)
  98. break
  99. except ValueError:
  100. # path is not .relative_to() the path from WHITELIST
  101. pass
  102. else:
  103. # not on whitelist: always complain
  104. return True
  105. if (message['path'], message['line']) in self:
  106. # on whitelist, but in diff: do complain
  107. return True
  108. # on whitelist and outside diff: don't complain
  109. return False
  110. def main():
  111. diff = Diff()
  112. with sys.stdin:
  113. pylint = json.load(sys.stdin)
  114. ret = 0
  115. for message in pylint:
  116. # shellcheck
  117. if 'path' not in message:
  118. message['path'] = message['file']
  119. if 'symbol' not in message:
  120. message['symbol'] = message['code']
  121. if diff.message_is_important(message):
  122. if not ret:
  123. print('MESSAGES AFFECTING THIS PR:')
  124. print('{path} +{line}:{column}: {symbol}: {message}'.format(
  125. **message))
  126. ret += 1
  127. return min(ret, 255)
  128. if __name__ == '__main__':
  129. sys.exit(main())