source: trunk/dasscm/dasscm@ 213

Last change on this file since 213 was 209, checked in by joergs, on Jul 2, 2007 at 6:01:28 PM

migrated from dasscm_chkconf to dasscm status

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 14.3 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 209 2007-07-02 16:01:28Z joergs $
4
5use strict;
6
7use Env
8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD);
9use Cwd;
10use POSIX qw/getpgrp tcgetpgrp/;
11use Term::ReadKey;
12use File::Basename;
13use File::Compare;
14use File::Copy;
15use File::Find;
16use File::stat;
17use File::Path;
18use Getopt::Long;
19
20#
21# used ConfigFile instead of SmartClient::Config,
22# because the huge amount of SmartClient dependencies
23#use SmartClient::Config;
24use ConfigFile;
25
26#####################################################################
27#
28# global
29#
30
31my $config_file = "/etc/dasscm.conf";
32
33# my $config = SmartClient::Config->( $config_file );
34my $config = ConfigFile::read_config_file($config_file);
35my $DASSCM_LOCAL_REPOSITORY_BASE;
36my $DASSCM_REPOSITORY_NAME;
37my $DASSCM_SVN_REPOSITORY;
38
39my $SVN = "svn ";
40my $svnOptions = "";
41my $svnCheckoutCredentials = "";
42my $svnPasswordCredentials = "";
43
44# command line options get stored in options hash
45my %options = ();
46
47# subcommand, that gets executed (add, commit, ...)
48my $command;
49
50my $verbose = 0;
51
52# stores result from status (cvscheck)
53my %status_removedfiles = ();
54my %status_changedfiles = ();
55
56#####################################################################
57#
58# util functions
59#
60sub usage()
61{
62 print "usage: dasscm <subcommand> [options] [args]\n";
63 print "\n";
64 print "dasscm is intended to help versioning configuration files\n";
65 print "\n";
66 print "Available subcommands:\n";
67 print " init\n";
68 print " login\n";
69 print " add <filename>\n";
70 print " commit <filename>\n";
71 print " diff <filename>\n";
72 print " help <subcommand>\n";
73 print "\n";
74 print "preperation:\n";
75 print "check out the configuration repository, e.g.\n";
76 print
77 "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/dasscm/HOSTNAME\n";
78 print "environment variables\n", " DASSCM_REPO\n", " DASSCM_PROD¸n",
79 " DASSCM_USERNAME\n", " DASSCM_PASSWORD\n", "are evaluated.\n";
80 print "\n";
81}
82
83sub check_env()
84{
85
86 # DASSCM_PROD
87 if ( !$DASSCM_PROD ) {
88 $DASSCM_PROD = "/";
89 }
90
91 if ( !-d $DASSCM_PROD ) {
92 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
93 }
94 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
95
96 # DASSCM_REPOSITORY_NAME
97 if ( !$DASSCM_REPOSITORY_NAME ) {
98 die
99 "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";
100 }
101
102 # DASSCM_REPO
103 if ( !$DASSCM_REPO ) {
104 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
105 $DASSCM_REPO =
106 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
107 } else {
108 die
109 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
110 }
111 }
112 print "DASSCM_REPO: " . $DASSCM_REPO . "\n";
113
114 #
115 # check if local repository directory exist (if not creating by init)
116 #
117 if ( $command ne "init" ) {
118 if ( not -d $DASSCM_REPO ) {
119 die
120 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
121 }
122
123 #
124 # user settings
125 #
126
127 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
128 if ( !$DASSCM_USERNAME ) {
129 $DASSCM_USERNAME = $DASSCM_USER;
130 }
131
132 # user root is not allowed for checkins.
133 # if user is root, DASSCM_USER has to be set,
134 # otherwise USER can be used
135 if ( "$USER" eq "root" ) {
136 if ( ( not $DASSCM_USERNAME ) and ( $command ne "login" ) ) {
137 die
138 "Envirnonment variable DASSCM_USERNAME not set.\nSet DASSCM_USERNAME to your subversion user account.\n";
139 }
140 $svnOptions .= " --no-auth-cache ";
141 } elsif ( !$DASSCM_USERNAME ) {
142 $DASSCM_USERNAME = $USER;
143 }
144
145 #
146 # password
147 #
148 if ($DASSCM_PASSWORD) {
149 $svnPasswordCredentials = " --password $DASSCM_PASSWORD ";
150 }
151 }
152
153 #$svnOptions .= " --username $DASSCM_USERNAME "
154}
155
156sub check_parameter(@)
157{
158}
159
160sub get_filenames(@)
161{
162 my $filename_prod = $_[0];
163 if ( !( $filename_prod =~ m/^\// ) ) {
164 $filename_prod = cwd() . "/" . $filename_prod;
165 }
166
167 -r $filename_prod or die "$filename_prod is not accessable";
168
169 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
170 # "/etc" is used as filename
171 my $dirname_prod = dirname($filename_prod);
172 chdir $dirname_prod or die $!;
173 $dirname_prod = cwd();
174 my $basename = basename($filename_prod);
175
176 print "dir: " . $dirname_prod . "\n";
177 print "fn: " . $basename . "\n";
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 }
304}
305
306sub login(@)
307{
308 check_parameter( @_, 1 );
309 check_env();
310
311 my $output_username = "";
312 if ($DASSCM_USERNAME) {
313 $output_username = " ($DASSCM_USERNAME)";
314 }
315
316 print "Enter DASSCM user name", $output_username, ": ";
317 my $input_username = <STDIN>;
318 chomp($input_username);
319
320 # hidden password input
321 print "Enter DASSCM user password: ";
322 ReadMode('noecho');
323 my $input_password = <STDIN>;
324 ReadMode('normal');
325 chomp($input_password);
326
327 svn_check_credentials( $input_username, $input_password );
328
329 #
330 # set environment variables
331 #
332 $ENV{'DASSCM_USERNAME'} = $input_username;
333 $ENV{'DASSCM_PASSWORD'} = $input_password;
334
335 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
336 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
337 "DASSCM_REPO: $DASSCM_REPO\n",
338 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n", "[dasscm shell]\n\n";
339
340 exec("bash") or die "failed to start new shell";
341}
342
343sub init(@)
344{
345 check_parameter( @_, 1 );
346 check_env();
347
348 # update complete repository
349 my $retcode =
350 run_interactive(
351 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY"
352 );
353}
354
355#
356# add (is used for command add and commit)
357#
358sub add(@)
359{
360 check_parameter( @_, 1 );
361 check_env();
362
363 (
364 my $basename,
365 my $dirname_prod,
366 my $dirname_repo,
367 my $filename_prod,
368 my $filename_repo
369 )
370 = get_filenames( $_[0] );
371
372 if ( $command eq "add" ) {
373 mkpath($dirname_repo);
374 }
375
376 # update complete repository
377 my $retcode = run_interactive("$SVN update $svnOptions $DASSCM_REPO");
378
379 copy( $filename_prod, $filename_repo ) or die $!;
380
381 if ( $command eq "add" ) {
382
383 # already checked in?
384 chdir($DASSCM_REPO);
385
386 # also add the path to filename.
387 for my $dir ( split( '/', $dirname_prod ) ) {
388 if ($dir) {
389 run_command("$SVN add --non-recursive $dir");
390 chdir $dir;
391 }
392 }
393 run_command("$SVN add $basename");
394 }
395
396 if ( $options{'message'} ) {
397 $svnOptions .= " --message \"$options{'message'}\" ";
398 }
399
400 # commit calls $EDITOR. uses "interactive" here, to display output
401 $retcode =
402 run_interactive(
403 "$SVN commit $svnOptions --username $DASSCM_USERNAME $svnPasswordCredentials $DASSCM_REPO"
404 );
405
406 print $filename_prod. "\n";
407 print $dirname_repo. "\n";
408}
409
410sub blame(@)
411{
412 check_parameter( @_, 1 );
413 check_env();
414
415 (
416 my $basename,
417 my $dirname_prod,
418 my $dirname_repo,
419 my $filename_prod,
420 my $filename_repo
421 )
422 = get_filenames( $_[0] );
423
424 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
425}
426
427sub diff(@)
428{
429 check_parameter( @_, 1 );
430 check_env();
431
432 (
433 my $basename,
434 my $dirname_prod,
435 my $dirname_repo,
436 my $filename_prod,
437 my $filename_repo
438 )
439 = get_filenames( $_[0] );
440
441 #print "$basename,$dirname_prod,$dirname_repo\n";
442
443 ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
444 if ( $rc_update != 0 ) {
445 print @result;
446 die;
447 }
448
449 ( my $rc_diff, my @diff ) =
450 run_command("diff $filename_repo $filename_prod");
451 print @diff;
452}
453
454sub status(@)
455{
456 check_parameter( @_, 1 );
457 check_env();
458
459 #
460 # update local repository
461 #
462 svn_update();
463
464 # TODO: start at subdirectories ?
465 my $cvsworkdir = $DASSCM_REPO;
466
467 File::Find::find( \&cvscheck, $cvsworkdir );
468
469 # Liste der geänderten Files ausgeben, falls nicht leer
470 # Anzahl Elemente im Hash???
471 my @changedfiles = keys %status_changedfiles;
472
473 if ( %status_changedfiles or %status_removedfiles ) {
474 if (%status_removedfiles) {
475 print "deleted files:\n";
476 foreach my $key ( values %status_removedfiles ) {
477 print "$key\n";
478 }
479 print "\n";
480 }
481
482 if (%status_changedfiles) {
483 print "modified files:\n";
484 foreach my $key ( keys %status_changedfiles ) {
485 print "$key\n";
486 }
487 }
488 } else {
489 print "no modified files found in $cvsworkdir\n";
490 }
491 print "\n";
492
493}
494
495#####################################################################
496#
497# main
498#
499
500my $number_arguments = @ARGV;
501
502if ( $number_arguments > 0 ) {
503
504 # get subcommand and remove it from @ARGV
505 $command = $ARGV[0];
506 shift @ARGV;
507
508 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
509 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
510
511 # TODO: check variables
512 $DASSCM_SVN_REPOSITORY =
513 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
514
515 my $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
516 my $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
517
518 #
519 # if a user is given by dasscm configuration file, we use it.
520 # Otherwise we expect that read-only account is configured
521 # as local subversion configuration.
522 # If this is also not the case,
523 # user is required to type username and password.
524 # This will be stored as local subversion configuration thereafter.
525 #
526 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
527 $svnCheckoutCredentials =
528 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
529 }
530
531 # get command line options and store them in options hash
532 my $result = GetOptions( \%options, 'message=s' );
533
534 # print options
535 foreach my $option ( keys %options ) {
536 print $option. ": " . $options{$option} . "\n";
537 }
538
539 $_ = $command;
540 if (m/help/i) {
541 help(@ARGV);
542 } elsif (m/login/i) {
543 $command = "login";
544 login(@ARGV);
545 } elsif (m/init/i) {
546 $command = "init";
547 init(@ARGV);
548 } elsif (m/add/i) {
549 ## rewrite command
550 $command = "add";
551 add(@ARGV);
552 } elsif (m/commit/i) {
553 $command = "commit";
554 add(@ARGV);
555 } elsif (m/blame/i) {
556 $command = "blame";
557 blame(@ARGV);
558 } elsif (m/diff/i) {
559 $command = "diff";
560 diff(@ARGV);
561 } elsif (m/status/i) {
562 $command = "status";
563 status(@ARGV);
564 } elsif (m/activate/i) {
565 ## TODO
566 activate(@ARGV);
567 } else {
568 usage();
569 check_env();
570 }
571
572 # up
573 # cleanup (svn-commit.tmp)
574 # commitall
575 # revert
576 # status (chkconf)
577}
Note: See TracBrowser for help on using the repository browser.