Update ooo320-m1
[ooovba.git] / transex3 / scripts / fast_merge.pl
blob8c99f818c22494fa4440af0bc1cb25091e4e8e9a
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
4 #*************************************************************************
6 # OpenOffice.org - a multi-platform office productivity suite
8 # $RCSfile: fast_merge.pl,v $
10 # $Revision: 1.1.2.2 $
12 # last change: $Author: ihi $ $Date: 2007/07/20 10:37:53 $
14 # The Contents of this file are made available subject to
15 # the terms of GNU Lesser General Public License Version 2.1.
18 # GNU Lesser General Public License Version 2.1
19 # =============================================
20 # Copyright 2005 by Sun Microsystems, Inc.
21 # 901 San Antonio Road, Palo Alto, CA 94303, USA
23 # This library is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU Lesser General Public
25 # License version 2.1, as published by the Free Software Foundation.
27 # This library is distributed in the hope that it will be useful,
28 # but WITHOUT ANY WARRANTY; without even the implied warranty of
29 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 # Lesser General Public License for more details.
32 # You should have received a copy of the GNU Lesser General Public
33 # License along with this library; if not, write to the Free Software
34 # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 # MA 02111-1307 USA
37 #*************************************************************************
39 use strict;
40 use Class::Struct;
41 use Getopt::Long;
42 use File::Temp;
43 use File::Path;
45 my @files;
46 my @file_names;
47 my $module_name = '';
48 my @current;
49 my @buffer;
50 my $last_file;
51 my $last_path;
52 my $last_localize_file;
53 my $first_run = "1";
54 my $sdf_filename;
55 my $merge_dir;
56 my $WIN;
57 my $state = "none";
59 if ( defined $ENV{USE_SHELL} && $ENV{USE_SHELL} eq '4nt' ) { $WIN = 'TRUE'; }
60 else { $WIN = ''; }
62 $SIG{INT} = 'inthandler';
63 $SIG{QUIT} = 'quithandler';
65 struct ( sdf_obj =>
67 module => '$',
68 file => '$',
69 dir => '$',
70 FILEHANDLE => '$',
71 line => '$',
72 endoffile => '$'
76 parse_options();
77 my $lock_file = $merge_dir."/lock.mk";
78 $lock_file =~ s/\//\\/g , if ( $WIN ) ;
79 acquire_lock();
80 read_sdf_file_names();
81 init();
82 my $reference;
83 my $path ;
84 my $localize_file;
85 while( hasLines() )
87 @current = ();
88 foreach ( @files )
90 push @current , $_;
93 $reference = getNextIdentifier( );
95 @current = ();
96 foreach ( @files )
98 if( $_->module eq $reference->module && $_->dir eq $reference->dir )
100 push @current , $_ ;
103 write_lines();
105 if( $#current+1 ne 0 )
107 ( $path , $localize_file ) = make_paths();
108 add_to_buffer();
109 write_buffer( $path , $localize_file );
111 release_lock();
112 exit( 0 );
114 ##########################################################################################
115 sub acquire_lock
117 if( -e $lock_file ){
118 $state = "blocked";
119 print "WARNING: Lock file '$lock_file' 'found, waiting ....\n";
120 my $cnt = 0;
121 sleep 10 , while( -e $lock_file && $cnt++ < 180 );
122 exit( 0 );
123 }else
125 $state = "locked";
126 print "Writing lock file '$lock_file'\n";
127 open FILE, ">$lock_file" or die "Can't create lock file '$lock_file'";
128 print FILE "L10N_LOCK=YES" ;
129 close ( FILE );
132 sub release_lock
134 print "Deleting lock file '$lock_file'\n";
135 unlink $lock_file, if( -e $lock_file );
136 $state = "none";
138 sub inthandler
140 release_lock() , if( $state eq "locked" );
141 exit( -1 );
143 sub quithandler
145 release_lock() , if( $state eq "locked" );
146 exit( 0 );
149 sub init
151 foreach my $file ( @file_names )
153 my $obj = new sdf_obj;
154 open my $FILEHANDLE , "<$file" or die "Can't open file '$file'";
155 $obj->FILEHANDLE ( $FILEHANDLE ) ;
156 getNextSdfObj( $obj );
157 push @files, $obj ;
158 print "Open file '$file'\n";
162 # get the next module/file
163 sub getNextIdentifier
165 my @sorted = sort {
166 return $a->module.$a->dir cmp $b->module.$b->dir;
167 } @current ;
168 return shift @sorted;
171 # update the obj with the next line
172 sub getNextSdfObj
174 my $obj = shift;
175 my $line = readline ( $obj->FILEHANDLE );
176 if ( $line eq undef )
178 $obj->endoffile( "true" );
180 else
182 $line =~ /^(([^\t]*)\t([^\t]*).*)/o ;
183 if( defined $1 && defined $2 && defined $3 )
185 $obj->line ( $1 );
186 $obj->module( $2 );
187 $obj->file ( $3 );
188 $obj->dir ( getDir( $3 ) );
190 else
192 $obj->line ( "" );
193 $obj->module( "" );
194 $obj->file ( "" );
195 $obj->dir ( "" );
198 return $obj;
200 sub getNextSdfObjModule
202 my $obj = shift;
203 while( !$obj->endoffile )
205 my $line = readline ( $obj->FILEHANDLE );
206 if ( $line eq undef )
208 $obj->endoffile( "true" );
210 else
212 $line =~ /^(([^\t]*)\t([^\t]*).*)/o ;
213 if( defined $1 && defined $2 && defined $3 )
215 $obj->line ( $1 );
216 $obj->module( $2 );
217 $obj->file ( $3 );
218 $obj->dir ( getDir( $3 ) );
220 else
222 $obj->line ( "" );
223 $obj->module( "" );
224 $obj->file ( "" );
225 $obj->dir ( "" );
227 return $obj , if( $obj->module eq $module_name )
230 #return $obj;
232 sub getDir
234 my $path = shift ;
235 $path =~ s/\//\\/g;
236 my @tmp_path = split /\\/ , $path;
237 pop @tmp_path;
238 $path = join '\\' , @tmp_path;
239 return $path;
242 sub hasLines
244 my $hasLines = "";
245 my @tmpfiles;
246 foreach ( @files )
248 push @tmpfiles , $_, if( !$_->endoffile );
250 @files = @tmpfiles;
251 return $#files+1;
254 sub make_paths
256 my $localizeFile = $merge_dir."\\".$current[ 0 ]->module."\\".$current[ 0 ]->file;
257 my $path = getDir( $localizeFile );
258 if ( !$WIN ) { $path =~ s/\\/\//g; }
260 $localizeFile = $path."\\localize.sdf";
261 if ( !$WIN ) { $localizeFile =~ s/\\/\//g; }
263 return ( $path , $localizeFile );
265 sub write_lines
267 if( $first_run ){
268 add_to_buffer();
269 my( $path , $localize_file ) = make_paths();
270 $last_path = $path;
271 $last_localize_file = $localize_file;
272 mkpath $path;
273 write_buffer( $path , $localize_file );
274 $first_run = '';
276 else
278 return , if ( $#current+1 eq 0 );
279 my( $path , $localize_file ) = make_paths();
280 if( $path eq $last_path )
282 add_to_buffer();
284 else
286 mkpath $path;
287 write_buffer( $last_path , $last_localize_file );
288 add_to_buffer();
289 $last_path = $path;
290 $last_localize_file = $localize_file;
294 sub add_to_buffer
296 my $plainline;
297 my $afile;
298 my $amodule;
299 foreach my $elem ( @current )
301 do {
302 $amodule=$elem->module;
303 $afile=$elem->file;
304 $plainline=$elem->line;
305 push @buffer, $plainline;
306 getNextSdfObj( $elem );
307 } while ( !$elem->endoffile && $amodule eq $elem->module && $afile eq $elem->file );
310 sub write_buffer
312 my $path = shift;
313 my $localize_file = shift;
314 my $cnt = $#buffer+1;
315 print "Write to $path $cnt lines\n";
316 open FILE , ">>$localize_file" or die "Can't open file '$localize_file'\n";
317 foreach ( @buffer )
319 print FILE $_."\n";
321 @buffer = ();
323 sub parse_options
325 my $success = GetOptions( 'sdf_files=s' => \$sdf_filename , 'merge_dir=s' => \$merge_dir ); #, 'module=s' => \$module_name );
326 if( ! ( $sdf_filename && $merge_dir && $success ) )
328 usage();
329 exit( -1 );
333 sub usage
335 print "Usage: fast_merge -sdf_files <file containing sdf file names> -merge_dir <directory>\n" ;
338 sub read_sdf_file_names
340 open FILE , "<$sdf_filename" or die "Can't open file '$sdf_filename'\n";
341 while ( <FILE> )
343 push @file_names , split " " , $_ ;
345 close ( FILE );