1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | # $Id: dasscm 203 2005-12-05 10:50:40Z 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 |
|
---|
86 | sub check_parameter(@)
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
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 |
|
---|
115 |
|
---|
116 | sub run_command
|
---|
117 | {
|
---|
118 | my $command = shift;
|
---|
119 |
|
---|
120 | #print "executing command: " . $command . "\n";
|
---|
121 |
|
---|
122 | open(RESULT, $command . ' 2>&1 |' );
|
---|
123 | my @result = <RESULT>;
|
---|
124 | close(RESULT);
|
---|
125 | my $retcode = $?>>8;
|
---|
126 |
|
---|
127 | #print @result;
|
---|
128 | #if( $retcode ) { print "return code: " . $retcode . "\n"; }
|
---|
129 |
|
---|
130 | return($retcode, @result);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
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 |
|
---|
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 | 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 |
|
---|
191 | #
|
---|
192 | # add (is used for command add and commit)
|
---|
193 | #
|
---|
194 | sub add(@)
|
---|
195 | {
|
---|
196 | check_parameter(@_,1);
|
---|
197 | check_env();
|
---|
198 |
|
---|
199 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
200 |
|
---|
201 | if( $command eq "add" ) {
|
---|
202 | mkpath($dirname_repo);
|
---|
203 | }
|
---|
204 |
|
---|
205 | # update complete repository
|
---|
206 | my $retcode = run_interactive( "$SVN update $svnOptions $DASSCM_REPO" );
|
---|
207 |
|
---|
208 | copy( $filename_prod, $filename_repo ) or die $!;
|
---|
209 |
|
---|
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 | }
|
---|
219 | }
|
---|
220 | run_command( "$SVN add $basename" );
|
---|
221 | }
|
---|
222 |
|
---|
223 | if( $options{'message'} ) {
|
---|
224 | $svnOptions .= " --message \"$options{'message'}\" ";
|
---|
225 | }
|
---|
226 |
|
---|
227 | # commit calls $EDITOR. uses "system" here, to display output
|
---|
228 | $retcode = run_interactive( "$SVN commit $svnOptions --username $DASSCM_USER $DASSCM_REPO" );
|
---|
229 |
|
---|
230 | print $filename_prod."\n";
|
---|
231 | print $dirname_repo."\n";
|
---|
232 | }
|
---|
233 |
|
---|
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 |
|
---|
242 | my $retcode = run_interactive( "$SVN blame $svnOptions $filename_repo" );
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
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 |
|
---|
266 | #####################################################################
|
---|
267 | #
|
---|
268 | # main
|
---|
269 | #
|
---|
270 |
|
---|
271 | my $number_arguments = @ARGV;
|
---|
272 |
|
---|
273 | if ($number_arguments > 0) {
|
---|
274 | # get subcommand and remove it from @ARGV
|
---|
275 | $command = $ARGV[0];
|
---|
276 | shift @ARGV;
|
---|
277 |
|
---|
278 |
|
---|
279 | # get command line options and store them in options hash
|
---|
280 | my $result = GetOptions ( \%options, 'message=s' );
|
---|
281 | # print options
|
---|
282 | foreach my $option (keys %options) {
|
---|
283 | print $option.": ".$options{$option}."\n";
|
---|
284 | }
|
---|
285 |
|
---|
286 | $_=$command;
|
---|
287 | if (m/help/i) {
|
---|
288 | help(@ARGV);
|
---|
289 | } elsif (m/login/i) {
|
---|
290 | # rewrite command. just to make sure
|
---|
291 | login( @ARGV );
|
---|
292 | } elsif (m/add/i) {
|
---|
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);
|
---|
300 | } elsif (m/blame/i) {
|
---|
301 | blame(@ARGV);
|
---|
302 | } elsif (m/diff/i) {
|
---|
303 | diff(@ARGV);
|
---|
304 | } elsif (m/activate/i) {
|
---|
305 | activate(@ARGV);
|
---|
306 | } else {
|
---|
307 | usage();
|
---|
308 | check_env();
|
---|
309 | }
|
---|
310 | # login
|
---|
311 | # commitall
|
---|
312 | # revert
|
---|
313 | # status (chkconf)
|
---|
314 | }
|
---|