Removed /usr/local from CDPATH
[clearscm.git] / cc / log_activity
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         log_activity,v
5 # Revision:     1.1.1.1
6 # Description:  Logs Clearcase activity
7 # Author:       Andrew@DeFaria.com
8 # Created:      Tue Dec 27 16:33:30 PST 2005
9 # Modified:     2007/05/17 07:45:48
10 # Language:     perl
11 #
12 # (c) Copyright 2000-2005, Andrew@DeFaria.com, all rights reserved.
13 #
14 ################################################################################
15 use strict;
16 use warnings;
17 use File::Spec;
18
19 my $me;
20
21 BEGIN {
22   # Set $lib_path
23   my $lib_path = $^O =~ /MSWin/ ? "\\\\brcm-irv\\dfs\\projects\\ccase\\SCM\\lib"
24                                 : "/projects/ccase/SCM/lib";
25
26   # Extract relative path and basename from script name.
27   $0 =~ /(.*)[\/\\](.*)/;
28
29   my $abs_path  = (!defined $1) ? "." : File::Spec->rel2abs ($1);
30   $me           = (!defined $2) ? $0  : $2;
31   $me           =~ s/\.pl$//;
32
33   # Add the appropriate path to our modules to @INC array.
34   unshift @INC, "$lib_path";
35   unshift @INC, $ENV {SITE_PERL_LIBPATH} if defined $ENV {SITE_PERL_LIBPATH};
36   unshift @INC, "$abs_path";
37 } # BEGIN
38
39 use OSDep;
40 use Display;
41 use DateUtils;
42 use Logger;
43 use Clearcase;
44 use Clearcase::Vobs;
45 use Clearcase::View;
46
47 # The lshistory command needs a view context. We'll create this view
48 # if necessary.
49 my $tag = $ENV {DEFAULT_VIEW} ? $ENV {DEFAULT_VIEW} : "default";
50
51 # Path to logs directory
52 my $logdir = "$scm_base$/logs";
53
54 error "Logdir $logdir does not exist - $!", 1 if !-d $logdir;
55
56 sub Usage {
57   my $msg = shift;
58
59   display "ERROR: $msg\n" if defined $msg;
60
61   display "Usage: $me\t[-u] [-v] [-d] [-n <# of days>]
62
63 Where:
64
65   -u:           Display usage
66   -v:           Turn on verbose mode
67   -d:           Turn on debug mode
68   -n:           Number of days to report (Default: 1)
69
70 Note: Number of days is relative to midnight. Output is to a logfile named
71 activity.<date>.log. Since we want <date> to be accurate this script attempts
72 to have each log file have only that days activity. Therefore -n 1 will report
73 everything in the last full 24 hour day, -n2 will be the last two full 24 hour
74 days, etc.
75 ";
76   exit 1;
77 } # Usage
78
79 my $today = time;
80
81 sub ReportActivity {
82   my $view      = shift;
83   my $since     = shift;
84
85   my $cc = Clearcase->new;
86   # This is Unix only!
87   my $cmd = "$Clearcase::cleartool setview -exec \"$Clearcase::cleartool lshistory -since $since -avobs -fmt '%Nd;%Fu;%u@%h;%e;%n\\n'\" $view";
88
89   open OUTPUT, "$cmd|"
90     or error "Unable to open pipe for $cmd", 1;
91
92   my $today_ymd = YMD;
93   my $date      = YMD;
94   my $logfile;
95
96   while (<OUTPUT>) {
97     # Split the line into fields. The first field is date and time
98     my @fields  = split /;/;
99
100     # Now split the first field by "." which separates the date and time
101     @fields     = split /\./, $fields [0];
102
103     # Never report today's activity because today's never over!
104     # Reporting on today may give a partial result
105     next if $fields [0] eq $today_ymd;
106
107     # Ugh - might have stuff that's future dated!
108     next if $fields [0] gt $today_ymd;
109
110     # Skip noise. In this case noise is activity to the perftest
111     # vob. No normal activity happens to perftest - just performance
112     # testing.
113     next if /${Clearcase::vobtag_prefix}perftest/;
114
115     if ($fields [0] lt $date) {
116       $date = $fields [0];
117       my $log_filename = $cc->sitename . ".activity.$date";
118       verbose "Starting logfile $log_filename";
119       $logfile = undef;
120       $logfile = Logger->new (name => $log_filename, path => $logdir);
121     } # if
122
123     chomp;
124     $logfile->log ($_);
125   } # while
126
127   close OUTPUT;
128 } # ReportActivity
129
130 my $nbr_days = 1;
131
132 while ($ARGV [0]) {
133   if ($ARGV [0] eq "-v") {
134     Display::set_verbose;
135   } elsif ($ARGV [0] eq "-d") {
136     set_debug;
137   } elsif ($ARGV [0] eq "-n") {
138     shift @ARGV;
139     if ($ARGV [0]) {
140       $nbr_days = $ARGV [0];
141     } else {
142       Usage "Need to specify nbr_days after -n";
143     } # if
144   } elsif ($ARGV [0] eq "-u") {
145     Usage;
146   } else {
147     Usage "Invalid argument: $ARGV [0]";
148   } # if
149
150   shift (@ARGV);
151 } # while
152
153 my $date = YMD;
154
155 verbose "Creating view $tag";
156 my $view = Clearcase::View->new (tag => $tag);
157 $view->create;
158
159 verbose "Mounting all vobs";
160 my $vobs = Clearcase::Vobs->new;
161 $vobs->mount;
162
163 # Compute $since
164 my $seconds_in_day = 60 * 60 * 24;
165 my $since = YMD ($today - ($nbr_days * $seconds_in_day));
166
167 verbose "Producing report";
168 ReportActivity $tag, $since;