Blender V2.61 - r43446

pep8.py

Go to the documentation of this file.
00001 # ##### BEGIN GPL LICENSE BLOCK #####
00002 #
00003 #  This program is free software; you can redistribute it and/or
00004 #  modify it under the terms of the GNU General Public License
00005 #  as published by the Free Software Foundation; either version 2
00006 #  of the License, or (at your option) any later version.
00007 #
00008 #  This program is distributed in the hope that it will be useful,
00009 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 #  GNU General Public License for more details.
00012 #
00013 #  You should have received a copy of the GNU General Public License
00014 #  along with this program; if not, write to the Free Software Foundation,
00015 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00016 #
00017 # ##### END GPL LICENSE BLOCK #####
00018 
00019 # <pep8-80 compliant>
00020 
00021 import os
00022 
00023 # depends on pep8, pyflakes, pylint
00024 # for ubuntu
00025 #
00026 #   sudo apt-get install pylint pyflakes
00027 #
00028 #   sudo apt-get install python-setuptools python-pip
00029 #   sudo pip install pep8
00030 #
00031 # in debian install pylint pyflakes pep8 with apt-get/aptitude/etc
00032 #
00033 # on *nix run
00034 #   python source/tests/pep8.py > test_pep8.log 2>&1
00035 
00036 # how many lines to read into the file, pep8 comment
00037 # should be directly after the licence header, ~20 in most cases
00038 PEP8_SEEK_COMMENT = 40
00039 SKIP_PREFIX = "./tools", "./config", "./scons", "./extern"
00040 FORCE_PEP8_ALL = False
00041 
00042 
00043 def file_list_py(path):
00044     for dirpath, dirnames, filenames in os.walk(path):
00045         for filename in filenames:
00046             if filename.endswith(".py") or filename.endswith(".cfg"):
00047                 yield os.path.join(dirpath, filename)
00048 
00049 
00050 def is_pep8(path):
00051     print(path)
00052     if open(path, 'rb').read(3) == b'\xef\xbb\xbf':
00053         print("\nfile contains BOM, remove first 3 bytes: %r\n" % path)
00054 
00055     # templates dont have a header but should be pep8
00056     for d in ("presets", "templates", "examples"):
00057         if ("%s%s%s" % (os.sep, d, os.sep)) in path:
00058             return 1
00059 
00060     f = open(path, 'r', encoding="utf8")
00061     for i in range(PEP8_SEEK_COMMENT):
00062         line = f.readline()
00063         if line.startswith("# <pep8"):
00064             if line.startswith("# <pep8 compliant>"):
00065                 return 1
00066             elif line.startswith("# <pep8-80 compliant>"):
00067                 return 2
00068     f.close()
00069     return 0
00070 
00071 
00072 def main():
00073     files = []
00074     files_skip = []
00075     for f in file_list_py("."):
00076         if [None for prefix in SKIP_PREFIX if f.startswith(prefix)]:
00077             continue
00078 
00079         pep8_type = FORCE_PEP8_ALL or is_pep8(f)
00080 
00081         if pep8_type:
00082             # so we can batch them for each tool.
00083             files.append((os.path.abspath(f), pep8_type))
00084         else:
00085             files_skip.append(f)
00086 
00087     print("\nSkipping...")
00088     for f in files_skip:
00089         print("    %s" % f)
00090 
00091     # strict imports
00092     print("\n\n\n# running pep8...")
00093     import re
00094     import_check = re.compile(r"\s*from\s+[A-z\.]+\s+import \*\s*")
00095     for f, pep8_type in files:
00096         for i, l in enumerate(open(f, 'r', encoding='utf8')):
00097             if import_check.match(l):
00098                 print("%s:%d:0: global import bad practice" % (f, i + 1))
00099 
00100     print("\n\n\n# running pep8...")
00101     for f, pep8_type in files:
00102         if pep8_type == 1:
00103             # E501:80 line length
00104             os.system("pep8 --repeat --ignore=E501 '%s'" % (f))
00105         else:
00106             os.system("pep8 --repeat '%s'" % (f))
00107 
00108     # pyflakes
00109     print("\n\n\n# running pyflakes...")
00110     for f, pep8_type in files:
00111         os.system("pyflakes '%s'" % f)
00112 
00113     print("\n\n\n# running pylint...")
00114     for f, pep8_type in files:
00115         # let pep8 complain about line length
00116         os.system("pylint "
00117                   "--disable="
00118                   "C0111,"  # missing docstring
00119                   "C0103,"  # invalid name
00120                   "W0613,"  # unused argument, may add this back
00121                             # but happens a lot for 'context' for eg.
00122                   "W0232,"  # class has no __init__, Operator/Panel/Menu etc
00123                   "W0142,"  # Used * or ** magic
00124                             # even needed in some cases
00125                   "R0902,"  # Too many instance attributes
00126                   "R0903,"  # Too many statements
00127                   "R0911,"  # Too many return statements
00128                   "R0912,"  # Too many branches
00129                   "R0913,"  # Too many arguments
00130                   "R0914,"  # Too many local variables
00131                   "R0915,"  # Too many statements
00132                   " "
00133                   "--include-ids=y "
00134                   "--output-format=parseable "
00135                   "--reports=n "
00136                   "--max-line-length=1000"
00137                   " '%s'" % f)
00138 
00139 if __name__ == "__main__":
00140     main()