# TODO: Define default python3 interpreter. # # Description: # Trigger to block submission of changelists which contain any variation of # "do not submit" (case/spacing insensitive) on the first line of the changelist # description. # # Use: # "DoNotSubmit.py -p %serverAddress% -c %changelist%" # # Trigger use: # DoNotSubmit change-submit //DepotPathForTargetFiles/... # ".../TriggerDirectory/DoNotSubmit.py -p %masterServer% -c %changelist%" # # Author: Joe Robinson # Created: 2013/06/26 # Editor: $Author$ # Edited: $DateTime$ # File: $File$ import sys, re, marshal, subprocess # sys: exit() # re: Regular expression matching. # marshal: Python dictionary loading. # subprocess: Process creation and piping # of stdout. from optparse import OptionParser # Command-line argument parser # ADMIN OPTIONS: #---------- # String to check top comment for. regexString = '^DO[\W_]*NOT[\W_]*SUBMIT[\W_]*$' # Compile expression with optional params (ignore case, multiline, etc) # By default, does not reject description with the regexString on any line other # than the first. # # re.I - Ignore case # re.M - '^' and '$' will match the begging and end of each line in addition to # the beginning and end of the string. regex = re.compile(r"%s" % regexString, re.I | re.M) # LOCATION of p4 binary (server-side) p4binary = "/usr/local/bin/p4" # User to run 'p4 change' command as. Example: "someServiceUser" userArg = "someServiceUser" #---------- # Options parsing: #---------- usage = "%prog -p %serverAddress% -c %changelist%" parser = OptionParser(usage = usage) parser.add_option("-c", "--changeNum", action = "store", type = "string", dest = "changelistNum", help = "Changelist number to check.", metavar = "%changelist%") parser.add_option("-p", action = "store", type = "string", dest = "serverAddress", help = "Address of server (hostname:port).", metavar = "%serverAddress%") (options, args) = parser.parse_args() # Load a dictionary from 'p4 -G change' with command-line options. #---------- reader = subprocess.Popen( [p4binary, "-G", "-p", options.serverAddress, "-u", userArg, "change", "-o", options.changelistNum], stdout=subprocess.PIPE ) stdout, stderr = reader.communicate() dictionary = marshal.loads(stdout) # Assign desc field to string for regular expression comparison. #---------- descString = (dictionary[b'Description']).decode() # Exit with error message if string is matched. if (regex.match(descString)): sys.exit(" --- DoNotSubmit (py) REJECTED changelist %s." % options.changelistNum) # If match is not found, exit(0) to allow the submit to pass. sys.exit(0)