[186] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | # $Id: dasscm 203 2005-12-05 10:50:40Z joergs $
|
---|
| 4 |
|
---|
| 5 | use strict;
|
---|
| 6 |
|
---|
[190] | 7 | use Env qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USER $DASSCM_PW);
|
---|
[186] | 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/;
|
---|
[196] | 14 | use Getopt::Long;
|
---|
[186] | 15 |
|
---|
[189] | 16 | #####################################################################
|
---|
| 17 | #
|
---|
[186] | 18 | # global
|
---|
[189] | 19 | #
|
---|
[186] | 20 | my $SVN = "svn ";
|
---|
[195] | 21 | my $svnOptions = "";
|
---|
[196] | 22 | # command line options get stored in options hash
|
---|
| 23 | my %options = ();
|
---|
[197] | 24 | # subcommand, that gets executed (add, commit, ...)
|
---|
[196] | 25 | my $command;
|
---|
[186] | 26 |
|
---|
[189] | 27 | #####################################################################
|
---|
| 28 | #
|
---|
[186] | 29 | # util functions
|
---|
[189] | 30 | #
|
---|
[187] | 31 | sub usage()
|
---|
| 32 | {
|
---|
[190] | 33 | print "usage: dasscm <subcommand> [options] [args]\n";
|
---|
| 34 | print "\n";
|
---|
| 35 | print "dasscm is intended to help versioning configuration files\n";
|
---|
| 36 | print "\n";
|
---|
[189] | 37 | print "Available subcommands:\n";
|
---|
| 38 | print " add <filename>\n";
|
---|
| 39 | print " commit <filename>\n";
|
---|
| 40 | print " diff <filename>\n";
|
---|
| 41 | print " help <subcommand>\n";
|
---|
[190] | 42 | print "\n";
|
---|
| 43 | print "preperation:\n";
|
---|
| 44 | print "check out the configuration repository, e.g.\n";
|
---|
[191] | 45 | print "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/lvermgeo/technical/config/\n";
|
---|
[190] | 46 | print "environment variables DASSCM_REPO, DASSCM_PROD and DASSCM_USER are evaluated.\n";
|
---|
| 47 | print "\n";
|
---|
[187] | 48 | }
|
---|
| 49 |
|
---|
[186] | 50 | sub check_env()
|
---|
| 51 | {
|
---|
| 52 | # DASSCM_PROD
|
---|
| 53 | if ( ! $DASSCM_PROD ) {
|
---|
| 54 | $DASSCM_PROD = "/";
|
---|
| 55 | }
|
---|
| 56 | print "DASSCM_PROD: ".$DASSCM_PROD."\n";
|
---|
| 57 | if ( ! -d $DASSCM_PROD ) {
|
---|
| 58 | die "DASSCM_PROD is not set to a directory.\n";
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | # DASSCM_REPO
|
---|
| 62 | if ( ! $DASSCM_REPO ) {
|
---|
[190] | 63 | die "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
|
---|
[186] | 64 | }
|
---|
| 65 | print "DASSCM_REPO: ".$DASSCM_REPO."\n";
|
---|
| 66 | if ( ! -d $DASSCM_REPO ) {
|
---|
| 67 | die "DASSCM_REPO must be is not set to the directory of the versioning system checkout for this machine.\n";
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[190] | 70 | # User settings
|
---|
| 71 | # user root is not allowed for checkins.
|
---|
| 72 | # if user is root, DASSCM_USER has to be set,
|
---|
| 73 | # otherwise USER can be used
|
---|
| 74 | if ( "$USER" eq "root" ) {
|
---|
| 75 | if ( ! $DASSCM_USER ) {
|
---|
| 76 | die "Envirnonment variable DASSCM_USER not set.\nSet DASSCM_USER to your subversion user account.\n";
|
---|
| 77 | }
|
---|
[195] | 78 | $svnOptions .= " --no-auth-cache "
|
---|
[190] | 79 | } elsif ( ! $DASSCM_USER ) {
|
---|
| 80 | $DASSCM_USER=$USER;
|
---|
[195] | 81 | }
|
---|
[196] | 82 | #$svnOptions .= " --username $DASSCM_USER "
|
---|
[186] | 83 | }
|
---|
| 84 |
|
---|
[202] | 85 |
|
---|
[186] | 86 | sub check_parameter(@)
|
---|
| 87 | {
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[187] | 90 | sub get_filenames(@)
|
---|
| 91 | {
|
---|
| 92 | my $filename_prod = $_[0];
|
---|
| 93 | if ( !($filename_prod =~ m/^\//) ) {
|
---|
| 94 | $filename_prod = cwd()."/".$filename_prod;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | -r $filename_prod or die "$filename_prod is not accessable";
|
---|
| 98 |
|
---|
| 99 | # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
|
---|
| 100 | # "/etc" is used as filename
|
---|
| 101 | my $dirname_prod = dirname($filename_prod);
|
---|
| 102 | chdir $dirname_prod or die $!;
|
---|
| 103 | $dirname_prod = cwd();
|
---|
| 104 | my $basename = basename($filename_prod);
|
---|
| 105 |
|
---|
| 106 | print "dir: ".$dirname_prod."\n";
|
---|
| 107 | print "fn: ".$basename."\n";
|
---|
| 108 |
|
---|
| 109 | my $dirname_repo = $DASSCM_REPO."/".$dirname_prod;
|
---|
| 110 | my $filename_repo = "$dirname_repo/$basename";
|
---|
| 111 |
|
---|
| 112 | return ($basename,$dirname_prod,$dirname_repo,$filename_prod,$filename_repo);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[196] | 115 |
|
---|
[186] | 116 | sub run_command
|
---|
| 117 | {
|
---|
| 118 | my $command = shift;
|
---|
| 119 |
|
---|
[187] | 120 | #print "executing command: " . $command . "\n";
|
---|
[186] | 121 |
|
---|
| 122 | open(RESULT, $command . ' 2>&1 |' );
|
---|
| 123 | my @result = <RESULT>;
|
---|
| 124 | close(RESULT);
|
---|
| 125 | my $retcode = $?>>8;
|
---|
| 126 |
|
---|
[187] | 127 | #print @result;
|
---|
| 128 | #if( $retcode ) { print "return code: " . $retcode . "\n"; }
|
---|
[186] | 129 |
|
---|
| 130 | return($retcode, @result);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
[196] | 134 |
|
---|
| 135 | sub run_interactive
|
---|
| 136 | {
|
---|
| 137 | system( @_ );
|
---|
| 138 | if ($? == -1) {
|
---|
| 139 | printf "failed to execute: $!\n";
|
---|
| 140 | } elsif ($? & 127) {
|
---|
| 141 | printf "child died with signal %d, %s coredump\n",
|
---|
| 142 | ($? & 127), ($? & 128) ? 'with' : 'without';
|
---|
| 143 | } elsif( $? >> 8 != 0 ) {
|
---|
| 144 | printf "child exited with value %d\n", $? >> 8;
|
---|
| 145 | }
|
---|
| 146 | return( $? >> 8 );
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
[189] | 150 | #####################################################################
|
---|
| 151 | #
|
---|
[186] | 152 | # functions
|
---|
| 153 |
|
---|
| 154 | sub help(;@)
|
---|
| 155 | {
|
---|
[189] | 156 | if( @_ == 0 ) {
|
---|
| 157 | usage();
|
---|
| 158 | } else {
|
---|
| 159 | print "help for @_: ...\n";
|
---|
| 160 | }
|
---|
[186] | 161 | }
|
---|
| 162 |
|
---|
[203] | 163 |
|
---|
| 164 | sub login(@)
|
---|
| 165 | {
|
---|
| 166 | check_parameter(@_,1);
|
---|
| 167 | #check_env();
|
---|
| 168 |
|
---|
| 169 | #$DASSCM_USER $DASSCM_PW
|
---|
| 170 | my $output_user = "";
|
---|
| 171 | if( $DASSCM_USER ) {
|
---|
| 172 | $output_user = " ($DASSCM_USER)";
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | print "Enter DASSCM user name",$output_user,": ";
|
---|
| 176 | my $input_user = <STDIN>;
|
---|
| 177 | chomp($input_user);
|
---|
| 178 | print "Enter DASSCM user password: ";
|
---|
| 179 | my $input_pw = <STDIN>;
|
---|
| 180 | chomp($input_pw);
|
---|
| 181 |
|
---|
| 182 | # TODO: test svn login
|
---|
| 183 |
|
---|
| 184 | $ENV{'DASSCM_USER'} = $input_user;
|
---|
| 185 | $ENV{'DASSCM_PW'} = $input_pw;
|
---|
| 186 |
|
---|
| 187 | exec( "bash" ) or die "can't login";
|
---|
| 188 |
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[196] | 191 | #
|
---|
| 192 | # add (is used for command add and commit)
|
---|
| 193 | #
|
---|
[186] | 194 | sub add(@)
|
---|
| 195 | {
|
---|
| 196 | check_parameter(@_,1);
|
---|
| 197 | check_env();
|
---|
| 198 |
|
---|
[187] | 199 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
[186] | 200 |
|
---|
[196] | 201 | if( $command eq "add" ) {
|
---|
| 202 | mkpath($dirname_repo);
|
---|
| 203 | }
|
---|
[186] | 204 |
|
---|
| 205 | # update complete repository
|
---|
[196] | 206 | my $retcode = run_interactive( "$SVN update $svnOptions $DASSCM_REPO" );
|
---|
[186] | 207 |
|
---|
| 208 | copy( $filename_prod, $filename_repo ) or die $!;
|
---|
| 209 |
|
---|
[196] | 210 | if( $command eq "add" ) {
|
---|
| 211 | # already checked in?
|
---|
| 212 | chdir($DASSCM_REPO);
|
---|
| 213 | # also add the path to filename.
|
---|
| 214 | for my $dir (split('/', $dirname_prod) ) {
|
---|
| 215 | if( $dir ) {
|
---|
| 216 | run_command( "$SVN add --non-recursive $dir" );
|
---|
| 217 | chdir $dir;
|
---|
| 218 | }
|
---|
[186] | 219 | }
|
---|
[196] | 220 | run_command( "$SVN add $basename" );
|
---|
[186] | 221 | }
|
---|
[196] | 222 |
|
---|
| 223 | if( $options{'message'} ) {
|
---|
[202] | 224 | $svnOptions .= " --message \"$options{'message'}\" ";
|
---|
[196] | 225 | }
|
---|
[186] | 226 |
|
---|
| 227 | # commit calls $EDITOR. uses "system" here, to display output
|
---|
[202] | 228 | $retcode = run_interactive( "$SVN commit $svnOptions --username $DASSCM_USER $DASSCM_REPO" );
|
---|
[186] | 229 |
|
---|
| 230 | print $filename_prod."\n";
|
---|
| 231 | print $dirname_repo."\n";
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[193] | 234 |
|
---|
| 235 | sub blame(@)
|
---|
| 236 | {
|
---|
| 237 | check_parameter(@_,1);
|
---|
| 238 | check_env();
|
---|
| 239 |
|
---|
| 240 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
| 241 |
|
---|
[196] | 242 | my $retcode = run_interactive( "$SVN blame $svnOptions $filename_repo" );
|
---|
[193] | 243 | }
|
---|
| 244 |
|
---|
| 245 |
|
---|
| 246 |
|
---|
[187] | 247 | sub diff(@)
|
---|
| 248 | {
|
---|
| 249 | check_parameter(@_,1);
|
---|
| 250 | check_env();
|
---|
| 251 |
|
---|
| 252 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
| 253 |
|
---|
| 254 | #print "$basename,$dirname_prod,$dirname_repo\n";
|
---|
| 255 |
|
---|
| 256 | (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
|
---|
| 257 | if( $rc_update != 0 ) {
|
---|
| 258 | print @result;
|
---|
| 259 | die;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | (my $rc_diff, my @diff)=run_command( "diff $filename_repo $filename_prod" );
|
---|
| 263 | print @diff;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[189] | 266 | #####################################################################
|
---|
| 267 | #
|
---|
[186] | 268 | # main
|
---|
[189] | 269 | #
|
---|
[186] | 270 |
|
---|
| 271 | my $number_arguments = @ARGV;
|
---|
| 272 |
|
---|
| 273 | if ($number_arguments > 0) {
|
---|
[196] | 274 | # get subcommand and remove it from @ARGV
|
---|
| 275 | $command = $ARGV[0];
|
---|
[186] | 276 | shift @ARGV;
|
---|
| 277 |
|
---|
[196] | 278 |
|
---|
| 279 | # get command line options and store them in options hash
|
---|
| 280 | my $result = GetOptions ( \%options, 'message=s' );
|
---|
[197] | 281 | # print options
|
---|
[196] | 282 | foreach my $option (keys %options) {
|
---|
| 283 | print $option.": ".$options{$option}."\n";
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[186] | 286 | $_=$command;
|
---|
| 287 | if (m/help/i) {
|
---|
| 288 | help(@ARGV);
|
---|
[203] | 289 | } elsif (m/login/i) {
|
---|
| 290 | # rewrite command. just to make sure
|
---|
| 291 | login( @ARGV );
|
---|
[186] | 292 | } elsif (m/add/i) {
|
---|
[196] | 293 | # rewrite command. just to make sure
|
---|
| 294 | $command = "add";
|
---|
| 295 | add( @ARGV);
|
---|
| 296 | } elsif (m/commit/i) {
|
---|
| 297 | # rewrite command. just to make sure
|
---|
| 298 | $command = "commit";
|
---|
| 299 | add( @ARGV);
|
---|
[193] | 300 | } elsif (m/blame/i) {
|
---|
| 301 | blame(@ARGV);
|
---|
[186] | 302 | } elsif (m/diff/i) {
|
---|
| 303 | diff(@ARGV);
|
---|
| 304 | } elsif (m/activate/i) {
|
---|
| 305 | activate(@ARGV);
|
---|
[187] | 306 | } else {
|
---|
| 307 | usage();
|
---|
| 308 | check_env();
|
---|
[186] | 309 | }
|
---|
[203] | 310 | # login
|
---|
| 311 | # commitall
|
---|
| 312 | # revert
|
---|
| 313 | # status (chkconf)
|
---|
[186] | 314 | }
|
---|