CheckCaseTrigger.py #5

  • //
  • guest/
  • perforce_software/
  • sdp/
  • main/
  • Unsupported/
  • Samples/
  • triggers/
  • CheckCaseTrigger.py
  • View
  • Commits
  • Open Download .zip Download (16 KB)
# Version 2.1.6

# tag::includeManual[]
"""
CaseCheckTrigger.py

Example 1: Typical usage from the Helix Core server triggers table:

    Triggers:
        CheckCase change-submit //... "/usr/bin/python3 /p4/common/bin/triggers/CheckCaseTrigger.py %changelist% myuser=%user%"

SAMPLE OUTPUT:
    Submit validation failed -- fix problems then use 'p4 submit -c 1234'.
    'CheckCaseTrigger' validation failed:

    Your submission has been rejected because the following files
    are inconsistent in their use of case with respect to existing
    directories:

    Your file:         '//depot/dir/test'
    existing file/dir: '//depot/DIR'

BYPASS LOGIC:
    By default, this trigger can be bypassed by any user by adding the
    token BYPASS_CASE_CHECK to the changelist description.  Specify
    'allowbypass=no' on the command line to disable the ability to
    bypass this trigger

    As an exception, if the user is 'git-fusion-user', the case check
    is always bypassed if 'myuser' is defined.

DEPENDENCIES:
    This trigger requires P4Triggers.py and the P4Python API.

SEE ALSO:
    See 'p4 help triggers'.

"""
# end::includeManual[]

from __future__ import print_function

import P4
import P4Triggers
import os
import re
import subprocess
import sys

class CheckCaseTrigger(P4Triggers.P4Trigger):
# tag::includeManual[]
    """CaseCheckTrigger is a subclass of P4Trigger. Use this trigger to ensure
       that your depot does not contain two filenames or directories that only
       differ in case.
       Having files with different case spelling will cause problems in mixed
       environments where both case-insensitive clients like Windows and case-
       sensitive clients like UNIX access the same server.
    """
