source: trunk/dasscm/dasscm@ 214

Last change on this file since 214 was 214, checked in by joergs, on Jul 3, 2007 at 2:10:55 PM

improvements, bugfixes

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 14.6 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 214 2007-07-03 12:10:55Z joergs $
4
5use strict;
6
7use Env
8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD);
9use Cwd;
10use Getopt::Long;
11use File::Basename;
12use File::Compare;
13use File::Copy;
14use File::Find;
15use File::stat;
16use File::Path;
17use Term::ReadKey;
18
19#
20# used ConfigFile instead of SmartClient::Config,
21# because the huge amount of SmartClient dependencies
22#use SmartClient::Config;
23use ConfigFile;
24
25#####################################################################
26#
27# global
28#
29
30my $config_file = "/etc/dasscm.conf";
31my $config = ConfigFile::read_config_file($config_file);
32my $DASSCM_LOCAL_REPOSITORY_BASE;
33my $DASSCM_REPOSITORY_NAME;
34my $DASSCM_SVN_REPOSITORY;
35
36my $SVN = "svn ";
37my $svnOptions = "";
38my $svnCheckoutCredentials = "";
39my $svnPasswordCredentials = "";
40
41# command line options get stored in options hash
42my %options = ();
43
44# subcommand, that gets executed (add, commit, ...)
45my $command;
46
47my $verbose = 0;
48
49# stores result from status (cvscheck)
50my %status_removedfiles = ();
51my %status_changedfiles = ();
52
53#####################################################################
54#
55# util functions
56#
57sub usage()
58{
59 print "usage: dasscm <subcommand> [options] [args]\n";
60 print "\n";
61 print "dasscm is intended to help versioning configuration files\n";
62 print "\n";
63 print "Available subcommands:\n";
64 print " init\n";
65 print " login\n";
66 print " add <filename>\n";
67 print " commit <filename>\n";
68 print " status <filename>\n";
69 print " diff <filename>\n";
70 print " help <subcommand>\n";
71 print "\n";
72 print "preperation:\n";
73 print "check out the configuration repository, e.g.\n";
74 print
75 "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/dasscm/HOSTNAME\n";
76 print "environment variables\n", " DASSCM_REPO\n", " DASSCM_PROD\n",
77 " DASSCM_USERNAME\n", " DASSCM_PASSWORD\n", "are evaluated.\n";
78 print "\n";
79}
80
81sub check_env()
82{
83
84 # DASSCM_PROD
85 if ( !$DASSCM_PROD ) {
86 $DASSCM_PROD = "/";
87 }
88
89 if ( !-d $DASSCM_PROD ) {
90 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
91 }
92 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
93
94 # DASSCM_REPOSITORY_NAME
95 if ( !$DASSCM_REPOSITORY_NAME ) {
96 die
97 "Variable DASSCM_REPOSITORY_NAME is not defined.\nIt needs to be a unique name.\nNormally the full qualified host name is used.\nUse file $config_file to configure it.\n";
98 }
99
100 # DASSCM_REPO
101 if ( !$DASSCM_REPO ) {
102 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
103 $DASSCM_REPO =
104 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
105 } else {
106 die
107 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
108 }
109 }
110 print "DASSCM_REPO: " . $DASSCM_REPO . "\n";
111
112 #
113 # check if local repository directory exist (if not creating by init)
114 #
115 if ( $command ne "init" ) {
116 if ( not -d $DASSCM_REPO ) {
117 die
118 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
119 }
120
121 #
122 # user settings
123 #
124
125 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
126 if ( !$DASSCM_USERNAME ) {
127 $DASSCM_USERNAME = $DASSCM_USER;
128 }
129
130 # user root is not allowed for checkins.
131 # if user is root, DASSCM_USER has to be set,
132 # otherwise USER can be used
133 if ( "$USER" eq "root" ) {
134 if ( ( not $DASSCM_USERNAME ) and ( $command ne "login" ) ) {
135 die
136 "Envirnonment variable DASSCM_USERNAME not set.\nSet DASSCM_USERNAME to your subversion user account.\n";
137 }
138 $svnOptions .= " --no-auth-cache ";
139 } elsif ( !$DASSCM_USERNAME ) {
140 $DASSCM_USERNAME = $USER;
141 }
142
143 #
144 # password
145 #
146 if ($DASSCM_PASSWORD) {
147 $svnPasswordCredentials = " --password $DASSCM_PASSWORD ";
148 }
149 }
150
151 #$svnOptions .= " --username $DASSCM_USERNAME "
152}
153
154sub check_parameter(@)
155{
156}
157
158sub get_filenames(@)
159{
160 my $filename_prod = $_[0];
161 if ( !( $filename_prod =~ m/^\// ) ) {
162 $filename_prod = cwd() . "/" . $filename_prod;
163 }
164
165 -r $filename_prod or die "$filename_prod is not accessable";
166
167 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
168 # "/etc" is used as filename
169 my $dirname_prod = dirname($filename_prod);
170 chdir $dirname_prod or die $!;
171 $dirname_prod = cwd();
172 my $basename = basename($filename_prod);
173
174 if ($verbose) {
175 print "dir: " . $dirname_prod . "\n";
176 print "fn: " . $basename . "\n";
177 }
178
179 my $dirname_repo = $DASSCM_REPO . "/" . $dirname_prod;
180 my $filename_repo = "$dirname_repo/$basename";
181
182 return (
183 $basename, $dirname_prod, $dirname_repo,
184 $filename_prod, $filename_repo
185 );
186}
187
188#
189# used by status
190# checks for differences between PROD and (local) REPO
191#
192sub cvscheck
193{
194 return unless -f; # keine Directories
195 return if $File::Find::dir =~ /\/CVS$/; # ignoriere CVS-Verzeichnisse
196 return
197 if $File::Find::dir =~
198 /\/\.svn/; # ignoriere Subversion Verzeichnisse (inkl. Unterverzeichnisse)
199 my $cvsworkfile = "$File::Find::dir/$_";
200
201 # Ursprungspfad ermitteln
202 # TODO: get_filename ?
203 $cvsworkfile =~ /${DASSCM_REPO}\/(.+)/;
204 my $realfile = "/" . $1;
205
206 # relativer Pfad zur CVS-Arbeitsdatei
207 my $relcvsworkfile = $1;
208
209 if ( !-r $realfile ) {
210 $status_removedfiles{"$realfile"} = $cvsworkfile;
211 } else {
212 ( -r "$cvsworkfile" ) || die("Fehler: $cvsworkfile ist nicht lesbar");
213 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
214
215 # Dateien unterscheiden sich
216 #(-w $cvsworkfile) || die("failed: no Fehler: kein Schreibrecht auf $cvsworkfile");
217 # Arbeitskopie durch Kopie ersetzen
218 #copy($realfile,$cvsworkfile) || die("Fehler beim kopieren $realfile --> $cvsworkfile");
219 $status_changedfiles{"$realfile"} = $cvsworkfile;
220 }
221 }
222}
223
224sub run_command
225{
226 my $command = shift;
227
228 #print "executing command: " . $command . "\n";
229
230 open( RESULT, $command . ' 2>&1 |' );
231 my @result = <RESULT>;
232 close(RESULT);
233 my $retcode = $? >> 8;
234
235 #print @result;
236 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
237
238 return ( $retcode, @result );
239}
240
241sub run_interactive
242{
243
244 if ($verbose) {
245 print "run_interactive:" . join( " ", @_ ) . "\n";
246 }
247
248 system(@_);
249 if ( $? == -1 ) {
250 printf "failed to execute: $!\n";
251 } elsif ( $? & 127 ) {
252 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
253 ( $? & 128 ) ? 'with' : 'without';
254 } elsif ( $? >> 8 != 0 ) {
255 printf "child exited with value %d\n", $? >> 8;
256 }
257 return ( $? >> 8 );
258}
259
260sub svn_check_credentials( $$ )
261{
262 my $username = shift;
263 my $password = shift;
264
265 ( my $rc_update, my @result ) =
266 run_command(
267 "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
268 );
269
270 print @result;
271
272 if ( $rc_update != 0 ) {
273 print @result;
274 die;
275 }
276
277}
278
279sub svn_update( ;$ )
280{
281 my $update_path = shift || $DASSCM_REPO;
282 ( my $rc_update, my @result ) =
283 run_command("$SVN update $svnCheckoutCredentials $update_path");
284 if ( $rc_update != 0 ) {
285 print @result;
286 die;
287 }
288
289 print @result;
290
291}
292
293#####################################################################
294#
295# functions
296
297sub help(;@)
298{
299 if ( @_ == 0 ) {
300 usage();
301 } else {
302 print "help for @_: ...\n";
303 usage();
304 }
305}
306
307sub login(@)
308{
309 check_parameter( @_, 1 );
310 check_env();
311
312 my $input_username = $1;
313
314 if ( not $input_username ) {
315 my $output_username = "";
316 if ($DASSCM_USERNAME) {
317 $output_username = " ($DASSCM_USERNAME)";
318 }
319
320 print "Enter DASSCM user name", $output_username, ": ";
321 $input_username = <STDIN>;
322 chomp($input_username);
323 }
324
325 # hidden password input
326 print "Enter DASSCM user password: ";
327 ReadMode('noecho');
328 my $input_password = <STDIN>;
329 ReadMode('normal');
330 chomp($input_password);
331
332 svn_check_credentials( $input_username, $input_password );
333
334 #
335 # set environment variables
336 #
337 $ENV{'DASSCM_USERNAME'} = $input_username;
338 $ENV{'DASSCM_PASSWORD'} = $input_password;
339
340 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
341 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
342 "DASSCM_REPO: $DASSCM_REPO\n",
343 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n", "[dasscm shell]\n\n";
344
345 exec("bash") or die "failed to start new shell";
346}
347
348sub init(@)
349{
350 check_parameter( @_, 1 );
351 check_env();
352
353 # update complete repository
354 my $retcode =
355 run_interactive(
356 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY"
357 );
358}
359
360#
361# add (is used for command add and commit)
362#
363sub add(@)
364{
365 check_parameter( @_, 1 );
366 check_env();
367
368 (
369 my $basename,
370 my $dirname_prod,
371 my $dirname_repo,
372 my $filename_prod,
373 my $filename_repo
374 )
375 = get_filenames( $_[0] );
376
377 if ( $command eq "add" ) {
378 mkpath($dirname_repo);
379 }
380
381 # update complete repository
382 my $retcode = run_interactive("$SVN update $svnOptions $DASSCM_REPO");
383
384 copy( $filename_prod, $filename_repo ) or die $!;
385
386 if ( $command eq "add" ) {
387
388 # already checked in?
389 chdir($DASSCM_REPO);
390
391 # also add the path to filename.
392 for my $dir ( split( '/', $dirname_prod ) ) {
393 if ($dir) {
394 run_command("$SVN add --non-recursive $dir");
395 chdir $dir;
396 }
397 }
398 run_command("$SVN add $basename");
399 }
400
401 if ( $options{'message'} ) {
402 $svnOptions .= " --message \"$options{'message'}\" ";
403 }
404
405 # commit calls $EDITOR. uses "interactive" here, to display output
406 $retcode =
407 run_interactive(
408 "$SVN commit $svnOptions --username $DASSCM_USERNAME $svnPasswordCredentials $DASSCM_REPO"
409 );
410
411 print $filename_prod. "\n";
412 print $dirname_repo. "\n";
413}
414
415sub blame(@)
416{
417 check_parameter( @_, 1 );
418 check_env();
419
420 (
421 my $basename,
422 my $dirname_prod,
423 my $dirname_repo,
424 my $filename_prod,
425 my $filename_repo
426 )
427 = get_filenames( $_[0] );
428
429 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
430}
431
432sub diff(@)
433{
434 check_parameter( @_, 1 );
435 check_env();
436
437 (
438 my $basename,
439 my $dirname_prod,
440 my $dirname_repo,
441 my $filename_prod,
442 my $filename_repo
443 )
444 = get_filenames( $_[0] );
445
446 #print "$basename,$dirname_prod,$dirname_repo\n";
447
448 ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
449 if ( $rc_update != 0 ) {
450 print @result;
451 die;
452 }
453
454 ( my $rc_diff, my @diff ) =
455 run_command("diff $filename_repo $filename_prod");
456 print @diff;
457}
458
459sub status(@)
460{
461 check_parameter( @_, 1 );
462 check_env();
463
464 #
465 # update local repository
466 #
467 svn_update();
468
469 # TODO: start at subdirectories ?
470 my $cvsworkdir = $DASSCM_REPO;
471
472 File::Find::find( \&cvscheck, $cvsworkdir );
473
474 # Liste der geänderten Files ausgeben, falls nicht leer
475 # Anzahl Elemente im Hash???
476 my @changedfiles = keys %status_changedfiles;
477
478 if ( %status_changedfiles or %status_removedfiles ) {
479 if (%status_removedfiles) {
480 print "deleted files:\n";
481 foreach my $key ( values %status_removedfiles ) {
482 print "$key\n";
483 }
484 print "\n";
485 }
486
487 if (%status_changedfiles) {
488 print "modified files:\n";
489 foreach my $key ( keys %status_changedfiles ) {
490 print "$key\n";
491 }
492 }
493 } else {
494 print "no modified files found in $cvsworkdir\n";
495 }
496 print "\n";
497
498}
499
500#####################################################################
501#
502# main
503#
504
505my $number_arguments = @ARGV;
506
507if ( $number_arguments > 0 ) {
508
509 # get subcommand and remove it from @ARGV
510 $command = $ARGV[0];
511 shift @ARGV;
512
513 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
514 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
515
516 # TODO: check variables
517 $DASSCM_SVN_REPOSITORY =
518 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
519
520 my $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
521 my $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
522
523 #
524 # if a user is given by dasscm configuration file, we use it.
525 # Otherwise we expect that read-only account is configured
526 # as local subversion configuration.
527 # If this is also not the case,
528 # user is required to type username and password.
529 # This will be stored as local subversion configuration thereafter.
530 #
531 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
532 $svnCheckoutCredentials =
533 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
534 }
535
536 # get command line options and store them in options hash
537 my $result = GetOptions( \%options, 'verbose', 'message=s' );
538
539 # print options
540 foreach my $option ( keys %options ) {
541 print $option. ": " . $options{$option} . "\n";
542 }
543
544 # set verbose to command line option
545 $verbose = $options{'verbose'};
546
547 #
548 # action accordinly to command are taken
549 # $command is rewritten in standard format,
550 # so we can test for it later on more simply
551 #
552 $_ = $command;
553 if (m/help/i) {
554 help(@ARGV);
555 } elsif (m/login/i) {
556 $command = "login";
557 login(@ARGV);
558 } elsif (m/init/i) {
559 $command = "init";
560 init(@ARGV);
561 } elsif (m/add/i) {
562 $command = "add";
563 add(@ARGV);
564 } elsif (m/commit/i) {
565 $command = "commit";
566 add(@ARGV);
567 } elsif (m/blame/i) {
568 $command = "blame";
569 blame(@ARGV);
570 } elsif (m/diff/i) {
571 $command = "diff";
572 diff(@ARGV);
573 } elsif (m/status/i) {
574 $command = "status";
575 status(@ARGV);
576
577 # } elsif (m/activate/i) {
578 # ## TODO
579 # activate(@ARGV);
580 } else {
581 usage();
582 check_env();
583 }
584
585 # up
586 # cleanup (svn-commit.tmp)
587 # commitall
588 # revert
589}
Note: See TracBrowser for help on using the repository browser.