P4Python: Python interface to the Perforce API

Contents

  1. New Official P4Python
  2. Introduction
  3. License
  4. Support
  5. Documentation
  6. Building P4Python
  7. Downloads
  8. Sample Code
  9. P4Python Script Library
  10. Porting
  11. Credits
  12. Changelog

New Official P4Python

Please note that Perforce have now released P4Python as a supported product as of 2007.3.

This has been done with my agreement and support.

The new version is fully documented.

A summary of differences between the official version 2007.3 and my version 1.x can be found in the release notes.

I have made a minor mod to get the official version to compile with Python 2.4 (see changelist 6288 in Public Depot).

You can download a pre-built binary: P4Python-2007.3.win32-py2.4.exe.

Introduction

So, what's it all about? The observant amongst you may notice a distinct resemblance to P4Rubyy - 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:

Licence

This extension is distributed under the terms of this license. You use it at your own risk.

Support

P4Python is supported by me personally. I aim to be helpful, but paying work tends to take priority! You can contact me directly at: robert at vaccaperna dot co dot uk. 

Documentation

If you're new to Python, then start with www.python.org 

There is also documentation for P4Python.

Building P4Python

1. Download and install Python

Linux users can probably skip this step as most Linux distributions these days come with Python installed (or on the CD's)

Windows users can download a Windows installer for Python from ActiveState. This is currently known to work with 2.3 and 2.4. The official Python distribution is at http://www.python.org/download/ and this has been built on Windows with 2.5.

Otherwise you can download the source for Python from www.python.org which is the main Python home page.

2. Download P4Python

Choose the build of P4Python you want to run. For now there is just a current release (in the future there may be "stable" releases).

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.

3. Get the Perforce API

This step is only required if you're building P4Python from source.

See the Porting section below to help you find the right Perforce API build for your platform (see note re gcc 3).

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. P4Python now built with 2007.2 API. Please note the API changed in version 2005.2 and affects the way various results are returned. To use old style APIs:

p4client = p4.P4()
p4client.api("57")

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).

Edit setup.cfg to point to the directory where p4api files reside.

4. Build and install P4Python

P4Python uses Distutils (Python 2.2 and later).

  python setup.py install
should do the trick (requires C++ compiler to be installed), and P4API to be in sub-directory as specified in setup.cfg

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.

5. Building RPMs

If you wish to build RPMs then this is kindly documented by Jason Dillon at: http://perforce.planet57.com/wiki/P4PythonRPMS

 

Downloads

Current Builds

Release Type Version Current Notes
Source Code 1.2 p4python.zip Unzip, edit setup.cfg if necessary and run "python setup.py install" to build and install

Windows Installers

Release Platform Python Version P4API Version Installer - just download and run
1.2 Windows 2.5 2007.3 P4Python-1.2.win32-py2.5.exe
1.2 Windows 2.4 2007.3 P4Python-1.2.win32-py2.4.exe
1.1 Windows 2.5 2007.2 P4Python-1.1.win32-py2.5.exe
1.1 Windows 2.4 2007.2 P4Python-1.1.win32-py2.4.exe

To find out what's changed, read the change log.

Sample code

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.
  import p4

  template = "my-client-template"
  client_root = r"c:\work\my-root"

  p4c = p4.P4()
  p4c.parse_forms()	# Important for easy processing of forms like client specs!
  p4c.connect()

  try:
      # Run a "p4 client -t template -o" and convert it into a Python dictionary
      client = p4c.fetch_client("-t", template)

      # Now edit the fields in the form
      client["Root"] = client_root

      # Now save the udpated spec and sync it
      p4c.save_client(client)
      p4c.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 p4c.errors:
        print e
        raise e

Porting

P4Python is as portable as Python and Perforce so porting it is relatively easy. Most of the porting problems revolve around people not knowing which build of the Perforce API to use. The key is to match up the compiler used to build Python with the compiler used to build Perforce and use that compiler to build P4Python.

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. However, Python did not include DistUtils package prior to 2.2 so you may need to download that package to build if you have an old version of Python.

If you get "unresolved symbol" errors when building or running P4Python, you probably used the wrong compiler or the wrong Perforce API build. Pay particular attention to the GCC version used to build the API and the one installed on your system.

Credits

Thanks to Mike Meyer for the original code.

Thanks to Tony Smith for producing P4Ruby after which this is modelled.

Changelog

See CHANGELOG.txt

File: $Id: //guest/robert_cowham/perforce/API/python/old_p4python.html#1 $