Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import argparse
  3. import fnmatch
  4. import os
  5. import re
  6. import sys
  7.  
  8. MIXED_TABS_SPACES = re.compile(r"^ +(\t+)")
  9. TRAILING_WHITESPACE = re.compile(r"(.*)([ \t\r\f\v]+)$")
  10.  
  11. TEXTCHARS = bytearray({7,8,9,10,12,13,27} | set(range(0x20, 0x100)) - {0x7f})
  12.  
  13. parser = argparse.ArgumentParser(description='Lint general text files without parsing code.')
  14. parser.add_argument('directory', help='directory to walk')
  15. parser.add_argument('-e', '--exclude', action='append', help='exclude this glob pattern',
  16. default=[])
  17. args = parser.parse_args()
  18.  
  19.  
  20. def is_binary_string(bytes):
  21. """Does the string look like binary data, i.e. non-text?"""
  22. return bool(bytes.translate(None, TEXTCHARS))
  23.  
  24.  
  25. def mixed_indent_line(line):
  26. """Checks for spaces and then tabs in indentation"""
  27. m = MIXED_TABS_SPACES.match(line)
  28. if m:
  29. return "{}: E101 indentation contains mixed spaces and tabs".format(m.start(1))
  30.  
  31.  
  32. def trailing_whitespace(line):
  33. """Checks for whitespace just before end of a line"""
  34. m = TRAILING_WHITESPACE.match(line)
  35. if m:
  36. if m.group(1):
  37. return "{}: W291 trailing whitespace".format(m.start(2))
  38. else:
  39. return "1: W293 blank line contains whitespace"
  40.  
  41.  
  42. def final_newline(line):
  43. """Checks for a final newline in a file"""
  44. if not line.endswith('\n'):
  45. return "1: W292 no newline at end of file"
  46.  
  47.  
  48. def mixed_indent_file(line, file_indent_type):
  49. """Checks for lines that don't match the file's indent style"""
  50. line_indent_type, result = None, None
  51. if line.startswith(' '):
  52. line_indent_type = 'space'
  53. elif line.startswith('\t'):
  54. line_indent_type = 'tab'
  55. if (
  56. file_indent_type is not None
  57. and line_indent_type is not None
  58. and line_indent_type != file_indent_type
  59. ):
  60. result = "1: E101 indentation contains mixed spaces and tabs"
  61. return line_indent_type, result
  62.  
  63.  
  64. def output(filename, line_no, result):
  65. if result is not None:
  66. print("{0}:{1}:{2}".format(filename, line_no, result))
  67.  
  68.  
  69. for dirpath, dirnames, filenames in os.walk(args.directory):
  70. for name in filenames:
  71. fullname = os.path.join(dirpath, name)
  72. if any(fnmatch.fnmatch(name, exclude) for exclude in args.exclude):
  73. continue
  74. with open(fullname, 'rb') as infile:
  75. if is_binary_string(infile.read(1024)):
  76. continue
  77. infile.seek(0)
  78. line_no = 1
  79. last_line = '\n'
  80. file_indent_type = None
  81. for line in infile:
  82. line_indent_type, result = mixed_indent_file(line, file_indent_type)
  83. results = (
  84. result,
  85. mixed_indent_line(line),
  86. trailing_whitespace(line),
  87. )
  88. for result in results:
  89. output(infile.name, line_no, result)
  90. line_no += 1
  91. last_line = line
  92. output(infile.name, line_no, final_newline(last_line))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement