-1

I want to make script to remove the terminated user from perforce but to do that first have to check whether user exists in perforce or not. I tried below command in python script but did not work.

p4.run("users"|grep user)

Is there any command in perforce or any way to use above command in my script.

Samwise
  • 68,105
  • 3
  • 30
  • 44
Darshan Jain
  • 5
  • 1
  • 5

3 Answers3

3

This line

p4.run("users"|grep user)

won't work. You seem to think that p4.run() executes an operating system command line that you can then pipe output to. That's not how p4.run() works. It is an API function that takes command strings that are similar to what you would type on a command line, but that is where the similarity ends. Try

myusers = p4.run("users")

and then work with what Perforce gives back to you in myusers.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
1

A couple of points. I don't know why your command failed, but guess you have not logged in (i.e. connect using p4.connect()). I have tried the following and found it working:

import getpass
import P4

def get_user(user):
    """
    Get information for a single user

    Return None if not found, or a dict similar to this one:
        {
            'Update': '1422308004',
            'Access': '1515088079',
            'User': 'abc', 
            'FullName': 'Anna Bell Clark',
            'Type': 'standard',
            'Email': 'abc@example.com'
        }
    """
    p4 = P4.P4()
    p4.password = getpass.getpass()
    p4.connect()
    try:
        users_info = p4.run('users', user)
        return users_info[0]
    except P4.P4Exception:
        return None


user = 'abc'
print(get_user(user))

Notes

  • use getpass.getpass() to prompt for the password from console. getpass does not echo the password, so it is reasonably safe to keep your password hidden.
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
0

Look at this sample P4Python code:

https://swarm.workshop.perforce.com/files/guest/robert_cowham/perforce/API/python/main/test/test_p4python.py#747

def _delete_user(self, new_user):
    user = self.p4.fetch_user(new_user)
    self.p4.save_user(user)
    self._change_user(config.p4_user)
    self.p4.login(config.p4_password)
    print self.p4.delete_user("-f", new_user)

This demonstrates fetching a user, logging in as that user, and deleting the user.

Samwise
  • 68,105
  • 3
  • 30
  • 44