[186] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | # $Id: dasscm 202 2004-12-16 17:45: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 = ();
|
---|
[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 |
|
---|
[196] | 163 | #
|
---|
| 164 | # add (is used for command add and commit)
|
---|
| 165 | #
|
---|
[186] | 166 | sub add(@)
|
---|
| 167 | {
|
---|
| 168 | check_parameter(@_,1);
|
---|
| 169 | check_env();
|
---|
| 170 |
|
---|
[187] | 171 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
[186] | 172 |
|
---|
[196] | 173 | if( $command eq "add" ) {
|
---|
| 174 | mkpath($dirname_repo);
|
---|
| 175 | }
|
---|
[186] | 176 |
|
---|
| 177 | # update complete repository
|
---|
[196] | 178 | my $retcode = run_interactive( "$SVN update $svnOptions $DASSCM_REPO" );
|
---|
[186] | 179 |
|
---|
| 180 | copy( $filename_prod, $filename_repo ) or die $!;
|
---|
| 181 |
|
---|
[196] | 182 | if( $command eq "add" ) {
|
---|
| 183 | # already checked in?
|
---|
| 184 | chdir($DASSCM_REPO);
|
---|
| 185 | # also add the path to filename.
|
---|
| 186 | for my $dir (split('/', $dirname_prod) ) {
|
---|
| 187 | if( $dir ) {
|
---|
| 188 | run_command( "$SVN add --non-recursive $dir" );
|
---|
| 189 | chdir $dir;
|
---|
| 190 | }
|
---|
[186] | 191 | }
|
---|
[196] | 192 | run_command( "$SVN add $basename" );
|
---|
[186] | 193 | }
|
---|
[196] | 194 |
|
---|
| 195 | if( $options{'message'} ) {
|
---|
[202] | 196 | $svnOptions .= " --message \"$options{'message'}\" ";
|
---|
[196] | 197 | }
|
---|
[186] | 198 |
|
---|
| 199 | # commit calls $EDITOR. uses "system" here, to display output
|
---|
[202] | 200 | $retcode = run_interactive( "$SVN commit $svnOptions --username $DASSCM_USER $DASSCM_REPO" );
|
---|
[186] | 201 |
|
---|
| 202 | print $filename_prod."\n";
|
---|
| 203 | print $dirname_repo."\n";
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[193] | 206 |
|
---|
| 207 | sub blame(@)
|
---|
| 208 | {
|
---|
| 209 | check_parameter(@_,1);
|
---|
| 210 | check_env();
|
---|
| 211 |
|
---|
| 212 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
| 213 |
|
---|
[196] | 214 | my $retcode = run_interactive( "$SVN blame $svnOptions $filename_repo" );
|
---|
[193] | 215 | }
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 |
|
---|
[187] | 219 | sub diff(@)
|
---|
| 220 | {
|
---|
| 221 | check_parameter(@_,1);
|
---|
| 222 | check_env();
|
---|
| 223 |
|
---|
| 224 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
| 225 |
|
---|
| 226 | #print "$basename,$dirname_prod,$dirname_repo\n";
|
---|
| 227 |
|
---|
| 228 | (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
|
---|
| 229 | if( $rc_update != 0 ) {
|
---|
| 230 | print @result;
|
---|
| 231 | die;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | (my $rc_diff, my @diff)=run_command( "diff $filename_repo $filename_prod" );
|
---|
| 235 | print @diff;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[189] | 238 | #####################################################################
|
---|
| 239 | #
|
---|
[186] | 240 | # main
|
---|
[189] | 241 | #
|
---|
[186] | 242 |
|
---|
| 243 | my $number_arguments = @ARGV;
|
---|
| 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' );
|
---|
[197] | 253 | # print options
|
---|
[196] | 254 | foreach my $option (keys %options) {
|
---|
| 255 | print $option.": ".$options{$option}."\n";
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[186] | 258 | $_=$command;
|
---|
| 259 | if (m/help/i) {
|
---|
| 260 | help(@ARGV);
|
---|
| 261 | } elsif (m/add/i) {
|
---|
[196] | 262 | # rewrite command. just to make sure
|
---|
| 263 | $command = "add";
|
---|
| 264 | add( @ARGV);
|
---|
| 265 | } elsif (m/commit/i) {
|
---|
| 266 | # rewrite command. just to make sure
|
---|
| 267 | $command = "commit";
|
---|
| 268 | add( @ARGV);
|
---|
[193] | 269 | } elsif (m/blame/i) {
|
---|
| 270 | blame(@ARGV);
|
---|
[186] | 271 | } elsif (m/diff/i) {
|
---|
| 272 | diff(@ARGV);
|
---|
| 273 | } elsif (m/activate/i) {
|
---|
| 274 | activate(@ARGV);
|
---|
[187] | 275 | } else {
|
---|
| 276 | usage();
|
---|
| 277 | check_env();
|
---|
[186] | 278 | }
|
---|
| 279 | }
|
---|