Perforce: Delete empty change lists using Python

Some of the tools I've written allow a user to check out chains of files- like Maya files as well as any exported FBX files and their unity metadata etc, all in one nice pass. This is great, but it has one annoying side effect. If the user presses the button twice the files are added to another change list, but the one they were just in stays there, empty. Press it a number of times and suddenly you have an army of zombie phantom changelists whose only purpose is to clutter up your pending list and annoy you. 

I have been using the P4Python API and found it to be... well... its kinda crap. It would be great if it had a 'delete all empty pending changelists' function, but it doesn't seem to. So I wrote one of my own. 

Beware! If you don't like hacking output out of strings the following code will make you cringe... 

There's my target...
import subprocess

"""Get a list of changelists from the command line. Try to delete them (will delete if empty)"""
# sInfo= subprocess.STARTUPINFO() # Use this instead of the code below to hide the output window. Kinda handy if you don't like annoying artists.  
# sInfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # Hide the cmd window
# p = subprocess.Popen(command, stdout=subprocess.PIPE, startupinfo=sInfo) # Same as below, but no cmd window. 

workspace = 'YourWorkspace'
# Get the pending changes on the local client using the P4 console commands and reading the output.
command = "p4 changes -s pending -c %s" % workspace 
p = subprocess.Popen(command, stdout=subprocess.PIPE) 
temp =

# Split the block of text at the line endings. This should give you one changelist per line. 
raw_data = temp.split('\n') 

print raw_data

# Now go through each line and split the text at the spaces. 
# This should end up with something that reads like ['Change', '99999', 'on' etc etc...
for line in raw_data: 
    target = line.rstrip().split(" ")
    if len(target) > 1: # Skip any list with single elements. That aint got what we want. 
        changeNum = target[1] # The changeNum should be the second element
        command = 'p4 change -d %s' % (changeNum) # Attempt to delete the changelist. P4 refuses to delete changelists with items.  
        p = subprocess.Popen(command, stdout=subprocess.PIPE) # Lets get some info back again. 
        temp = # Tell us about what you just did. 
        print temp # Good boy. 

The Result... poor 76610 got what was coming to him...

Output will be something like this:
["Change 76610 on 2014/02/06 by ****@**** *pending* '[EmptyChangeListToNuke] '\r", 
"Change 76607 on 2014/02/06 by ****@**** *pending* '[ToolDev] Stuff '\r", '']

Change 76610 deleted.
Change 76607 has 35 open file(s) associated with it and can't be deleted.

No comments:

Post a Comment