| 1 | #!/usr/bin/perl -w | 
|---|
| 2 |  | 
|---|
| 3 | # $Id: dasscm 195 2004-12-01 14:51:28Z joergs $ | 
|---|
| 4 |  | 
|---|
| 5 | use strict; | 
|---|
| 6 |  | 
|---|
| 7 | use Env qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USER $DASSCM_PW); | 
|---|
| 8 | use Cwd; | 
|---|
| 9 | use File::Basename; | 
|---|
| 10 | use File::stat; | 
|---|
| 11 | use File::Path; | 
|---|
| 12 | use File::Copy; | 
|---|
| 13 | #use POSIX qw/getpgrp tcgetpgrp/; | 
|---|
| 14 |  | 
|---|
| 15 | ##################################################################### | 
|---|
| 16 | # | 
|---|
| 17 | # global | 
|---|
| 18 | # | 
|---|
| 19 | my $SVN = "svn "; | 
|---|
| 20 | my $svnOptions = ""; | 
|---|
| 21 |  | 
|---|
| 22 | ##################################################################### | 
|---|
| 23 | # | 
|---|
| 24 | # util functions | 
|---|
| 25 | # | 
|---|
| 26 | sub usage() | 
|---|
| 27 | { | 
|---|
| 28 | print "usage: dasscm <subcommand> [options] [args]\n"; | 
|---|
| 29 | print "\n"; | 
|---|
| 30 | print "dasscm is intended to help versioning configuration files\n"; | 
|---|
| 31 | print "\n"; | 
|---|
| 32 | print "Available subcommands:\n"; | 
|---|
| 33 | print "   add <filename>\n"; | 
|---|
| 34 | print "   commit <filename>\n"; | 
|---|
| 35 | print "   diff <filename>\n"; | 
|---|
| 36 | print "   help <subcommand>\n"; | 
|---|
| 37 | print "\n"; | 
|---|
| 38 | print "preperation:\n"; | 
|---|
| 39 | print "check out the configuration repository, e.g.\n"; | 
|---|
| 40 | print "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/lvermgeo/technical/config/\n"; | 
|---|
| 41 | print "environment variables DASSCM_REPO, DASSCM_PROD and DASSCM_USER are evaluated.\n"; | 
|---|
| 42 | print "\n"; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | sub check_env() | 
|---|
| 46 | { | 
|---|
| 47 | # DASSCM_PROD | 
|---|
| 48 | if ( ! $DASSCM_PROD ) { | 
|---|
| 49 | $DASSCM_PROD = "/"; | 
|---|
| 50 | } | 
|---|
| 51 | print "DASSCM_PROD: ".$DASSCM_PROD."\n"; | 
|---|
| 52 | if ( ! -d $DASSCM_PROD ) { | 
|---|
| 53 | die "DASSCM_PROD is not set to a  directory.\n"; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | # DASSCM_REPO | 
|---|
| 57 | if ( ! $DASSCM_REPO ) { | 
|---|
| 58 | die "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n"; | 
|---|
| 59 | } | 
|---|
| 60 | print "DASSCM_REPO: ".$DASSCM_REPO."\n"; | 
|---|
| 61 | if ( ! -d $DASSCM_REPO ) { | 
|---|
| 62 | die "DASSCM_REPO must be is not set to the directory of the versioning system checkout for this machine.\n"; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | # User settings | 
|---|
| 66 | # user root is not allowed for checkins. | 
|---|
| 67 | # if user is root, DASSCM_USER has to be set, | 
|---|
| 68 | # otherwise USER can be used | 
|---|
| 69 | if ( "$USER" eq "root" ) { | 
|---|
| 70 | if ( ! $DASSCM_USER ) { | 
|---|
| 71 | die "Envirnonment variable DASSCM_USER not set.\nSet DASSCM_USER to your subversion user account.\n"; | 
|---|
| 72 | } | 
|---|
| 73 | $svnOptions .= " --no-auth-cache " | 
|---|
| 74 | } elsif ( ! $DASSCM_USER ) { | 
|---|
| 75 | $DASSCM_USER=$USER; | 
|---|
| 76 | } | 
|---|
| 77 | $svnOptions .= " --username $DASSCM_USER " | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | sub check_parameter(@) | 
|---|
| 81 | { | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | sub get_filenames(@) | 
|---|
| 85 | { | 
|---|
| 86 | my $filename_prod = $_[0]; | 
|---|
| 87 | if ( !($filename_prod =~ m/^\//) ) { | 
|---|
| 88 | $filename_prod = cwd()."/".$filename_prod; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | -r $filename_prod or die "$filename_prod is not accessable"; | 
|---|
| 92 |  | 
|---|
| 93 | # TODO: dirname buggy: eg. "/etc/" is reduced to "/", | 
|---|
| 94 | #       "/etc" is used as filename | 
|---|
| 95 | my $dirname_prod = dirname($filename_prod); | 
|---|
| 96 | chdir $dirname_prod or die $!; | 
|---|
| 97 | $dirname_prod = cwd(); | 
|---|
| 98 | my $basename = basename($filename_prod); | 
|---|
| 99 |  | 
|---|
| 100 | print "dir: ".$dirname_prod."\n"; | 
|---|
| 101 | print "fn: ".$basename."\n"; | 
|---|
| 102 |  | 
|---|
| 103 | my $dirname_repo = $DASSCM_REPO."/".$dirname_prod; | 
|---|
| 104 | my $filename_repo = "$dirname_repo/$basename"; | 
|---|
| 105 |  | 
|---|
| 106 | return ($basename,$dirname_prod,$dirname_repo,$filename_prod,$filename_repo); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | sub run_command | 
|---|
| 110 | { | 
|---|
| 111 | my $command = shift; | 
|---|
| 112 |  | 
|---|
| 113 | #print "executing command: " . $command . "\n"; | 
|---|
| 114 |  | 
|---|
| 115 | open(RESULT, $command . ' 2>&1 |' ); | 
|---|
| 116 | my @result = <RESULT>; | 
|---|
| 117 | close(RESULT); | 
|---|
| 118 | my $retcode = $?>>8; | 
|---|
| 119 |  | 
|---|
| 120 | #print @result; | 
|---|
| 121 | #if( $retcode ) { print "return code: " . $retcode . "\n"; } | 
|---|
| 122 |  | 
|---|
| 123 | return($retcode, @result); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 |  | 
|---|
| 127 | ##################################################################### | 
|---|
| 128 | # | 
|---|
| 129 | # functions | 
|---|
| 130 |  | 
|---|
| 131 | sub help(;@) | 
|---|
| 132 | { | 
|---|
| 133 | if( @_ == 0 ) { | 
|---|
| 134 | usage(); | 
|---|
| 135 | } else { | 
|---|
| 136 | print "help for @_: ...\n"; | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | sub add(@) | 
|---|
| 141 | { | 
|---|
| 142 | check_parameter(@_,1); | 
|---|
| 143 | check_env(); | 
|---|
| 144 |  | 
|---|
| 145 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]); | 
|---|
| 146 |  | 
|---|
| 147 | mkpath($dirname_repo); | 
|---|
| 148 |  | 
|---|
| 149 | # update complete repository | 
|---|
| 150 | #       (my $retcode, my @result)=run_command( "$SVN update $DASSCM_REPO" ); | 
|---|
| 151 | #       if( $retcode != 0 ) { | 
|---|
| 152 | #               print @result; | 
|---|
| 153 | #               die; | 
|---|
| 154 | #       } | 
|---|
| 155 | system( "$SVN update $svnOptions $DASSCM_REPO" ); | 
|---|
| 156 |  | 
|---|
| 157 | copy( $filename_prod, $filename_repo ) or die $!; | 
|---|
| 158 |  | 
|---|
| 159 | # already checked in? | 
|---|
| 160 | chdir($DASSCM_REPO); | 
|---|
| 161 | # also add the path to filename. | 
|---|
| 162 | for my $dir (split('/', $dirname_prod) ) { | 
|---|
| 163 | if( $dir ) { | 
|---|
| 164 | run_command( "$SVN add --non-recursive $dir" ); | 
|---|
| 165 | chdir $dir; | 
|---|
| 166 | } | 
|---|
| 167 | } | 
|---|
| 168 | run_command( "$SVN add $basename" ); | 
|---|
| 169 |  | 
|---|
| 170 | # commit calls $EDITOR. uses "system" here, to display output | 
|---|
| 171 | system( "$SVN commit $svnOptions $DASSCM_REPO" ); | 
|---|
| 172 | # TODO: commit (-m) | 
|---|
| 173 |  | 
|---|
| 174 | print $filename_prod."\n"; | 
|---|
| 175 | print $dirname_repo."\n"; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 |  | 
|---|
| 179 | sub blame(@) | 
|---|
| 180 | { | 
|---|
| 181 | check_parameter(@_,1); | 
|---|
| 182 | check_env(); | 
|---|
| 183 |  | 
|---|
| 184 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]); | 
|---|
| 185 |  | 
|---|
| 186 | system( "$SVN blame $svnOptions $filename_repo" ); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 |  | 
|---|
| 190 |  | 
|---|
| 191 | sub commit(@) | 
|---|
| 192 | { | 
|---|
| 193 | check_parameter(@_,1); | 
|---|
| 194 | check_env(); | 
|---|
| 195 |  | 
|---|
| 196 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]); | 
|---|
| 197 |  | 
|---|
| 198 | #       (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" ); | 
|---|
| 199 | #       if( $rc_update != 0 ) { | 
|---|
| 200 | #               print @result; | 
|---|
| 201 | #               die; | 
|---|
| 202 | #       } | 
|---|
| 203 | system( "$SVN update $svnOptions $DASSCM_REPO" ); | 
|---|
| 204 |  | 
|---|
| 205 | copy( $filename_prod, $filename_repo ) or die $!; | 
|---|
| 206 |  | 
|---|
| 207 | # commit calls $EDITOR. uses "system" here, to display output | 
|---|
| 208 | system( "$SVN commit $svnOptions $filename_repo" ); | 
|---|
| 209 | # TODO: commit (-m) | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | sub diff(@) | 
|---|
| 213 | { | 
|---|
| 214 | check_parameter(@_,1); | 
|---|
| 215 | check_env(); | 
|---|
| 216 |  | 
|---|
| 217 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]); | 
|---|
| 218 |  | 
|---|
| 219 | #print "$basename,$dirname_prod,$dirname_repo\n"; | 
|---|
| 220 |  | 
|---|
| 221 | (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" ); | 
|---|
| 222 | if( $rc_update != 0 ) { | 
|---|
| 223 | print @result; | 
|---|
| 224 | die; | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | (my $rc_diff, my @diff)=run_command( "diff $filename_repo $filename_prod" ); | 
|---|
| 228 | print @diff; | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | ##################################################################### | 
|---|
| 232 | # | 
|---|
| 233 | # main | 
|---|
| 234 | # | 
|---|
| 235 |  | 
|---|
| 236 | my $number_arguments = @ARGV; | 
|---|
| 237 | #print "$number_arguments\n"; | 
|---|
| 238 |  | 
|---|
| 239 | if ($number_arguments > 0) { | 
|---|
| 240 | my $command = $ARGV[0]; | 
|---|
| 241 | shift @ARGV; | 
|---|
| 242 | #$command =~ s/[A-Z]/[a-z]/m; | 
|---|
| 243 | #print "$command\n"; | 
|---|
| 244 |  | 
|---|
| 245 | $_=$command; | 
|---|
| 246 | if (m/help/i) { | 
|---|
| 247 | help(@ARGV); | 
|---|
| 248 | } elsif (m/add/i) { | 
|---|
| 249 | add(@ARGV); | 
|---|
| 250 | } elsif (m/blame/i) { | 
|---|
| 251 | blame(@ARGV); | 
|---|
| 252 | } elsif (m/commit/i) { | 
|---|
| 253 | commit(@ARGV); | 
|---|
| 254 | } elsif (m/diff/i) { | 
|---|
| 255 | diff(@ARGV); | 
|---|
| 256 | } elsif (m/activate/i) { | 
|---|
| 257 | activate(@ARGV); | 
|---|
| 258 | } else { | 
|---|
| 259 | usage(); | 
|---|
| 260 | check_env(); | 
|---|
| 261 | } | 
|---|
| 262 | } | 
|---|