1

I am writing a simple script which will sync one perforce directory through python however I am not able to do so. It is not allowing me to execute anything in that [perforce] directory through script. Can someone please suggest me how I can run commands like p4 login,p4 sync in that directory?

5
  • Are these external commands? Have you looked at the excellent subprocess module? Commented Jan 25, 2013 at 13:24
  • I want to execute these commands in perforce folder.. These commands are perforce commands.. Commented Jan 25, 2013 at 13:25
  • I'm sorry, I don't know what perforce is. Are perforce commands regular executables that live somewhere on your path? As far as executing them in a particular directory, you can always use os.chdir to change to the "perforce" directory... Commented Jan 25, 2013 at 13:29
  • ohh no problem.. perforce is a software Configuration management tool Commented Jan 25, 2013 at 13:33
  • To better help you, may I kindly ask you to provide some additional info on "It is not allowing me to execute anything in that [perforce] directory through script"? do you receive an error message? may you show us the script you're working on? So far it may be also a permission problem Commented Jan 25, 2013 at 13:36

6 Answers 6

4

There is an officially supported python API for perforce.

You should use that instead of subprocess because it allows you to treat a p4 connection as an object and manipulate it using the API instead of manually parsing the output of the p4 command.

Sign up to request clarification or add additional context in comments.

Comments

3

Running "p4 sync" without any other arguments will sync the files mapped to the current directory by the current client workspace. When running the command from a Python script you'll need to be aware of where the scrip is actually running.

For debugging purposes, try running "p4 info" first: it'll show what the current working directory is, as well as showing you the rest of your Perforce environment details.

If you always want the script to sync the same directory, no matter where you run it, you might want to consider specifying a path to sync. For example:

  • Depot syntax: p4 sync //depot/path/to/dir/...
  • Local syntax (Windows): p4 sync c:\users\user\path\to\dir...
  • Local syntax (*nix): p4 sync /home/user/path/to/dir/...

If you're client workspace, user or perforce server configuration is configured on a directory basis by P4CONFIG or being set by P4V, you may want to add the global options for these settings to the command as global options. For example:

  • p4 -p server:1666 -c client_ws -u user sync //depot/path/to/dir/...

As you're running the commands from Python, you might find that the P4Python scripting API makes the configuration easier: https://www.perforce.com/perforce/r14.2/manuals/p4script/python.programming.html

Comments

2

If P4Python is more than you want to deal with there is a Python class in the Perforce Workshop that provides that wraps the command line with the P4Python interface.

P4Python-esque CLI wrapper for Python

Comments

1

I have had good success running Perforce commands from Python, using the ActiveState Python 2.7, with the P4Python plugin. You will need a p4 definition line, like the following:

p4Params = {'Port': "perforce_server_name:1666", \
            'Pass': "mypassword", \
            'User': "myname", \
            'Client': "myclient"}

Plus you'll need the P4 and P4Exception libraries:

from P4 import P4,P4Exception

With that in hand, you can perform most any Perforce command with the following construct. This function syncs to Rev #0 to remove all controlled files from some depot location "depotPath":

  def removeWorkFiles(depotPath,p4Params):
  """Tell Perforce to remove the version files from the workspace"""
    p4 = P4(client=p4Params['Client'], port=p4Params['Port'], password=p4Params['Pass'])
    p4.user = p4Params['perfUser']  
    try:
      p4.connect()                 # Connect to the Perforce Server
      p4.run_login()
      deletePath = depotPath + "#0"
      p4.run("sync", "-f", deletePath) # force to version "0", ie remove from workspace
      p4.disconnect()
    except P4Exception:
      for e in p4.errors:          # Display errors
        logging.error( e)

I highly recommend you read Perforce's P4 Python help page at:

https://www.perforce.com/perforce/r14.2/manuals/p4script/python.programming.html

This will give you some perspective, though it is not complete. Hope this helps

Comments

0

If you know exact perforce commands that you could run in a CMD shell, you can make use of os.system('cmd '). You can also take a look at os.popen and subprocess.call to do the same thing.

1 Comment

I'd advise against recommending os.system or os.popen when the various utilities in subprocess will do a better job (They can be used to avoid shell-injection type attacks).
0

There's p4swamp which is also a wrapper for the CLI

from p4swamp import p4

result = p4('sync')
print(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.