1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | # $Id: dasscm 193 2004-11-17 21:20:26Z joergs $
|
---|
4 |
|
---|
5 | use strict;
|
---|
6 |
|
---|
7 | use Env qw($DASSCM_PROD $DASSCM_REPO $USER $DASSCM_USER $DASSCM_PW);
|
---|
8 | use Cwd;
|
---|
9 | use File::Basename;
|
---|
10 | use File::stat;
|
---|
11 | use File::Path;
|
---|
12 | use File::Copy;
|
---|
13 | #use POSIX qw/getpgrp tcgetpgrp/;
|
---|
14 |
|
---|
15 | #####################################################################
|
---|
16 | #
|
---|
17 | # global
|
---|
18 | #
|
---|
19 | my $SVN = "svn ";
|
---|
20 | my $subversion_options = "";
|
---|
21 |
|
---|
22 | #####################################################################
|
---|
23 | #
|
---|
24 | # util functions
|
---|
25 | #
|
---|
26 | sub 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 |
|
---|
45 | sub 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 |
|
---|
79 | sub check_parameter(@)
|
---|
80 | {
|
---|
81 | }
|
---|
82 |
|
---|
83 | sub 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 |
|
---|
108 | sub 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 |
|
---|
130 | sub help(;@)
|
---|
131 | {
|
---|
132 | if( @_ == 0 ) {
|
---|
133 | usage();
|
---|
134 | } else {
|
---|
135 | print "help for @_: ...\n";
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | sub 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 |
|
---|
176 |
|
---|
177 | sub blame(@)
|
---|
178 | {
|
---|
179 | check_parameter(@_,1);
|
---|
180 | check_env();
|
---|
181 |
|
---|
182 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
183 |
|
---|
184 | system( "$SVN blame --username $DASSCM_USER $subversion_options $filename_repo" );
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 |
|
---|
189 | sub commit(@)
|
---|
190 | {
|
---|
191 | check_parameter(@_,1);
|
---|
192 | check_env();
|
---|
193 |
|
---|
194 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
195 |
|
---|
196 | (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
|
---|
197 | if( $rc_update != 0 ) {
|
---|
198 | print @result;
|
---|
199 | die;
|
---|
200 | }
|
---|
201 |
|
---|
202 | copy( $filename_prod, $filename_repo ) or die $!;
|
---|
203 |
|
---|
204 | # commit calls $EDITOR. uses "system" here, to display output
|
---|
205 | system( "$SVN commit --username $DASSCM_USER $subversion_options $filename_repo" );
|
---|
206 | # TODO: commit (-m)
|
---|
207 | }
|
---|
208 |
|
---|
209 | sub diff(@)
|
---|
210 | {
|
---|
211 | check_parameter(@_,1);
|
---|
212 | check_env();
|
---|
213 |
|
---|
214 | (my $basename, my $dirname_prod, my $dirname_repo, my $filename_prod, my $filename_repo) = get_filenames($_[0]);
|
---|
215 |
|
---|
216 | #print "$basename,$dirname_prod,$dirname_repo\n";
|
---|
217 |
|
---|
218 | (my $rc_update, my @result)=run_command( "$SVN update $filename_repo" );
|
---|
219 | if( $rc_update != 0 ) {
|
---|
220 | print @result;
|
---|
221 | die;
|
---|
222 | }
|
---|
223 |
|
---|
224 | (my $rc_diff, my @diff)=run_command( "diff $filename_repo $filename_prod" );
|
---|
225 | print @diff;
|
---|
226 | }
|
---|
227 |
|
---|
228 | #####################################################################
|
---|
229 | #
|
---|
230 | # main
|
---|
231 | #
|
---|
232 |
|
---|
233 | my $number_arguments = @ARGV;
|
---|
234 | #print "$number_arguments\n";
|
---|
235 |
|
---|
236 | if ($number_arguments > 0) {
|
---|
237 | my $command = $ARGV[0];
|
---|
238 | shift @ARGV;
|
---|
239 | #$command =~ s/[A-Z]/[a-z]/m;
|
---|
240 | #print "$command\n";
|
---|
241 |
|
---|
242 | $_=$command;
|
---|
243 | if (m/help/i) {
|
---|
244 | help(@ARGV);
|
---|
245 | } elsif (m/add/i) {
|
---|
246 | add(@ARGV);
|
---|
247 | } elsif (m/blame/i) {
|
---|
248 | blame(@ARGV);
|
---|
249 | } elsif (m/commit/i) {
|
---|
250 | commit(@ARGV);
|
---|
251 | } elsif (m/diff/i) {
|
---|
252 | diff(@ARGV);
|
---|
253 | } elsif (m/activate/i) {
|
---|
254 | activate(@ARGV);
|
---|
255 | } else {
|
---|
256 | usage();
|
---|
257 | check_env();
|
---|
258 | }
|
---|
259 | }
|
---|