August 25, 2015

P4Python and Helix Distributed Version Control: Creating Local Repositories


Since the release of P4Python 2015.1 coincides with the new distributed version control system (DVCS) features in the Helix versioning engine, I couldn't help but wonder ... might it be useful to extend the scripting APIs to let you create local repositories?

You can see the result of these musings in the latest P4Python release. The P4 package gained two additional methods:

    P4.init(*args, **kargs)
    P4.clone(*args, **kargs)

These are factory methods - they create a personal server and then return an instance of the P4 class pointing to that server (If you are confused, remember that P4Python comes in a package called P4 that contains a class called P4).


P4.init() without any arguments simply creates a new personal server in your local directory. It uses the settings for case sensitivity and Unicode support from whatever the P4PORT environment variable points to (See this article for more details). If you like a little bit more control, you can use these keyword arguments:





workspace and server name



user name used for pushing



root directory for new server



specify case sensitivity



specify if Unicode is enabled



Usage example

    import P4
    p4 = P4.init(directory=’/Users/sknop/dvcs/’)
    with p4.connect():

The P4 instance returned by P4.init() has port, user and client workspace already set; all that is required is to connect to the server to perform any commands. Here, I am using the with statement described in a previous blog post to define a block, after which the connection will be automatically closed. This example opens all files in the directory '/Users/sknop/dvcs' and submits them to the new personal server. Note that the current directory is automatically switched to the root of the workspace created with the new personal server.

We do not connect automatically to give you the opportunity to set any protocol parameters. These parameters can only be set once before a connection is established.


P4.clone() requires additional arguments to specify the source server, just as for the P4 command line version. Note that the options casesensitive and unicode may not be specified. For example:

   import P4
   p4 = P4.clone('-m1', '-p', 'perforce:1666', '-r','p4pythonlib')

The resulting personal server inherits case sensitivity and Unicode setting from the source server.


Both P4.init and P4.clone are wrappers around the Helix command-line client executable (P4), since the C++ API does not expose these methods (yet). This requires P4 to be in the PATH to execute either function, otherwise an exception will be thrown. You also need to have the Helix versioning engine (P4D) in the path (as it is needed for your personal server), and both executables need to be at least 2015.1.

Note that P4Python will work fine without having the P4 executable in your path; the exception will be thrown only if you try to invoke either P4.init or P4.clone.


The DVCS support in P4Python should make it easy for script writers to spin up a local server. All the additional commands DVCS provides such as “p4 push” or “p4 switch” are available automatically in the usual fashion: P4.run_push(), for example.

I am curious to see what you will create using this new capability. As usual, contact me @p4sven if you have any questions or feedback.

Happy hacking.