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