#! /usr/local/bin/perl
# addP4User.pl
########################################################################
########################################################################
# ADD NEW PERFORCE USER
#
# Programmed by David Weintraub
# Date: 13-Dec-2005
# Purpose: To help setup a new Windows User in one easy step
#
########################################################################
########################################################################
# PRAGMAS
#
use lib qw(/home/merbld/etc/perl);
use strict;
use warnings;
use vars;
#
########################################################################
########################################################################
# MODULES
#
use Config;
use Getopt::Long;
use FormInfo;
use File::Basename;
#
########################################################################
########################################################################
# CONSTANTS
#
*P4_SOLARIS = \"/home/merbld/bin/sparc-sunos/p4";
our $P4_SOLARIS;
*P4_LINUX = \"/home/merbld/bin/i686-linux/p4";
our $P4_LINUX;
*MAIL_LINUX=\"/bin/mail";
our $MAIL_LINUX;
*MAIL_SOLARIS=\"/usr/bin/mailx";
our $MAIL_SOLARIS;
if ($Config{"osname"} eq "linux") {
*P4 = \"$P4_LINUX";
*MAIL_CMD = \"$MAIL_LINUX"
} else {
*P4 = \"$P4_SOLARIS";
*MAIL_CMD = \"$MAIL_SOLARIS";
}
our $P4;
our $MAIL_CMD;
*CLEAR_CMD = \"clear";
our $CLEAR_CMD;
*P4_SERVER = \"merlin-perforce:1666";
our $P4_SERVER;
*P4_GROUPS_CMD = \"$P4 groups";
our $P4_GROUPS_CMD;
*P4_GROUP_CMD = \"$P4 group";
our $P4_GROUP_CMD;
*P4_USER_CMD = \"$P4 user";
our $P4_USER_CMD;
*P4_CLIENT_CMD = \"$P4 client";
our $P4_CLIENT_CMD;
*BOFA_DOMAIN = \"bofasecurities.com";
our $BOFA_DOMAIN;
*TEMP_FILE = \"addUser.temp.$$";
our $TEMP_FILE;
*SUBJECT = \"Perforce and You. Perfect Together";
our $SUBJECT;
*MAIL_CC = \("David.Weintraub\@bofasecurities.com " .
"Anthony.C.Pagliazzo\@bofasecurities.com");
our $MAIL_CC;
#
########################################################################
########################################################################
# USAGE
#
my $USAGE=<<USAGE;
Usage: @{[basename($0)]} [-user <userId>] [-name <userName>] [-email <email>]
Where:
<userId>: User's Perforce ID (Should be Bank's NBK ID)
<name>: Full Name of User
<email>: User's Email Address
USAGE
#
########################################################################
########################################################################
# VARIABLES
#
my $nbkId;
my $userName;
my $emailAddress;
my $p4Group;
my $p4WorkDir;
my $p4Client;
my $p4;
my $help;
#
########################################################################
########################################################################
# GET COMMAND LINE OPTIONS
#
GetOptions (
"user=s" => \$nbkId,
"name=s" => \$userName,
"email=s" => \$emailAddress,
"help" => \$help
) or die "$USAGE\n";
if ($help) {
print "$USAGE\n";
exit 0;
}
########################################################################
# GET NEEDED INPUTS
#
for (;;) {
unless ($nbkId) {
print "User's NBK ID: ";
chomp ($nbkId = <STDIN>);
}
unless ($userName) {
print "User's Name: ";
chomp ($userName = <STDIN>);
}
unless ($emailAddress) {
print "Email Address: ";
chomp ($emailAddress = <STDIN>);
unless ($emailAddress) {
($emailAddress = $userName) =~ s/ /./g;
}
}
$emailAddress .= "\@" . "$BOFA_DOMAIN" if ($emailAddress !~ /\@/);
$p4Group = "";
while (not ($p4Group)) {
system("$CLEAR_CMD");
print "Which Group Does the User Belong to?\n";
open(GROUPS, "$P4_GROUPS_CMD|") or
die qq(Couldn't execute command "$P4_GROUPS_CMD"\n);
my $groupNum;
my @groupList;
$groupNum = 1;
while (my $group = <GROUPS>) {
chomp ($group);
print "\t$groupNum). $group\n";
$groupList[$groupNum] = "$group";
$groupNum++;
}
print "\nSELECTION: ";
chomp (my $answer = <STDIN>);
next if (not defined($groupList[$answer]));
$p4Group = $groupList[$answer];
}
#
########################################################################
########################################################################
# VERIFY ANSWERS
#
my $answer;
for (;;) {
system ($CLEAR_CMD);
print "Is the following Correct?\n";
print qq(\tUser's NBK ID = "$nbkId"\n);
print qq(\tUser's Name = "$userName"\n);
print qq(\tUser's Email = "$emailAddress"\n);
print qq(\tUser's Group = "$p4Group"\n);
print "\nCorrect? ";
chomp ($answer = <STDIN>);
last if ($answer =~ /^(y|n)/i);
}
last if ($answer =~ /^y/i);
undef $nbkId;
undef $userName;
undef $emailAddress;
}
#
########################################################################
########################################################################
# CREATE PASSWORD
#
my $password = sprintf("%s-%x", $nbkId, time);
#
########################################################################
########################################################################
# CREATE PERFORCE USER
#
{
my %userHash;
$userHash{"User"} = $nbkId;
$userHash{"Email"} = $emailAddress;
$userHash{"FullName"} = $userName;
formWrite("user -f", \%userHash);
my $error = system(qq(p4 passwd -P $password $nbkId));
if ($error) {
print "WARNING: Password is not set!\n";
}
}
#
########################################################################
########################################################################
# ADD USER TO GROUP
#
{
my %groupHash;
formRead("group", "$p4Group", \%groupHash);
$groupHash{"Users"} .="\n" . "$nbkId";
formWrite("group", \%groupHash);
}
#
########################################################################
########################################################################
# CREATE PERFORCE CLIENT
#
{
my (%clientHash, $description, $altRoot, $view);
if ($p4Group eq "unix") {
$p4Client = "$nbkId-unix";
$description = "Default Main Branch Client Unix View for";
$p4WorkDir = "/home/$nbkId/p4clients/unix";
$altRoot = 'U:\p4clients\unix';
$view =
"//depot/Efp/Unix/Apps/*/MAIN/... //$p4Client/apps/*/...\n" .
"//depot/Efp/Unix/Libs/*/MAIN/... //$p4Client/libs/*/...";
} elsif ($p4Group eq "statarb") {
$p4Client = "$nbkId-statarb";
$description = "Default Main Branch StatArb View for";
$p4WorkDir = 'C:\p4clients\statarb';
$view =
"//depot/Statarb/MAIN/... //$p4Client/...";
} elsif ($p4Group eq "FASL") {
$p4Client = "$nbkId-fasl";
$description = "Default ROOT Branch FASL View for";
$p4WorkDir = 'C:\p4clients\fasl';
$view =
"//depot/Efp/Fasl/... //$p4Client/...";
} else { #Group is Efp Main Group
$p4Client = "$nbkId-default";
$description = "Default Efp Root Client View";
$p4WorkDir = 'C:\p4clients\default';
$view =
"//depot/*/Assembly/... //$p4Client/*/Assembly/...\n" .
"//depot/*/Tools/... //$p4Client/*/Tools/...\n" .
"//depot/*/Components/... //$p4Client/*/Components/...\n" .
"//depot/DevelopmentTools/... //$p4Client/DevelopmentTools/...";
}
$clientHash{"Client"} = $p4Client;
$clientHash{"Owner"} = $nbkId;
$clientHash{"Description"} = $description;
$clientHash{"Description"} .= "\n" . "for $userName ($nbkId)";
$clientHash{"Root"} = $p4WorkDir;
$clientHash{"AltRoots"} = $altRoot if ($altRoot);
$clientHash{"View"} = $view;
formWrite("client", \%clientHash);
}
#
########################################################################
########################################################################
# EMAIL USER INSTRUCTIONS ABOUT SETTING THEIR P4PORT
#
open (MAIL, qq(|$MAIL_CMD -c "$MAIL_CC" -s "$SUBJECT" $emailAddress)) or
die qq(Can't mail user "$emailAddress"\n);
print MAIL <<MAIL;
$userName:
Congratulations! This is your lucky day. You are now a Perforce user.
Here's some quick information you need to know. Complete directions
on setting up your Perforce client follow.
Your Perforce ID is "$nbkId".
Your Perforce client is called "$p4Client".
Your Working Directory is "$p4WorkDir".
Your Perforce Password is "$password".
MAIL
if ($p4Group eq "unix") {
print MAIL <<MAIL;
INSTALLATION INSTRUCTIONS
=========================
In order to use Perforce, you need to do the following steps:
1). Please make sure you include the following directories in your
PATH:
/home/merbld/bin
/home/merbld/bin/sparc-sunos (On a Solaris machine only)
/home/merbld/bin/i686-linux (On a Linux machine only)
2). Add the following environment variable in your .profile:
P4CONFIG=.p4rc
export P4CONFIG
This environment variable tells Perforce to get the client
information it needs from this file. You should create the
".p4rc" file in your HOME directory to look like this:
P4PORT=$P4_SERVER
P4USER=$nbkId
This way, as long as you are in your HOME directory or any
subdirectory under your HOME directory, you will be connected
to the correct Perforce server.
3). You will need to create your working directory. To create
your working directory, use the following command:
\$ mkdir -p $p4WorkDir
4). You will need to create another .p4rc file in this directory
with the following lines:
P4PORT=$P4_SERVER
P4USER=$nbkId
P4CLIENT=$p4Client
5). Your Perforce password is "$password". You will probably
want to change it to something a little easier to remember.
To do this, make sure you are either in your HOME directory,
or some directory inside your HOME directory and type the
following command:
\$ p4 passwd
STARTING PERFORCE
=================
Once you follow these steps, your Perforce client will be set up.
To sync your working directory with the latest version of the software,
run the following commands:
\$ cd $p4WorkDir
\$ p4 sync
Once your client has finished being sync'd to the Perforce server,
you're all ready to work.
USING PERFORCE ON YOUR PC
=========================
Your Perforce Unix client was setup so that you can also use Perforce
on your PC. To do this, you will need to install Perforce which is
available from:
<\\\\crprdndfs01\\DS_DFS\\Equities\\Global Equities Technology\\ED_APPS\\Merlin\\Perforce\\Perforce Windows Client Installer.exe>
Directions can be found at:
<\\\\crprdndfs01\\DS_DFS\\Equities\\Global Equities Technology\\ED_APPS\\Merlin\\Perforce\\README.txt>
Your Perforce client was setup, assuming your Unix HOME directory is
mapped to your "U:" drive. If it isn't, run the following command from
a Windows Console Window:
\$ net use /PERSISTENT
\$ net use U: \\\\njefpdcs1\\$nbkId
You can also use the Cygwin client on your PC if you have Cygwin installed.
The Cygwin client is only available from the command line. This is
available from Perforce at:
<http://perforce.com/perforce/downloads/cygwinx86.html>
PERFORCE BUILT IN HELP
======================
Perforce has built in help by simply typing:
\$ p4 help
at the command line. To get a list of the basic commands, do:
\$ p4 help simple
To get a listing of all the commands, type:
\$ p4 help commands
To get help on a specific command, type:
\$ p4 help <command>
where <command> is the name of the Perforce command.
The documentation for Perforce is located at:
<\\\\crprdndfs01\\ds_dfs\\Equities\\Global Equities Technology\\ED_APPS\\Merlin\\Perforce\\Documentation\\Perforce User's Guide.pdf>
More information about Perforce can be found on our Wiki:
<http://scsefpldcs0110:8080/twiki/bin/view/Know/PerforceRevisionControl>
This also includes a CVS vs. Subversion vs. Perforce command chart:
<http://scsefpldcs0110:8080/twiki/bin/view/Know/PerforceRevisionControl#Perforce_commands_vs_CVS_and_Sub>
MAIL
} else {
print MAIL <<MAIL;
In order to use Perforce on your PC, you will need to do a few minor
tasks on your local PC:
1). Install Perforce Windows Client on your PC. You can find this
at:
<\\\\crprdndfs01\\DS_DFS\\Equities\\Global Equities Technology\\ED_APPS\\Merlin\\Perforce\\Perforce Windows Client Installer.exe>
Installation instructions are found in
<\\\\crprdndfs01\\DS_DFS\\Equities\\Global Equities Technology\\ED_APPS\\Merlin\\Perforce\\README.txt>
2). After you've installed, Perforce on your PC, you will need to
create a directory called "$p4WorkDir". This will
be your working directory where Perforce will download the files
from its depot for you to work on.
3). You will also need to know the name of your default Perforce Client
which you'll need when you sign into P4V. Your default Perforce
Client is:
"$p4Client".
4). You will need to add a few entries into your Windows Registry file.
Copy the lines that appear between the rows of equal signs (but
not including the row of equal signs), and paste them into a file.
============================================================
REGEDIT4
[HKEY_CURRENT_USER\\Software\\perforce\\environment]
"P4PORT"="$P4_SERVER"
"P4CONFIG"="_p4rc"
"P4CLIENT"="$p4Client"
============================================================
Call this file "perforce.reg" and place this file on your desktop.
Now, double click on this file. This should set the needed Perforce
registry entries for you.
5). Finally, you need to know your password. Your randomly assigned
password is "$password". You can change your password to something
a bit easier to remember by going into P4V and selecting
"Change Password" from the "Connection" menu.
6). If you are using VisualStudio, and want to integrate Perforce into
VisualStudio, you will need to read the following step-by-step
instructions:
<\\\\crprdndfs01\\DS_DFS\\Equities\\Global Equities Technology\\ED_APPS\\Merlin\\Perforce\\Documentation\\Perforce-VisualStudio Integration.pdf">
and
<\\\\crprdndfs01\\DS_DFS\\Equities\\Global Equities Technology\\ED_APPS\\Merlin\\Perforce\\Documentation\\EFP Perforce Documentation\\Mapping a Perforce Client into VisualStudio.doc>
MAIL
}
print MAIL <<MAIL;
And, if you need further assistance, contact your Perforce Administrator.
--
Your Friendly Perforce Administrator
MAIL
unless (close MAIL) {
die qq(Mail message was not sent: $!\n);
}
#
########################################################################