prfilter 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. 'Runtime/pal_loader',
  44. 'Scripts/clean-check',
  45. 'Scripts/clean-check-prepare',
  46. 'Scripts/clean-check-test-copy',
  47. 'Scripts/list-all-graphene.sh',
  48. 'Scripts/memusg',
  49. '.ci/run-pylint',
  50. '.ci/run-shellcheck',
  51. ]))
  52. def get_diff_ranges(ref=DEFAULT_REF):
  53. '''Get chunks affected by a merge request
  54. Args:
  55. ref (str): a reference to diff the HEAD against (default: origin/master)
  56. Returns:
  57. dict: a dict with filenames in keys and list of `(start, end)` ranges in
  58. values (start is inclusive, end is not, wrt :py:func:`range`)
  59. '''
  60. files = collections.defaultdict(list)
  61. data = subprocess.check_output(['git', 'diff', '-U0', ref, 'HEAD']).decode()
  62. path = None
  63. for line in data.split('\n'):
  64. if line.startswith('+++ '):
  65. path = (None if line == '+++ /dev/null'
  66. else line.split('/', maxsplit=1)[-1])
  67. continue
  68. if line.startswith('@@ '):
  69. # @@ -8,0 +9 @@ [class name or previous line or whatever]
  70. if path is None: # /dev/null
  71. continue
  72. _, _, plus, *_ = line.split()
  73. start, length, *_ = *(int(i) for i in plus[1:].split(',')), 1
  74. if not length:
  75. # remove-only chunk
  76. continue
  77. files[path].append((start, start + length))
  78. return files
  79. class Diff:
  80. '''A quick and dirty diff evaluator
  81. >>> diff = Diff()
  82. >>> (message['file'], message['line']) in diff
  83. True # or False
  84. >>> diff.message_is_important(message)
  85. True # or False
  86. The default diff is to the ``origin/master`` ref.
  87. '''
  88. # pylint: disable=too-few-public-methods
  89. def __init__(self, ref=DEFAULT_REF):
  90. self._files = get_diff_ranges(ref)
  91. def __contains__(self, pathline):
  92. path, line = pathline
  93. try:
  94. return any(start <= line < end for start, end in self._files[path])
  95. except KeyError:
  96. return False
  97. def message_is_important(self, message):
  98. path = pathlib.Path(message['path'])
  99. for i in THE_BIG_LIST_OF_NAUGHTY_FILES:
  100. try:
  101. path.relative_to(i)
  102. break
  103. except ValueError:
  104. # path is not .relative_to() the path from WHITELIST
  105. pass
  106. else:
  107. # not on whitelist: always complain
  108. return True
  109. if (message['path'], message['line']) in self:
  110. # on whitelist, but in diff: do complain
  111. return True
  112. # on whitelist and outside diff: don't complain
  113. return False
  114. def main():
  115. diff = Diff()
  116. with sys.stdin:
  117. pylint = json.load(sys.stdin)
  118. ret = 0
  119. for message in pylint:
  120. # shellcheck
  121. if 'path' not in message:
  122. message['path'] = message['file']
  123. if 'symbol' not in message:
  124. message['symbol'] = message['code']
  125. if diff.message_is_important(message):
  126. if not ret:
  127. print('MESSAGES AFFECTING THIS PR:')
  128. print('{path} +{line}:{column}: {symbol}: {message}'.format(
  129. **message))
  130. ret += 1
  131. return min(ret, 255)
  132. if __name__ == '__main__':
  133. sys.exit(main())