# end::includeManual[]

    def __init__(self, *args, maxErrors=10, **kwargs):
        kwargs['charset'] = 'none'
        kwargs['api_level'] = 71

        self.allowBypass=AllowBypass
 
        
        fileFilter = None
        if 'filefilter' in kwargs:
            fileFilter = kwargs['filefilter']
            del kwargs['filefilter']
        
        P4Triggers.P4Trigger.__init__(self, **kwargs)
        self.parse_args(__doc__, args)
        
        # Ensure that -ztag global option is used.
        self.p4.tagged = True

        # need to reset the args in case a p4config file overwrote them
        for (k, v) in kwargs.items():
            if k != "log":
                try:
                    setattr(self.p4, k, v)
                except:
                    self.logger.error("error setting p4 property: '%s' to '%v'" % (k, v))

        self.map = None
        if fileFilter:
            try:
                with open(fileFilter) as f:
                    self.map = P4.Map()
                    for line in f:
                        self.map.insert(line.strip())
            except IOError:
                self.logger.error("Could not open filter file %s" % fileFilter)
        
        self.maxErrors = maxErrors
        self.depotCache = {}
        self.masterCache = {}
        self.maxWildcards = 9

    def add_parse_args(self, parser):
        """Specific args for this trigger - also calls super class to add common trigger args"""
        parser.add_argument('change', help="Change to validate - %%change%% argument from triggers entry.")
        super(CheckCaseTrigger, self).add_parse_args(parser)

    def setUp(self):
        info = self.p4.run_info()[0]
        if "unicode" in info and info["unicode"] == "enabled":
            self.p4.charset = "utf8"
        self.p4.exception_level = 1 # ignore WARNINGS like "no such file"
        self.p4.prog = "CheckCaseTrigger"

        if self.allowBypass:
            self.USER_MESSAGE="""

    Your changelist submit attempt has been rejected because
    one or more file paths opened for add vary only by case
    from existing files/directory paths.  Creating file/folders
    that vary only in case from existing paths causes inconsistent
    behavior across platforms with different case handling behaviors
    (e.g. Windows, Linux/UNIX, Mac OSX). Thus, adding case-only
    variations of existing paths is strongly discouraged.

    If you are certain the files to be added will only be accessed
    from workspaces on case-sensitive platforms (like UNIX/Linux),
    then this trigger can be bypassed by adding the token
    BYPASS_CASE_CHECK to the changelist description and attempting
    the submit again.

    Alternately, you can revert any files opened for add in your
    changelists that vary only in case from existing files, or
    move them to new names that don't conflict with existing files.

    Offending files:
    """
        else:
            self.USER_MESSAGE="""

    Your changelist submit attempt has been rejected because
    one or more file paths opened for add vary only by case
    from existing files/directory paths.  Creating file/folders
    that vary only in case from existing paths causes inconsistent
    behavior across platforms with different case handling behaviors
    (e.g. Windows, Linux/UNIX, Mac OSX). Thus, adding case-only
    variations of existing paths is disallowed.

    To move forward, you can revert any files opened for add in your
    changelists that vary only in case from existing files, or
    move them to new names that don't conflict with existing files.

    Offending files:
    """

        self.BADFILE_FORMAT="""
      Your file:         '%s'
      existing file/dir: '%s'
      """

    def validate(self):
        """Here the fun begins. This method overrides P4Trigger.validate()"""
        badlist = {}
        files = self.change.files
        #self.logger.debug("validate: Files to submit: %s", files)

        self.filterRenames(files)
        
        # Determine valid file list and depots to cache,
        # including the max directory depth for each cache
        
        depth = 0
        validFiles = []
        uniqueDepots = {}
        
        for file in files:
            action = file.revisions[0].action
            if self.map and self.map.includes(file.depotFile):
                continue
            if action not in ("add", "branch", "move/add"):
                continue
            
            mismatch = ""
            
            path = file.depotFile[2:]
            #self.logger.debug("validate: path = %s", path)
            
            parts = path.split('/')            
            #self.logger.debug("validate: parts = %s", parts)
            
            depot = parts[0]
            
            #Determine if we have a valid depot.
            mismatch = self.findDepotMismatch(depot)
            if mismatch:
                badlist[file.depotFile] = mismatch
                continue
            
            if depot in uniqueDepots :
                if len(parts) > uniqueDepots[depot] :
                    uniqueDepots[depot] = len(parts)
            else :
                uniqueDepots[depot] = len(parts)
                        
            validFiles.append(file.depotFile)
            #self.logger.debug("validate: file.depotFile = %s", file.depotFile)
                
        #self.logger.debug("validate: uniqueDepots = %s", uniqueDepots)
        #self.logger.debug("validate: validFiles = %s",validFiles)
        
        # Build cache for each unique depot.
        self.buildCache(uniqueDepots, validFiles)
        #self.logger.debug("validate: masterCache_1 = %s", self.masterCache)
        
        # Look for files in cache. This includes looking for directories in file path.
        
        self.searchCache(validFiles, badlist)
 
        
        self.logger.debug("validate: badlist = %s", badlist)
        #self.logger.debug("validate: masterCache_2 = %s", self.masterCache)

        if len(badlist) > 0:
            self.report(badlist)
        
        return len(badlist) == 0
    
    #end of validate
  
    # This method builds a global cache to use for mismatch searches.
    # IN: depots to use, including depth of cache for each depot
    # OUT: None
    
    def buildCache(self, depots, fileList):
        
        #self.logger.debug("buildCache: depots = %s", depots)
        #self.logger.debug("buildCache: fileList = %s", fileList)
        
        for u in depots :
 
            #Optimize by looking for files from shared root in current depot.
            
            commonList = []
                
            for f in fileList:                
                if f.find('//' + u) != -1:
                    commonList.append(f)
            
            if len(commonList) == 1:
                fileStr = '//' + u + '/...'               
            else:                
                fileStr = '/' + os.path.commonpath( commonList) + '/...'            
            
            #self.logger.debug("buildCache: fileStr = %s", fileStr)
            
            # Ignore case when we get files.
                    
            for f in self.p4.run_files( "-i",fileStr) :
                #self.logger.debug("validate: f = %s", f)
                if "delete" not in f["action"] :
                    f = f["depotFile"]
                    cf = self.canonical(f)
                    
                    if not cf in self.masterCache :
                        self.masterCache[cf] = f
                        
                    #Need to add components of file's path.
                    for i in range(2, len(f)):
                        
                        if f[i] == '/':
                            substr = f[:i]
                            csubstr = self.canonical(substr)
                            if not csubstr in self.masterCache :
                                self.masterCache[csubstr] = substr  
                            #self.logger.debug("buildCache: substr = %s", substr)                          
        
    #end buildCache
    
    # Method canonical 
    # IN:  string, utf8 compatible
    # OUT: unicode string, all lower case
    def canonical(self, aString):
        return aString.lower()

    # Method filterRenames:
    # Removes pairs of files that only differ in case
    # where one action is branch, and the other delete
    def filterRenames(self, files):
        branches = [x for x in files if x.revisions[0].action == 'branch']
        deletes = [x.depotFile.lower() for x in files if x.revisions[0].action == 'delete']
        
        for f in branches:
            if f.depotFile.lower() in deletes:
                files.remove(f)

    # This method returns either a depot or directory that differs from 
    # the supplied path only in case, or None if no mismatch is found
    
    # This method looks for a depot mismatch
    # It caches the depots as it found them (Global cache)
    # IN: depot name (as provided by the change list
    # OUT: None if (no mismatch found) else (stored depot name)

    def findDepotMismatch(self, depot):
        canonicalDepot = self.canonical(depot)

        # if the depot is in the cache, the cached version is authoritative
    
        if canonicalDepot in self.depotCache:
            if self.depotCache[canonicalDepot] == depot:
                return None
            else:
                return '//' + self.depotCache[canonicalDepot]

        # depot not found in cache. Populate the cache
    
        mismatch = None
        for d in self.p4.run_depots():
            dname = d["name"]
            cd = self.canonical(dname)
            self.depotCache[cd] = dname
            if canonicalDepot == cd and depot != dname:
                mismatch = '//' + dname

        return mismatch
    # end findDepotMismatch
    
    def report(self, badfiles):
        msg = self.USER_MESSAGE
        for (n, (file, mismatch)) in enumerate(badfiles.items()):
            if n >= self.maxErrors:
                break
            msg += self.BADFILE_FORMAT % (file, mismatch)

        self.message(msg)

    def run(self):
        """Runs trigger"""
        try:
            self.logger.debug("CheckCaseTrigger firing")
            self.setupP4()
            return self.parseChange(self.options.change)
        except Exception:
            return self.reportException()
        return 0

    # This method searches a global cache to find case mismatches for changelist files.
    # File subdirectories and file itself are added to cache if no mismatches are found.
    # IN: List of changelist files for which we want verify there are no case mismatches.
    #     Mismatch dictionary that records mismatches.
    # OUT: May modifiy mismatches parameter
    
    def searchCache(self, cfiles, mismatches):
        
        #self.logger.debug("searchCache: changelist files = %s, mismatches = %s", cfiles, mismatches)
        
        for f in cfiles :
            
            #self.logger.debug("searchCache: f = %s", f)
            
            mismatch = ""
            
            # Look for file and continue if it's in the cache. It's already been added.
            
            cf = self.canonical(f)
            
            if cf in self.masterCache :
                #self.logger.debug("searchCache: found %s in %s", f, self.masterCache)
                if f != self.masterCache[cf] :
                    mismatch = self.masterCache[cf]
                    mismatches[f] = mismatch
                continue
                
            # Need to check for mismatch of file path components.
            # If none, add components to cache.
            
            for i in range(2, len(f)) :
                
                mismatch =  ""
                 
                if f[i] == '/':
                    substr = f[:i]
                    csubstr = self.canonical(substr)
                    #self.logger.debug("searchCache: csubstr = %s, substr = %s", csubstr, substr)          
                 
                    # Break if we find a mismatch. Otherwise, add subName to cache.
                
                    if csubstr in self.masterCache :
                        if self.masterCache[csubstr] != substr :
                            mismatch = self.masterCache[csubstr]
                            break
                    else :  
                        self.masterCache[csubstr] = substr           
            
            # If no mismatch, add the file.
            
            if mismatch :
                mismatches[f] = mismatch
            else :
                self.masterCache[cf] = f     
    
    #end searchCache
    
