#!c:/perl/bin/perl -w ############################################################################## # p4mx.pl - Perforce Metrics ############################################################################## use Getopt::Std; use File::Basename; ############################################################################## # Usage ############################################################################## sub Usage { print <'', ); # Parse command line switches getopts("hvm:", \%opts); # Display script version if need be if (exists($opts{"v"})) { Version(); exit 0; } # Display script usage if need be if (exists($opts{"h"})) { Usage(); exit 0; } # Check for required switches %req_opts=(m=>' '); foreach $req_opt (sort keys %req_opts) { if (!(exists($opts{$req_opt}))) { Error(1, "missing required \"-$req_opt\" switch."); } } # Process required switches if (exists($opts{'m'})) { $metric=$opts{'m'}; } if (!(exists($knownMetrics{$metric}))) { Error(1, "unknown metric type \"$metric\"."); } $depotPath=$ARGV[0]; if ($depotPath eq "") { Error(1, "need depot path."); } } ############################################################################## # GetFiles ############################################################################## sub GetFiles { my $action=""; my $cmd=""; my $ext=""; my $fn=""; my $ftype=""; my $nf=0; my $ng=0; my $null=""; my @F=(); my @G=(); $cmd="$p4 fstat $depotPath"; open(CMD, "$cmd |") || die "can't open command \"$cmd\" for piped output"; while() { chomp; if (/^$/) { $fn=""; $ftype=""; $ext=""; } if (/^\.\.\. /) { @F=split; $nf=@F; if ($F[1] eq "depotFile") { ($fn, $null)=split(/#/, substr($_, 14)); @G=split(/\./, $fn); $ng=@G; if ($ng==1) { $ext=""; } else { $ext=$G[$ng-1]; } } if ($F[1] eq "headAction") { $action=$F[2]; } if ($F[1] eq "headType") { $ftype=$F[2]; if ($action ne "delete") { $files{$fn}=$ext; $nFiles++; if(!(exists($fileTypes{$ext}))) { $nFileTypes++; $fileTypes{$ext}=1; } else { $fileTypes{$ext}++; } $ftypeFiles{$fn}=$ftype; } } } } close(CMD) || die "can't close command \"$cmd\" for piped output"; } ############################################################################## # CalcLOT ############################################################################## sub CalcLOT { my $LOT=0; my $nLOT=0; printf("%5s %15s %15s\n", "Ext", "#", "LOT"); printf("%5s %15s %15s\n", "-"x5, "-"x15, "-"x15); foreach $fileType (sort keys %fileTypes) { $LOT=0; foreach $file (sort keys %files) { if ($files{$file} eq $fileType) { if ($ftypeFiles{$file}=~"text") { $cmd="$p4 print \"$file\""; open(CMD, "$cmd |") || die "can't open command \"$cmd\" for piped output"; while() { $LOT++; $nLOT++; } close(CMD) || die "can't close command \"$cmd\" for piped output"; } } } printf("%5s %15d %15d\n", $fileType, $fileTypes{$fileType}, $LOT); } printf("%5s %15s %15s\n", "-"x5, "-"x15, "-"x15); printf("%5s %15s %15s\n", $nFileTypes, $nFiles, $nLOT); } ############################################################################## # CollectMetrics ############################################################################## sub CollectMetrics { if ($metric eq "lot") { CalcLOT(); } } ############################################################################## # Process ############################################################################## sub Process { GetFiles(); CollectMetrics(); } ############################################################################## # MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN # ############################################################################## Init(); Process();