# This progam is used to automatically submit files # from a default changelist. # An email module is called to send email when submit completes. # The email module is named submit_mail.py # Gerry Thompson Python Class Perforce Software 2004 # Version 1.1 import os, string, re p4path = 'p4' # Path to p4 executable. p4 = p4path def filesopen(): opened = 1 single = os.popen(p4 + ' -s opened -c default','r').readline() # If -s is not used no stderr output is captured by python and # 'result' will be an empty list. # 'File(s) not opened on this client.' is converted to stdout by # the -s flag and prepended with 'error:'. result = string.split(single, ) if result[0] == 'error:': opened = 0 print 'There are no open files in the default changelist to submit!' return opened def submit(user, client, server): newchange = [] for changeline in os.popen(p4 + ' change -o ', 'r').readlines(): if string.count(changeline, "") > 0: changeline = ' Submit agent for user "'+user+'" using client "' + \ client+'" to Perforce server '+server+'.' newchange.append(changeline) # The following method works with the list created above: # submit = os.popen(p4 + ' submit -i ', 'w').writelines(newchange) # If I use popen I can query Perforce to see what the last # submitted changelist number my client spec submitted. It's perhaps not # so important here, but if my script submits a pending numbered changelist # (not in this script) then I may need to know the final changelist number. # for lastsubmit in os.popen(p4 + ' changes -s submitted -m1 @' + client).readlines(): # changenumber = string.split(lastsubmit, ) # print "Here is the changelist number of my last submit: ", changenumber[1], "\n" # Using popen2 I can use stdin and stdout to get # the submitted changelist number. i,o = os.popen2(p4 + ' -s submit -i ', 'b') # I need to convert the list to a string when using popen2. makestring = string.join(newchange, '\n') i.write(makestring) i.close() submit = o.read() o.close() # Using this method I can retrieve the submitted # changelist number from Perforce if I need it. findchange = re.compile('(info: Change )([0-9]*)( submitted\.)') change = findchange.search(submit).group(2) print '\n' print 'Submitted change number is: ', change, '\n' def autosubmit(): for aline in os.popen(p4 + ' info','r').readlines(): if string.count(aline, "User name") > 0: auser = string.strip(string.split(aline, 'User name: ')[-1]) if string.count(aline, "Client name") > 0: aclient = string.strip(string.split(aline, 'Client name: ')[-1]) if string.count(aline, "Server address") > 0: aserver = string.strip(string.split(aline, 'Server address: ')[-1]) submit(auser, aclient, aserver) return auser, aclient, aserver def tryit (from_user, to_user): if filesopen(): myuser, myclient, myserver = autosubmit() from submit_mail import sendmail sendmail (from_user, to_user, myuser, myclient, myserver)