1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
# -*- coding:utf-8;mode:python;mode:font-lock -*-
##
# Example configuration for enforcer.
##
# Copyright (c) 2005 Wilfredo Sanchez Vega <wsanchez@wsanchez.net>.
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHORS BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
##
import os
import re
def cxx_comment_start(line):
# FIXME: This doesn't work correctly if // is quoted (eg. in a string).
return line.find("//")
def verify_file_modified(filename):
"""
Here we verify files which may not meet our requirements.
Any failure, even if not due to the specific changes in the commit
will raise an error.
"""
ext = os.path.splitext(filename)[1]
#
# Find WODebug=true in WOD files
#
# Test cases:
# r19866:
#
if ext == ".wod":
wod_file = open_file(filename)
try:
regex_wodebug = re.compile("WODebug\s*=\s*(true|yes|1)")
for line in wod_file:
if line[-1] == "\n": line = line[:-1] # Zap trailing newline
comment_start = cxx_comment_start(line)
if comment_start != -1:
line = line[:comment_start]
if regex_wodebug.match(line):
raise ValueError("WODebug enabled in WOD file %r" % filename)
finally: wod_file.close()
def verify_line_added(filename, line):
"""
Here we verify new lines of code which may not meet our requirements.
Code not changed as part of this commit is not verified.
"""
ext = os.path.splitext(filename)[1]
#
# Find NSLog calls and unauthorized println calls in Java code
#
# Test cases:
# r10814: unauthorized println calls
# r25692: NSLog calls
# r25729: authorized println calls
#
if ext == ".java":
comment_start = cxx_comment_start(line)
if comment_start == -1:
authorized = False
code = line
else:
if line[comment_start:].startswith("// (authorized)"):
authorized = True
else:
authorized = False
code = line[:comment_start] # Strip out comment
if code.find("NSLog") != -1:
raise ValueError("NSLog call found in Java code file %r: %s" % (filename, line.strip()))
if not authorized and code.find("println") != -1:
raise ValueError("unauthorized println call found in Java code file %r: %s" % (filename, line.strip()))
|