source: dasscm/trunk/usr/bin/dasscm@ 918

Last change on this file since 918 was 918, checked in by joergs, on Jul 26, 2010 at 6:16:32 PM

make plugin support work, permissions are now also handeld by plugin, dasscm permissions outputs only to STDOUT

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 46.8 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 918 2010-07-26 16:16:32Z joergs $
4
5use strict;
6
7use Env
8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD $SHELL);
9use Cwd;
10use Getopt::Long;
11use File::Basename;
12use File::Compare;
13## used system("cp -a"), because File::Copy does not keep permissions
14##use File::Copy;
15use File::Find;
16use File::stat;
17use File::Path;
18use Term::ReadKey;
19
20use Data::Dumper;
21
22#####################################################################
23#
24# global
25#
26
27# shell exit codes
28my $RETURN_OK = 0;
29my $RETURN_NOK = 1;
30
31# Nagios return codes
32my $RETURN_WARN = 1;
33my $RETURN_CRIT = 2;
34my $RETURN_UNKNOWN = 3;
35
36# documentation file (for usage)
37my $doc_file = "/usr/share/doc/packages/dasscm/dasscm_howto.txt";
38
39my $config_file = "/etc/dasscm.conf";
40my $config = get_config($config_file);
41
42my @OPTIONS_GLOBAL = ( 'help', 'verbose' );
43
44my %COMMANDS = (
45 'help' => 'help',
46 'login' => 'login',
47 'init' => 'init',
48 'ls' => 'ls',
49 'update' => 'update',
50 'up' => 'update',
51 'add' => 'add',
52 'commit' => 'commit',
53 'checkin' => 'commit',
54 'ci' => 'commit',
55 'revert' => 'revert',
56 'blame' => 'blame',
57 'diff' => 'diff',
58 'status' => 'status',
59 'st' => 'status',
60 'check' => 'check',
61 'permissions' => 'permissions',
62 'cleanup' => 'cleanup',
63 'complete' => 'complete',
64 'complete_path' => 'complete_path',
65 'complete_repopath' => 'complete_repopath',
66 'plugins' => 'plugins',
67);
68
69# desc: description (eg. for usage)
70# params: parameters
71# CMD
72# USER
73# PATH_PROD
74# PATH_REPO
75# require:
76# WRITE commands that require write access (and therefore a login)
77my %COMMAND_DEFINITIONS = (
78 'help' => {
79 'desc' => ["print help and usage information"],
80 'params' => ["CMD"],
81 'function' => \&help,
82 },
83 'login' => {
84 'desc' => ["user login to Subversion repositoty"],
85 'params' => ["USER"],
86 'function' => \&login
87 },
88 'init' => {
89 'desc' => [
90 "initialize local subversion checkout.",
91 "This is the first thing to do (after configuring $config_file)"
92 ],
93 'params' => [],
94 'function' => \&init
95 },
96 'ls' => {
97 'desc' => ["list file from repository"],
98 'params' => ["PATH_REPO"],
99 'function' => \&ls
100 },
101 'update' => {
102 'desc' => [
103 "update local repository checkout",
104 "Normally, this is done automatically"
105 ],
106 'params' => ["PATH_REPO"],
107 'function' => \&update
108 },
109 'add' => {
110 'desc' => [
111 "add a file to the subversion repository",
112 "Unlike the native svn command,",
113 "dasscm adds and immediatly submits a file to the subversion repository"
114 ],
115 'params' => ["PATH_PROD"],
116 'options' => [ 'verbose', 'message=s' ],
117 'require' => ["WRITE"],
118 'function' => \&add
119 },
120 'commit' => {
121 'desc' => ["commit a changed file to the subversion repository"],
122 ## TODO: only modified files
123 'params' => ["PATH_REPO"],
124 'options' => [ 'verbose', 'message=s' ],
125 'require' => ["WRITE"],
126 'function' => \&commit
127 },
128 'revert' => {
129 'desc' => [
130 "revert local changes back to version from the repository (see diff)"
131 ],
132 'params' => ["PATH_REPO"],
133 'function' => \&revert
134 },
135 'blame' => {
136 'desc' => ['like "svn blame"'],
137 ## TODO: only files from PATH_REPO
138 'params' => ["PATH_REPO"],
139 'function' => \&blame
140 },
141 'diff' => {
142 'desc' => [
143 'display the differences between files on the system and the repository'
144 ],
145 'params' => ["PATH_REPO"],
146 'function' => \&diff
147 },
148 'status' => {
149 'desc' => [
150 'display status information about modified and deleted files.',
151 'If no path is given "/" is assumed',
152 '(in contract to "svn" with assumes ".")'
153 ],
154 'params' => ["PATH_REPO"],
155 'function' => \&status
156 },
157 'check' => {
158 'desc' => ["perform Nagios NRPE conform check"],
159 'params' => [],
160 'function' => \&check
161 },
162 'permissions' => {
163 'desc' =>
164 ["internal, print permissions for all files in the repository"],
165 'params' => [],
166 'function' => \&permissions
167 },
168 'cleanup' => {
169 'desc' => ["internal, used to clean repository checkout"],
170 'params' => [],
171 'function' => \&cleanup
172 },
173 'complete' => {
174 'desc' => ["internal, used for bash completion"],
175 'params' => ["CMD"],
176 'function' => \&complete
177 },
178 'complete_path' => {
179 'desc' => ["internal, used for bash completion"],
180 'params' => [],
181 'function' => \&complete_path
182 },
183 'complete_repopath' => {
184 'desc' => ["internal, used for bash completion"],
185 'params' => [],
186 'function' => \&complete_repopath
187 },
188 'plugins' => {
189 'desc' => ["internal, perform plugins"],
190 'params' => [],
191 'function' => \&perform_plugins
192 },
193
194);
195
196# configuration file
197my $DASSCM_LOCAL_REPOSITORY_BASE;
198my $DASSCM_REPOSITORY_NAME;
199my $DASSCM_PLUGIN_RESULTS_PATH;
200my $DASSCM_SVN_REPOSITORY;
201my $DASSCM_CHECKOUT_USERNAME;
202my $DASSCM_CHECKOUT_PASSWORD;
203my @DASSCM_ADDITIONAL_FILES;
204
205# current directory at program start
206my $StartDirectory = cwd();
207
208my $diff = "diff --exclude .svn ";
209my $SVN = "svn ";
210my $svnOptions = "";
211my $svnCheckoutCredentials = "";
212my $svnPasswordCredentials = "";
213
214# flag. Set to true by svn_update
215# This prevents, that svn_update is called multiple times
216my $svnRepositoryIsUptodate = 0;
217
218# command line options get stored in options hash
219my %options = ();
220
221# subcommand, that gets executed (add, commit, ...)
222my $command;
223
224my $verbose = 0;
225
226#####################################################################
227#
228# util functions
229#
230sub usage()
231{
232 print '$Id: dasscm 918 2010-07-26 16:16:32Z joergs $';
233 print "\n\n";
234 print "usage: dasscm <subcommand> [options] [args]\n";
235 print "\n";
236 print "dasscm is intended to help versioning configuration files\n";
237 print "\n";
238 print "Available subcommands:\n";
239 foreach my $i ( sort keys(%COMMAND_DEFINITIONS) ) {
240 print " ", $i, " ", join( " ", get_command_possible_params($i) ),
241 "\n";
242 foreach my $line ( get_command_desc($i) ) {
243 print " " x 20, $line, "\n";
244 }
245 }
246 print "\n";
247 print "If dasscm is not yet configured, read $doc_file\n";
248}
249
250sub warning(@)
251{
252 print "Warning: " . join( "\n ", @_ ) . "\n";
253}
254
255sub error(@)
256{
257 print "Error: " . join( "\n ", @_ ) . "\n";
258}
259
260sub fatalerror(@)
261{
262 error(@_);
263
264 #print "Exiting\n";
265 exit 1;
266}
267
268#
269# reading config file and return key/value pairs as hash
270#
271sub get_config
272{
273 my $file = $_[0];
274
275 if ( !$file ) {
276 fatalerror( "failed to open config file" . $file );
277 }
278
279 my $data = {};
280
281 # try to open config file
282 if ( !open( FH, $file ) ) {
283 fatalerror( "failed to open config file" . $file );
284 } else {
285 while (<FH>) {
286 chomp;
287 if (/^#/) {
288 next;
289 }
290 if ( $_ =~ /=/g ) {
291
292 # splitting in 2 fields at maximum
293 my ( $option, $value ) = split( /=/, $_, 2 );
294 $option =~ s/^\s+//g;
295 $option =~ s/\s+$//g;
296 $option =~ s/\"+//g;
297 $value =~ s/^\s+//g;
298 $value =~ s/\s+$//g;
299 $value =~ s/\"+//g;
300
301 if ( length($option) ) {
302 $data->{$option} = $value;
303 }
304 }
305 }
306 }
307 close(FH);
308
309 return $data;
310}
311
312#
313# check and evaluate environment variables
314#
315sub check_env()
316{
317
318 # DASSCM_PROD
319 if ( !$DASSCM_PROD ) {
320 $DASSCM_PROD = "/";
321 }
322
323 if ( !-d $DASSCM_PROD ) {
324 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
325 }
326 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
327
328 # DASSCM_REPOSITORY_NAME
329 if ( !$DASSCM_REPOSITORY_NAME ) {
330 die
331 "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";
332 }
333
334 # DASSCM_REPO
335 if ( !$DASSCM_REPO ) {
336 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
337 $DASSCM_REPO =
338 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
339 } else {
340 die
341 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
342 }
343 }
344 $DASSCM_REPO = normalize_path($DASSCM_REPO);
345 if ($verbose) { print "DASSCM_REPO: " . $DASSCM_REPO . "\n"; }
346
347 #
348 # subversion checkout user
349 #
350 if ( !$DASSCM_CHECKOUT_USERNAME ) {
351 fatalerror(
352 "variable DASSCM_CHECKOUT_USERNAME is not defined.",
353 "Use file $config_file to configure it."
354 );
355 }
356
357 if ( !$DASSCM_CHECKOUT_PASSWORD ) {
358 fatalerror(
359 "variable DASSCM_CHECKOUT_PASSWORD is not defined.",
360 "Use file $config_file to configure it."
361 );
362 }
363
364 #
365 # check if local repository directory exist
366 # (if not creating by init)
367 #
368 if ( $command ne "init" ) {
369 if ( not -d $DASSCM_REPO ) {
370 fatalerror(
371 "Can't access local repository DASSCM_REPO",
372 "($DASSCM_REPO)",
373 "Check configuration and execute",
374 "dasscm init"
375 );
376 }
377
378 #
379 # user settings
380 #
381
382 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
383 if ( !$DASSCM_USERNAME ) {
384 $DASSCM_USERNAME = $DASSCM_USER;
385 }
386
387 # user root is not allowed for checkins.
388 # if user is root, DASSCM_USER has to be set,
389 # otherwise USER can be used
390 if ( "$USER" eq "root" ) {
391 if ( ( not $DASSCM_USERNAME )
392 and ( get_command_requires_write($command) ) )
393 {
394
395 #( $command ne "login" ) and ( $command ne "status" ) ) {
396 fatalerror(
397 "Envirnonment variable DASSCM_USERNAME not set.",
398 "Set DASSCM_USERNAME to your subversion user account or",
399 "use 'dasscm login'"
400 );
401 }
402 $svnOptions .= " --no-auth-cache ";
403 } elsif ( !$DASSCM_USERNAME ) {
404 $DASSCM_USERNAME = $USER;
405 }
406
407 #
408 # password
409 #
410 if ($DASSCM_PASSWORD) {
411 $svnPasswordCredentials = " --password '$DASSCM_PASSWORD' ";
412 }
413 }
414
415 #$svnOptions .= " --username $DASSCM_USERNAME "
416}
417
418#
419# has been intendend,
420# to check addtitional parameters.
421# Currently not used.
422#
423sub check_parameter(@)
424{
425}
426
427sub get_command_uniform_name( $ )
428{
429 my $command_abbrivation = $_[0];
430 if ( defined( $COMMANDS{$command_abbrivation} ) ) {
431 return $COMMANDS{$command_abbrivation};
432 }
433 return;
434}
435
436sub get_command_desc( $ )
437{
438 my $command = get_command_uniform_name( $_[0] );
439 my @desc = ();
440 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'desc'} ) ) {
441 @desc = @{ $COMMAND_DEFINITIONS{$command}{'desc'} };
442 }
443 return @desc;
444}
445
446sub get_command_function( $ )
447{
448 my $command = get_command_uniform_name( $_[0] );
449 my $func;
450 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'function'} ) ) {
451 $func = $COMMAND_DEFINITIONS{$command}{'function'};
452 }
453 return $func;
454}
455
456sub get_command_possible_params( $ )
457{
458 my $command = get_command_uniform_name( $_[0] );
459 my @params = ();
460 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'params'} ) ) {
461 @params = @{ $COMMAND_DEFINITIONS{$command}{'params'} };
462 }
463 return @params;
464}
465
466sub get_command_possible_options( $ )
467{
468 my $command = get_command_uniform_name( $_[0] );
469 my @params = ();
470 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'options'} ) ) {
471 @params = @{ $COMMAND_DEFINITIONS{$command}{'options'} };
472 }
473 return @params;
474}
475
476sub get_command_requirements( $ )
477{
478 my $command = get_command_uniform_name( $_[0] );
479 my @requirements = ();
480 if ( $command && defined( $COMMAND_DEFINITIONS{$command}{'require'} ) ) {
481 @requirements = @{ $COMMAND_DEFINITIONS{$command}{'require'} };
482 }
483 return @requirements;
484}
485
486sub get_command_requires_write( $ )
487{
488 return grep( /^WRITE$/, get_command_requirements( $_[0] ) );
489}
490
491#
492# normalize path namens:
493# - directories should end with "/"
494# - use only single "/"
495#
496sub normalize_path($)
497{
498 my $path = shift || "";
499
500 if ( $path =~ m|^/| ) {
501
502 # full path
503 if ( -d $path ) {
504
505 # ensure, a directory ends with '/'
506 $path .= '/';
507 }
508 } elsif ( -d cwd() . '/' . $path ) {
509
510 # ensure, a directory ends with '/'
511 $path .= '/';
512 }
513
514 # remove double (triple) slashes (/)
515 $path =~ s|/[/]*|/|g;
516
517 # remove self reference path
518 $path =~ s|/./|/|g;
519
520 return $path;
521}
522
523#
524# generate from (relative) filename
525# all required file and directory names:
526# $basename, $dirname_prod, $dirname_repo,
527# $filename_prod, $filename_repo
528#
529sub get_filenames(@)
530{
531 my $filename_prod = $_[0] || ".";
532
533 # make filename absolut
534 if ( !( $filename_prod =~ m/^\// ) ) {
535 $filename_prod = cwd() . '/' . $filename_prod;
536 }
537
538 # file must be readable.
539 # The only exceptions are,
540 # - if the file parameter is to be completed or
541 # - if a file should be reverted
542 if ( $command ne "revert" && $command !~ m/^complete/ ) {
543 if ( not -r $filename_prod ) {
544 fatalerror( $filename_prod . " is not accessable" );
545 }
546 }
547
548 # dirname buggy: eg. "/etc/" is reduced to "/",
549 # "/etc" is used as filename
550 # herefore make sure, that if filename is a directory,
551 # it will end by "/"
552 $filename_prod = normalize_path($filename_prod);
553
554 ( my $basename, my $dirname_prod ) = fileparse($filename_prod);
555
556 # normalize path.
557 # not done for reverting, because in this case, the directory may not exist
558 # and the correct path should already be stored in the repository
559 if ( $command ne "revert" ) {
560
561 # uses chdir to determine real directory in a unique way
562 chdir $dirname_prod
563 or fatalerror( "failed to access directory $dirname_prod: " . $! );
564 $dirname_prod = normalize_path( cwd() );
565 chdir $StartDirectory;
566 }
567
568 my $dirname_repo = normalize_path( $DASSCM_REPO . "/" . $dirname_prod );
569 my $filename_repo = normalize_path("$dirname_repo/$basename");
570
571 if ($verbose) {
572 print "filename_repo: " . $filename_repo . "\n";
573 print "dirname_repo: " . $dirname_repo . "\n";
574 print "filename_prod: " . $filename_prod . "\n";
575 print "dirname_prod: " . $dirname_prod . "\n";
576 print "basename: " . $basename . "\n";
577 }
578
579 return (
580 $basename, $dirname_prod, $dirname_repo,
581 $filename_prod, $filename_repo
582 );
583}
584
585sub copy_file_to_repository( $ )
586{
587 my $filename = shift;
588
589 (
590 my $basename,
591 my $dirname_prod,
592 my $dirname_repo,
593 my $filename_prod,
594 my $filename_repo
595 ) = get_filenames($filename);
596
597 #copy( $filename_prod, $filename_repo )
598 ( my $rc, my @result ) =
599 run_command("cp -a \"$filename_prod\" \"$filename_repo\"");
600 if ( $rc != 0 ) {
601 error( "failed to copy $filename_prod to repository: ", @result );
602 }
603
604 # return success
605 return $rc == 0;
606}
607
608sub copy_file_from_repository_to_system( $ )
609{
610 my $filename = shift;
611
612 (
613 my $basename,
614 my $dirname_prod,
615 my $dirname_repo,
616 my $filename_prod,
617 my $filename_repo
618 ) = get_filenames($filename);
619
620 ( my $rc, my @result ) =
621 run_command("cp -a \"$filename_repo\" \"$filename_prod\"");
622 if ( $rc != 0 ) {
623 error( "failed to copy $filename_repo to $filename_prod: ", @result );
624 }
625
626 # return success
627 return $rc == 0;
628}
629
630#
631# creates a file with permissions
632#
633sub generatePermissionList
634{
635
636 # generieren der Zeilen für Permission-Savefile
637 my @files = @_;
638 my @permlist = ();
639 foreach my $file (@files) {
640 $file = "/" . $file;
641 if ( -e $file ) {
642 my $info = stat($file) || die "failed to stat $file: aborting";
643 my $mode = get_type( $info->mode ) & 07777;
644 my $modestring = sprintf( "%04o", $mode );
645 my $uidnumber = $info->uid;
646 my $uid = getpwuid($uidnumber) || $uidnumber;
647 my $gidnumber = $info->gid;
648 my $gid = getgrgid($gidnumber) || $gidnumber;
649 push(
650 @permlist,
651 sprintf( "%-55s %-17s %4d",
652 $file, "${uid}:${gid}", $modestring )
653 );
654 }
655 }
656 return @permlist;
657}
658
659sub get_type
660{
661
662 # Funktion übernommen aus /usr/bin/chkstat
663 my $S_IFLNK = 0120000; # symbolic link
664 my $S_IFREG = 0100000; # regular file
665 my $S_IFDIR = 0040000; # directory
666 my $S_IFCHAR = 0020000; # character device
667 my $S_IFBLK = 0060000; # block device
668 my $S_IFFIFO = 0010000; # fifo
669 my $S_IFSOCK = 0140000; # socket
670 my $S_IFMT = 0170000; # type of file
671
672 my $S_m;
673 if ( ( $_[0] & $S_IFMT ) == $S_IFLNK ) { $S_m = $_[0] - $S_IFLNK; }
674 elsif ( ( $_[0] & $S_IFMT ) == $S_IFREG ) { $S_m = $_[0] - $S_IFREG; }
675 elsif ( ( $_[0] & $S_IFMT ) == $S_IFDIR ) { $S_m = $_[0] - $S_IFDIR; }
676 elsif ( ( $_[0] & $S_IFMT ) == $S_IFCHAR ) { $S_m = $_[0] - $S_IFCHAR; }
677 elsif ( ( $_[0] & $S_IFMT ) == $S_IFBLK ) { $S_m = $_[0] - $S_IFBLK; }
678 elsif ( ( $_[0] & $S_IFMT ) == $S_IFFIFO ) { $S_m = $_[0] - $S_IFFIFO; }
679 elsif ( ( $_[0] & $S_IFMT ) == $S_IFSOCK ) { $S_m = $_[0] - $S_IFSOCK; }
680 $S_m;
681}
682
683sub run_command
684{
685 my $command = shift;
686
687 #print "executing command: " . $command . "\n";
688
689 open( RESULT, $command . ' 2>&1 |' );
690 my @result = <RESULT>;
691 close(RESULT);
692 my $retcode = $? >> 8;
693
694 #print @result;
695 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
696
697 return ( $retcode, @result );
698}
699
700sub run_interactive
701{
702
703 if ($verbose) {
704 print "run_interactive:" . join( " ", @_ ) . "\n";
705 }
706
707 system(@_);
708 if ( $? == -1 ) {
709 printf "failed to execute: $!\n";
710 } elsif ( $? & 127 ) {
711 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
712 ( $? & 128 ) ? 'with' : 'without';
713 } elsif ( $? >> 8 != 0 ) {
714 printf "child exited with value %d\n", $? >> 8;
715 }
716 return ( $? >> 8 );
717}
718
719#
720# en- or disable echo mode.
721# used for reading passwords from STDIN
722#
723sub setEchoMode( $ )
724{
725 my $mode = shift;
726 if ($mode) {
727 run_command("stty echo");
728 } else {
729 run_command("stty -echo");
730 }
731}
732
733sub write_array_to_file( $@ )
734{
735 my $filename = shift;
736 my @array = @_;
737
738 if ( !-w dirname($filename) ) {
739 warning( "failed to write to $filename", "directory does not exist" );
740 return;
741 }
742
743 # directory exists => write
744 if ( !open( OUTFILE, ">$filename" ) ) {
745 warning("failed to open $filename: $!");
746 return;
747 }
748
749 foreach my $line (@array) {
750 print OUTFILE "$line";
751 }
752 close(OUTFILE);
753
754 return 1;
755}
756
757sub perform_plugins()
758{
759 check_env();
760
761 my @plugin_results = ();
762
763 # get all defined plugins.
764 # Plugin definitions starting with DASSCM_PLUGIN_
765 my @plugins = grep( /^DASSCM_PLUGIN_CMD_/, keys( %{$config} ) );
766
767 for my $plugin (@plugins) {
768 my $plugin_name = substr( $plugin, length("DASSCM_PLUGIN_CMD_") );
769 my $plugin_test = $config->{ 'DASSCM_PLUGIN_TEST_' . $plugin_name };
770 ( my $rc_test, my @result_test ) = run_command($plugin_test);
771 if ($verbose) { print "Plugin $plugin_name: "; }
772 if ( $rc_test != 0 ) {
773 if ($verbose) { print "skipped\n"; }
774 } else {
775 if ($verbose) { print "$config->{$plugin}\n"; }
776 ( my $rc, my @result ) = run_command( $config->{$plugin} );
777 if ( $rc != 0 ) {
778 warning("failed to run plugin $plugin");
779 } else {
780 my $plugin_result_file =
781 $DASSCM_PLUGIN_RESULTS_PATH . "/" . $plugin_name;
782 write_array_to_file( $plugin_result_file, @result );
783 push @plugin_results, $plugin_result_file;
784 }
785 }
786 }
787 return @plugin_results;
788}
789
790sub svn_check_credentials( $$;$$ )
791{
792 my $username = shift;
793 my $password = shift;
794
795 # check silently are allow user interaction?
796 my $interactive = shift || 0;
797
798 # default: exit program, if repository is not accessable
799 # (do not exit for 'init')
800 my $fatalerror = shift || 1;
801
802 print "checking credentials ";
803
804 if ( !$username ) {
805 fatalerror("no username given");
806 }
807
808 if ( !$password ) {
809 fatalerror("no password given");
810 }
811
812 print "for " . $username . "@" . $DASSCM_SVN_REPOSITORY . ": ";
813
814 # Options for "svn info" are not supported by subversion 1.0.0 (SLES9),
815 # therefore switching to "svn status"
816 # ( my $rc_update, my @result ) =
817 # run_command(
818 # "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
819 # );
820 #print @result;
821
822 my $rc_update;
823 if ($interactive) {
824 $rc_update = run_interactive(
825 "$SVN ls --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
826 );
827 } else {
828 ( $rc_update, my @result ) = run_command(
829 "$SVN ls --non-interactive --no-auth-cache --username '$username' --password '$password' $DASSCM_SVN_REPOSITORY"
830 );
831
832 if ( $rc_update != 0 ) {
833 print "\n", @result;
834 if ($fatalerror) {
835 fatalerror();
836 }
837 return;
838 }
839 }
840
841 # return success
842 return $rc_update == 0;
843}
844
845sub svn_update( ;$ )
846{
847 my $update_path = shift || "";
848
849 # return value
850 my $update_ok = 1;
851
852 # use this flag to do only one update per run
853 if ( !$svnRepositoryIsUptodate ) {
854 ( my $rc_update, my @result ) = run_command(
855 "$SVN update --non-interactive $svnCheckoutCredentials '$DASSCM_REPO/$update_path'"
856 );
857 print @result;
858 if ( $rc_update != 0 ) {
859 error("failed to update local repository ($update_path)");
860 $update_ok = 0;
861 } elsif ( not $update_path ) {
862
863 # set this flag if a full update is done
864 $svnRepositoryIsUptodate = 1;
865 }
866 }
867 return $update_ok;
868}
869
870sub svn_ls( ;@ )
871{
872 (
873 my $basename,
874 my $dirname_prod,
875 my $dirname_repo,
876 my $filename_prod,
877 my $filename_repo
878 ) = get_filenames( $_[0] );
879
880 # svn ls -R is better, but much, much slower
881 # ( my $rc, my @result ) = run_command("$SVN ls --recursive $svnCheckoutCredentials $path");
882
883 my @files = ();
884 my @links = ();
885 my @dirs = ();
886 my @others = ();
887
888 find(
889 {
890 wanted => sub {
891 my $name = normalize_path($File::Find::name);
892 $name =~ s|^$dirname_repo||;
893
894 #print "($name)\n";# . $File::Find::dir . "\n";
895 if ( not $name ) {
896
897 # name string is empty (top directory).
898 # do nothing
899 } elsif ( $name =~ m/\.svn/ ) {
900
901 # skip svn meta data
902 } elsif ( -l $_ ) {
903
904 # soft link
905 # important: check for links first
906 # to exclude them from further checks
907 push( @links, $name );
908 } elsif ( -d $_ ) {
909
910 # directories
911 push( @dirs, $name );
912 } elsif ( -f $_ ) {
913
914 # regular file
915 push( @files, $name );
916 } else {
917 push( @others, $name );
918 }
919 }
920 },
921 ($filename_repo)
922 );
923
924 return ( sort( @dirs, @files ) );
925}
926
927sub svn_revert( ;$ )
928{
929 my $path = shift || $DASSCM_REPO;
930
931 ( my $rc_update, my @result ) = run_command("$SVN revert -R '$path'");
932
933 if ( $rc_update != 0 ) {
934 print "\n", @result;
935 error("failed to revert subversion repository changes");
936 }
937}
938
939sub svn_remove_unknown_files( ;$ )
940{
941 my $path = shift || $DASSCM_REPO;
942
943 ( my $rc_update, my @result ) = run_command("$SVN status '$path'");
944
945 if ( $rc_update != 0 ) {
946 print "\n", @result;
947 error("failed to receive subversion repository information");
948 } else {
949 foreach (@result) {
950 if (s/^\? +//) {
951 chomp;
952
953 # if file is unknown to subversion (line starts with "?")
954 # remove it
955 print "removing $_\n";
956
957 # unlink doesn't work recursive, there "rm -rf" is used
958 #unlink($_);
959 system("rm -rf $_");
960 }
961 }
962 }
963}
964
965sub getModifiedFiles( ;$ )
966{
967 (
968 my $basename,
969 my $dirname_prod,
970 my $dirname_repo,
971 my $filename_prod,
972 my $filename_repo
973 ) = get_filenames( $_[0] );
974
975 my @files = svn_ls($filename_prod);
976
977 # stores result from status (cvscheck)
978 my %removedfiles = ();
979 my %changedfiles = ();
980 my %unknownfiles = ();
981
982 # create list of modified files
983 if (@files) {
984
985 foreach my $file (@files) {
986
987 my $realfile = $dirname_prod . $file;
988 my $cvsworkfile = $dirname_repo . $file;
989
990 if ( -d $realfile ) {
991
992 # directory
993 if ( !-d "$cvsworkfile" ) {
994
995 # real is directory, repository is not. This is a problem
996 $changedfiles{"$realfile"} = $cvsworkfile;
997 }
998 } elsif ( !-e $realfile ) {
999 $removedfiles{"$realfile"} = $cvsworkfile;
1000 } elsif ( !-r $realfile ) {
1001
1002 # don't have permission to read the file,
1003 # can't check it
1004 $unknownfiles{"$realfile"} = $cvsworkfile;
1005 } else {
1006 ( -r "$cvsworkfile" )
1007 || fatalerror("failed to read $cvsworkfile");
1008 if ( compare( $cvsworkfile, $realfile ) != 0 ) {
1009 $changedfiles{"$realfile"} = $cvsworkfile;
1010 }
1011 }
1012 }
1013 }
1014
1015 return ( \%changedfiles, \%removedfiles, \%unknownfiles );
1016}
1017
1018#
1019# from an array of files/dirs,
1020# generates list of files
1021# sorted by type
1022#
1023sub get_files( @ )
1024{
1025 my @files = ();
1026 my @links = ();
1027 my @dirs = ();
1028 my @others = ();
1029
1030 if (@_) {
1031 find(
1032 {
1033 wanted => sub {
1034 my $fullname = cwd() . "/" . $_;
1035 if ( -l $_ ) {
1036
1037 # soft link
1038 # important: check for links first
1039 # to exclude them from further checks
1040 push( @links, $fullname );
1041 } elsif ( -d $_ ) {
1042
1043 # directories
1044 push( @dirs, $fullname );
1045 } elsif ( -f $_ ) {
1046
1047 # regular file
1048 push( @files, $fullname );
1049 } else {
1050 push( @others, $fullname );
1051 }
1052 }
1053 },
1054 @_
1055 );
1056 }
1057
1058 # don't rely on others.
1059 # If more specific file types are needed,
1060 # they will be added
1061 return {
1062 files => \@files,
1063 links => \@links,
1064 dirs => \@dirs,
1065 others => \@others
1066 };
1067}
1068
1069sub print_files_hash( $ )
1070{
1071 my $href_files = shift;
1072
1073 my @files = @{ $href_files->{files} };
1074 my @links = @{ $href_files->{links} };
1075
1076 if (@files) {
1077 my $number = $#files + 1;
1078 print "files to check-in ($number): \n";
1079 print join( "\n", @files );
1080 print "\n";
1081 }
1082
1083 # TODO: check in links and also link target? At least warn about link target
1084 if (@links) {
1085 my $number = $#links + 1;
1086 print "\n";
1087 print "ignoring links ($number):\n";
1088 print join( "\n", @links );
1089 print "\n";
1090 }
1091
1092}
1093
1094#
1095# use globbing to get lsit of files
1096# that matches the given prefix
1097# used for bash completion
1098#
1099sub get_complete_path_globbing( $ )
1100{
1101 my $path = shift;
1102
1103 # add globbing
1104 $path .= "*";
1105
1106 # get files
1107 my @files = glob($path);
1108
1109 if ( $#files == 0 ) {
1110
1111 # if only one result is available
1112 # and this result is a directory,
1113 # add another result entry
1114 # (directory with and withour trainling /),
1115 # otherwise complete will stop here and continue with the next parameter
1116 my $path = normalize_path( $files[0] );
1117 if ( -d $path ) {
1118 @files = ( substr( $path, 0, -1 ), $path );
1119 }
1120 } else {
1121
1122 # add "/" to all directories
1123 @files = map( { normalize_path($_) } @files );
1124 }
1125
1126 return @files;
1127}
1128
1129#####################################################################
1130#
1131# functions
1132sub help(;@)
1133{
1134 if ( not @_ ) {
1135 usage();
1136 } else {
1137 print "help for ", join( " ", @_ ), ": ...\n";
1138 usage();
1139 }
1140}
1141
1142sub login(@)
1143{
1144 check_parameter( @_, 1 );
1145 check_env();
1146
1147 my $input_username = $_[0];
1148
1149 if ( not $input_username ) {
1150 my $output_username = "";
1151 if ($DASSCM_USERNAME) {
1152 $output_username = " ($DASSCM_USERNAME)";
1153 }
1154
1155 print "Enter DASSCM user name", $output_username, ": ";
1156 $input_username = <STDIN>;
1157 chomp($input_username);
1158
1159 $input_username = $input_username || $DASSCM_USERNAME;
1160 }
1161
1162 # hidden password input
1163 print "Enter password for $input_username: ";
1164 setEchoMode(0);
1165 my $input_password = <STDIN>;
1166 setEchoMode(1);
1167 chomp($input_password);
1168 print "\n";
1169
1170 # checking checkout username/password
1171 svn_check_credentials( $DASSCM_CHECKOUT_USERNAME,
1172 $DASSCM_CHECKOUT_PASSWORD );
1173 print "checkout access okay\n";
1174
1175 svn_check_credentials( $input_username, $input_password );
1176
1177 #
1178 # set environment variables
1179 #
1180 $ENV{'DASSCM_USERNAME'} = "$input_username";
1181 $ENV{'DASSCM_PASSWORD'} = "$input_password";
1182
1183 print "subversion access okay\n\n", "DASSCM_USERNAME: $input_username\n",
1184 "DASSCM_PASSWORD: (hidden)\n", "DASSCM_PROD: $DASSCM_PROD\n",
1185 "DASSCM_REPO: $DASSCM_REPO\n",
1186 "Server Repository: $DASSCM_SVN_REPOSITORY\n", "\n";
1187
1188 status();
1189
1190 print "\n[dasscm shell]\n\n";
1191 my $shell = $SHELL || "bash";
1192 exec($shell) or die "failed to start new shell";
1193}
1194
1195#
1196# initialize local checkout directory (initial checkout)
1197#
1198sub init(@)
1199{
1200 check_parameter( @_, 1 );
1201 check_env();
1202
1203 # don't do repository creation (svn mkdir) here,
1204 # because then their must be a lot of prior checks
1205
1206 # update complete repository
1207 my $retcode = run_interactive(
1208 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY"
1209 );
1210}
1211
1212sub ls(@)
1213{
1214 check_parameter( @_, 1 );
1215 check_env();
1216
1217 my @files = svn_ls(@_);
1218
1219 if (@files) {
1220 print join( "\n", @files );
1221 print "\n";
1222 }
1223}
1224
1225sub update(@)
1226{
1227 check_parameter( @_, 1 );
1228 check_env();
1229
1230 #
1231 # update local repository
1232 #
1233 svn_update();
1234}
1235
1236#
1237# helper function for "add" command
1238#
1239sub add_helper(@)
1240{
1241 (
1242 my $basename,
1243 my $dirname_prod,
1244 my $dirname_repo,
1245 my $filename_prod,
1246 my $filename_repo
1247 ) = get_filenames( $_[0] );
1248
1249 mkpath($dirname_repo);
1250 copy_file_to_repository($filename_prod);
1251
1252 # already checked in?
1253 chdir $DASSCM_REPO;
1254
1255 # also add the path to filename.
1256 for my $dir ( split( '/', $dirname_prod ) ) {
1257 if ($dir) {
1258 my ( $rc, @out ) = run_command("$SVN add --non-recursive '$dir'");
1259 if ( $rc > 0 ) {
1260 print join( "\n", @out );
1261 }
1262 chdir $dir;
1263 }
1264 }
1265 my ( $rc, @out ) = run_command("$SVN add '$basename'");
1266 if ( $rc > 0 ) {
1267 print join( "\n", @out );
1268 }
1269 chdir $StartDirectory;
1270
1271}
1272
1273sub add_helper_multi(@)
1274{
1275
1276 # get all regular files and links
1277 my $href_files = get_files(@_);
1278
1279 #print Dumper( $href_files );
1280
1281 my @files = @{ $href_files->{files} };
1282 my @links = @{ $href_files->{links} };
1283
1284 # copy files one by one to local repository
1285 for my $file (@files) {
1286
1287 # add file
1288 add_helper($file);
1289 }
1290
1291 return $href_files;
1292}
1293
1294#
1295# adding new files (or directories)
1296#
1297sub add(@)
1298{
1299 check_parameter( @_, 1 );
1300 check_env();
1301
1302 #
1303 # update local repository
1304 #
1305 svn_update();
1306
1307 # add files to repository, print information about added files
1308 print_files_hash( add_helper_multi(@_) );
1309
1310 # perform plugins and add additional files, like plugin results
1311 perform_plugins();
1312 add_helper_multi(@DASSCM_ADDITIONAL_FILES);
1313
1314 if ( $options{'message'} ) {
1315 $svnOptions .= " --message \"$options{'message'}\" ";
1316 }
1317
1318 # commit calls $EDITOR.
1319 # use "interactive" here, to display output
1320 my $retcode = run_interactive(
1321 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1322 );
1323
1324 # svn commit does not deliever an error return code, if commit is canceld,
1325 # so a revert is performed in any case
1326 svn_revert();
1327}
1328
1329#
1330# checks in all modified files
1331#
1332sub commit(@)
1333{
1334 check_parameter( @_, 1 );
1335 check_env();
1336
1337 (
1338 my $basename,
1339 my $dirname_prod,
1340 my $dirname_repo,
1341 my $filename_prod,
1342 my $filename_repo
1343 ) = get_filenames( $_[0] );
1344
1345 #
1346 # update local repository
1347 #
1348 svn_update();
1349
1350 ( my $refChangedFiles, my $refRemovedFiles ) =
1351 getModifiedFiles($filename_prod);
1352 my %changedfiles = %{$refChangedFiles};
1353 my %removedfiles = %{$refRemovedFiles};
1354
1355 if (%removedfiles) {
1356 my $removedFilesString =
1357 '"' . join( '" "', values(%removedfiles) ) . '"';
1358 my ( $rc, @out ) = run_command("$SVN rm $removedFilesString");
1359 if ( $rc > 0 ) {
1360 print join( "\n", @out );
1361 }
1362 }
1363
1364 # copy files one by one to local repository
1365 for my $file ( keys(%changedfiles) ) {
1366 copy_file_to_repository($file);
1367 }
1368
1369 perform_plugins();
1370 add_helper_multi(@DASSCM_ADDITIONAL_FILES);
1371
1372 if ( $options{'message'} ) {
1373 $svnOptions .= " --message \"$options{'message'}\" ";
1374 }
1375
1376 # commit calls $EDITOR.
1377 # use "interactive" here, to display output
1378 my $retcode = run_interactive(
1379 "$SVN commit $svnOptions --username '$DASSCM_USERNAME' $svnPasswordCredentials $DASSCM_REPO"
1380 );
1381
1382 # svn commit does not deliever an error return code, if commit is canceld,
1383 # so a revert is performed in any case
1384 svn_revert();
1385}
1386
1387#
1388# revert: copies files back from repository to system
1389#
1390sub revert(@)
1391{
1392 check_parameter( @_, 1 );
1393 check_env();
1394
1395 (
1396 my $basename,
1397 my $dirname_prod,
1398 my $dirname_repo,
1399 my $filename_prod,
1400 my $filename_repo
1401 ) = get_filenames( $_[0] );
1402
1403 # return code for the shell
1404 # default: error
1405 my $return_code = $RETURN_OK;
1406
1407 # cleanup repository
1408 cleanup();
1409
1410 #svn_update();
1411
1412 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1413 getModifiedFiles($filename_prod);
1414 my %changedfiles = %{$refChangedFiles};
1415 my %removedfiles = %{$refRemovedFiles};
1416 my %unknownfiles = %{$refUnknownFiles};
1417
1418 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1419
1420 if (%removedfiles) {
1421 print "DELETED files and directories. Recreated from repository:\n";
1422 my @removedPaths =
1423 ( sort { length $a > length $b } keys %removedfiles );
1424 print join( "\n", @removedPaths ) . "\n\n";
1425
1426 # copy files one by one from local repository to system
1427 # and also create directories
1428 # paths are sorted, so that directories are created first
1429 for my $real_path (@removedPaths) {
1430 if ( -d $removedfiles{"$real_path"} ) {
1431 mkpath("$real_path");
1432 } else {
1433 copy_file_from_repository_to_system($real_path);
1434 }
1435 }
1436 }
1437
1438 if (%changedfiles) {
1439 print "MODIFIED files. Copied from repository to the system:\n";
1440 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1441
1442 # copy files one by one from local repository to system
1443 for my $real_file ( keys(%changedfiles) ) {
1444 copy_file_from_repository_to_system($real_file);
1445 }
1446
1447 }
1448
1449 if (%unknownfiles) {
1450 print "UNKNOWN: insufficient permission to check files:\n";
1451 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1452
1453 $return_code = $RETURN_NOK;
1454 }
1455
1456 } else {
1457 print "no modified files found in $dirname_repo\n";
1458 }
1459
1460 return $return_code;
1461}
1462
1463sub blame(@)
1464{
1465 check_parameter( @_, 1 );
1466 check_env();
1467
1468 (
1469 my $basename,
1470 my $dirname_prod,
1471 my $dirname_repo,
1472 my $filename_prod,
1473 my $filename_repo
1474 ) = get_filenames( $_[0] );
1475
1476 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
1477}
1478
1479sub diff(@)
1480{
1481 check_parameter( @_, 1 );
1482 check_env();
1483
1484 (
1485 my $basename,
1486 my $dirname_prod,
1487 my $dirname_repo,
1488 my $filename_prod,
1489 my $filename_repo
1490 ) = get_filenames( $_[0] );
1491
1492 #print "$basename,$dirname_prod,$dirname_repo\n";
1493
1494 svn_update();
1495
1496 ( my $rc_diff, my @diff_result ) =
1497 run_command( $diff . " $filename_repo $filename_prod" );
1498
1499 print @diff_result;
1500}
1501
1502sub status(@)
1503{
1504 check_parameter( @_, 1 );
1505 check_env();
1506
1507 (
1508 my $basename,
1509 my $dirname_prod,
1510 my $dirname_repo,
1511 my $filename_prod,
1512 my $filename_repo
1513 ) = get_filenames( $_[0] || "/" );
1514
1515 # return code for the shell
1516 # default: error
1517 my $return_code = $RETURN_NOK;
1518
1519 #
1520 # update local repository
1521 #
1522 #svn_update( $filename_prod );
1523
1524 # perform plugins (required to see changes in plugin results)
1525 perform_plugins();
1526
1527 # get modified files
1528 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1529 getModifiedFiles($dirname_prod);
1530 my %changedfiles = %{$refChangedFiles};
1531 my %removedfiles = %{$refRemovedFiles};
1532 my %unknownfiles = %{$refUnknownFiles};
1533
1534 if ( %removedfiles or %changedfiles or %unknownfiles ) {
1535
1536 if (%removedfiles) {
1537 print "DELETED: files found in repository, but not in system:\n";
1538 print join( "\n", sort ( keys %removedfiles ) ) . "\n\n";
1539 }
1540
1541 if (%changedfiles) {
1542 print "MODIFIED: files differs between repository and system:\n";
1543 print join( "\n", ( keys %changedfiles ) ) . "\n\n";
1544 }
1545
1546 if (%unknownfiles) {
1547 print "UNKNOWN: insufficient permission to check files:\n";
1548 print join( "\n", ( keys %unknownfiles ) ) . "\n\n";
1549 }
1550
1551 } else {
1552 print "no modified files found in $dirname_repo\n";
1553 $return_code = $RETURN_OK;
1554 }
1555
1556 return $return_code;
1557}
1558
1559#
1560# return short status in Nagios plugin conform way
1561#
1562sub check()
1563{
1564 check_env();
1565
1566 # return code for the shell
1567 my $return_code = $RETURN_OK;
1568 my $return_string = "OK: no modified files";
1569
1570 # perform plugins (required to see changes in plugin results)
1571 perform_plugins();
1572
1573 # get modified files
1574 ( my $refChangedFiles, my $refRemovedFiles, my $refUnknownFiles ) =
1575 getModifiedFiles("/");
1576 my %changedfiles = %{$refChangedFiles};
1577 my %removedfiles = %{$refRemovedFiles};
1578 my %unknownfiles = %{$refUnknownFiles};
1579
1580 if ( %removedfiles or %changedfiles ) {
1581 $return_string = "Warning: ";
1582 if (%changedfiles) {
1583 $return_string .=
1584 "changed: " . join( ", ", ( keys %changedfiles ) ) . ". ";
1585 }
1586 if (%removedfiles) {
1587 $return_string .=
1588 "removed: " . join( ", ", ( keys %removedfiles ) ) . ". ";
1589 }
1590 if (%unknownfiles) {
1591 $return_string .=
1592 "unknown: " . join( ", ", ( keys %unknownfiles ) ) . ". ";
1593 }
1594 $return_code = $RETURN_WARN;
1595 }
1596
1597 # addition nagios Service Status
1598 #Critical
1599 #Unknown
1600
1601 print "$return_string\n";
1602 return $return_code;
1603}
1604
1605sub permissions()
1606{
1607 check_env();
1608
1609 my $return_code = $RETURN_OK;
1610
1611 #
1612 # update local repository
1613 #
1614 #svn_update();
1615
1616 my $dir = $DASSCM_REPO;
1617 my @files = svn_ls("/");
1618
1619 if (@files) {
1620
1621 print "#\n";
1622 print "# created by dasscm permissions\n";
1623 print "# It is intended to be used for restoring permissions\n";
1624 print "#\n";
1625
1626 # generate and print permissions
1627 foreach my $line ( generatePermissionList(@files) ) {
1628 print "$line\n";
1629 }
1630
1631 }
1632
1633 return $return_code;
1634}
1635
1636#
1637# remove all uncommited changes in the repository
1638#
1639sub cleanup()
1640{
1641 check_env();
1642
1643 svn_revert($DASSCM_REPO);
1644 svn_remove_unknown_files($DASSCM_REPO);
1645}
1646
1647#
1648# used for bash completion
1649# prints the next possible command line parameters
1650#
1651sub complete(@)
1652{
1653 my @input = @_;
1654 my %options_complete = ();
1655
1656 # check and remove global options. if options are wrong, nothing to do
1657 @ARGV = @input;
1658 if ( GetOptions( \%options_complete, @OPTIONS_GLOBAL ) ) {
1659 my $number_arguments = @input;
1660 if ( $number_arguments <= 1 ) {
1661
1662 # complete dasscm commands
1663 my $input = $input[0] || "";
1664 map { m/^$input/ && print $_, "\n" } ( keys %COMMANDS );
1665 } else {
1666
1667 # complete dasscm parameter
1668 my $command = get_command_uniform_name( $input[0] );
1669 if ($command) {
1670
1671 # remove command
1672 shift @input;
1673
1674 # check and remove options
1675 my @options = get_command_possible_options($command);
1676 @ARGV = @input;
1677 if ( ( not @options )
1678 || ( GetOptions( \%options_complete, @options ) ) )
1679 {
1680
1681 my @params = get_command_possible_params($command);
1682 if ($verbose) { print "params: ", Dumper(@params); }
1683
1684 my $number_arguments = @input;
1685
1686 #print "input: ", join( ",", @input ), " (", $number_arguments, ")\n";
1687
1688 if ( $number_arguments > 0 ) {
1689 my $parameter_number = $number_arguments - 1;
1690 if ( defined( $params[$parameter_number] )
1691 && $params[$parameter_number] )
1692 {
1693 my $param = $params[$parameter_number];
1694 if ($verbose) {
1695 print "param used: ", $param, "\n";
1696 }
1697 if ( $param eq "PATH_PROD" ) {
1698 complete_path(
1699 $input[ $number_arguments - 1 ] );
1700 } elsif ( $param eq "PATH_REPO" ) {
1701 complete_repopath(
1702 $input[ $number_arguments - 1 ] );
1703 }
1704 }
1705 }
1706 }
1707 }
1708 }
1709 }
1710}
1711
1712sub complete_path(@)
1713{
1714 check_parameter( @_, 1 );
1715 check_env();
1716
1717 (
1718 my $basename,
1719 my $dirname_prod,
1720 my $dirname_repo,
1721 my $filename_prod,
1722 my $filename_repo
1723 ) = get_filenames( $_[0] );
1724
1725 my @files = get_complete_path_globbing($filename_prod);
1726
1727 if (@files) {
1728 print join( "\n", @files );
1729 print "\n";
1730 }
1731}
1732
1733sub complete_repopath(@)
1734{
1735 check_parameter( @_, 1 );
1736 check_env();
1737
1738 (
1739 my $basename,
1740 my $dirname_prod,
1741 my $dirname_repo,
1742 my $filename_prod,
1743 my $filename_repo
1744 ) = get_filenames( $_[0] );
1745
1746 my @files = get_complete_path_globbing($filename_repo);
1747
1748 if (@files) {
1749
1750 # remove DASSCM_REPO path again
1751 print join(
1752 "\n",
1753 map( {
1754 s|^${DASSCM_REPO}|/|;
1755 $_
1756 } @files )
1757 );
1758 print "\n";
1759 }
1760
1761}
1762
1763#####################################################################
1764#
1765# main
1766#
1767
1768my $return_code = $RETURN_OK;
1769my $number_arguments = @ARGV;
1770
1771# global options
1772# stops at first non-option
1773Getopt::Long::Configure('require_order');
1774if ( not GetOptions( \%options, @OPTIONS_GLOBAL ) ) {
1775 usage();
1776 exit $RETURN_NOK;
1777}
1778
1779# set verbose to command line option
1780$verbose = $options{'verbose'};
1781
1782if ( $options{'help'} ) {
1783 help(@ARGV);
1784 exit;
1785}
1786
1787# get subcommand and remove it from @ARGV
1788if ( defined( $ARGV[0] ) ) {
1789 $command = get_command_uniform_name( $ARGV[0] );
1790 shift @ARGV;
1791}
1792
1793if ( not defined($command) ) {
1794 usage();
1795 exit $RETURN_NOK;
1796}
1797
1798$DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
1799$DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
1800
1801$DASSCM_PLUGIN_RESULTS_PATH =
1802 $config->{'DASSCM_LOCAL_REPOSITORY_BASE'} . "/" . "plugin-results/";
1803
1804# get list of additional directories and files, seperated by blank (" ")
1805# these files are always stored in subversion
1806if ( $config->{'DASSCM_ADDITIONAL_FILES'} ) {
1807 @DASSCM_ADDITIONAL_FILES = split / /, $config->{'DASSCM_ADDITIONAL_FILES'};
1808} else {
1809 @DASSCM_ADDITIONAL_FILES = ( $DASSCM_PLUGIN_RESULTS_PATH );
1810}
1811
1812# TODO: check variables
1813$DASSCM_SVN_REPOSITORY =
1814 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
1815
1816$DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
1817$DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
1818
1819#
1820# if a user is given by dasscm configuration file, we use it.
1821# Otherwise we expect that read-only account is configured
1822# as local subversion configuration.
1823# If this is also not the case,
1824# user is required to type username and password.
1825# This will be stored as local subversion configuration thereafter.
1826#
1827if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
1828 $svnCheckoutCredentials =
1829 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
1830}
1831
1832
1833# check for command options
1834my @cmd_options = get_command_possible_options($command);
1835if (@cmd_options) {
1836
1837 # get command line options and store them in options hash
1838 my $result = GetOptions( \%options, @cmd_options );
1839
1840 # print options
1841 foreach my $option ( keys %options ) {
1842 print "${option}: $options{$option}\n";
1843 }
1844}
1845
1846#
1847# action accordinly to command are taken
1848#
1849&{ get_command_function($command) }(@ARGV);
1850
1851exit $return_code;
Note: See TracBrowser for help on using the repository browser.