Creating a Perforce ClientSpec with P4Python

We’re beginning our migration to Perforce at work and I’ve been doing most of the scripting work since. . .well, since I’m apparently the expert. I’m using Python and the P4Python API to Perforce and I ran into a little issue that isn’t documented in the P4Python documentation. It makes sense after the fact but it took me a while to figure it out. I thought it might be useful to someone if I wrote it up here.

When you’re dealing with the client specification’s details in P4Python, you reference them as a dictionary. Pretty much all the values in the dictionary are strings (Root, User, etc) and I kept trying to make the View a string that I built up from scratch. I just assumed that the multiple lines in the view should be manually created using a new line character. However, as in everything else Python, it’s easier than that. The View should be a list of the mappings you want to include in the client spec. The example below shows how to use P4Python to create a new workspace in Perforce.

import p4

p4c = p4.P4()

  spec = p4c.fetch_client("bbim-MySampleClient")
  spec["Root"] = "C:/Workspace/MySample"
  view = []
  view.append("//main/Workspace/BrettBim/MySample/...  //bbim-MySampleClient/...")
  view.append("//main/NUnit/nunit.framework.dll  //bbim-MySampleClient/tools/nunit/nunit.framework.dll")
  spec["View"] = view

except p4.P4Error:
  # If any errors occur, we'll jump in here. Just log them
  # and raise the exception up to the higher level
  for e in p4.errors:
    print e

It makes sense that the view is a list since you can have multiple lines to map several regions of the depot to your workspace but it would be a nice addition to the documentation to detail that.

Leave a Reply

Your email address will not be published. Required fields are marked *