source: trunk/dasscm/dasscm@ 191

Last change on this file since 191 was 191, checked in by joergs, on Nov 11, 2004 at 1:30:36 PM

fixed comment

  • Property keyword set to id
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1#!/usr/bin/perl -w
2
3# $Id: dasscm 191 2004-11-11 12:30:36Z joergs $
4
5use strict;
6
7use Env qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USER $DASSCM_PW);
8use Cwd;
9use File::Basename;
10use File::stat;
11use File::Path;
12use File::Copy;
13#use POSIX qw/getpgrp tcgetpgrp/;
14
15#####################################################################
16#
17# global
18#
19my $SVN = "svn ";
20my $subversion_options = "";
21
22#####################################################################
23#
24# util functions
25#
26sub usage()
27{
28 print "usage: dasscm <subcommand> [options] [args]\n";
29 print "\n";
30 print "dasscm is intended to help versioning configuration files\n";
31 print "\n";
32 print "Available subcommands:\n";
33 print " add <filename>\n";
34 print " commit <filename>\n";
35 print " diff <filename>\n";
36 print " help <subcommand>\n";
37 print "\n";
38 print "preperation:\n";
39 print "check out the configuration repository, e.g.\n";
40 print "svn checkout --no-auth-cache --username USERNAME https://dass-it.de/svn/lvermgeo/technical/config/\n";
41 print "environment variables DASSCM_REPO, DASSCM_PROD and DASSCM_USER are evaluated.\n";
42 print "\n";
43}
44
45sub check_env()
46{
47 # DASSCM_PROD
48 if ( ! $DASSCM_PROD ) {
49 $DASSCM_PROD = "/";
50 }
51 print "DASSCM_PROD: ".$DASSCM_PROD."\n";
52 if ( ! -d $DASSCM_PROD ) {
53 die "DASSCM_PROD is not set to a directory.\n";
54 }
55
56 # DASSCM_REPO
57 if ( ! $DASSCM_REPO ) {
58 die "Envirnonment variable DASSCM_REPO not set.\nSet DASSCM_REPO to the directory of the versioning system checkout for this machine.\n";
59 }
60 print "DASSCM_REPO: ".$DASSCM_REPO."\n";
61 if ( ! -d $DASSCM_REPO ) {
62 die "DASSCM_REPO must be is not set to the directory of the versioning system checkout for this machine.\n";
63 }
64
65 # User settings
66 # user root is not allowed for checkins.
67 # if user is root, DASSCM_USER has to be set,
68 # otherwise USER can be used
69 if ( "$USER" eq "root" ) {
70 if ( ! $DASSCM_USER ) {
71 die "Envirnonment variable DASSCM_USER not set.\nSet DASSCM_USER to your subversion user account.\n";
72 }
73 $subversion_options .= " --no-auth-cache "
74 } elsif ( ! $DASSCM_USER ) {
75 $DASSCM_USER=$USER;
76 }
77}
78
79sub check_parameter(@)
80{
81}
82
83sub get_filenames(@)
84{
85 my $filename_prod = $_[0];
86 if ( !($filename_prod =~ m/^\//) ) {
87 $filename_prod = cwd()."/".$filename_prod;
88 }
89
90 -r $filename_prod or die "$filename_prod is not accessable";
91
92 # TODO: dirname buggy: eg. "/etc/" is reduced to "/",
93 # "/etc" is used as filename
94 my $dirname_prod = dirname($filename_prod);
95 chdir $dirname_prod or die $!;
96 $dirname_prod = cwd();
97 my $basename = basename($filename_prod);
98
99 print "dir: ".$dirname_prod."\n";
100 print "fn: ".$basename."\n";
101
102 my $dirname_repo = $DASSCM_REPO."/".$dirname_prod;
103 my $filename_repo = "$dirname_repo/$basename";
104
105 return ($basename,$dirname_prod,$dirname_repo,$filename_prod,$filename_repo);
106}
107
108sub run_command
109{
110 my $command = shift;
111
112 #print "executing command: " . $command . "\n";
113
114 open(RESULT, $command . ' 2>&1 |' );
115 my @result = <RESULT>;
116 close(RESULT);
117 my $retcode = $?>>8;
118
119 #print @result;
120 #if( $retcode ) { print "return code: " . $retcode . "\n"; }
121
122 return($retcode, @result);
123}
124
125
126#####################################################################
127#
128# functions
129
130sub help(;@)
131{
132 if( @_ == 0 ) {
133 usage();
134 } else {
135 print "help for @_: ...\n";
136 }
137}
138
139sub add(@)
140{
141 check_parameter(@_,1);
142 check_env();
143
144 (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
145
146 mkpath($dirname_repo);
147
148 # update complete repository
149 (my $retcode, my @result)=run_command( "$SVN update $DASSCM_REPO" );
150 if( $retcode != 0 ) {
151 print @result;
152 die;
153 }
154
155 copy( $filename_prod, $filename_repo ) or die $!;
156
157 # already checked in?
158 chdir($DASSCM_REPO);
159 # also add the path to filename.
160 for my $dir (split('/', $dirname_prod) ) {
161 if( $dir ) {
162 run_command( "$SVN add --non-recursive $dir" );
163 chdir $dir;
164 }
165 }
166 run_command( "$SVN add $basename" );
167
168 # commit calls $EDITOR. uses "system" here, to display output
169 system( "$SVN commit --username $DASSCM_USER $subversion_options $DASSCM_REPO" );
170 # TODO: commit (-m)
171
172 print $filename_prod."\n";
173 print $dirname_repo."\n";
174}
175
176sub commit(@)
177{
178 check_parameter(@_,1);
179 check_env();
180
181 (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
182
183 (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
184 if( $rc_update != 0 ) {
185 print @result;
186 die;
187 }
188
189 copy( $filename_prod, $filename_repo ) or die $!;
190
191 # commit calls $EDITOR. uses "system" here, to display output
192 system( "$SVN commit --username $DASSCM_USER $subversion_options $DASSCM_REPO" );
193 # TODO: commit (-m)
194}
195
196sub diff(@)
197{
198 check_parameter(@_,1);
199 check_env();
200
201 (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
202
203 #print "$basename,$dirname_prod,$dirname_repo\n";
204
205 (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
206 if( $rc_update != 0 ) {
207 print @result;
208 die;
209 }
210
211 (my $rc_diff, my @diff)=run_command( "diff $filename_repo $filename_prod" );
212 print @diff;
213}
214
215#####################################################################
216#
217# main
218#
219
220my $number_arguments = @ARGV;
221#print "$number_arguments\n";
222
223if ($number_arguments > 0) {
224 my $command = $ARGV[0];
225 shift @ARGV;
226 #$command =~ s/[A-Z]/[a-z]/m;
227 #print "$command\n";
228
229 $_=$command;
230 if (m/help/i) {
231 help(@ARGV);
232 } elsif (m/add/i) {
233 add(@ARGV);
234 } elsif (m/commit/i) {
235 commit(@ARGV);
236 } elsif (m/diff/i) {
237 diff(@ARGV);
238 } elsif (m/activate/i) {
239 activate(@ARGV);
240 } else {
241 usage();
242 check_env();
243 }
244}
Note: See TracBrowser for help on using the repository browser.