#!c:/perl/bin/perl -w ############################################################################## # p4mx.pl - Perforce Metrics ############################################################################## use Getopt::Std; use File::Basename; ############################################################################## # Usage ############################################################################## sub Usage { print <<EOF; Usage: $scriptName [-h] [-v] -m {metrictype} {depotpath} -h Displays help -v Displays version -m Metric type: lot lines of text EOF } ############################################################################## # Version ############################################################################## sub Version { print <<EOF; $scriptName (v$scriptVersion) by $scriptAuthorName ($scriptAuthorEmail, $scriptAuthorExt) EOF } ############################################################################## # Error ############################################################################## sub Error { my ($flagUsage, $errorStr)=@_; print "\n$scriptName: Error: $errorStr\n"; if ($flagUsage==1) { Usage(); } exit(1); } ############################################################################## # Init ############################################################################## sub Init { my $req_opt=""; my $tmp=""; my @F=(); # Script Information $scriptVersion="1.0"; $scriptName=basename($0); $scriptAuthorName="Steve Smythe"; $scriptAuthorEmail="ssmythe\@docent.com"; $scriptAuthorExt="x9546"; # Init function variable %opts=(); # Script globals $nFiles=0; $nFileTypes=0; %files=(); %fileTypes=(); %ftypeFiles=(); $p4="p4"; $metric=""; $depotPath=""; # known metric types %knownMetrics= ( 'lot'=>'', ); # 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(<CMD>) { 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(<CMD>) { $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();
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 1891 | Steve Smythe |
Must quote filenames to print. :-) |
||
#1 | 1890 | Steve Smythe |
First version of a simple Perforce metric utility. Currently can count lines of text, skipping binary files. |