P4Python: Python interface to the Perforce API |
So, what's it all about? The observant amongst may notice a distinct resemblance to P4Ruby - which is intentional (thanks Tony)! P4Python allows you to write Python code that interacts nicely and intuitively with a Perforce server. The main features are:
This extension is distributed under the terms of this license. You use it at your own risk.
Windows users can download a Windows installer for Python from ActiveState. This is currently known to work with 2.3.
Otherwise you can download the source for Python from www.python.org which is the main Python home page.
Pick the appropriate file from the downloads section.
If you chose to use the Windows installer then the process ends with running the executable. You're then ready to start using P4Python. What follows is only applicable to those building P4Python from source.
See the Porting section below to help you find the right Perforce API build for your platform.
Then go to the Perforce download site and grab the p4api.tar file for your platform. Try to get the latest available release, but certainly no older than 2001.1.
Extract the files in the p4api.tar tarfile (WinZip can handle it for Windows users) into a sub directory called p4api of where the source files are (or edit setup.py below).
P4Python uses Distutils (Python 2.2 and later).
python setup.py installshould do the trick (requires C++ compile to be installed).
Note that there is a supplied test harness (though it requires a working Perforce server with a copy of the training repository to run since it relies on certain history). It is valid example for how to use functions.
Release Type | Source Code | Windows Installer (Python 2.3) |
---|---|---|
Current | p4python.zip | P4Python-0.5.win32-py2.3.exe |
Other platforms (binaries kindly sent in):
Release Type | Platform | Python Version | Built Binary (copy to appropriate place) | Thanks To |
---|---|---|---|---|
Current | Cygwin | 2.4 | P4Python-0.5.cygwin-1.5.12-i686.tar.gz | Adam Heinz |
Current | Linux | 2.4 | P4Python-0.5.linux-i686.tar.gz | Tony Smith |
To find out what's changed, read the change log.
Please note that all functions are documented in the test harness. See comments for details.
Just a small example to whet your appetite. This small sample shows how to create a new client workspace based on an existing template. You can of course construct the view manually but most people who are scripting client creation will use a template so that seemed like a good example.from p4 import P4 template = "my-client-template" client_root = r"c:\work\my-root" p4 = P4() p4.parse_forms() p4.connect() try: # Run a "p4 client -t template -o" and convert it into a Python dictionary spec = p4.fetch_client("-t", template) # Now edit the fields in the form spec["Root"] = client_root # Now save the udpated spec and sync it p4.save_client(spec) p4.run_sync() except: # 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 raise e
P4Python has been built on at least the following platforms:
Note that P4Python is compatible with older versions of Python and the Perforce API, so on older platforms you should still be able to get a working build.
If you get "unresolved symbol" errors when building or running P4Python, you probably used the wrong compiler or the wrong Perforce API build.
Thanks to Tony Smith for producing P4Ruby after which this is modelled.