Blender  V2.59
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 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 
00041 
00042 def file_list_py(path):
00043     for dirpath, dirnames, filenames in os.walk(path):
00044         for filename in filenames:
00045             if filename.endswith(".py") or filename.endswith(".cfg"):
00046                 yield os.path.join(dirpath, filename)
00047 
00048 
00049 def is_pep8(path):
00050     print(path)
00051     if open(path, 'rb').read(3) == b'\xef\xbb\xbf':
00052         print("\nfile contains BOM, remove first 3 bytes: %r\n" % path)
00053 
00054     # templates dont have a header but should be pep8
00055     for d in ("presets", "templates", "examples"):
00056         if ("%s%s%s" % (os.sep, d, os.sep)) in path:
00057             return 1
00058 
00059     f = open(path, 'r', encoding="utf8")
00060     for i in range(PEP8_SEEK_COMMENT):
00061         line = f.readline()
00062         if line.startswith("# <pep8"):
00063             if line.startswith("# <pep8 compliant>"):
00064                 return 1
00065             elif line.startswith("# <pep8-80 compliant>"):
00066                 return 2
00067     f.close()
00068     return 0
00069 
00070 
00071 def main():
00072     files = []
00073     files_skip = []
00074     for f in file_list_py("."):
00075         if [None for prefix in SKIP_PREFIX if f.startswith(prefix)]:
00076             continue
00077 
00078         pep8_type = is_pep8(f)
00079 
00080         if pep8_type:
00081             # so we can batch them for each tool.
00082             files.append((os.path.abspath(f), pep8_type))
00083         else:
00084             files_skip.append(f)
00085 
00086     print("\nSkipping...")
00087     for f in files_skip:
00088         print("    %s" % f)
00089 
00090     # strict imports
00091     print("\n\n\n# running pep8...")
00092     import re
00093     import_check = re.compile(r"\s*from\s+[A-z\.]+\s+import \*\s*")
00094     for f, pep8_type in files:
00095         for i, l in enumerate(open(f, 'r', encoding='utf8')):
00096             if import_check.match(l):
00097                 print("%s:%d:0: global import bad practice" % (f, i + 1))
00098 
00099     print("\n\n\n# running pep8...")
00100     for f, pep8_type in files:
00101         if pep8_type == 1:
00102             # E501:80 line length
00103             os.system("pep8 --repeat --ignore=E501 '%s'" % (f))
00104         else:
00105             os.system("pep8 --repeat '%s'" % (f))
00106 
00107     # pyflakes
00108     print("\n\n\n# running pyflakes...")
00109     for f, pep8_type in files:
00110         os.system("pyflakes '%s'" % f)
00111 
00112     print("\n\n\n# running pylint...")
00113     for f, pep8_type in files:
00114         # let pep8 complain about line length
00115         os.system("pylint --reports=n --max-line-length=1000 '%s'" % f)
00116 
00117 if __name__ == "__main__":
00118     main()