# If called from the command line, go in here

if __name__ == "__main__":
    # Generate new args - parsing out port=123 style way of specifying
    # parameters intended for p4 properties
    kwargs = {}
    args = []
    for arg in sys.argv[1:]:
        p = arg.split("=", 1)
        if len(p) == 1:
            args.append(arg)
        else:
            kwargs[p[0]] = p[1]

    # Example of how to exclude the 'git-fusion-user'
    # Note: Need to remove 'myuser' after test as it's not a valid P4 argument.
    if 'myuser' in kwargs: 
        if kwargs['myuser'] == 'git-fusion-user':
            sys.exit(0)
        else:
            del kwargs['myuser']

    AllowBypass = 1

    if 'allowbypass' in kwargs: 
        if kwargs['allowbypass'] == 'no':
            AllowBypass = 0

        # Remove 'allowbypass' after test as it's not a valid P4 argument.
        del kwargs['allowbypass']

    if AllowBypass:
        # Grab the changelist description, and scan for the bypass token string.
        # If the token is detected, silently and immediately exit with a happy 0
        # exit code.
        changelist = sys.argv[1]
        cmd = "%s -ztag -F %%desc%% describe -f -s %s" % (os.getenv('P4BIN','p4'), changelist)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        (changeDesc, err) = p.communicate()
        p_status = p.wait()

        # If the changelist description contains the text BYPASS_CASE_CHECK,
        # bypass the case check logic.
        if (re.search (b'BYPASS_CASE_CHECK', changeDesc, re.MULTILINE)):
            sys.exit(0)

    ct = CheckCaseTrigger(*args, maxErrors=10, **kwargs)
    sys.exit(ct.run())
