Stumbling Toward 'Awesomeness'

A Technical Art Blog

Saturday, September 25, 2010

Perforce Triggers in Python (Pt 2)

So last time I more introduced you to the idea of triggers, here’s a more complex example. This worked on my db, but if you have branching you would need to check each returned file against the branch you are submitting to.

Check if The File is Already in the Depot

This is a trigger that checks the hash digest of the incoming file against that of the server. This way you can see if the user is checking in a file that already exists.

import sys
from P4 import P4, P4Exception
p4 = P4()
describe = []
	p4.user = "admin"
	p4.password = "admin"
	p4.port = "1666"
	lst = sys.argv[2]
	stat ='fstat', ('-Ol','//depot/...@'+ str(lst)))
	hash = stat[0]['digest']
	fname = stat[0]['depotFile']
	m ='fstat', ('-Ol','-F', ('digest = ' + str(hash)),'//depot/...'))
	existing = []
	for file in m:
		if file['depotFile'] != fname: existing.append(file)
	if existing != []:
		print '\n\nFILE EXISTS IN DEPOT!!'
		print  'YOUR FILE:  ' + (fname.split('/')[-1])
		for  file in existing: print 'EXACTLY MATCHES:  ' + file['depotFile']
		print 'P4 DIGEST:  ' + hash
		print 'SOLUTION: Contact your lead if you believe this message was generated in error.'
except Exception, e:
	print "Error: %s" % (e)

Then your trigger line looks like this:

	dupeCheck change-submit //depot/... "python X:/projects/2010/p4/ %user% %changelist%"

This is what the user will see when they try to check in:

posted by admin at 7:39 PM  

Powered by WordPress