Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / l10ntools / scripts / fast_merge.pl
blob7074beaa3ff553151c84cfd3f1dbf25e3ebf4476
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 # Copyright 2000, 2010 Oracle and/or its affiliates.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # This file is part of OpenOffice.org.
14 # OpenOffice.org is free software: you can redistribute it and/or modify
15 # it under the terms of the GNU Lesser General Public License version 3
16 # only, as published by the Free Software Foundation.
18 # OpenOffice.org is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU Lesser General Public License version 3 for more details
22 # (a copy is included in the LICENSE file that accompanied this code).
24 # You should have received a copy of the GNU Lesser General Public License
25 # version 3 along with OpenOffice.org. If not, see
26 # <http://www.openoffice.org/license.html>
27 # for a copy of the LGPLv3 License.
29 #*************************************************************************
31 use strict;
32 use Class::Struct;
33 use Getopt::Long;
34 use File::Temp;
35 use File::Path;
37 my @files;
38 my @file_names;
39 my $module_name = '';
40 my @current;
41 my @buffer;
42 my $last_file;
43 my $last_path;
44 my $last_localize_file;
45 my $first_run = "1";
46 my $sdf_filename;
47 my $merge_dir;
48 my $state = "none";
50 $SIG{INT} = 'inthandler';
51 $SIG{QUIT} = 'quithandler';
53 struct ( sdf_obj =>
55 module => '$',
56 file => '$',
57 dir => '$',
58 FILEHANDLE => '$',
59 line => '$',
60 endoffile => '$'
64 parse_options();
65 my $lock_file = $merge_dir."/lock.mk";
66 acquire_lock();
67 read_sdf_file_names();
68 init();
69 my $reference;
70 my $path ;
71 my $localize_file;
72 while( hasLines() )
74 @current = ();
75 foreach ( @files )
77 push @current , $_;
80 $reference = getNextIdentifier( );
82 @current = ();
83 foreach ( @files )
85 if( $_->module eq $reference->module && $_->dir eq $reference->dir )
87 push @current , $_ ;
90 write_lines();
92 # write content of the last localize.sdf file
93 if( $#buffer ge 0 )
95 write_buffer( $last_path , $last_localize_file );
97 release_lock();
98 exit( 0 );
100 ##########################################################################################
101 sub acquire_lock
103 if( -e $lock_file ){
104 $state = "blocked";
105 print "WARNING: Lock file '$lock_file' 'found, waiting ....\n";
106 my $cnt = 0;
107 sleep 10 , while( -e $lock_file && $cnt++ < 180 );
108 exit( 0 );
109 }else
111 $state = "locked";
112 print "Writing lock file '$lock_file'\n";
113 open FILE, ">$lock_file" or die "Can't create lock file '$lock_file'";
114 print FILE "L10N_LOCK=YES" ;
115 close ( FILE );
118 sub release_lock
120 print "Deleting lock file '$lock_file'\n";
121 unlink $lock_file, if( -e $lock_file );
122 $state = "none";
124 sub inthandler
126 release_lock() , if( $state eq "locked" );
127 exit( -1 );
129 sub quithandler
131 release_lock() , if( $state eq "locked" );
132 exit( 0 );
135 sub init
137 foreach my $file ( @file_names )
139 my $obj = new sdf_obj;
140 open my $FILEHANDLE , "<$file" or die "Can't open file '$file'";
141 $obj->FILEHANDLE ( $FILEHANDLE ) ;
142 getNextSdfObj( $obj );
143 push @files, $obj ;
144 print "Open file '$file'\n";
148 # get the next module/file
149 sub getNextIdentifier
151 my @sorted = sort {
152 return $a->module.$a->dir cmp $b->module.$b->dir;
153 } @current ;
154 return shift @sorted;
157 # update the obj with the next line
158 sub getNextSdfObj
160 my $obj = shift;
161 my $line = readline ( $obj->FILEHANDLE );
162 if ( $line eq undef )
164 $obj->endoffile( "true" );
166 else
168 $line =~ /^(([^\t]*)\t([^\t]*)[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t*)/o ;
169 if( defined $1 && defined $2 && defined $3 )
171 $obj->line ( $1 );
172 $obj->module( $2 );
173 $obj->file ( $3 );
174 $obj->dir ( getDir( $3 ) );
176 else
178 $obj->line ( "" );
179 $obj->module( "" );
180 $obj->file ( "" );
181 $obj->dir ( "" );
184 return $obj;
186 sub getNextSdfObjModule
188 my $obj = shift;
189 while( !$obj->endoffile )
191 my $line = readline ( $obj->FILEHANDLE );
192 if ( $line eq undef )
194 $obj->endoffile( "true" );
196 else
198 $line =~ /^(([^\t]*)\t([^\t]*).*)/o ;
199 if( defined $1 && defined $2 && defined $3 )
201 $obj->line ( $1 );
202 $obj->module( $2 );
203 $obj->file ( $3 );
204 $obj->dir ( getDir( $3 ) );
206 else
208 $obj->line ( "" );
209 $obj->module( "" );
210 $obj->file ( "" );
211 $obj->dir ( "" );
213 return $obj , if( $obj->module eq $module_name )
216 #return $obj;
218 sub getDir
220 my $path = shift ;
221 $path =~ s/\//\\/g;
222 my @tmp_path = split /\\/ , $path;
223 pop @tmp_path;
224 $path = join '\\' , @tmp_path;
225 return $path;
228 sub hasLines
230 my $hasLines = "";
231 my @tmpfiles;
232 foreach ( @files )
234 push @tmpfiles , $_, if( !$_->endoffile );
236 @files = @tmpfiles;
237 return $#files+1;
240 sub make_paths
242 my $localizeFile = $merge_dir."\\".$current[ 0 ]->module."\\".$current[ 0 ]->file;
243 my $path = getDir( $localizeFile );
244 $path =~ s/\\/\//g;
246 $localizeFile = $path."/localize.sdf";
248 return ( $path , $localizeFile );
251 sub write_lines
253 if( $first_run ){
254 my( $path , $localize_file ) = make_paths();
255 $last_path = $path;
256 $last_localize_file = $localize_file;
257 add_to_buffer();
258 $first_run = '';
260 else
262 return , if ( $#current+1 eq 0 );
263 my( $path , $localize_file ) = make_paths();
264 if( $path eq $last_path )
266 add_to_buffer();
268 else
270 write_buffer( $last_path , $last_localize_file );
271 add_to_buffer();
272 $last_path = $path;
273 $last_localize_file = $localize_file;
278 # Adds all lines that contain strings from one source file from every input file.
279 # TODO: Would it not be better to add lines for all files from a directory (i.e., replace
280 # "$afile eq $elem->file" by "$adir eq $elem->dir")? We could get rid of the delayed
281 # writing then. But maybe there is a reason for doing it this way...
282 sub add_to_buffer
284 my $plainline;
285 my $afile;
286 my $amodule;
287 foreach my $elem ( @current )
289 do {
290 $amodule=$elem->module;
291 $afile=$elem->file;
292 $plainline=$elem->line;
293 push @buffer, $plainline;
294 getNextSdfObj( $elem );
295 } while ( !$elem->endoffile && $amodule eq $elem->module && $afile eq $elem->file );
299 # Writes the buffer to currently selected localize.sdf file.
300 sub write_buffer
302 my $path = shift;
303 my $localize_file = shift;
304 my $cnt = $#buffer+1;
305 print "Write to $path $cnt lines\n";
306 mkpath $path;
307 open FILE , ">>$localize_file" or die "Can't open file '$localize_file'\n";
308 foreach ( @buffer )
310 print FILE $_."\n";
312 close ( FILE );
313 @buffer = ();
315 sub parse_options
317 my $success = GetOptions( 'sdf_files=s' => \$sdf_filename , 'merge_dir=s' => \$merge_dir ); #, 'module=s' => \$module_name );
318 if( ! ( $sdf_filename && $merge_dir && $success ) )
320 usage();
321 exit( -1 );
325 sub usage
327 print "Usage: fast_merge -sdf_files <file containing sdf file names> -merge_dir <directory>\n" ;
330 sub read_sdf_file_names
332 open FILE , "<$sdf_filename" or die "Can't open file '$sdf_filename'\n";
333 while ( <FILE> )
335 push @file_names , split " " , $_ ;
337 close ( FILE );