Creating a Perforce ClientSpec with P4Python

We’re begin­ning our migra­tion to Per­force at work and I’ve been doing most of the script­ing work since…well, since I’m appar­ently the expert. I’m using Python and the P4Python API to Per­force and I ran into a lit­tle issue that isn’t doc­u­mented in the P4Python doc­u­men­ta­tion. It makes sense after the fact but it took me a while to fig­ure it out. I thought it might be use­ful to some­one if I wrote it up here.

When you’re deal­ing with the client specification’s details in P4Python, you ref­er­ence them as a dic­tio­nary. Pretty much all the val­ues in the dic­tio­nary are strings (Root, User, etc) and I kept try­ing to make the View a string that I built up from scratch. I just assumed that the mul­ti­ple lines in the view should be man­u­ally cre­ated using a new line char­ac­ter. How­ever, as in every­thing else Python, it’s eas­ier than that. The View should be a list of the map­pings you want to include in the client spec. The exam­ple below shows how to use P4Python to cre­ate a new work­space in Perforce.


import p4

p4c = p4.P4()
p4c.parse_forms()

try:
  p4c.connect()
  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
  p4c.save_client(spec)
  p4c.run_sync()

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 mul­ti­ple lines to map sev­eral regions of the depot to your work­space but it would be a nice addi­tion to the doc­u­men­ta­tion to detail that.

No Comments

Leave a Reply

Your email is never shared.Required fields are marked *