# -*- 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()))
