#!/usr/bin/env python import os import re import sys import optparse import subprocess from os import path from collections import defaultdict from humbug_tools import check_output parser = optparse.OptionParser() parser.add_option('--full', action='store_true', help='Check some things we typically ignore') (options, args) = parser.parse_args() os.chdir(path.join(path.dirname(__file__), '..')) # Exclude some directories and files from lint checking exclude_trees = """ zephyr/static/third confirmation zephyr/tests/frontend/casperjs zephyr/migrations node_modules """.split() exclude_files = """ humbug/test_settings.py tools/jslint/jslint.js """.split() # Categorize by language all files known to Git git_files = map(str.strip, check_output(['git', 'ls-files']).split('\n')) by_lang = defaultdict(list) for filepath in git_files: if (not filepath or not path.isfile(filepath) or (filepath in exclude_files) or any(filepath.startswith(d+'/') for d in exclude_trees)): continue _, exn = path.splitext(filepath) if not exn: # No extension; look at the first line with file(filepath) as f: if re.match(r'^#!.*\bpython', f.readline()): exn = '.py' by_lang[exn].append(filepath) def has_trailing_whitespace(fn): return any(re.search('\s+$', line.strip('\n')) for line in open(fn)) # Invoke the appropriate lint checker for each language, # and also check files for extra whitespace. try: failed = False # Make the lint output bright red sys.stdout.write('\x1B[1;31m') sys.stdout.flush() try: subprocess.check_call(['tools/node', 'tools/jslint/check-all.js'] + by_lang['.js']) except subprocess.CalledProcessError: failed = True for fn in by_lang['.js'] + by_lang['.py']: if has_trailing_whitespace(fn): sys.stdout.write('Fix whitespace in %s\n' % fn) failed = True pyflakes = subprocess.Popen(['pyflakes'] + by_lang['.py'], stdout = subprocess.PIPE, stderr = subprocess.PIPE) # pyflakes writes some output (like syntax errors) to stderr. :/ for pipe in (pyflakes.stdout, pyflakes.stderr): for ln in pipe: if options.full or not \ ('imported but unused' in ln or ("zephyr_mirror_backend.py:" in ln and "redefinition of unused 'simplejson' from line" in ln)): sys.stdout.write(ln) failed = True sys.exit(1 if failed else 0) finally: # Restore normal terminal colors sys.stdout.write('\x1B[0m')