Created
June 25, 2018 09:36
-
-
Save jeffryang24/af2257d5c8ba0b8c943834ca78e9e6f6 to your computer and use it in GitHub Desktop.
Revisions
-
dreadatour revised this gist
Aug 26, 2012 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -125,7 +125,8 @@ def main(): # TODO: remove pep8 lint when flake8 will be updated (see E127, E128) for err in lint('pep8', files, settings['pep8']): divider = err.find(' ', err.find(' ') + 1) pep8_error = err[0:divider] if not any(error[0:divider] == pep8_error for error in errors): errors.append(err) if not errors: @@ -134,7 +135,7 @@ def main(): print "Python lint: %(red)sFAIL%(off)s" % COLOR print print "\n".join(sorted(errors)) print print "Aborting commit due to python lint errors." sys.exit(1) -
dreadatour revised this gist
Aug 26, 2012 . 1 changed file with 24 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,10 @@ #!/usr/bin/env python """ Lint python files before commit with flake8 and pep8 lint tools. You can define lint settings in '.pep8' file in the project root. Section '[pep8]' if for pep8 linter, and section '[flake8]' is for flake8. INSTALL: 1. sudo pip install flake8 pep8 2. Save this file to '.git/hooks/pre-commit' file in your git repository @@ -26,9 +28,12 @@ 'exclude', 'filename', 'select', 'ignore', 'max-line-length', 'format' ) # colorize output COLOR = { 'red': '\033[1;31m', 'green': '\033[1;32m', 'yellow': '\033[1;33m', 'off': '\033[1;m' } def parse_settings(config_file): @@ -80,14 +85,14 @@ def system(*args, **kwargs): def get_changed_files(): """ Get python files from 'files to commit' git cache list. """ files = [] filelist = system('git', 'diff', '--cached', '--name-status').strip() for line in filelist.split('\n'): action, filename = line.strip().split() if filename.endswith('.py') and action != 'D': files.append(filename) return files @@ -109,6 +114,7 @@ def main(): """ files = get_changed_files() if not files: print "Python lint: %(yellow)sSKIP%(off)s" % COLOR return config_file = os.path.join(os.path.abspath(os.curdir), '.pep8') @@ -122,11 +128,16 @@ def main(): if not any(err[0:divider] == error[0:divider] for error in errors): errors.append(err) if not errors: print "Python lint: %(green)sOK%(off)s" % COLOR return print "Python lint: %(red)sFAIL%(off)s" % COLOR print print "\n".join(errors) print print "Aborting commit due to python lint errors." sys.exit(1) if __name__ == '__main__': -
dreadatour renamed this gist
Aug 26, 2012 . 1 changed file with 31 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,24 @@ #!/usr/bin/env python """ Lint python files before commit with flake8 and pep8 lint tools. INSTALL: 1. sudo pip install flake8 pep8 2. Save this file to '.git/hooks/pre-commit' file in your git repository 3. Enjoy TODO: - Remove pep8 lint after flake8 will be updated (E127, E128 not recognized now) """ import os import sys import subprocess import ConfigParser # available settings list AVAILABLE_SETTINGS = ( 'exclude', 'filename', 'select', 'ignore', 'max-line-length', 'count', 'quiet', 'show-pep8', 'show-source', 'statistics', 'verbose' ) @@ -19,6 +27,7 @@ ) # colorize output COLOR_RED = "\033[1;31m" COLOR_GREEN = "\033[1;32m" COLOR_OFF = "\033[1;m" @@ -45,7 +54,7 @@ def parse_settings(config_file): for linter in settings.keys(): try: for key, value in config.items(linter): if key in AVAILABLE_SETTINGS: if key in SETTINGS_WITH_PARAMS: settings[linter].append("--%s=%s" % (key, value)) else: @@ -82,24 +91,16 @@ def get_changed_files(): return files def lint(cmd, files, settings): """ Run pep8 or flake8 lint. """ if cmd not in ('pep8', 'flake8'): raise Exception("Unknown lint command: %s" % cmd) args = settings[:] args.insert(0, cmd) args.extend(files) return filter(None, system(*args).strip().split('\n')) def main(): @@ -113,20 +114,19 @@ def main(): config_file = os.path.join(os.path.abspath(os.curdir), '.pep8') settings = parse_settings(config_file) errors = lint('flake8', files, settings['flake8']) # TODO: remove pep8 lint when flake8 will be updated (see E127, E128) for err in lint('pep8', files, settings['pep8']): divider = err.find(' ', err.find(' ') + 1) if not any(err[0:divider] == error[0:divider] for error in errors): errors.append(err) if errors: print "Python lint: {}FAIL{}".format(COLOR_RED, COLOR_OFF) print "\n".join(errors) sys.exit(1) print "Python lint: {}OK{}".format(COLOR_GREEN, COLOR_OFF) if __name__ == '__main__': -
dreadatour revised this gist
Aug 26, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -58,6 +58,7 @@ def parse_settings(config_file): return settings def system(*args, **kwargs): """ Run system command. -
dreadatour created this gist
Aug 26, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,132 @@ #!/usr/bin/env python import os import sys import subprocess import ConfigParser # available settings list AVAILABLE_SETTINGS_PEP8 = ( 'exclude', 'filename', 'select', 'ignore', 'max-line-length', 'count', 'format', 'quiet', 'show-pep8', 'show-source', 'statistics', 'verbose' ) AVAILABLE_SETTINGS_FLAKE8 = ( 'exclude', 'filename', 'select', 'ignore', 'max-line-length', 'count', 'quiet', 'show-pep8', 'show-source', 'statistics', 'verbose' ) SETTINGS_WITH_PARAMS = ( 'exclude', 'filename', 'select', 'ignore', 'max-line-length', 'format' ) # colorize output COLOR_RED = "\033[1;31m" COLOR_OFF = "\033[1;m" def parse_settings(config_file): """ Get pep8 and flake8 lint settings from config file. Useful for define per-project lint options. """ settings = {'pep8': list(), 'flake8': list()} # read project settings if not os.path.exists(config_file) or not os.path.isfile(config_file): return settings try: config = ConfigParser.ConfigParser() config.read(config_file) except ConfigParser.MissingSectionHeaderError, e: print "ERROR: project lint config file is broken:\n" print repr(e) sys.exit(1) # read project lint settings for pep8 and flake8 for linter in settings.keys(): try: for key, value in config.items(linter): if key in AVAILABLE_SETTINGS_PEP8: if key in SETTINGS_WITH_PARAMS: settings[linter].append("--%s=%s" % (key, value)) else: settings[linter].append("--%s" % key) else: print "WARNING: unknown %s linter config: %s" % ( linter, key) except ConfigParser.NoSectionError: pass return settings def system(*args, **kwargs): """ Run system command. """ kwargs.setdefault('stdout', subprocess.PIPE) proc = subprocess.Popen(args, **kwargs) out, err = proc.communicate() return out def get_changed_files(): """ Get python files in 'files to commit' git cache list. """ files = [] filelist = system('git', 'diff', '--cached', '--name-status').strip() for line in filelist.split('\n'): for action, filename in [line.strip().split()]: if filename.endswith('.py') and action != 'D': files.append(filename) return files def lint_pep8(files, settings): """ Run pep8 lint. """ args = settings[:] args.insert(0, 'pep8') args.extend(files) return [err for err in system(*args).strip().split('\n') if err] def lint_flake8(files, settings): """ Run flake8 lint. """ args = settings[:] args.insert(0, 'flake8') args.extend(files) return [err for err in system(*args).strip().split('\n') if err] def main(): """ Do work """ files = get_changed_files() if not files: return config_file = os.path.join(os.path.abspath(os.curdir), '.pep8') settings = parse_settings(config_file) pep8_errors = lint_pep8(files, settings['pep8']) if pep8_errors: print "{}Pep8 lint errors:{}".format(COLOR_RED, COLOR_OFF) for error in pep8_errors: print error flake8_errors = lint_flake8(files, settings['flake8']) if flake8_errors: print "{}Flake8 lint errors:{}".format(COLOR_RED, COLOR_OFF) for error in flake8_errors: print error if pep8_errors or flake8_errors: sys.exit(1) if __name__ == '__main__': main()