Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

How to login in perforce(p4 command line) in python

Posted on July 7, 2021July 7, 2021 by admin

To login into p4 programmatically, You may write a script something like the below which takes username and password. It is done in two steps.

Step 1: Set environment variables. (create entries in ~/.bashrc or ~/.zshrc or equivalent and source it)

export P4USER=myuser
export P4PORT=ssl:my.server.perforce.com:31211

source ~/.bashrc or ~/.zshrc

Step 2: Trust the machine using ‘p4 trust’ and try to login

There are two functions in the below code. One is for running the OS command and another is to perform the login. In the script, We first check that is a trust established between machine and server? then further we try to login.

import os
import sys
import subprocess

#Utility function to run the OS commands
def execute_shell_command(commands, raise_if_error=True, capture_output=True):
    print('Running Command: %s' % ' '.join(commands))

    if capture_output:
        p = subprocess.Popen(commands, cwd=None, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
        out = p.stdout.read().strip().decode('utf-8')
        err = p.stderr.read().strip().decode('utf-8')
    else:
        p = subprocess.Popen(commands)

    p.communicate()
    exit_status = p.returncode

    print('Output: Exit Status  : %s' % exit_status)

    if exit_status != 0 and raise_if_error:
        if capture_output:
            raise Exception('Error in running os command: %s. Exit Code: %d, Stdout: `%s`, Stderr: `%s`' % (' '.join(commands), exit_status, out, err))
        else:
            raise Exception('Error in running os command: %s. Exit Code: %d' % (' '.join(commands), exit_status))

    return exit_status

#Function to perform perforce login 
def p4_login(ticket, attempt_login=True):
    #First check trust is established between P4 server and the machine.
    if ticket is not None:
        exitStatus = execute_shell_command(['p4', 'trust', '-y', '-f'], raise_if_error=False)
        if exitStatus != 0:
            sys.exit('Error: p4 trust has been failed. Please check perforce documentation')

        exitStatus = execute_shell_command(['p4', '-P', ticket, 'monitor', 'show'], raise_if_error=False)
        if exitStatus != 0:
            sys.exit('Error: Ticket may be expired. please check.')
    else:
        exitStatus = execute_shell_command(['p4', 'login', '-s'], raise_if_error=False)
        if exitStatus != 0:
            if attempt_login:
                print('Please enter your Password:')
                execute_shell_command(['p4', 'login'], capture_output=False)
                p4_login(ticket, attempt_login=False)
            else:
                sys.exit('Error: You have not logged into Perforce successfully. Please check perforce documentation')

#Now use it something like this
p4_ticket = os.environ.get('P4_TICKET')
p4_login(p4_ticket)

To use the script, save the above script in some file say ‘p4_login.py’, and then run the following command in the terminal.

source ~/.bashrc or ~/.zshrc 
python3 p4_login.py

  • p4
  • p4 login
  • perforce
  • python
  • python3
  • ©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme