# 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% -u %user% -c %changelist% # # Trigger use: # DoNotSubmit change-submit //... ".../DoNotSubmit.py [options]" # # Author: Joe Robinson # Created: 2013/06/26 # Edited: $Date$ # File: DoNotSubmit.py 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 option 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) regex = re.compile(r"%s" % regexString, re.I | re.M) # LOCATION of p4 binary (server-side) p4binary = "/usr/local/bin/p4" #---------- # CHECK PYTHON VERSION -- MODULE USES python3.3 # # # Options parsing. #---------- usage = "%prog -c %changelist% -p %serverAddress% -u %user%" 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%") parser.add_option("-u", "--user", action = "store", type = "string", dest = "userName", help = "Name of user submitting changelist.", metavar = "%user%") (options, args) = parser.parse_args() # Load a dictionary from 'p4 -G change' with command-line options. readChange = subprocess.Popen( [p4binary, "-G", "-p", ("%s" % options.serverAddress), "-u", ("%s" % options.userName), "change", "-o", ("%s" % options.changelistNum)], stdout=subprocess.PIPE ) stdout, stderr = readChange.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)