2012-07-14 02:14:17 +04:00
|
|
|
#!/usr/bin/python
|
|
|
|
##
|
2016-03-29 18:27:40 +03:00
|
|
|
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
|
|
|
##
|
|
|
|
## This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
## was not distributed with this source code in the LICENSE file, you can
|
|
|
|
## obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
## Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
## PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
2012-07-14 02:14:17 +04:00
|
|
|
##
|
|
|
|
"""Performs style checking on each diff hunk."""
|
|
|
|
import getopt
|
|
|
|
import os
|
|
|
|
import StringIO
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import diff
|
|
|
|
|
|
|
|
|
|
|
|
SHORT_OPTIONS = "h"
|
|
|
|
LONG_OPTIONS = ["help"]
|
|
|
|
|
|
|
|
TOPLEVEL_CMD = ["git", "rev-parse", "--show-toplevel"]
|
2012-10-18 01:23:08 +04:00
|
|
|
DIFF_CMD = ["git", "diff"]
|
2012-10-19 01:34:53 +04:00
|
|
|
DIFF_INDEX_CMD = ["git", "diff-index", "-u", "HEAD", "--"]
|
2012-07-14 02:14:17 +04:00
|
|
|
SHOW_CMD = ["git", "show"]
|
2013-10-16 02:43:42 +04:00
|
|
|
CPPLINT_FILTERS = ["-readability/casting"]
|
2012-07-14 02:14:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Usage(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SubprocessException(Exception):
|
|
|
|
def __init__(self, args):
|
|
|
|
msg = "Failed to execute '%s'"%(" ".join(args))
|
|
|
|
super(SubprocessException, self).__init__(msg)
|
|
|
|
|
|
|
|
|
|
|
|
class Subprocess(subprocess.Popen):
|
|
|
|
"""Adds the notion of an expected returncode to Popen."""
|
|
|
|
|
|
|
|
def __init__(self, args, expected_returncode=0, **kwargs):
|
|
|
|
self._args = args
|
|
|
|
self._expected_returncode = expected_returncode
|
|
|
|
super(Subprocess, self).__init__(args, **kwargs)
|
|
|
|
|
|
|
|
def communicate(self, *args, **kwargs):
|
|
|
|
result = super(Subprocess, self).communicate(*args, **kwargs)
|
|
|
|
if self._expected_returncode is not None:
|
|
|
|
try:
|
|
|
|
ok = self.returncode in self._expected_returncode
|
|
|
|
except TypeError:
|
|
|
|
ok = self.returncode == self._expected_returncode
|
|
|
|
if not ok:
|
|
|
|
raise SubprocessException(self._args)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv=None):
|
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv
|
|
|
|
try:
|
|
|
|
try:
|
2012-10-18 01:23:08 +04:00
|
|
|
opts, args = getopt.getopt(argv[1:], SHORT_OPTIONS, LONG_OPTIONS)
|
2012-07-14 02:14:17 +04:00
|
|
|
except getopt.error, msg:
|
|
|
|
raise Usage(msg)
|
|
|
|
|
|
|
|
# process options
|
|
|
|
for o, _ in opts:
|
|
|
|
if o in ("-h", "--help"):
|
|
|
|
print __doc__
|
|
|
|
sys.exit(0)
|
|
|
|
|
2012-10-18 01:23:08 +04:00
|
|
|
if args and len(args) > 1:
|
|
|
|
print __doc__
|
|
|
|
sys.exit(0)
|
|
|
|
|
2012-07-14 02:14:17 +04:00
|
|
|
# Find the fully qualified path to the root of the tree
|
|
|
|
tl = Subprocess(TOPLEVEL_CMD, stdout=subprocess.PIPE)
|
|
|
|
tl = tl.communicate()[0].strip()
|
|
|
|
|
2012-10-18 01:23:08 +04:00
|
|
|
# See if we're working on the index or not.
|
|
|
|
if args:
|
|
|
|
diff_cmd = DIFF_CMD + [args[0] + "^!"]
|
|
|
|
else:
|
|
|
|
diff_cmd = DIFF_INDEX_CMD
|
|
|
|
|
2012-07-14 02:14:17 +04:00
|
|
|
# Build the command line to execute cpplint
|
|
|
|
cpplint_cmd = [os.path.join(tl, "tools", "cpplint.py"),
|
|
|
|
"--filter=" + ",".join(CPPLINT_FILTERS),
|
|
|
|
"-"]
|
|
|
|
|
|
|
|
# Get a list of all affected lines
|
|
|
|
file_affected_line_map = {}
|
2012-10-18 01:23:08 +04:00
|
|
|
p = Subprocess(diff_cmd, stdout=subprocess.PIPE)
|
2012-07-14 02:14:17 +04:00
|
|
|
stdout = p.communicate()[0]
|
|
|
|
for hunk in diff.ParseDiffHunks(StringIO.StringIO(stdout)):
|
|
|
|
filename = hunk.right.filename[2:]
|
|
|
|
if filename not in file_affected_line_map:
|
|
|
|
file_affected_line_map[filename] = set()
|
|
|
|
file_affected_line_map[filename].update(hunk.right.delta_line_nums)
|
|
|
|
|
|
|
|
# Run each affected file through cpplint
|
|
|
|
lint_failed = False
|
|
|
|
for filename, affected_lines in file_affected_line_map.iteritems():
|
|
|
|
if filename.split(".")[-1] not in ("c", "h", "cc"):
|
|
|
|
continue
|
2012-10-19 01:34:53 +04:00
|
|
|
|
|
|
|
if args:
|
|
|
|
# File contents come from git
|
|
|
|
show_cmd = SHOW_CMD + [args[0] + ":" + filename]
|
|
|
|
show = Subprocess(show_cmd, stdout=subprocess.PIPE)
|
|
|
|
lint = Subprocess(cpplint_cmd, expected_returncode=(0, 1),
|
|
|
|
stdin=show.stdout, stderr=subprocess.PIPE)
|
|
|
|
lint_out = lint.communicate()[1]
|
|
|
|
else:
|
|
|
|
# File contents come from the working tree
|
|
|
|
lint = Subprocess(cpplint_cmd, expected_returncode=(0, 1),
|
|
|
|
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
stdin = open(os.path.join(tl, filename)).read()
|
|
|
|
lint_out = lint.communicate(stdin)[1]
|
2012-07-14 02:14:17 +04:00
|
|
|
|
|
|
|
for line in lint_out.split("\n"):
|
|
|
|
fields = line.split(":")
|
|
|
|
if fields[0] != "-":
|
|
|
|
continue
|
|
|
|
warning_line_num = int(fields[1])
|
|
|
|
if warning_line_num in affected_lines:
|
|
|
|
print "%s:%d:%s"%(filename, warning_line_num,
|
|
|
|
":".join(fields[2:]))
|
2012-10-18 08:43:18 +04:00
|
|
|
lint_failed = True
|
2012-07-14 02:14:17 +04:00
|
|
|
|
2012-10-18 08:43:18 +04:00
|
|
|
# Set exit code if any relevant lint errors seen
|
2012-07-14 02:14:17 +04:00
|
|
|
if lint_failed:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
except Usage, err:
|
|
|
|
print >>sys.stderr, err
|
|
|
|
print >>sys.stderr, "for help use --help"
|
|
|
|
return 2
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|