# Change User Description Committed
#9 30297 C. Thomas Tyler Released SDP 2023.2.30295 (2024/05/08).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#8 30043 C. Thomas Tyler Released SDP 2023.2.30041 (2023/12/22).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#7 29954 C. Thomas Tyler Released SDP 2023.1.29949 (2023/12/01).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#6 29612 C. Thomas Tyler Released SDP 2023.1.29610 (2023/05/25).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#5 29401 C. Thomas Tyler Released SDP 2022.2.29399 (2023/02/06).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#4 29205 C. Thomas Tyler Released SDP 2022.1.29203 (2022/11/22).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#3 29143 C. Thomas Tyler Released SDP 2022.1.29141 (2022/10/29).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#2 27761 C. Thomas Tyler Released SDP 2020.1.27759 (2021/05/07).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#1 27331 C. Thomas Tyler Released SDP 2020.1.27325 (2021/01/29).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
//guest/perforce_software/sdp/dev/Unsupported/Samples/triggers/CheckCaseTrigger.py
#3 27104 C. Thomas Tyler CheckCaseTrigger.py v2.1.3:
* Added an optional 'allowbypass=no' parameter to enforce the case
check policy strictly, disallowing the bypass.
* If bypass is allowed, the BYPASS_CASE_CHECK feature is now
documented in that it appears in the message users see when the
trigger fires, instructing them how to bypass the trigger, but
warning of the perils of doing so.
* Generally improved the user message when the trigger fires,
prescribing next steps for the user, with different options
output depending on whether the bypass is enabled.
* Changed sample trigger call to avoid calling a random interpreter
in the PATH on Linux systems, where the best practice is to use the
shebang line in the script. (The Windows sample still illustrates
calling an interpreter from the PATH, as that is the best practice
on Windows.)
* Made some internal coding style consistency tweak (no functional
impact).

#review-27105
#2 26681 Robert Cowham Removing Deprecated folder - if people want it they can look at past history!
All functions have been replaced with standard functionality such as built in LDAP,
or default change type.
Documentation added for the contents of Unsupported folder.
Changes to scripts/triggers are usually to insert tags for inclusion in ASCII Doctor docs.
#1 26652 Robert Cowham This is Tom's change:

Introduced new 'Unsupported' directory to clarify that some files
in the SDP are not officially supported. These files are samples for
illustration, to provide examples, or are deprecated but not yet
ready for removal from the package.

The Maintenance and many SDP triggers have been moved under here,
along with other SDP scripts and triggers.

Added comments to p4_vars indicating that it should not be edited
directly. Added reference to an optional site_global_vars file that,
if it exists, will be sourced to provide global user settings
without needing to edit p4_vars.

As an exception to the refactoring, the totalusers.py Maintenance
script will be moved to indicate that it is supported.

Removed settings to support long-sunset P4Web from supported structure.

Structure under new .../Unsupported folder is:
   Samples/bin             Sample scripts.
   Samples/triggers        Sample trigger scripts.
   Samples/triggers/tests  Sample trigger script tests.
   Samples/broker          Sample broker filter scripts.
   Deprecated/triggers     Deprecated triggers.

