1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | # $Id: dasscm 197 2004-12-06 21:26:58Z 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 | use Getopt::Long;
|
---|
15 |
|
---|
16 | #####################################################################
|
---|
17 | #
|
---|
18 | # global
|
---|
19 | #
|
---|
20 | my $SVN = "svn ";
|
---|
21 | my $svnOptions = "";
|
---|
22 | # command line options get stored in options hash
|
---|
23 | my %options = ();
|
---|
24 | # subcommand, that gets executed (add, commit, ...)
|
---|
25 | my $command;
|
---|
26 |
|
---|
27 | #####################################################################
|
---|
28 | #
|
---|
29 | # util functions
|
---|
30 | #
|
---|
31 | sub usage()
|
---|
32 | {
|
---|
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";
|
---|
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";
|
---|
42 | print "\n";
|
---|
43 | print "preperation:\n";
|
---|
44 | print "check out the configuration repository, e.g.\n";
|
---|
45 | print "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/lvermgeo/technical/config/\n";
|
---|
46 | print "environment variables DASSCM_REPO, DASSCM_PROD and DASSCM_USER are evaluated.\n";
|
---|
47 | print "\n";
|
---|
48 | }
|
---|
49 |
|
---|
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 ) {
|
---|
63 | die "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
|
---|
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 |
|
---|
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 | }
|
---|
78 | $svnOptions .= " --no-auth-cache "
|
---|
79 | } elsif ( ! $DASSCM_USER ) {
|
---|
80 | $DASSCM_USER=$USER;
|
---|
81 | }
|
---|
82 | #$svnOptions .= " --username $DASSCM_USER "
|
---|
83 | }
|
---|
84 |
|
---|
85 | sub check_parameter(@)
|
---|
86 | {
|
---|
87 | }
|
---|
88 |
|
---|
89 | sub get_filenames(@)
|
---|
90 | {
|
---|
91 | my $filename_prod = $_[0];
|
---|
92 | if ( !($filename_prod =~ m/^\//) ) {
|
---|
93 | $filename_prod = cwd()."/".$filename_prod;
|
---|
94 | }
|
---|
95 |
|
---|
96 | -r $filename_prod or die "$filename_prod is not accessable";
|
---|
97 |
|
---|
98 | # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
|
---|
99 | # "/etc" is used as filename
|
---|
100 | my $dirname_prod = dirname($filename_prod);
|
---|
101 | chdir $dirname_prod or die $!;
|
---|
102 | $dirname_prod = cwd();
|
---|
103 | my $basename = basename($filename_prod);
|
---|
104 |
|
---|
105 | print "dir: ".$dirname_prod."\n";
|
---|
106 | print "fn: ".$basename."\n";
|
---|
107 |
|
---|
108 | my $dirname_repo = $DASSCM_REPO."/".$dirname_prod;
|
---|
109 | my $filename_repo = "$dirname_repo/$basename";
|
---|
110 |
|
---|
111 | return ($basename,$dirname_prod,$dirname_repo,$filename_prod,$filename_repo);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | sub run_command
|
---|
116 | {
|
---|
117 | my $command = shift;
|
---|
118 |
|
---|
119 | #print "executing command: " . $command . "\n";
|
---|
120 |
|
---|
121 | open(RESULT, $command . ' 2>&1 |' );
|
---|
122 | my @result = <RESULT>;
|
---|
123 | close(RESULT);
|
---|
124 | my $retcode = $?>>8;
|
---|
125 |
|
---|
126 | #print @result;
|
---|
127 | #if( $retcode ) { print "return code: " . $retcode . "\n"; }
|
---|
128 |
|
---|
129 | return($retcode, @result);
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | sub run_interactive
|
---|
135 | {
|
---|
136 | system( @_ );
|
---|
137 | if ($? == -1) {
|
---|
138 | printf "failed to execute: $!\n";
|
---|
139 | } elsif ($? & 127) {
|
---|
140 | printf "child died with signal %d, %s coredump\n",
|
---|
141 | ($? & 127), ($? & 128) ? 'with' : 'without';
|
---|
142 | } elsif( $? >> 8 != 0 ) {
|
---|
143 | printf "child exited with value %d\n", $? >> 8;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | return( $? >> 8 );
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | #####################################################################
|
---|
151 | #
|
---|
152 | # functions
|
---|
153 |
|
---|
154 | sub help(;@)
|
---|
155 | {
|
---|
156 | if( @_ == 0 ) {
|
---|
157 | usage();
|
---|
158 | } else {
|
---|
159 | print "help for @_: ...\n";
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | #
|
---|
164 | # add (is used for command add and commit)
|
---|
165 | #
|
---|
166 | sub add(@)
|
---|
167 | {
|
---|
168 | check_parameter(@_,1);
|
---|
169 | check_env();
|
---|
170 |
|
---|
171 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
172 |
|
---|
173 | if( $command eq "add" ) {
|
---|
174 | mkpath($dirname_repo);
|
---|
175 | }
|
---|
176 |
|
---|
177 | # update complete repository
|
---|
178 | my $retcode = run_interactive( "$SVN update $svnOptions $DASSCM_REPO" );
|
---|
179 |
|
---|
180 | copy( $filename_prod, $filename_repo ) or die $!;
|
---|
181 |
|
---|
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 | }
|
---|
191 | }
|
---|
192 | run_command( "$SVN add $basename" );
|
---|
193 | }
|
---|
194 |
|
---|
195 | if( $options{'message'} ) {
|
---|
196 | $svnOptions .= " --message $options{'message'} ";
|
---|
197 | }
|
---|
198 |
|
---|
199 | # commit calls $EDITOR. uses "system" here, to display output
|
---|
200 | my $retcode = run_interactive( "$SVN commit $svnOptions --username $DASSCM_USER $DASSCM_REPO" );
|
---|
201 |
|
---|
202 | print $filename_prod."\n";
|
---|
203 | print $dirname_repo."\n";
|
---|
204 | }
|
---|
205 |
|
---|
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 |
|
---|
214 | my $retcode = run_interactive( "$SVN blame $svnOptions $filename_repo" );
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 |
|
---|
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 |
|
---|
238 | #####################################################################
|
---|
239 | #
|
---|
240 | # main
|
---|
241 | #
|
---|
242 |
|
---|
243 | my $number_arguments = @ARGV;
|
---|
244 |
|
---|
245 | if ($number_arguments > 0) {
|
---|
246 | # get subcommand and remove it from @ARGV
|
---|
247 | $command = $ARGV[0];
|
---|
248 | shift @ARGV;
|
---|
249 |
|
---|
250 |
|
---|
251 | # get command line options and store them in options hash
|
---|
252 | my $result = GetOptions ( \%options, 'message=s' );
|
---|
253 | # print options
|
---|
254 | foreach my $option (keys %options) {
|
---|
255 | print $option.": ".$options{$option}."\n";
|
---|
256 | }
|
---|
257 |
|
---|
258 | $_=$command;
|
---|
259 | if (m/help/i) {
|
---|
260 | help(@ARGV);
|
---|
261 | } elsif (m/add/i) {
|
---|
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);
|
---|
269 | } elsif (m/blame/i) {
|
---|
270 | blame(@ARGV);
|
---|
271 | } elsif (m/diff/i) {
|
---|
272 | diff(@ARGV);
|
---|
273 | } elsif (m/activate/i) {
|
---|
274 | activate(@ARGV);
|
---|
275 | } else {
|
---|
276 | usage();
|
---|
277 | check_env();
|
---|
278 | }
|
---|
279 | }
|
---|