source: trunk/dasscm/dasscm@ 219

Last change on this file since 219 was 218, checked in by joergs, on Sep 13, 2007 at 4:09:52 PM

performance improvements (svn ls)

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 18.3 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 218 2007-09-13 14:09:52Z 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::stat;
15use File::Path;
16use Term::ReadKey;
17
18#
19# used ConfigFile instead of SmartClient::Config,
20# because the huge amount of SmartClient dependencies
21#use SmartClient::Config;
22use ConfigFile;
23
24#####################################################################
25#
26# global
27#
28
29# file to store permissions
30my $permissions_file = "/etc/permissions.d/dasscm.permission_backup";
31# configuration file
32my $config_file = "/etc/dasscm.conf";
33my $config = ConfigFile::read_config_file($config_file);
34my $DASSCM_LOCAL_REPOSITORY_BASE;
35my $DASSCM_REPOSITORY_NAME;
36my $DASSCM_SVN_REPOSITORY;
37
38my $SVN = "svn ";
39my $svnOptions = "";
40my $svnCheckoutCredentials = "";
41my $svnPasswordCredentials = "";
42
43# command line options get stored in options hash
44my %options = ();
45
46# subcommand, that gets executed (add, commit, ...)
47my $command;
48
49my $verbose = 0;
50
51
52#####################################################################
53#
54# util functions
55#
56sub usage()
57{
58 print "usage: dasscm <subcommand> [options] [args]\n";
59 print "\n";
60 print "dasscm is intended to help versioning configuration files\n";
61 print "\n";
62 print "Available subcommands:\n";
63 print " help <subcommand>\n";
64 print " init\n";
65 print " login\n";
66 print " up\n";
67 print " ls\n";
68 print " add <filename>\n";
69 print " commit <filename>\n";
70 print " status <filename>\n";
71 print " diff <filename>\n";
72 print " permissions\n";
73 print "\n";
74 print "preperation:\n",
75 " if dasscm is already configured,\n",
76 " use 'dasscm login' and than eg. 'add'.\n",
77 " The environment variables\n",
78 " DASSCM_REPO\n",
79 " DASSCM_PROD\n",
80 " DASSCM_USERNAME\n",
81 " DASSCM_PASSWORD\n",
82 " are evaluated, but set automatically by 'dasscm login'.\n",
83 "\n",
84 " If dasscm is not yet configured, read",
85 " /usr/share/doc/packages/dasscm/dasscm_howto.txt\n";
86}
87
88sub check_env()
89{
90
91 # DASSCM_PROD
92 if ( !$DASSCM_PROD ) {
93 $DASSCM_PROD = "/";
94 }
95
96 if ( !-d $DASSCM_PROD ) {
97 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
98 }
99 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
100
101 # DASSCM_REPOSITORY_NAME
102 if ( !$DASSCM_REPOSITORY_NAME ) {
103 die
104 "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";
105 }
106
107 # DASSCM_REPO
108 if ( !$DASSCM_REPO ) {
109 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
110 $DASSCM_REPO =
111 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
112 } else {
113 die
114 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
115 }
116 }
117 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
118
119 #
120 # check if local repository directory exist (if not creating by init)
121 #
122 if ( $command ne "init" ) {
123 if ( not -d $DASSCM_REPO ) {
124 die
125 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
126 }
127
128 #
129 # user settings
130 #
131
132 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
133 if ( !$DASSCM_USERNAME ) {
134 $DASSCM_USERNAME = $DASSCM_USER;
135 }
136
137 # user root is not allowed for checkins.
138 # if user is root, DASSCM_USER has to be set,
139 # otherwise USER can be used
140 if ( "$USER" eq "root" ) {
141 if ( ( not $DASSCM_USERNAME ) and ( $command ne "login" ) ) {
142 die
143 "Envirnonment variable DASSCM_USERNAME not set.\nSet DASSCM_USERNAME to your subversion user account.\n";
144 }
145 $svnOptions .= " --no-auth-cache ";
146 } elsif ( !$DASSCM_USERNAME ) {
147 $DASSCM_USERNAME = $USER;
148 }
149
150 #
151 # password
152 #
153 if ($DASSCM_PASSWORD) {
154 $svnPasswordCredentials = " --password $DASSCM_PASSWORD ";
155 }
156 }
157
158 #$svnOptions .= " --username $DASSCM_USERNAME "
159}
160
161sub check_parameter(@)
162{
163}
164
165sub get_filenames(@)
166{
167 my $filename_prod = $_[0];
168 if ( !( $filename_prod =~ m/^\// ) ) {
169 $filename_prod = cwd() . "/" . $filename_prod;
170 }
171
172 -r $filename_prod or die "$filename_prod is not accessable";
173
174 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
175 # "/etc" is used as filename
176 my $dirname_prod = dirname($filename_prod);
177 chdir $dirname_prod or die $!;
178 $dirname_prod = cwd();
179 my $basename = basename($filename_prod);
180
181 if ($verbose) {
182 print "dir: " . $dirname_prod . "\n";
183 print "fn: " . $basename . "\n";
184 }
185
186 my $dirname_repo = $DASSCM_REPO . "/" . $dirname_prod;
187 my $filename_repo = "$dirname_repo/$basename";
188
189 return (
190 $basename, $dirname_prod, $dirname_repo,
191 $filename_prod, $filename_repo
192 );
193}
194
195
196
197sub generatePermissionList
198{
199
200 # generieren der Zeilen für Permission-Savefile
201 my @files = @_;
202 my @permlist = ();
203 foreach my $file (@files) {
204 my $info = stat( "/" . $file ) || die "$file: stat error";
205 my $mode = get_type( $info->mode ) & 07777;
206 my $modestring = sprintf( "%04o", $mode );
207 my $uid = $info->uid;
208 my $uidname = getpwuid($uid);
209 my $gid = $info->gid;
210 my $gidname = getgrgid($gid);
211 push(
212 @permlist,
213 sprintf( "%-55s %-17s %4d",
214 $file, "${uidname}:${gidname}", $modestring )
215 );
216 }
217 return @permlist;
218}
219
220sub get_type
221{
222
223 # Funktion übernommen aus /usr/bin/chkstat
224 my $S_IFLNK = 0120000; # symbolic link
225 my $S_IFREG = 0100000; # regular file
226 my $S_IFDIR = 0040000; # directory
227 my $S_IFCHAR = 0020000; # character device
228 my $S_IFBLK = 0060000; # block device
229 my $S_IFFIFO = 0010000; # fifo
230 my $S_IFSOCK = 0140000; # socket
231 my $S_IFMT = 0170000; # type of file
232
233 my $S_m;
234 if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
235 elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
236 elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
237 elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
238 elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
239 elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
240 elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
241 $S_m;
242}
243
244sub run_command
245{
246 my $command = shift;
247
248 #print "executing command: " . $command . "\n";
249
250 open( RESULT, $command . ' 2>&1 |' );
251 my @result = <RESULT>;
252 close(RESULT);
253 my $retcode = $? >> 8;
254
255 #print @result;
256 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
257
258 return ( $retcode, @result );
259}
260
261sub run_interactive
262{
263
264 if ($verbose) {
265 print "run_interactive:" . join( " ", @_ ) . "\n";
266 }
267
268 system(@_);
269 if ( $? == -1 ) {
270 printf "failed to execute: $!\n";
271 } elsif ( $? & 127 ) {
272 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
273 ( $? & 128 ) ? 'with' : 'without';
274 } elsif ( $? >> 8 != 0 ) {
275 printf "child exited with value %d\n", $? >> 8;
276 }
277 return ( $? >> 8 );
278}
279
280sub svn_check_credentials( $$ )
281{
282 my $username = shift;
283 my $password = shift;
284
285 ( my $rc_update, my @result ) =
286 run_command(
287 "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
288 );
289
290 print @result;
291
292 if ( $rc_update != 0 ) {
293 die;
294 }
295
296}
297
298sub svn_update( ;$ )
299{
300 my $update_path = shift || $DASSCM_REPO;
301 ( my $rc_update, my @result ) =
302 run_command("$SVN update $svnCheckoutCredentials $update_path");
303 print @result;
304 if ( $rc_update != 0 ) {
305 die;
306 }
307}
308
309sub svn_getStoredFiles( ;$ )
310{
311 # TODO: get_filenames?
312 #my $rel_path = shift || "";
313 #my $path = "${DASSCM_REPO}/${rel_path}";
314 my $path = ${DASSCM_REPO};
315 # svn ls -R is better, but much, much slower
316 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
317 ( my $rc, my @result ) = run_command("cd $path && find | grep -v '/.svn' | sed -e 's/\.\\///' | grep -v '^\$'");
318 if ( $rc != 0 ) {
319 print @result;
320 die;
321 }
322 chomp(@result);
323 return @result;
324}
325
326#####################################################################
327#
328# functions
329
330sub help(;@)
331{
332 if ( @_ == 0 ) {
333 usage();
334 } else {
335 print "help for @_: ...\n";
336 usage();
337 }
338}
339
340sub login(@)
341{
342 check_parameter( @_, 1 );
343 check_env();
344
345 my $input_username = $1;
346
347 if ( not $input_username ) {
348 my $output_username = "";
349 if ($DASSCM_USERNAME) {
350 $output_username = " ($DASSCM_USERNAME)";
351 }
352
353 print "Enter DASSCM user name", $output_username, ": ";
354 $input_username = <STDIN>;
355 chomp($input_username);
356 }
357
358 # hidden password input
359 print "Enter DASSCM user password: ";
360 ReadMode('noecho');
361 my $input_password = <STDIN>;
362 ReadMode('normal');
363 chomp($input_password);
364
365 svn_check_credentials( $input_username, $input_password );
366
367 #
368 # set environment variables
369 #
370 $ENV{'DASSCM_USERNAME'} = $input_username;
371 $ENV{'DASSCM_PASSWORD'} = $input_password;
372
373 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
374 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
375 "DASSCM_REPO: $DASSCM_REPO\n",
376 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n", "[dasscm shell]\n\n";
377
378 exec("bash") or die "failed to start new shell";
379}
380
381sub init(@)
382{
383 check_parameter( @_, 1 );
384 check_env();
385
386 # update complete repository
387 # and create permission file
388 my $retcode =
389 run_interactive(
390 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; touch $permissions_file"
391 );
392}
393
394sub ls(@)
395{
396 check_parameter( @_, 1 );
397 check_env();
398
399 my @files = svn_getStoredFiles(@_);
400
401 print join( "\n", @files );
402 print "\n";
403}
404
405
406sub update(@)
407{
408 check_parameter( @_, 1 );
409 check_env();
410
411 #
412 # update local repository
413 #
414 svn_update();
415}
416
417
418sub add_helper(@)
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 if ( $command eq "add" ) {
430 mkpath($dirname_repo);
431 }
432
433 copy( $filename_prod, $filename_repo ) or die $!;
434
435 if ( $command eq "add" ) {
436
437 # already checked in?
438 chdir($DASSCM_REPO);
439
440 # also add the path to filename.
441 for my $dir ( split( '/', $dirname_prod ) ) {
442 if ($dir) {
443 run_command("$SVN add --non-recursive $dir");
444 chdir $dir;
445 }
446 }
447 run_command("$SVN add $basename");
448 }
449}
450
451
452
453#
454# add (is used for command add and commit)
455#
456sub add(@)
457{
458 check_parameter( @_, 1 );
459 check_env();
460
461 #
462 # update local repository
463 #
464 svn_update();
465
466 # add file
467 add_helper( $_[0] );
468 # create new permissions file
469 permissions();
470 # add permissions file
471 add_helper( $permissions_file );
472
473 if ( $options{'message'} ) {
474 $svnOptions .= " --message \"$options{'message'}\" ";
475 }
476
477 # commit calls $EDITOR. uses "interactive" here, to display output
478 my $retcode =
479 run_interactive(
480 "$SVN commit $svnOptions --username $DASSCM_USERNAME $svnPasswordCredentials $DASSCM_REPO"
481 );
482
483 #print $filename_prod. "\n";
484 #print $dirname_repo. "\n";
485}
486
487
488
489sub blame(@)
490{
491 check_parameter( @_, 1 );
492 check_env();
493
494 (
495 my $basename,
496 my $dirname_prod,
497 my $dirname_repo,
498 my $filename_prod,
499 my $filename_repo
500 )
501 = get_filenames( $_[0] );
502
503 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
504}
505
506sub diff(@)
507{
508 check_parameter( @_, 1 );
509 check_env();
510
511 (
512 my $basename,
513 my $dirname_prod,
514 my $dirname_repo,
515 my $filename_prod,
516 my $filename_repo
517 )
518 = get_filenames( $_[0] );
519
520 #print "$basename,$dirname_prod,$dirname_repo\n";
521
522 ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
523 if ( $rc_update != 0 ) {
524 print @result;
525 die;
526 }
527
528 ( my $rc_diff, my @diff ) =
529 run_command("diff $filename_repo $filename_prod");
530 print @diff;
531}
532
533sub status(@)
534{
535 check_parameter( @_, 1 );
536 check_env();
537
538 #
539 # update local repository
540 #
541 svn_update();
542
543 # TODO: start at subdirectories ?
544 my $dir = $DASSCM_REPO;
545 my @files = svn_getStoredFiles($dir);
546
547 # Liste der geänderten Files ausgeben, falls nicht leer
548 if (@files) {
549
550 # stores result from status (cvscheck)
551 my %removedfiles = ();
552 my %changedfiles = ();
553
554 foreach my $file (@files) {
555
556 my $realfile = "/" . $file;
557 my $cvsworkfile = "${DASSCM_REPO}/${file}";
558
559 if ( -d $realfile ) {
560
561 # directory. do nothing
562 } elsif ( !-r $realfile ) {
563 $removedfiles{"$realfile"} = $cvsworkfile;
564 } else {
565 ( -r "$cvsworkfile" )
566 || die("Fehler: $cvsworkfile ist nicht lesbar");
567 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
568 $changedfiles{"$realfile"} = $cvsworkfile;
569 }
570 }
571 }
572
573 if (%removedfiles) {
574 print "deleted files (found in repository, but not in system):\n";
575 foreach my $key ( values %removedfiles ) {
576 print "$key\n";
577 }
578 print "\n";
579 }
580
581 if (%changedfiles) {
582 print "modified files:\n";
583 foreach my $key ( keys %changedfiles ) {
584 print "$key\n";
585 }
586 }
587 } else {
588 print "no modified files found in $dir\n";
589 }
590
591 print "\n";
592}
593
594
595
596sub permissions(@)
597{
598 check_parameter( @_, 1 );
599 check_env();
600
601 #
602 # update local repository
603 #
604 #svn_update();
605
606 # TODO: start at subdirectories ?
607 my $dir = $DASSCM_REPO;
608 my @files = svn_getStoredFiles($dir);
609
610 if (@files) {
611
612 # generieren der Permissions
613 my @permissions = generatePermissionList(@files);
614 my $OUTFILE;
615 my $tofile = 0; # Status für schreiben in File
616
617 if ( -w dirname($permissions_file) ) {
618
619 # Verzeichnis existiert => schreiben
620 print "storing permissions in file $permissions_file\n";
621 open( OUTFILE, ">$permissions_file" )
622 || die("failed to write to $permissions_file: $!");
623 $tofile = 1; # Merken, daß in File geschrieben wird
624 print OUTFILE "#\n";
625 print OUTFILE "# created by dasscm permissions\n";
626 print OUTFILE "# It is intended to be used for restoring permissions\n";
627 } else {
628
629 # Pfad für Sicherungsdatei existiert nicht => schreiben auf stdout
630 # Alias Filehandle für stdout erzeugen
631 *OUTFILE = *STDOUT;
632 }
633 foreach my $line (@permissions) {
634 print OUTFILE "$line\n";
635 }
636
637 if ($tofile) {
638 close(OUTFILE);
639 }
640 }
641}
642
643#####################################################################
644#
645# main
646#
647
648my $number_arguments = @ARGV;
649
650if ( $number_arguments > 0 ) {
651
652 # get subcommand and remove it from @ARGV
653 $command = $ARGV[0];
654 shift @ARGV;
655
656 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
657 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
658
659 # TODO: check variables
660 $DASSCM_SVN_REPOSITORY =
661 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
662
663 my $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
664 my $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
665
666 #
667 # if a user is given by dasscm configuration file, we use it.
668 # Otherwise we expect that read-only account is configured
669 # as local subversion configuration.
670 # If this is also not the case,
671 # user is required to type username and password.
672 # This will be stored as local subversion configuration thereafter.
673 #
674 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
675 $svnCheckoutCredentials =
676 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
677 }
678
679 # get command line options and store them in options hash
680 my $result = GetOptions( \%options, 'verbose', 'message=s' );
681
682 # print options
683 foreach my $option ( keys %options ) {
684 print "${option}: $options{$option}\n";
685 }
686
687 # set verbose to command line option
688 $verbose = $options{'verbose'};
689
690 #
691 # action accordinly to command are taken
692 # $command is rewritten in standard format,
693 # so we can test for it later on more simply
694 #
695 $_ = $command;
696 if (m/help/i) {
697 help(@ARGV);
698 } elsif (m/login/i) {
699 $command = "login";
700 login(@ARGV);
701 } elsif (m/init/i) {
702 $command = "init";
703 init(@ARGV);
704 } elsif (m/ls/i) {
705 $command = "ls";
706 ls(@ARGV);
707 } elsif (m/up/i) {
708 $command = "update";
709 update(@ARGV);
710 } elsif (m/add/i) {
711 $command = "add";
712 add(@ARGV);
713 } elsif (m/commit/i) {
714 $command = "commit";
715 add(@ARGV);
716 } elsif (m/blame/i) {
717 $command = "blame";
718 blame(@ARGV);
719 } elsif (m/diff/i) {
720 $command = "diff";
721 diff(@ARGV);
722 } elsif (m/status/i) {
723 $command = "status";
724 status(@ARGV);
725 } elsif (m/permissions/i) {
726 $command = "permissions";
727 permissions(@ARGV);
728 } else {
729 print "unknown command: $command\n\n";
730 usage();
731 check_env();
732 }
733
734 # cleanup (svn-commit.tmp)
735 # commitall
736 # revert
737 # activate
738}
Note: See TracBrowser for help on using the repository browser.