February 23, 2009

Using p4 user -o and -i to automate creating new users

Traceability
Integration
What's New

For smaller sites, this may not be a big deal, but if you are the admin for a busy site, and tasked with creating new user accounts, there is an easier way to do it than just running p4 user -f username and updating the email address manually. You can create a script to do the following (This example use batch file terminology):

set P4CLIENT=company.com p4 user -o -f %1 > temp.txt p4 user -i -f < temp.txt del temp.txt

Just save the commands above as a batch file, and run it with the new user name as a parameter. You can also set the user's password by adding p4 passwd %1 to the end of the script, and just entering the password when prompted. I even automated setting the password with simple Python script to answer the p4 passwd prompts. You can go beyond this simple batch file using a scripting language such as Python or Perl, and have it read in the user from a file rather than a parameter on the command line. Using this method, the script can loop through a list of users, and create them all for you automatically. You can also expand this to add the user to some default group(s) as well, but that takes more scripting with tools like Python or Perl. It is beyond basic batch commands since you have to add the user name to the user list in a group, but the concept of using -o and -i to update the group would be the same. The basic idea is:

p4 group -o groupname > group.txt modify group.txt to add in the user name p4 group -i < group.txt

Note: Setting P4CLIENT to your company's domain name is a trick to get the email address set correctly automatically. It works as long as the Perforce user name matches the person's email user name because Perforce defaults the domain name to be the P4CLIENT setting.