1 # ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17 # ##### END GPL LICENSE BLOCK #####
23 # depends on pep8, frosted, pylint
26 # sudo apt-get install pylint
28 # sudo apt-get install python-setuptools python-pip
29 # sudo pip install pep8
30 # sudo pip install frosted
32 # in Debian install pylint pep8 with apt-get/aptitude/etc
35 # python tests/pep8.py > test_pep8.log 2>&1
37 # how many lines to read into the file, pep8 comment
38 # should be directly after the license header, ~20 in most cases
39 PEP8_SEEK_COMMENT = 40
40 SKIP_PREFIX = "./tools", "./config", "./extern"
42 FORCE_PEP8_ALL = False
45 def file_list_py(path):
46 for dirpath, dirnames, filenames in os.walk(path):
47 for filename in filenames:
48 if filename.endswith((".py", ".cfg")):
49 yield os.path.join(dirpath, filename)
54 if open(path, 'rb').read(3) == b'\xef\xbb\xbf':
55 print("\nfile contains BOM, remove first 3 bytes: %r\n" % path)
57 # templates don't have a header but should be pep8
58 for d in ("presets", "templates_py", "examples"):
59 if ("%s%s%s" % (os.sep, d, os.sep)) in path:
62 f = open(path, 'r', encoding="utf8")
63 for i in range(PEP8_SEEK_COMMENT):
65 if line.startswith("# <pep8"):
66 if line.startswith("# <pep8 compliant>"):
68 elif line.startswith("# <pep8-80 compliant>"):
77 for f in file_list_py("."):
78 if [None for prefix in SKIP_PREFIX if f.startswith(prefix)]:
82 if (os.sep + "addons") in f:
85 pep8_type = FORCE_PEP8_ALL or is_pep8(f)
88 # so we can batch them for each tool.
89 files.append((os.path.abspath(f), pep8_type))
93 print("\nSkipping...")
98 print("\n\n\n# checking imports...")
100 import_check = re.compile(r"\s*from\s+[A-z\.]+\s+import \*\s*")
101 for f, pep8_type in files:
102 for i, l in enumerate(open(f, 'r', encoding='utf8')):
103 if import_check.match(l):
104 print("%s:%d:0: global import bad practice" % (f, i + 1))
107 print("\n\n\n# checking class definitions...")
109 class_check = re.compile(r"\s*class\s+.*\(\):.*")
110 for f, pep8_type in files:
111 for i, l in enumerate(open(f, 'r', encoding='utf8')):
112 if class_check.match(l):
113 print("%s:%d:0: empty class (), remove" % (f, i + 1))
116 print("\n\n\n# running flake8...")
118 # these are very picky and often hard to follow
119 # while keeping common script formatting.
128 # "imports not at top of file."
129 # prefer to load as needed (lazy load addons etc).
131 # "do not compare types, use 'isinstance()'"
132 # times types are compared,
133 # I rather keep them specific
137 for f, pep8_type in files:
140 # E501:80 line length
141 ignore_tmp = ignore + ("E501", )
148 (",".join(ignore_tmp), f))
151 print("\n\n\n# running frosted...")
152 for f, pep8_type in files:
153 os.system("frosted '%s'" % f)
155 print("\n\n\n# running pylint...")
156 for f, pep8_type in files:
157 # let pep8 complain about line length
160 "C0111," # missing doc string
161 "C0103," # invalid name
162 "C0413," # import should be placed at the top
163 "W0613," # unused argument, may add this back
164 # but happens a lot for 'context' for eg.
165 "W0232," # class has no __init__, Operator/Panel/Menu etc
166 "W0142," # Used * or ** magic
167 # even needed in some cases
168 "R0902," # Too many instance attributes
169 "R0903," # Too many statements
170 "R0911," # Too many return statements
171 "R0912," # Too many branches
172 "R0913," # Too many arguments
173 "R0914," # Too many local variables
174 "R0915," # Too many statements
176 "--output-format=parseable "
178 "--max-line-length=1000"
182 if __name__ == "__main__":