source: trunk/dasscm/dasscm@ 208

Last change on this file since 208 was 208, checked in by joergs, on Jul 2, 2007 at 5:17:18 PM

perltidy

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 11.7 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 208 2007-07-02 15:17:18Z joergs $
4
5use strict;
6
7use Env
8 qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USERNAME $DASSCM_USER $DASSCM_PASSWORD);
9use Cwd;
10use Term::ReadKey;
11use File::Basename;
12use File::stat;
13use File::Path;
14use File::Copy;
15use Getopt::Long;
16
17#
18# used ConfigFile instead of SmartClient::Config,
19# because the huge amount of SmartClient dependencies
20#use SmartClient::Config;
21use ConfigFile;
22
23#####################################################################
24#
25# global
26#
27
28my $config_file = "/etc/dasscm.conf";
29
30# my $config = SmartClient::Config->( $config_file );
31my $config = ConfigFile::read_config_file($config_file);
32my $DASSCM_LOCAL_REPOSITORY_BASE;
33my $DASSCM_REPOSITORY_NAME;
34my $DASSCM_SVN_REPOSITORY;
35
36my $SVN = "svn ";
37my $svnOptions = "";
38my $svnCheckoutCredentials = "";
39my $svnPasswordCredentials = "";
40
41# command line options get stored in options hash
42my %options = ();
43
44# subcommand, that gets executed (add, commit, ...)
45my $command;
46
47my $verbose = 0;
48
49#####################################################################
50#
51# util functions
52#
53sub usage()
54{
55 print "usage: dasscm <subcommand> [options] [args]\n";
56 print "\n";
57 print "dasscm is intended to help versioning configuration files\n";
58 print "\n";
59 print "Available subcommands:\n";
60 print " init\n";
61 print " login\n";
62 print " add <filename>\n";
63 print " commit <filename>\n";
64 print " diff <filename>\n";
65 print " help <subcommand>\n";
66 print "\n";
67 print "preperation:\n";
68 print "check out the configuration repository, e.g.\n";
69 print
70 "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/dasscm/HOSTNAME\n";
71 print "environment variables\n", " DASSCM_REPO\n", " DASSCM_PROD¸n",
72 " DASSCM_USERNAME\n", " DASSCM_PASSWORD\n", "are evaluated.\n";
73 print "\n";
74}
75
76sub check_env()
77{
78
79 # DASSCM_PROD
80 if ( !$DASSCM_PROD ) {
81 $DASSCM_PROD = "/";
82 }
83
84 if ( !-d $DASSCM_PROD ) {
85 die "DASSCM_PROD ($DASSCM_PROD) is not set to a directory.\n";
86 }
87 if ($verbose) { print "DASSCM_PROD: " . $DASSCM_PROD . "\n"; }
88
89 # DASSCM_REPOSITORY_NAME
90 if ( !$DASSCM_REPOSITORY_NAME ) {
91 die
92 "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";
93 }
94
95 # DASSCM_REPO
96 if ( !$DASSCM_REPO ) {
97 if ( $DASSCM_LOCAL_REPOSITORY_BASE && $DASSCM_REPOSITORY_NAME ) {
98 $DASSCM_REPO =
99 $DASSCM_LOCAL_REPOSITORY_BASE . "/" . $DASSCM_REPOSITORY_NAME;
100 } else {
101 die
102 "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
103 }
104 }
105 print "DASSCM_REPO: " . $DASSCM_REPO . "\n";
106
107 #
108 # check if local repository directory exist (if not creating by init)
109 #
110 if ( $command ne "init" ) {
111 if ( not -d $DASSCM_REPO ) {
112 die
113 "Can't access local repository DASSCM_REPO\n($DASSCM_REPO)\nCheck configuration and execute\n dasscm init\n";
114 }
115
116 #
117 # user settings
118 #
119
120 # DASSCM_USER is legacy. Use DASSCM_USERNAME instead
121 if ( !$DASSCM_USERNAME ) {
122 $DASSCM_USERNAME = $DASSCM_USER;
123 }
124
125 # user root is not allowed for checkins.
126 # if user is root, DASSCM_USER has to be set,
127 # otherwise USER can be used
128 if ( "$USER" eq "root" ) {
129 if ( ( not $DASSCM_USERNAME ) and ( $command ne "login" ) ) {
130 die
131 "Envirnonment variable DASSCM_USERNAME not set.\nSet DASSCM_USERNAME to your subversion user account.\n";
132 }
133 $svnOptions .= " --no-auth-cache ";
134 } elsif ( !$DASSCM_USERNAME ) {
135 $DASSCM_USERNAME = $USER;
136 }
137
138 #
139 # password
140 #
141 if ($DASSCM_PASSWORD) {
142 $svnPasswordCredentials = " --password $DASSCM_PASSWORD ";
143 }
144 }
145
146 #$svnOptions .= " --username $DASSCM_USERNAME "
147}
148
149sub check_parameter(@)
150{
151}
152
153sub get_filenames(@)
154{
155 my $filename_prod = $_[0];
156 if ( !( $filename_prod =~ m/^\// ) ) {
157 $filename_prod = cwd() . "/" . $filename_prod;
158 }
159
160 -r $filename_prod or die "$filename_prod is not accessable";
161
162 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
163 # "/etc" is used as filename
164 my $dirname_prod = dirname($filename_prod);
165 chdir $dirname_prod or die $!;
166 $dirname_prod = cwd();
167 my $basename = basename($filename_prod);
168
169 print "dir: " . $dirname_prod . "\n";
170 print "fn: " . $basename . "\n";
171
172 my $dirname_repo = $DASSCM_REPO . "/" . $dirname_prod;
173 my $filename_repo = "$dirname_repo/$basename";
174
175 return (
176 $basename, $dirname_prod, $dirname_repo,
177 $filename_prod, $filename_repo
178 );
179}
180
181sub run_command
182{
183 my $command = shift;
184
185 #print "executing command: " . $command . "\n";
186
187 open( RESULT, $command . ' 2>&1 |' );
188 my @result = <RESULT>;
189 close(RESULT);
190 my $retcode = $? >> 8;
191
192 #print @result;
193 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
194
195 return ( $retcode, @result );
196}
197
198sub run_interactive
199{
200
201 if ($verbose) {
202 print "run_interactive:" . join( " ", @_ ) . "\n";
203 }
204
205 system(@_);
206 if ( $? == -1 ) {
207 printf "failed to execute: $!\n";
208 } elsif ( $? & 127 ) {
209 printf "child died with signal %d, %s coredump\n", ( $? & 127 ),
210 ( $? & 128 ) ? 'with' : 'without';
211 } elsif ( $? >> 8 != 0 ) {
212 printf "child exited with value %d\n", $? >> 8;
213 }
214 return ( $? >> 8 );
215}
216
217sub svn_check_credentials( $$ )
218{
219 my $username = shift;
220 my $password = shift;
221
222 ( my $rc_update, my @result ) =
223 run_command(
224 "$SVN info --non-interactive --no-auth-cache --username $username --password $password $DASSCM_SVN_REPOSITORY"
225 );
226
227 print @result;
228
229 if ( $rc_update != 0 ) {
230 print @result;
231 die;
232 }
233
234}
235
236sub svn_update( ;$ )
237{
238 my $update_path = shift || $DASSCM_REPO;
239 ( my $rc_update, my @result ) =
240 run_command("$SVN update $svnCheckoutCredentials $update_path");
241 if ( $rc_update != 0 ) {
242 print @result;
243 die;
244 }
245
246 print @result;
247
248}
249
250#####################################################################
251#
252# functions
253
254sub help(;@)
255{
256 if ( @_ == 0 ) {
257 usage();
258 } else {
259 print "help for @_: ...\n";
260 }
261}
262
263sub login(@)
264{
265 check_parameter( @_, 1 );
266 check_env();
267
268 my $output_username = "";
269 if ($DASSCM_USERNAME) {
270 $output_username = " ($DASSCM_USERNAME)";
271 }
272
273 print "Enter DASSCM user name", $output_username, ": ";
274 my $input_username = <STDIN>;
275 chomp($input_username);
276
277 # hidden password input
278 print "Enter DASSCM user password: ";
279 ReadMode('noecho');
280 my $input_password = <STDIN>;
281 ReadMode('normal');
282 chomp($input_password);
283
284 svn_check_credentials( $input_username, $input_password );
285
286 #
287 # set environment variables
288 #
289 $ENV{'DASSCM_USERNAME'} = $input_username;
290 $ENV{'DASSCM_PASSWORD'} = $input_password;
291
292 # TODO: print environment
293
294 exec("bash") or die "can't login";
295}
296
297sub init(@)
298{
299 check_parameter( @_, 1 );
300 check_env();
301
302 # update complete repository
303 my $retcode =
304 run_interactive(
305 "cd $DASSCM_LOCAL_REPOSITORY_BASE; $SVN checkout $svnCheckoutCredentials $svnOptions $DASSCM_SVN_REPOSITORY"
306 );
307}
308
309#
310# add (is used for command add and commit)
311#
312sub add(@)
313{
314 check_parameter( @_, 1 );
315 check_env();
316
317 (
318 my $basename,
319 my $dirname_prod,
320 my $dirname_repo,
321 my $filename_prod,
322 my $filename_repo
323 )
324 = get_filenames( $_[0] );
325
326 if ( $command eq "add" ) {
327 mkpath($dirname_repo);
328 }
329
330 # update complete repository
331 my $retcode = run_interactive("$SVN update $svnOptions $DASSCM_REPO");
332
333 copy( $filename_prod, $filename_repo ) or die $!;
334
335 if ( $command eq "add" ) {
336
337 # already checked in?
338 chdir($DASSCM_REPO);
339
340 # also add the path to filename.
341 for my $dir ( split( '/', $dirname_prod ) ) {
342 if ($dir) {
343 run_command("$SVN add --non-recursive $dir");
344 chdir $dir;
345 }
346 }
347 run_command("$SVN add $basename");
348 }
349
350 if ( $options{'message'} ) {
351 $svnOptions .= " --message \"$options{'message'}\" ";
352 }
353
354 # commit calls $EDITOR. uses "interactive" here, to display output
355 $retcode =
356 run_interactive(
357 "$SVN commit $svnOptions --username $DASSCM_USERNAME $svnPasswordCredentials $DASSCM_REPO"
358 );
359
360 print $filename_prod. "\n";
361 print $dirname_repo. "\n";
362}
363
364sub blame(@)
365{
366 check_parameter( @_, 1 );
367 check_env();
368
369 (
370 my $basename,
371 my $dirname_prod,
372 my $dirname_repo,
373 my $filename_prod,
374 my $filename_repo
375 )
376 = get_filenames( $_[0] );
377
378 my $retcode = run_interactive("$SVN blame $svnOptions $filename_repo");
379}
380
381sub diff(@)
382{
383 check_parameter( @_, 1 );
384 check_env();
385
386 (
387 my $basename,
388 my $dirname_prod,
389 my $dirname_repo,
390 my $filename_prod,
391 my $filename_repo
392 )
393 = get_filenames( $_[0] );
394
395 #print "$basename,$dirname_prod,$dirname_repo\n";
396
397 ( my $rc_update, my @result ) = run_command("$SVN update $filename_repo");
398 if ( $rc_update != 0 ) {
399 print @result;
400 die;
401 }
402
403 ( my $rc_diff, my @diff ) =
404 run_command("diff $filename_repo $filename_prod");
405 print @diff;
406}
407
408#####################################################################
409#
410# main
411#
412
413my $number_arguments = @ARGV;
414
415if ( $number_arguments > 0 ) {
416
417 # get subcommand and remove it from @ARGV
418 $command = $ARGV[0];
419 shift @ARGV;
420
421 $DASSCM_LOCAL_REPOSITORY_BASE = $config->{'DASSCM_LOCAL_REPOSITORY_BASE'};
422 $DASSCM_REPOSITORY_NAME = $config->{'DASSCM_REPOSITORY_NAME'};
423
424 # TODO: check variables
425 $DASSCM_SVN_REPOSITORY =
426 $config->{'DASSCM_SVN_REPOSITORY_BASE'} . "/" . $DASSCM_REPOSITORY_NAME;
427
428 my $DASSCM_CHECKOUT_USERNAME = $config->{'DASSCM_CHECKOUT_USERNAME'};
429 my $DASSCM_CHECKOUT_PASSWORD = $config->{'DASSCM_CHECKOUT_PASSWORD'};
430
431 #
432 # if a user is given by dasscm configuration file, we use it.
433 # Otherwise we expect that read-only account is configured
434 # as local subversion configuration.
435 # If this is also not the case,
436 # user is required to type username and password.
437 # This will be stored as local subversion configuration thereafter.
438 #
439 if ( $DASSCM_CHECKOUT_USERNAME && $DASSCM_CHECKOUT_PASSWORD ) {
440 $svnCheckoutCredentials =
441 " --username $DASSCM_CHECKOUT_USERNAME --password $DASSCM_CHECKOUT_PASSWORD ";
442 }
443
444 # get command line options and store them in options hash
445 my $result = GetOptions( \%options, 'message=s' );
446
447 # print options
448 foreach my $option ( keys %options ) {
449 print $option. ": " . $options{$option} . "\n";
450 }
451
452 $_ = $command;
453 if (m/help/i) {
454 help(@ARGV);
455 } elsif (m/login/i) {
456 $command = "login";
457 login(@ARGV);
458 } elsif (m/init/i) {
459 $command = "init";
460 init(@ARGV);
461 } elsif (m/add/i) {
462 ## rewrite command
463 $command = "add";
464 add(@ARGV);
465 } elsif (m/commit/i) {
466 $command = "commit";
467 add(@ARGV);
468 } elsif (m/blame/i) {
469 $command = "blame";
470 blame(@ARGV);
471 } elsif (m/diff/i) {
472 $command = "diff";
473 diff(@ARGV);
474 } elsif (m/activate/i) {
475 ## TODO
476 activate(@ARGV);
477 } else {
478 usage();
479 check_env();
480 }
481
482 # login
483 # up
484 # commitall
485 # revert
486 # status (chkconf)
487}
Note: See TracBrowser for help on using the repository browser.