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, pyflakes, pylint
26 # sudo apt-get install pylint pyflakes
28 # sudo apt-get install python-setuptools python-pip
29 # sudo pip install pep8
31 # in debian install pylint pyflakes pep8 with apt-get/aptitude/etc
34 # python source/tests/pep8.py > test_pep8.log 2>&1
36 # how many lines to read into the file, pep8 comment
37 # should be directly after the licence header, ~20 in most cases
38 PEP8_SEEK_COMMENT = 40
39 SKIP_PREFIX = "./tools", "./config", "./scons", "./extern"
40 FORCE_PEP8_ALL = False
43 def file_list_py(path):
44 for dirpath, dirnames, filenames in os.walk(path):
45 for filename in filenames:
46 if filename.endswith((".py", ".cfg")):
47 yield os.path.join(dirpath, filename)
52 if open(path, 'rb').read(3) == b'\xef\xbb\xbf':
53 print("\nfile contains BOM, remove first 3 bytes: %r\n" % path)
55 # templates don't have a header but should be pep8
56 for d in ("presets", "templates", "examples"):
57 if ("%s%s%s" % (os.sep, d, os.sep)) in path:
60 f = open(path, 'r', encoding="utf8")
61 for i in range(PEP8_SEEK_COMMENT):
63 if line.startswith("# <pep8"):
64 if line.startswith("# <pep8 compliant>"):
66 elif line.startswith("# <pep8-80 compliant>"):
75 for f in file_list_py("."):
76 if [None for prefix in SKIP_PREFIX if f.startswith(prefix)]:
79 pep8_type = FORCE_PEP8_ALL or is_pep8(f)
82 # so we can batch them for each tool.
83 files.append((os.path.abspath(f), pep8_type))
87 print("\nSkipping...")
92 print("\n\n\n# running pep8...")
94 import_check = re.compile(r"\s*from\s+[A-z\.]+\s+import \*\s*")
95 for f, pep8_type in files:
96 for i, l in enumerate(open(f, 'r', encoding='utf8')):
97 if import_check.match(l):
98 print("%s:%d:0: global import bad practice" % (f, i + 1))
100 print("\n\n\n# running pep8...")
101 for f, pep8_type in files:
103 # E501:80 line length
104 os.system("pep8 --repeat --ignore=E501 '%s'" % (f))
106 os.system("pep8 --repeat '%s'" % (f))
109 print("\n\n\n# running pyflakes...")
110 for f, pep8_type in files:
111 os.system("pyflakes '%s'" % f)
113 print("\n\n\n# running pylint...")
114 for f, pep8_type in files:
115 # let pep8 complain about line length
118 "C0111," # missing docstring
119 "C0103," # invalid name
120 "W0613," # unused argument, may add this back
121 # but happens a lot for 'context' for eg.
122 "W0232," # class has no __init__, Operator/Panel/Menu etc
123 "W0142," # Used * or ** magic
124 # even needed in some cases
125 "R0902," # Too many instance attributes
126 "R0903," # Too many statements
127 "R0911," # Too many return statements
128 "R0912," # Too many branches
129 "R0913," # Too many arguments
130 "R0914," # Too many local variables
131 "R0915," # Too many statements
134 "--output-format=parseable "
136 "--max-line-length=1000"
139 if __name__ == "__main__":