[186] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | # $Id: dasscm 189 2004-11-10 08:53:48Z joergs $
|
---|
| 4 |
|
---|
| 5 | use strict;
|
---|
| 6 |
|
---|
| 7 | use Env qw($DASSCM_PROD $DASSCM_REPO);
|
---|
| 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 |
|
---|
[189] | 15 | #####################################################################
|
---|
| 16 | #
|
---|
[186] | 17 | # global
|
---|
[189] | 18 | #
|
---|
[186] | 19 | my $SVN = "svn ";
|
---|
| 20 |
|
---|
[189] | 21 | #####################################################################
|
---|
| 22 | #
|
---|
[186] | 23 | # util functions
|
---|
[189] | 24 | #
|
---|
[187] | 25 | sub usage()
|
---|
| 26 | {
|
---|
[189] | 27 | print "dasscm is intended to help versioning configuration files\n\n";
|
---|
| 28 |
|
---|
| 29 | print "usage: dasscm <subcommand> [options] [args]\n\n";
|
---|
| 30 | print "Available subcommands:\n";
|
---|
| 31 | print " add <filename>\n";
|
---|
| 32 | print " commit <filename>\n";
|
---|
| 33 | print " diff <filename>\n";
|
---|
| 34 | print " help <subcommand>\n";
|
---|
[187] | 35 | }
|
---|
| 36 |
|
---|
[186] | 37 | sub check_env()
|
---|
| 38 | {
|
---|
| 39 | # DASSCM_PROD
|
---|
| 40 | if ( ! $DASSCM_PROD ) {
|
---|
| 41 | $DASSCM_PROD = "/";
|
---|
| 42 | }
|
---|
| 43 | print "DASSCM_PROD: ".$DASSCM_PROD."\n";
|
---|
| 44 | if ( ! -d $DASSCM_PROD ) {
|
---|
| 45 | die "DASSCM_PROD is not set to a directory.\n";
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | # DASSCM_REPO
|
---|
| 49 | if ( ! $DASSCM_REPO ) {
|
---|
| 50 | die "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.";
|
---|
| 51 | }
|
---|
| 52 | print "DASSCM_REPO: ".$DASSCM_REPO."\n";
|
---|
| 53 | if ( ! -d $DASSCM_REPO ) {
|
---|
| 54 | die "DASSCM_REPO must be is not set to the directory of the versioning system checkout for this machine.\n";
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | sub check_parameter(@)
|
---|
| 60 | {
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[187] | 63 | sub get_filenames(@)
|
---|
| 64 | {
|
---|
| 65 | my $filename_prod = $_[0];
|
---|
| 66 | if ( !($filename_prod =~ m/^\//) ) {
|
---|
| 67 | $filename_prod = cwd()."/".$filename_prod;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | -r $filename_prod or die "$filename_prod is not accessable";
|
---|
| 71 |
|
---|
| 72 | # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
|
---|
| 73 | # "/etc" is used as filename
|
---|
| 74 | my $dirname_prod = dirname($filename_prod);
|
---|
| 75 | chdir $dirname_prod or die $!;
|
---|
| 76 | $dirname_prod = cwd();
|
---|
| 77 | my $basename = basename($filename_prod);
|
---|
| 78 |
|
---|
| 79 | print "dir: ".$dirname_prod."\n";
|
---|
| 80 | print "fn: ".$basename."\n";
|
---|
| 81 |
|
---|
| 82 | my $dirname_repo = $DASSCM_REPO."/".$dirname_prod;
|
---|
| 83 | my $filename_repo = "$dirname_repo/$basename";
|
---|
| 84 |
|
---|
| 85 | return ($basename,$dirname_prod,$dirname_repo,$filename_prod,$filename_repo);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[186] | 88 | sub run_command
|
---|
| 89 | {
|
---|
| 90 | my $command = shift;
|
---|
| 91 |
|
---|
[187] | 92 | #print "executing command: " . $command . "\n";
|
---|
[186] | 93 |
|
---|
| 94 | open(RESULT, $command . ' 2>&1 |' );
|
---|
| 95 | my @result = <RESULT>;
|
---|
| 96 | close(RESULT);
|
---|
| 97 | my $retcode = $?>>8;
|
---|
| 98 |
|
---|
[187] | 99 | #print @result;
|
---|
| 100 | #if( $retcode ) { print "return code: " . $retcode . "\n"; }
|
---|
[186] | 101 |
|
---|
| 102 | return($retcode, @result);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
[189] | 106 | #####################################################################
|
---|
| 107 | #
|
---|
[186] | 108 | # functions
|
---|
| 109 |
|
---|
| 110 | sub help(;@)
|
---|
| 111 | {
|
---|
[189] | 112 | if( @_ == 0 ) {
|
---|
| 113 | usage();
|
---|
| 114 | } else {
|
---|
| 115 | print "help for @_: ...\n";
|
---|
| 116 | }
|
---|
[186] | 117 | }
|
---|
| 118 |
|
---|
| 119 | sub add(@)
|
---|
| 120 | {
|
---|
| 121 | check_parameter(@_,1);
|
---|
| 122 | check_env();
|
---|
| 123 |
|
---|
[187] | 124 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
[186] | 125 |
|
---|
| 126 | mkpath($dirname_repo);
|
---|
| 127 |
|
---|
| 128 | # update complete repository
|
---|
[187] | 129 | (my $retcode, my @result)=run_command( "$SVN update $DASSCM_REPO" );
|
---|
| 130 | if( $retcode != 0 ) {
|
---|
| 131 | print @result;
|
---|
| 132 | die;
|
---|
| 133 | }
|
---|
[186] | 134 |
|
---|
| 135 | copy( $filename_prod, $filename_repo ) or die $!;
|
---|
| 136 |
|
---|
| 137 | # already checked in?
|
---|
| 138 | chdir($DASSCM_REPO);
|
---|
| 139 | # also add the path to filename.
|
---|
| 140 | for my $dir (split('/', $dirname_prod) ) {
|
---|
| 141 | if( $dir ) {
|
---|
| 142 | run_command( "$SVN add --non-recursive $dir" );
|
---|
| 143 | chdir $dir;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | run_command( "$SVN add $basename" );
|
---|
| 147 |
|
---|
| 148 | # commit calls $EDITOR. uses "system" here, to display output
|
---|
| 149 | system( "$SVN commit $DASSCM_REPO" );
|
---|
| 150 | # TODO: commit (-m)
|
---|
| 151 |
|
---|
| 152 | print $filename_prod."\n";
|
---|
| 153 | print $dirname_repo."\n";
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[187] | 156 | sub commit(@)
|
---|
| 157 | {
|
---|
| 158 | check_parameter(@_,1);
|
---|
| 159 | check_env();
|
---|
| 160 |
|
---|
| 161 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
[186] | 162 |
|
---|
[187] | 163 | (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
|
---|
| 164 | if( $rc_update != 0 ) {
|
---|
| 165 | print @result;
|
---|
| 166 | die;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | # commit calls $EDITOR. uses "system" here, to display output
|
---|
| 170 | system( "$SVN commit $DASSCM_REPO" );
|
---|
| 171 | # TODO: commit (-m)
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | sub diff(@)
|
---|
| 175 | {
|
---|
| 176 | check_parameter(@_,1);
|
---|
| 177 | check_env();
|
---|
| 178 |
|
---|
| 179 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
| 180 |
|
---|
| 181 | #print "$basename,$dirname_prod,$dirname_repo\n";
|
---|
| 182 |
|
---|
| 183 | (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
|
---|
| 184 | if( $rc_update != 0 ) {
|
---|
| 185 | print @result;
|
---|
| 186 | die;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | (my $rc_diff, my @diff)=run_command( "diff $filename_repo $filename_prod" );
|
---|
| 190 | print @diff;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[189] | 193 | #####################################################################
|
---|
| 194 | #
|
---|
[186] | 195 | # main
|
---|
[189] | 196 | #
|
---|
[186] | 197 |
|
---|
| 198 | my $number_arguments = @ARGV;
|
---|
[187] | 199 | #print "$number_arguments\n";
|
---|
[186] | 200 |
|
---|
| 201 | if ($number_arguments > 0) {
|
---|
| 202 | my $command = $ARGV[0];
|
---|
| 203 | shift @ARGV;
|
---|
| 204 | #$command =~ s/[A-Z]/[a-z]/m;
|
---|
| 205 | #print "$command\n";
|
---|
| 206 |
|
---|
| 207 | $_=$command;
|
---|
| 208 | if (m/help/i) {
|
---|
| 209 | help(@ARGV);
|
---|
| 210 | } elsif (m/add/i) {
|
---|
| 211 | add(@ARGV);
|
---|
| 212 | } elsif (m/commit/i) {
|
---|
| 213 | commit(@ARGV);
|
---|
| 214 | } elsif (m/diff/i) {
|
---|
| 215 | diff(@ARGV);
|
---|
| 216 | } elsif (m/activate/i) {
|
---|
| 217 | activate(@ARGV);
|
---|
[187] | 218 | } else {
|
---|
| 219 | usage();
|
---|
| 220 | check_env();
|
---|
[186] | 221 | }
|
---|
| 222 | }
|
---|