pep8.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8-80 compliant>
  19. import os
  20. import subprocess
  21. import shutil
  22. # depends on pep8, frosted, pylint
  23. # for Ubuntu
  24. #
  25. # sudo apt-get install pylint
  26. #
  27. # sudo apt-get install python-setuptools python-pip
  28. # sudo pip install pep8
  29. # sudo pip install frosted
  30. #
  31. # in Debian install pylint pep8 with apt-get/aptitude/etc
  32. #
  33. # on *nix run
  34. # python tests/pep8.py > test_pep8.log 2>&1
  35. # how many lines to read into the file, pep8 comment
  36. # should be directly after the license header, ~20 in most cases
  37. PEP8_SEEK_COMMENT = 40
  38. SKIP_PREFIX = "./tools", "./config", "./extern"
  39. SKIP_ADDONS = True
  40. FORCE_PEP8_ALL = False
  41. def file_list_py(path):
  42. for dirpath, _dirnames, filenames in os.walk(path):
  43. for filename in filenames:
  44. if filename.endswith((".py", ".cfg")):
  45. yield os.path.join(dirpath, filename)
  46. def is_pep8(path):
  47. print(path)
  48. if open(path, 'rb').read(3) == b'\xef\xbb\xbf':
  49. print("\nfile contains BOM, remove first 3 bytes: %r\n" % path)
  50. # templates don't have a header but should be pep8
  51. for d in ("presets", "templates_py", "examples"):
  52. if ("%s%s%s" % (os.sep, d, os.sep)) in path:
  53. return 1
  54. f = open(path, 'r', encoding="utf8")
  55. for _ in range(PEP8_SEEK_COMMENT):
  56. line = f.readline()
  57. if line.startswith("# <pep8"):
  58. if line.startswith("# <pep8 compliant>"):
  59. return 1
  60. elif line.startswith("# <pep8-80 compliant>"):
  61. return 2
  62. f.close()
  63. return 0
  64. def check_files_flake8(files):
  65. print("\n\n\n# running flake8...")
  66. # these are very picky and often hard to follow
  67. # while keeping common script formatting.
  68. ignore = (
  69. "E122",
  70. "E123",
  71. "E124",
  72. "E125",
  73. "E126",
  74. "E127",
  75. "E128",
  76. # "imports not at top of file."
  77. # prefer to load as needed (lazy load addons etc).
  78. "E402",
  79. # "do not compare types, use 'isinstance()'"
  80. # times types are compared,
  81. # I rather keep them specific
  82. "E721",
  83. )
  84. for f, pep8_type in files:
  85. if pep8_type == 1:
  86. # E501:80 line length
  87. ignore_tmp = ignore + ("E501", )
  88. else:
  89. ignore_tmp = ignore
  90. subprocess.call((
  91. "flake8",
  92. "--isolated",
  93. "--ignore=%s" % ",".join(ignore_tmp),
  94. f,
  95. ))
  96. def check_files_frosted(files):
  97. print("\n\n\n# running frosted...")
  98. for f, pep8_type in files:
  99. subprocess.call(("frosted", f))
  100. def check_files_pylint(files):
  101. print("\n\n\n# running pylint...")
  102. for f, pep8_type in files:
  103. # let pep8 complain about line length
  104. subprocess.call((
  105. "pylint",
  106. "--disable="
  107. "C0111," # missing doc string
  108. "C0103," # invalid name
  109. "C0413," # import should be placed at the top
  110. "W0613," # unused argument, may add this back
  111. # but happens a lot for 'context' for eg.
  112. "W0232," # class has no __init__, Operator/Panel/Menu etc
  113. "W0142," # Used * or ** magic
  114. # even needed in some cases
  115. "R0902," # Too many instance attributes
  116. "R0903," # Too many statements
  117. "R0911," # Too many return statements
  118. "R0912," # Too many branches
  119. "R0913," # Too many arguments
  120. "R0914," # Too many local variables
  121. "R0915,", # Too many statements
  122. "--output-format=parseable",
  123. "--reports=n",
  124. "--max-line-length=1000",
  125. f,
  126. ))
  127. def main():
  128. files = []
  129. files_skip = []
  130. for f in file_list_py("."):
  131. if [None for prefix in SKIP_PREFIX if f.startswith(prefix)]:
  132. continue
  133. if SKIP_ADDONS:
  134. if (os.sep + "addons") in f:
  135. continue
  136. pep8_type = FORCE_PEP8_ALL or is_pep8(f)
  137. if pep8_type:
  138. # so we can batch them for each tool.
  139. files.append((os.path.abspath(f), pep8_type))
  140. else:
  141. files_skip.append(f)
  142. print("\nSkipping...")
  143. for f in files_skip:
  144. print(" %s" % f)
  145. # strict imports
  146. print("\n\n\n# checking imports...")
  147. import re
  148. import_check = re.compile(r"\s*from\s+[A-z\.]+\s+import \*\s*")
  149. for f, pep8_type in files:
  150. for i, l in enumerate(open(f, 'r', encoding='utf8')):
  151. if import_check.match(l):
  152. print("%s:%d:0: global import bad practice" % (f, i + 1))
  153. del re, import_check
  154. print("\n\n\n# checking class definitions...")
  155. import re
  156. class_check = re.compile(r"\s*class\s+.*\(\):.*")
  157. for f, pep8_type in files:
  158. for i, l in enumerate(open(f, 'r', encoding='utf8')):
  159. if class_check.match(l):
  160. print("%s:%d:0: empty class (), remove" % (f, i + 1))
  161. del re, class_check
  162. if shutil.which("flake8"):
  163. check_files_flake8(files)
  164. else:
  165. print("Skipping flake8 checks (command not found)")
  166. if shutil.which("frosted"):
  167. check_files_frosted(files)
  168. else:
  169. print("Skipping frosted checks (command not found)")
  170. if shutil.which("pylint"):
  171. check_files_pylint(files)
  172. else:
  173. print("Skipping pylint checks (command not found)")
  174. if __name__ == "__main__":
  175. main()