| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | # ##### BEGIN GPL LICENSE BLOCK ##### | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | #  modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | #  as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | #  of the License, or (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | #  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | #  GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | #  along with this program; if not, write to the Free Software Foundation, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  | #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # ##### END GPL LICENSE BLOCK ##### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-19 16:17:35 +00:00
										 |  |  | # <pep8-80 compliant> | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # depends on pep8, pyflakes, pylint | 
					
						
							| 
									
										
										
										
											2009-12-14 00:06:24 +00:00
										 |  |  | # for ubuntu | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | # | 
					
						
							|  |  |  | #   sudo apt-get install pylint pyflakes | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #   sudo apt-get install python-setuptools python-pip | 
					
						
							|  |  |  | #   sudo pip install pep8 | 
					
						
							| 
									
										
										
										
											2009-12-14 00:06:24 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # in debian install pylint pyflakes pep8 with apt-get/aptitude/etc | 
					
						
							| 
									
										
										
										
											2009-12-14 20:56:19 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-12-14 14:21:06 +00:00
										 |  |  | # on *nix run | 
					
						
							| 
									
										
										
										
											2011-04-10 10:45:56 +00:00
										 |  |  | #   python source/tests/pep8.py > test_pep8.log 2>&1 | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # how many lines to read into the file, pep8 comment | 
					
						
							|  |  |  | # should be directly after the licence header, ~20 in most cases | 
					
						
							|  |  |  | PEP8_SEEK_COMMENT = 40 | 
					
						
							|  |  |  | SKIP_PREFIX = "./tools", "./config", "./scons", "./extern" | 
					
						
							| 
									
										
										
										
											2011-11-19 16:17:35 +00:00
										 |  |  | FORCE_PEP8_ALL = False | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def file_list_py(path): | 
					
						
							|  |  |  |     for dirpath, dirnames, filenames in os.walk(path): | 
					
						
							|  |  |  |         for filename in filenames: | 
					
						
							| 
									
										
										
										
											2011-03-27 05:23:14 +00:00
										 |  |  |             if filename.endswith(".py") or filename.endswith(".cfg"): | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  |                 yield os.path.join(dirpath, filename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def is_pep8(path): | 
					
						
							| 
									
										
										
										
											2010-11-28 06:03:30 +00:00
										 |  |  |     print(path) | 
					
						
							| 
									
										
										
										
											2011-01-25 01:51:28 +00:00
										 |  |  |     if open(path, 'rb').read(3) == b'\xef\xbb\xbf': | 
					
						
							|  |  |  |         print("\nfile contains BOM, remove first 3 bytes: %r\n" % path) | 
					
						
							| 
									
										
										
										
											2011-02-04 09:27:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # templates dont have a header but should be pep8 | 
					
						
							|  |  |  |     for d in ("presets", "templates", "examples"): | 
					
						
							|  |  |  |         if ("%s%s%s" % (os.sep, d, os.sep)) in path: | 
					
						
							|  |  |  |             return 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-01 07:20:34 +00:00
										 |  |  |     f = open(path, 'r', encoding="utf8") | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  |     for i in range(PEP8_SEEK_COMMENT): | 
					
						
							|  |  |  |         line = f.readline() | 
					
						
							|  |  |  |         if line.startswith("# <pep8"): | 
					
						
							|  |  |  |             if line.startswith("# <pep8 compliant>"): | 
					
						
							|  |  |  |                 return 1 | 
					
						
							|  |  |  |             elif line.startswith("# <pep8-80 compliant>"): | 
					
						
							|  |  |  |                 return 2 | 
					
						
							|  |  |  |     f.close() | 
					
						
							|  |  |  |     return 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     files = [] | 
					
						
							|  |  |  |     files_skip = [] | 
					
						
							|  |  |  |     for f in file_list_py("."): | 
					
						
							| 
									
										
										
										
											2010-11-28 06:03:30 +00:00
										 |  |  |         if [None for prefix in SKIP_PREFIX if f.startswith(prefix)]: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-19 16:17:35 +00:00
										 |  |  |         pep8_type = FORCE_PEP8_ALL or is_pep8(f) | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if pep8_type: | 
					
						
							|  |  |  |             # so we can batch them for each tool. | 
					
						
							| 
									
										
										
										
											2009-12-14 20:56:19 +00:00
										 |  |  |             files.append((os.path.abspath(f), pep8_type)) | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2010-11-28 06:03:30 +00:00
										 |  |  |             files_skip.append(f) | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     print("\nSkipping...") | 
					
						
							|  |  |  |     for f in files_skip: | 
					
						
							|  |  |  |         print("    %s" % f) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 15:25:24 +00:00
										 |  |  |     # strict imports | 
					
						
							|  |  |  |     print("\n\n\n# running pep8...") | 
					
						
							|  |  |  |     import re | 
					
						
							|  |  |  |     import_check = re.compile(r"\s*from\s+[A-z\.]+\s+import \*\s*") | 
					
						
							|  |  |  |     for f, pep8_type in files: | 
					
						
							|  |  |  |         for i, l in enumerate(open(f, 'r', encoding='utf8')): | 
					
						
							|  |  |  |             if import_check.match(l): | 
					
						
							|  |  |  |                 print("%s:%d:0: global import bad practice" % (f, i + 1)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  |     print("\n\n\n# running pep8...") | 
					
						
							|  |  |  |     for f, pep8_type in files: | 
					
						
							|  |  |  |         if pep8_type == 1: | 
					
						
							|  |  |  |             # E501:80 line length | 
					
						
							|  |  |  |             os.system("pep8 --repeat --ignore=E501 '%s'" % (f)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             os.system("pep8 --repeat '%s'" % (f)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 15:25:24 +00:00
										 |  |  |     # pyflakes | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  |     print("\n\n\n# running pyflakes...") | 
					
						
							|  |  |  |     for f, pep8_type in files: | 
					
						
							|  |  |  |         os.system("pyflakes '%s'" % f) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print("\n\n\n# running pylint...") | 
					
						
							|  |  |  |     for f, pep8_type in files: | 
					
						
							|  |  |  |         # let pep8 complain about line length | 
					
						
							| 
									
										
										
										
											2011-11-19 16:17:35 +00:00
										 |  |  |         os.system("pylint " | 
					
						
							|  |  |  |                   "--disable=" | 
					
						
							|  |  |  |                   "C0111,"  # missing docstring | 
					
						
							|  |  |  |                   "C0103,"  # invalid name | 
					
						
							|  |  |  |                   "W0613,"  # unused argument, may add this back | 
					
						
							|  |  |  |                             # but happens a lot for 'context' for eg. | 
					
						
							|  |  |  |                   "W0232,"  # class has no __init__, Operator/Panel/Menu etc | 
					
						
							|  |  |  |                   "W0142,"  # Used * or ** magic | 
					
						
							|  |  |  |                             # even needed in some cases | 
					
						
							| 
									
										
										
										
											2011-11-24 19:36:12 +00:00
										 |  |  |                   "R0902,"  # Too many instance attributes | 
					
						
							|  |  |  |                   "R0903,"  # Too many statements | 
					
						
							| 
									
										
										
										
											2011-11-19 16:17:35 +00:00
										 |  |  |                   "R0911,"  # Too many return statements | 
					
						
							|  |  |  |                   "R0912,"  # Too many branches | 
					
						
							|  |  |  |                   "R0913,"  # Too many arguments | 
					
						
							|  |  |  |                   "R0914,"  # Too many local variables | 
					
						
							| 
									
										
										
										
											2011-11-24 19:36:12 +00:00
										 |  |  |                   "R0915,"  # Too many statements | 
					
						
							| 
									
										
										
										
											2011-11-19 16:17:35 +00:00
										 |  |  |                   " " | 
					
						
							|  |  |  |                   "--include-ids=y " | 
					
						
							|  |  |  |                   "--output-format=parseable " | 
					
						
							|  |  |  |                   "--reports=n " | 
					
						
							|  |  |  |                   "--max-line-length=1000" | 
					
						
							|  |  |  |                   " '%s'" % f) | 
					
						
							| 
									
										
										
										
											2009-12-13 13:59:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     main() |