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?
6 Answers
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.
Comments
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
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.
Comments
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
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
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).
subprocessmodule?perforceis. Are perforce commands regular executables that live somewhere on your path? As far as executing them in a particular directory, you can always useos.chdirto change to the "perforce" directory...