#!/usr/bin/perl ######################################################################### # # trig_eviltwin.pl # # Description: # This trigger is invoked when creating new elements. # This trigger looks to see if the element already exists on another branch # of the directory, and if so, refuses to create (the duplicate) element. # # This trigger also has a self install feature. # ######################################################################### use strict; my $TRIGGER_NAME = "trig_eviltwin"; my $WINPERL = "c:/program files/rational/clearcase/bin/ccperl"; my $UNIX_TRIG_EXE = "/net/MACHINE/PATH/trig_eviltwin.pl"; my $WIN_TRIG_EXE = "$WINPERL //MACHINE/PATH/trig_eviltwin.pl"; my $ADMIN_EMAIL = "clearcase_admins\@x.com"; my $EXCLUDED_USERS = "vobadmin"; my $OS = (eval{Win32::IsWinNT();},$@) ? "UNIX" : "NT"; my $ATRIAHOME; my $CT; if ($OS eq "NT") { $ATRIAHOME = $ENV{"ATRIA_HOME"} || 'c:/program files/rational/clearcase'; $CT = "$ATRIAHOME/bin/cleartool.exe"; (-x $CT) || die "Cannot find an executable cleartool"; $CT = "\"$CT\""; } else { $ATRIAHOME = "/opt/rational/clearcase"; $CT = "$ATRIAHOME/bin/cleartool"; (-x $CT) || die "Cannot find an executable cleartool"; } # subroutine determines the basename of the object sub basename { my ($pname) = @_; $_ = $pname; # The whole thing is the basename if there are no slashes return $pname unless /[\\\/]/; /(.*)[\\\/](.*)/; # $2 is the base name return $2; } my $me = &basename($0); my $usage = "Usage: $me [-i/nstall] [-r/eplace] [-v/ob vob-tag]\n" . " $me [-h]\n" . " $me\n"; my $install; my $replace; my $vob; while ($_ = shift(@ARGV)) { if (/^-i/) { $install = 1; next; } if (/^-r/) { $replace = "-replace"; next; } if (/^-v/) { $vob = shift(@ARGV); next; } if (/^-h/) { print($usage); exit 1; } } if ($install) { unless ($vob) { print ("Error: Vob not specified.\n"); exit 1; } # Check to see that it is a valid vob. my @voblist = `$CT lsvob -short $vob`; (my $vob_vf) = @voblist; chomp($vob_vf); if ($vob ne $vob_vf) { print ("Error: Invalid vob specified.\n"); exit 1; } # If the replace flag is not set, check to see if the trigger type # already exists on the vob. Fail if it already exists. if (! $replace) { my @triglist = `$CT lstype -short trtype:${TRIGGER_NAME}\@${vob}`; (my $trig) = @triglist; chomp($trig); if ($trig eq "${TRIGGER_NAME}") { print ("Error: Trigger already exists and -replace option not specified.\n"); exit 1; } } my $cmd = "$CT mktrtype " . " -c \"Evil Twin\" " . " -element -all ${replace} " . " -nusers ${EXCLUDED_USERS} " . " -preop mkelem,lnname " . " -execunix ${UNIX_TRIG_EXE} " . " -execwin \"${WIN_TRIG_EXE}\" " . " ${TRIGGER_NAME}\@${vob} "; my @response = `${cmd}`; if (grep(/Error:/,@response)) { print ("Error: Trigger creation failed.\n"); exit 1; } print ("OK: Trigger successfully installed.\n"); exit 0; } # For each element to be created as clearcase element my $ele_arg = $ENV{'CLEARCASE_PN'}; my @EL_ARRAY = split(/[\\\/]/, $ele_arg, 9999); # Get filename portion my $ele_nam = pop(@EL_ARRAY); my $ele_dir; # Get the directory where the element is to be created if ($OS eq "NT") { $ele_dir = join("\\", @EL_ARRAY); } else { $ele_dir = join("/", @EL_ARRAY); } my @ARRAY = `cleartool lsvtree -short $ele_dir`; my @brdir_array; # @brdir_array = chop; foreach $_ (@ARRAY) { if (!m/[\\\/][0-9]+$/) { chop($_); if (m/[\\\/]CHECKEDOUT$/){ my $ewco = $_; my @ewco_split = split(/[\\\/]/, $ewco); my $ewco_co = pop(@ewco_split); my $ewco_noco; if ($OS eq "NT") { $ewco_noco = join("\\", @ewco_split); } else { $ewco_noco = join("/", @ewco_split); } my @co_list = `cleartool ls -s $ewco_noco`; my @checkedout_versions = grep(/CHECKEDOUT/,@co_list); my $checkedout_version; foreach $checkedout_version (@checkedout_versions) { chomp($checkedout_version); $_ = $checkedout_version; push(@brdir_array, $_); } $_ = "$_" . "\n"; } else { $_ = "$_" . "\/LATEST\n"; } push(@brdir_array, $_); } } my $FOUND="0"; my $dirbranch; foreach $dirbranch (@brdir_array) { if ($dirbranch) { my $DIR=$dirbranch; chomp($DIR); my @ARRAY2 = `cleartool ls -short $DIR`; # CHECKEDOUT.71 foreach $_ (@ARRAY2) { if (m/${ele_nam}$/ || m/${ele_nam}\@\@$/) { $FOUND="1"; print(" ${ele_nam} already resides on branch $DIR\n"); } } } } if ($FOUND) { print("\nThis element was already found on another branch.\n"); print("Contact clearcase_admins\@x.com for assistance.\n\n"); exit(1); } else { print("Not found on any branch\n"); exit(0); }