prfilter 4.8 KB

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