common_lint.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python
  2. #
  3. # Common lint functions applicable to multiple types of files.
  4. from __future__ import print_function
  5. import re
  6. def VerifyLineLength(filename, lines, max_length):
  7. """Checks to make sure the file has no lines with lines exceeding the length
  8. limit.
  9. Args:
  10. filename: the file under consideration as string
  11. lines: contents of the file as string array
  12. max_length: maximum acceptable line length as number
  13. Returns:
  14. A list of tuples with format [(filename, line number, msg), ...] with any
  15. violations found.
  16. """
  17. lint = []
  18. line_num = 1
  19. for line in lines:
  20. length = len(line.rstrip('\n'))
  21. if length > max_length:
  22. lint.append((filename, line_num,
  23. 'Line exceeds %d chars (%d)' % (max_length, length)))
  24. line_num += 1
  25. return lint
  26. def VerifyTabs(filename, lines):
  27. """Checks to make sure the file has no tab characters.
  28. Args:
  29. filename: the file under consideration as string
  30. lines: contents of the file as string array
  31. Returns:
  32. A list of tuples with format [(line_number, msg), ...] with any violations
  33. found.
  34. """
  35. lint = []
  36. tab_re = re.compile(r'\t')
  37. line_num = 1
  38. for line in lines:
  39. if tab_re.match(line.rstrip('\n')):
  40. lint.append((filename, line_num, 'Tab found instead of whitespace'))
  41. line_num += 1
  42. return lint
  43. def VerifyTrailingWhitespace(filename, lines):
  44. """Checks to make sure the file has no lines with trailing whitespace.
  45. Args:
  46. filename: the file under consideration as string
  47. lines: contents of the file as string array
  48. Returns:
  49. A list of tuples with format [(filename, line number, msg), ...] with any
  50. violations found.
  51. """
  52. lint = []
  53. trailing_whitespace_re = re.compile(r'\s+$')
  54. line_num = 1
  55. for line in lines:
  56. if trailing_whitespace_re.match(line.rstrip('\n')):
  57. lint.append((filename, line_num, 'Trailing whitespace'))
  58. line_num += 1
  59. return lint
  60. class BaseLint:
  61. def RunOnFile(filename, lines):
  62. raise Exception('RunOnFile() unimplemented')
  63. def RunLintOverAllFiles(linter, filenames):
  64. """Runs linter over the contents of all files.
  65. Args:
  66. lint: subclass of BaseLint, implementing RunOnFile()
  67. filenames: list of all files whose contents will be linted
  68. Returns:
  69. A list of tuples with format [(filename, line number, msg), ...] with any
  70. violations found.
  71. """
  72. lint = []
  73. for filename in filenames:
  74. file = open(filename, 'r')
  75. if not file:
  76. print('Cound not open %s' % filename)
  77. continue
  78. lines = file.readlines()
  79. lint.extend(linter.RunOnFile(filename, lines))
  80. return lint