merged tag ooo/OOO330_m14
[LibreOffice.git] / l10ntools / scripts / fast_merge.pl
blob5dc63cf95d79ff83a0308067734073ad20a9bfab
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 if( $#current+1 ne 0 )
94 ( $path , $localize_file ) = make_paths();
95 add_to_buffer();
96 write_buffer( $path , $localize_file );
98 release_lock();
99 exit( 0 );
101 ##########################################################################################
102 sub acquire_lock
104 if( -e $lock_file ){
105 $state = "blocked";
106 print "WARNING: Lock file '$lock_file' 'found, waiting ....\n";
107 my $cnt = 0;
108 sleep 10 , while( -e $lock_file && $cnt++ < 180 );
109 exit( 0 );
110 }else
112 $state = "locked";
113 print "Writing lock file '$lock_file'\n";
114 open FILE, ">$lock_file" or die "Can't create lock file '$lock_file'";
115 print FILE "L10N_LOCK=YES" ;
116 close ( FILE );
119 sub release_lock
121 print "Deleting lock file '$lock_file'\n";
122 unlink $lock_file, if( -e $lock_file );
123 $state = "none";
125 sub inthandler
127 release_lock() , if( $state eq "locked" );
128 exit( -1 );
130 sub quithandler
132 release_lock() , if( $state eq "locked" );
133 exit( 0 );
136 sub init
138 foreach my $file ( @file_names )
140 my $obj = new sdf_obj;
141 open my $FILEHANDLE , "<$file" or die "Can't open file '$file'";
142 $obj->FILEHANDLE ( $FILEHANDLE ) ;
143 getNextSdfObj( $obj );
144 push @files, $obj ;
145 print "Open file '$file'\n";
149 # get the next module/file
150 sub getNextIdentifier
152 my @sorted = sort {
153 return $a->module.$a->dir cmp $b->module.$b->dir;
154 } @current ;
155 return shift @sorted;
158 # update the obj with the next line
159 sub getNextSdfObj
161 my $obj = shift;
162 my $line = readline ( $obj->FILEHANDLE );
163 if ( $line eq undef )
165 $obj->endoffile( "true" );
167 else
169 $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 ;
170 if( defined $1 && defined $2 && defined $3 )
172 $obj->line ( $1 );
173 $obj->module( $2 );
174 $obj->file ( $3 );
175 $obj->dir ( getDir( $3 ) );
177 else
179 $obj->line ( "" );
180 $obj->module( "" );
181 $obj->file ( "" );
182 $obj->dir ( "" );
185 return $obj;
187 sub getNextSdfObjModule
189 my $obj = shift;
190 while( !$obj->endoffile )
192 my $line = readline ( $obj->FILEHANDLE );
193 if ( $line eq undef )
195 $obj->endoffile( "true" );
197 else
199 $line =~ /^(([^\t]*)\t([^\t]*).*)/o ;
200 if( defined $1 && defined $2 && defined $3 )
202 $obj->line ( $1 );
203 $obj->module( $2 );
204 $obj->file ( $3 );
205 $obj->dir ( getDir( $3 ) );
207 else
209 $obj->line ( "" );
210 $obj->module( "" );
211 $obj->file ( "" );
212 $obj->dir ( "" );
214 return $obj , if( $obj->module eq $module_name )
217 #return $obj;
219 sub getDir
221 my $path = shift ;
222 $path =~ s/\//\\/g;
223 my @tmp_path = split /\\/ , $path;
224 pop @tmp_path;
225 $path = join '\\' , @tmp_path;
226 return $path;
229 sub hasLines
231 my $hasLines = "";
232 my @tmpfiles;
233 foreach ( @files )
235 push @tmpfiles , $_, if( !$_->endoffile );
237 @files = @tmpfiles;
238 return $#files+1;
241 sub make_paths
243 my $localizeFile = $merge_dir."\\".$current[ 0 ]->module."\\".$current[ 0 ]->file;
244 my $path = getDir( $localizeFile );
245 $path =~ s/\\/\//g;
247 $localizeFile = $path."/localize.sdf";
249 return ( $path , $localizeFile );
251 sub write_lines
253 if( $first_run ){
254 add_to_buffer();
255 my( $path , $localize_file ) = make_paths();
256 $last_path = $path;
257 $last_localize_file = $localize_file;
258 mkpath $path;
259 write_buffer( $path , $localize_file );
260 $first_run = '';
262 else
264 return , if ( $#current+1 eq 0 );
265 my( $path , $localize_file ) = make_paths();
266 if( $path eq $last_path )
268 add_to_buffer();
270 else
272 mkpath $path;
273 write_buffer( $last_path , $last_localize_file );
274 add_to_buffer();
275 $last_path = $path;
276 $last_localize_file = $localize_file;
280 sub add_to_buffer
282 my $plainline;
283 my $afile;
284 my $amodule;
285 foreach my $elem ( @current )
287 do {
288 $amodule=$elem->module;
289 $afile=$elem->file;
290 $plainline=$elem->line;
291 push @buffer, $plainline;
292 getNextSdfObj( $elem );
293 } while ( !$elem->endoffile && $amodule eq $elem->module && $afile eq $elem->file );
296 sub write_buffer
298 my $path = shift;
299 my $localize_file = shift;
300 my $cnt = $#buffer+1;
301 print "Write to $path $cnt lines\n";
302 open FILE , ">>$localize_file" or die "Can't open file '$localize_file'\n";
303 foreach ( @buffer )
305 print FILE $_."\n";
307 @buffer = ();
309 sub parse_options
311 my $success = GetOptions( 'sdf_files=s' => \$sdf_filename , 'merge_dir=s' => \$merge_dir ); #, 'module=s' => \$module_name );
312 if( ! ( $sdf_filename && $merge_dir && $success ) )
314 usage();
315 exit( -1 );
319 sub usage
321 print "Usage: fast_merge -sdf_files <file containing sdf file names> -merge_dir <directory>\n" ;
324 sub read_sdf_file_names
326 open FILE , "<$sdf_filename" or die "Can't open file '$sdf_filename'\n";
327 while ( <FILE> )
329 push @file_names , split " " , $_ ;
331 close ( FILE );