Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

check-basic-style.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. # Checks
  4. # - trailing whitespaces (not allowed)
  5. # - single trailing newline (required)
  6. # - bad windows/mac line endings
  7. # - tab characters
  8. # - lines longer than XX chars
  9. from os import walk
  10. from os.path import join
  11. import re
  12. FOLDER = "hw"
  13. EXTENSIONS = ["rs", "toml", "sh"]
  14. MAX_WIDTH = 120
  15. def get_files():
  16. file_list = []
  17. # for all files and folders in pwd
  18. for root, dirs, files in walk("."):
  19. # if folder starts with "hw"
  20. if root.startswith("./" + FOLDER):
  21. # for all files in sub folder of "hw*"
  22. for file in files:
  23. # if extension match
  24. for ext in EXTENSIONS:
  25. if file.lower().endswith("." + ext):
  26. file_list.append(join(root, file))
  27. return file_list
  28. def check_file(file):
  29. line_num = 1
  30. error = False
  31. # open in binary mode to disable universal newlines (to see "\r")
  32. with open(file, mode='rb') as f:
  33. content = f.read().decode(encoding='utf-8')
  34. line_feed = re.compile("\r")
  35. if line_feed.search(content):
  36. error = True
  37. print("\t! Contains windows/mac line ending")
  38. if not content.endswith("\n"):
  39. error = True
  40. print("\t! Has no single trailing newline")
  41. for line in content.split("\n"):
  42. line_format = "Line {: >3d}: {}\n\t{}"
  43. doc_string = re.compile("^\s*//[/!]")
  44. trailing_whitespaces = re.compile(" +$")
  45. tab_char = re.compile("\t")
  46. # ignore doc string in rust
  47. if file.lower().endswith(".rs") and doc_string.match(line):
  48. continue
  49. if trailing_whitespaces.search(line):
  50. error = True
  51. print(line_format.format(line_num, line, "! Line has trailing whitespaces"))
  52. if tab_char.search(line):
  53. error = True
  54. print(line_format.format(line_num, line, "! Line has tab character"))
  55. if len(line) >= MAX_WIDTH:
  56. error = True
  57. print(line_format.format(line_num, line, "! Line is longer than {} characters".format(MAX_WIDTH)))
  58. line_num += 1
  59. return not error
  60. def main():
  61. errors = 0
  62. for file in get_files():
  63. print(file)
  64. if not check_file(file):
  65. errors += 1
  66. exit(errors)
  67. if __name__ == '__main__':
  68. main()