prfilter 4.8 KB

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