[ACCEPTED]-How do I check out a file from perforce in python?-perforce

Accepted answer
Score: 21

Perforce has Python wrappers around their 6 C/C++ tools, available in binary form for 5 Windows, and source for other platforms:

http://www.perforce.com/perforce/loadsupp.html#api

You 4 will find their documentation of the scripting 3 API to be helpful:

http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf

Use of the Python API 2 is quite similar to the command-line client:

PythonWin 2.5.1 (r251:54863, May  1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> import P4
>>> p4 = P4.P4()
>>> p4.connect() # connect to the default server, with the default clientspec
>>> desc = {"Description": "My new changelist description",
...         "Change": "new"
...         }
>>> p4.input = desc
>>> p4.run("changelist", "-i")
['Change 2579505 created.']
>>> 

I'll 1 verify it from the command line:

P:\>p4 changelist -o 2579505
# A Perforce Change Specification.
#
#  Change:      The change number. 'new' on a new changelist.
#  Date:        The date this specification was last modified.
#  Client:      The client on which the changelist was created.  Read-only.
#  User:        The user who created the changelist.
#  Status:      Either 'pending' or 'submitted'. Read-only.
#  Description: Comments about the changelist.  Required.
#  Jobs:        What opened jobs are to be closed by this changelist.
#               You may delete jobs from this list.  (New changelists only.)
#  Files:       What opened files from the default changelist are to be added
#               to this changelist.  You may delete files from this list.
#               (New changelists only.)

Change: 2579505

Date:   2008/10/08 13:57:02

Client: MYCOMPUTER-DT

User:   myusername

Status: pending

Description:
        My new changelist description
Score: 7

Here's what I came up with:

import os

def CreateNewChangeList(description):
    "Create a new changelist and returns the changelist number as a string"
    p4in, p4out = os.popen2("p4 changelist -i")
    p4in.write("change: new\n")
    p4in.write("description: " + description)
    p4in.close()
    changelist = p4out.readline().split()[1]
    return changelist

def OpenFileForEdit(file, changelist = ""):
    "Open a file for edit, if a changelist is passed in then open it in that list"
    cmd = "p4 edit "
    if changelist:
        cmd += " -c " + changelist + " "
    ret = os.popen(cmd + file).readline().strip()
    if not ret.endswith("opened for edit"):
        print "Couldn't open", file, "for edit:"
        print ret
        raise ValueError

0

Score: 4

Perforce's P4 Python module mentioned in another answer is the way 3 to go, but if installing this module isn't 2 an option you can use the -G flag to help 1 parse p4.exe output:

p4 [ options ] command [ arg ... ]
    options:
            -c client -C charset -d dir -H host -G -L language
            -p port -P pass -s -Q charset -u user -x file
    The -G flag causes all output (and batch input for form commands
    with -i) to be formatted as marshalled Python dictionary objects.
Score: 3

Building from p4python source requires downloading 25 and extracting the p4 api recommended for 24 that version. For example, if building the 23 Windows XP x86 version of P4Python 2008.2 22 for activepython 2.5:

  • download and extract both the p4python and p4api
  • fixup the setup.cfg for p4python to point to the p4api directory.

To open files for edit 21 (do a checkout), on the command line, see 20 'p4 help open'.

You can check out files without 19 making a changelist if you add the file 18 to the default changelist, but it's a good 17 idea to make a changelist first.

P4Python 16 doesn't currently compile for activepython 15 2.6 without visual studio 2008; the provided 14 libs are built with 2005 or 2003. Forcing 13 p4python to build against mingw is nearly 12 impossible, even with pexports of python26.dll 11 and reimp/reassembly of the provided .lib 10 files into .a files.

In that case, you'll 9 probably rather use subprocess, and return 8 p4 results as marshalled python objects. You 7 can write your own command wrapper that 6 takes an arg array, constructs and runs 5 the commands, and returns the results dictionary.

You 4 might try changing everything, testing, and 3 on success, opening the files that are different 2 with something equivalent to 'p4 diff -se 1 //...'

Score: 2

You may want to check out the P4Python module. It's 2 available on the perforce site and it makes 1 things very simple.

Score: 2

Remember guys to install the development 3 package for Python for the p4api or it will 2 complain about missing headers. In Ubuntu 1 10.10, just do a simple:

apt-get install python2.6-dev

Or

apt-get install python3.1-dev

More Related questions