To Do in a subsequent change: Make corresponding doc changes.
//guest/perforce_software/sdp/dev/Server/Unix/p4/common/bin/triggers/CheckCaseTrigger.py
#6 25715 Robert Cowham Refactor CheckCaseTrigger to work in SDP trigger style - and fix SDP failures.
       Added modified version of Sven's test harness which works (for Mac at least where
some tests must be skipped due to filesystem being case insensitive).
#5 24510 C. Thomas Tyler Enhanced CheckCaseTrigger.py so BYPASS_CASE_CHECK override feature
works even if the defaultChangeType configurable is set to
restricted.
#4 24460 C. Thomas Tyler Tweak to case check trigger to allow user to bypass the safety
feature by including the token BYPASS_CASE_CHECK in the changelist
description.

This is likely useful just after initial rollout of the CaseCheckTrigger
on an existing server that already contains case inconsistencies.
Bypassing the trigger may be required to do some cleanup of existing
data.

If this trigger is deployed on a new server initially, the bypass may
not be needed.
#3 21120 C. Thomas Tyler Corrected shebang line in CheckCaseTrigger.
Added manual-update version id to replace keyword tag.
#2 21098 C. Thomas Tyler SDP-ified:
* Changed sample path to reference SDP /p4/common/bin/triggers
location.
* Changed shebang line to use SDP standard python, which includes
P4Python.
* Removed the '$Id:' RCS keywordt ag line, as RCS tags aren't allowed
in the SDP (since SDP scripts live in many Perforce servers).
* Changed file time from text+kx to text+x.
* Updated copyright up thru 2016.
* One minor cosmetic tweak to doc text.

#review-21099
#1 21097 C. Thomas Tyler Branched CheckCase trigger into the SDP.
//guest/robert_cowham/perforce/utils/triggers/CheckCaseTrigger.py
#7 19940 Robert Cowham Tabs->spaces, adjust some other whitespace
#6 19939 Robert Cowham Update with latest changes by Sven etc.
#5 8050 Robert Cowham P4Python 2009.1
#4 8049 Robert Cowham Whitespace only change and comments.
Made indents standard and removed tabs
#3 8048 Robert Cowham Add comment about installing
#2 8046 Robert Cowham Latest change from Sven
#1 7531 Robert Cowham Personal branch
//guest/sven_erik_knop/P4Pythonlib/triggers/CheckCaseTrigger.py
#4 7379 Sven Erik Knop Added output to a log file.
The default is the send output to p4triggers.log in the P4ROOT directory, this can be overridden with the parameter log=<path>
Also, errors now cause the trigger to fail with sensible output first.
#3 7372 Sven Erik Knop Rollback Rename/move file(s).
To folder "perforce" is needed.
#2 7370 Sven Erik Knop Rename/move file(s) again - this time to the right location inside a perforce directory.
#1 7367 Sven Erik Knop New locations for the Python triggers.
//guest/sven_erik_knop/perforce/P4Pythonlib/triggers/CheckCaseTrigger.py
#1 7370 Sven Erik Knop Rename/move file(s) again - this time to the right location inside a perforce directory.
//guest/sven_erik_knop/P4Pythonlib/triggers/CheckCaseTrigger.py
#1 7367 Sven Erik Knop New locations for the Python triggers.
//guest/sven_erik_knop/triggers/CheckCaseTrigger.py
#3 7219 Sven Erik Knop First attempt for renamer support, not finished yet, therefore disabled.
#2 7218 Sven Erik Knop Updated CheckCaseTrigger.py to fix problems with files within directories.

The trigger would not detect case problems for files that are located in
subdirectories. Unintentional side effect of modifying the dirs list recursively
when checking for mismatched directories.
The solution was simple: make a copy of the directory list for the file check.
#1 6413 Sven Erik Knop Added some P4Python-based Perforce triggers.

P4Triggers.py is the based class for change trigger in Python modelled on
Tony Smith's Ruby trigger with the same name.

CheckCaseTrigger.py is a trigger that ensures that no-one enters a file
or directory with a name only differing by case from an existing file. This
trigger is Unicode aware and uses Unicode-comparison of file names, so it
can be used on nocase-Unicode based Perforce servers, which cannot catch
the difference between, say, "�re" and "�re" at the moment.

clienttrigger.py is a simple trigger that modifies the option "normdir" to
"rmdir" for new client specs only. It is meant as a template to create more
complex default settings like standard views.