common_lint.py 2.5 KB

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