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
RevLine 
[186]1#!/usr/bin/perl -w
2
3# $Id: dasscm 218 2007-09-13 14:09:52Z joergs $
4
5use strict;
6
[208]7use Env
8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD);
[186]9use Cwd;
[214]10use Getopt::Long;
[186]11use File::Basename;
[209]12use File::Compare;
13use File::Copy;
[186]14use File::stat;
15use File::Path;
[214]16use Term::ReadKey;
[186]17
[205]18#
19# used ConfigFile instead of SmartClient::Config,
20# because the huge amount of SmartClient dependencies
21#use SmartClient::Config;
22use ConfigFile;
23
[189]24#####################################################################
25#
[186]26# global
[189]27#
[205]28
[215]29# file to store permissions
30my $permissions_file = "/etc/permissions.d/dasscm.permission_backup";
31# configuration file
[205]32my $config_file = "/etc/dasscm.conf";
[214]33my $config = ConfigFile::read_config_file($config_file);
[205]34my $DASSCM_LOCAL_REPOSITORY_BASE;
35my $DASSCM_REPOSITORY_NAME;
36my $DASSCM_SVN_REPOSITORY;
37
38my $SVN = "svn ";
39my $svnOptions = "";
40my $svnCheckoutCredentials = "";
41my $svnPasswordCredentials = "";
42
[196]43# command line options get stored in options hash
[205]44my %options = ();
45
[197]46# subcommand, that gets executed (add, commit, ...)
[196]47my $command;
[186]48
[205]49my $verbose = 0;
50
[209]51
[189]52#####################################################################
53#
[186]54# util functions
[189]55#
[187]56sub usage()
57{
[205]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";
[215]63 print " help <subcommand>\n";
[205]64 print " init\n";
65 print " login\n";
[215]66 print " up\n";
67 print " ls\n";
[205]68 print " add <filename>\n";
69 print " commit <filename>\n";
[214]70 print " status <filename>\n";
[205]71 print " diff <filename>\n";
[215]72 print " permissions\n";
[205]73 print "\n";
[217]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";
[187]86}
87
[186]88sub check_env()
89{
[205]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
[208]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 }
[205]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 }
[215]117 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
[205]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
[208]125 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
[205]126 }
[208]127
[205]128 #
129 # user settings
130 #
[208]131
[205]132 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
[208]133 if ( !$DASSCM_USERNAME ) {
134 $DASSCM_USERNAME = $DASSCM_USER;
[205]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" ) {
[208]141 if ( ( not $DASSCM_USERNAME ) and ( $command ne "login" ) ) {
[205]142 die
[208]143 "Envirnonment variable DASSCM_USERNAME not set.\nSet DASSCM_USERNAME to your subversion user account.\n";
[205]144 }
145 $svnOptions .= " --no-auth-cache ";
146 } elsif ( !$DASSCM_USERNAME ) {
147 $DASSCM_USERNAME = $USER;
148 }
149
150 #
151 # password
152 #
[208]153 if ($DASSCM_PASSWORD) {
[205]154 $svnPasswordCredentials = " --password $DASSCM_PASSWORD ";
155 }
156 }
157
158 #$svnOptions .= " --username $DASSCM_USERNAME "
[186]159}
160
161sub check_parameter(@)
162{
163}
164
[187]165sub get_filenames(@)
166{
[205]167 my $filename_prod = $_[0];
168 if ( !( $filename_prod =~ m/^\// ) ) {
169 $filename_prod = cwd() . "/" . $filename_prod;
170 }
[187]171
[205]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
[214]181 if ($verbose) {
182 print "dir: " . $dirname_prod . "\n";
183 print "fn: " . $basename . "\n";
184 }
[205]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 );
[187]193}
194
[215]195
196
197sub generatePermissionList
[209]198{
199
[215]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}
[209]219
[215]220sub get_type
221{
[209]222
[215]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
[209]232
[215]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;
[209]242}
243
[186]244sub run_command
245{
[205]246 my $command = shift;
[186]247
[205]248 #print "executing command: " . $command . "\n";
[186]249
[205]250 open( RESULT, $command . ' 2>&1 |' );
251 my @result = <RESULT>;
252 close(RESULT);
253 my $retcode = $? >> 8;
[186]254
[205]255 #print @result;
256 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
[186]257
[205]258 return ( $retcode, @result );
[186]259}
260
[205]261sub run_interactive
262{
[186]263
[208]264 if ($verbose) {
[205]265 print "run_interactive:" . join( " ", @_ ) . "\n";
266 }
[196]267
[205]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( $$ )
[196]281{
[205]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
[196]296}
297
[205]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");
[215]303 print @result;
[205]304 if ( $rc_update != 0 ) {
305 die;
306 }
[215]307}
[196]308
[215]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};
[218]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 '^\$'");
[215]318 if ( $rc != 0 ) {
319 print @result;
320 die;
321 }
322 chomp(@result);
323 return @result;
[205]324}
325
[189]326#####################################################################
327#
[186]328# functions
329
330sub help(;@)
331{
[205]332 if ( @_ == 0 ) {
333 usage();
334 } else {
335 print "help for @_: ...\n";
[214]336 usage();
[205]337 }
[186]338}
339
[203]340sub login(@)
341{
[205]342 check_parameter( @_, 1 );
343 check_env();
[203]344
[214]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);
[205]356 }
[203]357
[205]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);
[203]364
[205]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
[209]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";
[205]377
[209]378 exec("bash") or die "failed to start new shell";
[203]379}
380
[205]381sub init(@)
382{
383 check_parameter( @_, 1 );
384 check_env();
385
386 # update complete repository
[216]387 # and create permission file
[208]388 my $retcode =
389 run_interactive(
[216]390 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY; touch $permissions_file"
[208]391 );
[205]392}
393
[215]394sub ls(@)
[186]395{
[205]396 check_parameter( @_, 1 );
397 check_env();
[186]398
[215]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{
[205]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] );
[186]428
[205]429 if ( $command eq "add" ) {
430 mkpath($dirname_repo);
431 }
[186]432
[205]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 }
[215]449}
[205]450
[215]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
[205]473 if ( $options{'message'} ) {
474 $svnOptions .= " --message \"$options{'message'}\" ";
475 }
476
477 # commit calls $EDITOR. uses "interactive" here, to display output
[215]478 my $retcode =
[205]479 run_interactive(
[208]480 "$SVN commit $svnOptions --username $DASSCM_USERNAME $svnPasswordCredentials $DASSCM_REPO"
481 );
[205]482
[215]483 #print $filename_prod. "\n";
484 #print $dirname_repo. "\n";
[186]485}
486
[215]487
488
[193]489sub blame(@)
490{
[205]491 check_parameter( @_, 1 );
492 check_env();
[193]493
[205]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");
[193]504}
505
[187]506sub diff(@)
507{
[205]508 check_parameter( @_, 1 );
509 check_env();
[187]510
[205]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;
[187]531}
532
[209]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 ?
[215]544 my $dir = $DASSCM_REPO;
545 my @files = svn_getStoredFiles($dir);
[209]546
547 # Liste der geänderten Files ausgeben, falls nicht leer
[215]548 if (@files) {
[209]549
[215]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 ) {
[209]576 print "$key\n";
577 }
578 print "\n";
579 }
580
[215]581 if (%changedfiles) {
[209]582 print "modified files:\n";
[215]583 foreach my $key ( keys %changedfiles ) {
[209]584 print "$key\n";
585 }
586 }
587 } else {
[215]588 print "no modified files found in $dir\n";
[209]589 }
[215]590
[209]591 print "\n";
[215]592}
[209]593
[215]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" )
[216]622 || die("failed to write to $permissions_file: $!");
[215]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 }
[209]641}
642
[189]643#####################################################################
644#
[186]645# main
[189]646#
[186]647
648my $number_arguments = @ARGV;
649
[205]650if ( $number_arguments > 0 ) {
[186]651
[205]652 # get subcommand and remove it from @ARGV
653 $command = $ARGV[0];
654 shift @ARGV;
[196]655
[205]656 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
657 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
[196]658
[205]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
[214]680 my $result = GetOptions( \%options, 'verbose', 'message=s' );
[205]681
682 # print options
683 foreach my $option ( keys %options ) {
[215]684 print "${option}: $options{$option}\n";
[205]685 }
686
[214]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 #
[205]695 $_ = $command;
696 if (m/help/i) {
697 help(@ARGV);
698 } elsif (m/login/i) {
[208]699 $command = "login";
[205]700 login(@ARGV);
701 } elsif (m/init/i) {
[208]702 $command = "init";
[205]703 init(@ARGV);
[215]704 } elsif (m/ls/i) {
705 $command = "ls";
706 ls(@ARGV);
707 } elsif (m/up/i) {
708 $command = "update";
709 update(@ARGV);
[205]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) {
[208]717 $command = "blame";
[205]718 blame(@ARGV);
719 } elsif (m/diff/i) {
[208]720 $command = "diff";
[205]721 diff(@ARGV);
[209]722 } elsif (m/status/i) {
723 $command = "status";
724 status(@ARGV);
[215]725 } elsif (m/permissions/i) {
726 $command = "permissions";
727 permissions(@ARGV);
[205]728 } else {
[215]729 print "unknown command: $command\n\n";
[205]730 usage();
731 check_env();
732 }
733
[209]734 # cleanup (svn-commit.tmp)
[205]735 # commitall
736 # revert
[215]737 # activate
[186]738}
Note: See TracBrowser for help on using the repository browser.