Added client work scripts
[clearscm.git] / clients / Ameriquest / triggers / CommentSQLCode.pl
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         CommentSQLCode.pl
5 # Description:  This trigger script will gather certain information and write
6 #               that information into the element being checked in in the form
7 #               of a comment.
8 #
9 #               Here are the requirements as I understand them for the
10 #               trigger that Steve Lipson wants for the SQL
11 #               checkins. Basically he desires a trigger that will
12 #               capture the checkin comment and other information and
13 #               insert that information in the form of a comment at
14 #               the top of the checked in element. This trigger will:
15 #
16 #                       * Be a postop trigger for the checkin action
17 #                       * Not be an all element trigger rather it will
18 #                       be attached to certain file elements in the
19 #                       vob
20 #                       * Be made for the <fill in vob name here> vob
21 #                       * Only work on file elements - directory
22 #                       elements are to be skipped
23 #                       * Only work on file elements that have an
24 #                       extension of .sql - other elements will be
25 #                       skipped
26 #
27 # Author:       Andrew@DeFaria.com
28 # Created:      Mon Jul 19 10:54:01 PDT 2004
29 # Language:     Perl
30 # Modifications:Wed Aug  4 12:41:47 PDT 2004
31 #
32 # (c) Copyright 2004, Andrew@DeFaria.com, all rights reserved
33 #
34 ################################################################################
35 use strict;
36 use warnings;
37 use File::Spec;
38
39 # This will be set in the BEGIN block but by putting them here the become
40 # available for the whole script.
41 my (
42   $abs_path,
43   $lib_path,
44   $log_path,
45   $me,
46   $triggers_path
47 );
48
49 BEGIN {
50   # Extract relative path and basename from script name.
51   $0 =~ /(.*)[\/\\](.*)/;
52
53   $abs_path     = (!defined $1) ? "." : File::Spec->rel2abs ($1);
54   $me           = (!defined $2) ? $0  : $2;
55
56   # Setup paths
57   $lib_path             = "$abs_path/../lib";
58   $log_path             = "$abs_path/../log";
59   $triggers_path        = "$abs_path/../triggers";
60
61   # Add the appropriate path to our modules to @INC array.
62   unshift (@INC, "$lib_path");
63 } # BEGIN
64
65 use TriggerUtils;
66
67 sub getCurrentTime {
68   my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time);
69   $mon++;
70   $year += 1900;
71   $hour = "0" . $hour if $hour < 10;
72   $min  = "0" . $min  if $min  < 10;
73   return "$mon/$mday/$year\@$hour:$min";
74 } # getCurrentTime
75
76 sub parseLSActivity {
77   my ($activity_id, $activity_title, $activity_owner);
78
79   my @output = `cleartool lsactivity -cact -long`;
80
81   if ($? ne 0 || $#output eq -1) {
82     clearmsg "You are not set to an activity!";
83     exit 1;
84   } # if
85
86   foreach (@output) {
87     if (/^activity \"(\S*)\"/) {
88       $activity_id = $1;
89       next;
90     } elsif (/owner: AMERIQUEST\\(\S*)/) {
91       $activity_owner = $1;
92       next;
93     } elsif (/title: (.*)/) {
94       $_ = $1;
95       chomp; chop if /\r/;
96       $activity_title = $_;
97       next;
98     } # if
99   } # foreach
100
101   return ($activity_id, $activity_owner, $activity_title);
102 } # parseLSActivity
103
104 # Get name of element and its type
105 my $pname        = $ENV{CLEARCASE_PN};
106 my $element_type = $ENV{CLEARCASE_ELTYPE};
107
108 # Skip directories and elements that aren't .sql
109 exit if $element_type =~ /directory/i || $pname !~ /\.sql$/i;
110
111 # Get comment and user
112 my $comment   = $ENV{CLEARCASE_COMMENT};
113 my $userid    = $ENV{CLEARCASE_USER};
114
115 # Format timestamp
116 my $timestamp = getCurrentTime;
117
118 # Parse output of lsactivity -cact -long
119 my ($activity_id, $activity_owner, $activity_title) = parseLSActivity;
120
121 # Open up $pname for reading and $pname.trig for writting
122 open PNAME_IN, $pname
123   or clearlogmsg "Unable to open $pname for reading - $!\n", exit 1;
124
125 open PNAME_OUT, ">$pname.trig"
126   or clearlogmsg "Unable to open $pname.trig for writing - $!\n", exit 1;
127
128 # Add comment to top of file
129 my $activity_str        = "$activity_id: $activity_title";
130 my $owner_str           = $activity_owner =~ /\$userid/i ? "$activity_owner ($userid)" : "$activity_owner";
131
132 print PNAME_OUT <<END;
133 -- Date:\t$timestamp
134 -- Activity:\t$activity_str
135 -- Owner:\t$owner_str
136 -- Comment:\t$comment
137 --------------------------------------------------------------------------------\n
138 END
139
140 # Append $pname
141 while (<PNAME_IN>) {
142   print PNAME_OUT $_;
143 } # while
144
145 close PNAME_IN;
146 close PNAME_OUT;
147
148 # Switch $pname.trig -> $pname
149 rename "$pname.trig", $pname
150   or clearlogmsg "Internal error - Unable to rename $pname.trig to $pname", exit 1;
151
152 # Allow checkin to proceed
153 exit 0;