# Task: form trigger that refuses "p4 protect" entries that
# give permissions to "user *"
#
# will work on 2004.2 (and later) Perforce servers, but you
# need to remember to install the trigger thusly:
# (p4 triggers line follows - the '#' is a comment char for Python.)
# example in protect "python /path/to/scripts/on/server/machine/trig_protect_paranoid.py --formfile %formfile%"
# vi: set ts=4:
# vim: set ts=4:
#
# num of calls to 'p4': 0
# status: tested on Darwin Mac OS X using python 2.3
#
# Copyright 2004 Piccolo Engineering, Inc. All rights reserved.
import getopt
import sys
import re
defaultFormFile = None
[options,args] = getopt.getopt(sys.argv[1:], '',
[ 'formfile=', 'ff='])
for [opt,arg] in options:
if opt == "--formfile" or opt == '--ff': defaultFormFile = arg
if defaultFormFile is None:
print "--formfile XXXX must be given on command-line"
sys.exit(1)
errorList = []
# debugfd = open("/Users/jeffbowles/work/ruby-work/contracts/tmp-trigger-scripts/debug.out", "w")
# debugfd.writelines(["Looking at %s\n" % defaultFormFile])
fd = open(defaultFormFile, 'r')
if fd is None:
print "Cannot open file %s" % defaultFormFile
# close(debugdb)
sys.exit(1)
protect_re = re.compile('^\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)')
for ln in fd.readlines():
m = protect_re.match(ln)
if m is None : continue
(perm, entityType, entity, ipAddr, pathName) = m.groups()
print perm, entityType, entity, ipAddr, pathName
if entityType == "user" and entity == "*" and pathName[0] != '-':
errorList.append("Cannot add reference to 'user *'. Sorry.\n")
# close(fd)
if len(errorList) > 0:
for e in errorList:
print e
# debugfd.writelines([e])
sys.exit(1)
else:
sys.exit(0)