#!/usr/bin/perl # # # # Description: # # This script will process two usernames. It will change the owner of changelists, # labels and workspaces from one user to the other. You should create the new user # by hand and make sure they are in the same access groups as the old one # # Copyright 2009 Lucasfilm Entertainment Company Ltd. ############################################## # # Start editing variables here! # # commandline tools and paths. my $p4_bin = "/usr/bin/p4"; # path to p4 executable my $p4_port = "perforce:1666"; # p4 port for P4Auth server my $p4_user = "[CHANGE_THIS]"; # admin user that will add/manage users and groups my $p4 = "$p4_bin -p $p4_port -u $p4_user"; # use login ticket for P4PASSWD my $p4_job_username_field = "user"; # field in job that holds username my $old_name = "[CHANGE_THIS]"; # old username my $new_name = "[CHANGE_THIS]"; # new username # # Stop editing variables here! # ############################################## # # let's start with the clients # ############################################## @client_listing = `$p4 clients -u $old_name`; # check if there are any clients in the list if ($#client_listing lt 0 ) { print "Looks like this user has no clients, skipping ...\n" } else { # strip out the gunk then do some magic foreach (@client_listing) { $_ =~ s/(.*? )(.*?)( .*)/$2/; # isolate the client name # dump out the client spec @current_client_spec = `$p4 client -o $_` or die "Can't dump client spec\n"; # we need to change the name in the spec foreach (@current_client_spec) { $_ =~ s/(^Owner: *)(.*)$/$1\t$new_name/; } # end foreach # p4 expects clientspec definition input to be coming from a local file, so let's create that open (TEMPFILE, ">client.tmp") or die ("Problem with opening $input: $!"); print TEMPFILE @current_client_spec; close (TEMPFILE); # update the clientspec with the new owner @client_edit_results = `$p4 client -f -i \< client.tmp` or die "Can't update client.\n"; print @client_edit_results; } # end foreach } # end else # # now let's do the labels # ############################################## @labels_listing = `$p4 labels -u $old_name`; # check if there are any labels in the list if ($#labels_listing lt 0 ) { print "Looks like this user has no labels, skipping ...\n" } else { # strip out the gunk then do some magic foreach (@labels_listing) { $_ =~ s/(.*? )(.*?)( .*)/$2/; # isolate the label name # dump out the label spec @current_label_spec = `$p4 label -o $_` or die "Can't dump label spec\n"; # we need to change the name in the spec foreach (@current_label_spec) { $_ =~ s/(^Owner: *)(.*)$/$1\t$new_name/; } # end foreach # p4 expects label definition input to be coming from a local file, so let's create that open (TEMPFILE, ">label.tmp") or die ("Problem with opening $input: $!"); print TEMPFILE @current_label_spec; close (TEMPFILE); # update the labelspec with the new owner @label_edit_results = `$p4 label -f -i \< label.tmp` or die "Can't update label.\n"; print @label_edit_results; } # end foreach } # end else # # almost forgot the branch specs! # ############################################## @branches_listing = `$p4 branches -u $old_name`; # check if there are any branches in the list if ($#branches_listing lt 0 ) { print "Looks like this user has no branches, skipping ...\n" } else { # strip out the gunk then do some magic foreach (@branches_listing) { $_ =~ s/(.*? )(.*?)( .*)/$2/; # isolate the branch name # dump out the label spec @current_branch_spec = `$p4 branch -o $_` or die "Can't dump branch spec\n"; # we need to change the name in the spec foreach (@current_branch_spec) { $_ =~ s/(^Owner: *)(.*)$/$1\t$new_name/; } # end foreach # p4 expects branch definition input to be coming from a local file, so let's create that open (TEMPFILE, ">branch.tmp") or die ("Problem with opening $input: $!"); print TEMPFILE @current_branch_spec; close (TEMPFILE); # update the branchspec with the new owner @branch_edit_results = `$p4 branch -f -i \< branch.tmp` or die "Can't update branch.\n"; print @branch_edit_results; } # end foreach } # end else # # here go the jobs ... # ############################################## @jobs_listing = `$p4 jobs -e $p4_job_username_field=$old_name`; # check if there are any jobs in the list if ($#jobs_listing lt 0 ) { print "Looks like this user has no jobs, skipping ...\n" } else { # strip out the gunk then do some magic foreach (@jobs_listing) { $_ =~ s/(.*? )(.*?)( .*)/$1/; # isolate the job name # dump out the job spec @current_job_spec = `$p4 job -o $_` or die "Can't dump job spec\n"; # we need to change the name in the spec foreach (@current_job_spec) { $_ =~ s/(^$p4_job_username_field: *)(.*)$/$1\t$new_name/; } # end foreach # p4 expects job definition input to be coming from a local file, so let's create that open (TEMPFILE, ">job.tmp") or die ("Problem with opening $input: $!"); print TEMPFILE @current_job_spec; close (TEMPFILE); # update the branchspec with the new owner @job_edit_results = `$p4 job -f -i \< job.tmp` or die "Can't update job.\n"; print @job_edit_results; } # end foreach } # end else # # finally, how about changelists? # ############################################## @changes_listing = `$p4 changes -u $old_name`; # check if there are any changes in the list if ($#changes_listing lt 0 ) { print "Looks like this user has no changes, skipping ...\n" } else { # strip out the gunk then do some magic foreach (@changes_listing) { $_ =~ s/(.*? )(.*?)( .*)/$2/; # isolate the branch name # dump out the change spec @current_change_spec = `$p4 change -o $_` or die "Can't dump change spec\n"; # we need to change the name in the spec foreach (@current_change_spec) { $_ =~ s/(^User: *)(.*)$/$1\t$new_name/; } # end foreach # p4 expects change definition input to be coming from a local file, so let's create that open (TEMPFILE, ">change.tmp") or die ("Problem with opening $input: $!"); print TEMPFILE @current_change_spec; close (TEMPFILE); # update the branchspec with the new owner @change_edit_results = `$p4 change -f -i \< change.tmp` or die "Can't update change.\n"; print @change_edit_results; } # end foreach } # end else