xref: /aoo42x/main/l10ntools/scripts/fast_merge.pl (revision cdf0e10c)
1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#*************************************************************************
5#
6# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7#
8# Copyright 2000, 2010 Oracle and/or its affiliates.
9#
10# OpenOffice.org - a multi-platform office productivity suite
11#
12# This file is part of OpenOffice.org.
13#
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.
17#
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).
23#
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.
28#
29#*************************************************************************
30
31use strict;
32use Class::Struct;
33use Getopt::Long;
34use File::Temp;
35use File::Path;
36
37my @files;
38my @file_names;
39my $module_name = '';
40my @current;
41my @buffer;
42my $last_file;
43my $last_path;
44my $last_localize_file;
45my $first_run = "1";
46my $sdf_filename;
47my $merge_dir;
48my $state = "none";
49
50$SIG{INT}  = 'inthandler';
51$SIG{QUIT} = 'quithandler';
52
53struct ( sdf_obj =>
54{
55    module      => '$',
56    file        => '$',
57    dir         => '$',
58    FILEHANDLE  => '$',
59    line        => '$',
60    endoffile   => '$'
61}
62);
63
64parse_options();
65my $lock_file   = $merge_dir."/lock.mk";
66acquire_lock();
67read_sdf_file_names();
68init();
69my $reference;
70my $path ;
71my $localize_file;
72while( hasLines() )
73{
74    @current = ();
75    foreach ( @files )
76    {
77        push @current , $_;
78    }
79
80    $reference = getNextIdentifier( );
81
82    @current = ();
83    foreach ( @files )
84    {
85        if( $_->module eq $reference->module && $_->dir eq $reference->dir )
86        {
87            push @current , $_ ;
88        }
89    }
90    write_lines();
91}
92if( $#current+1 ne 0 )
93{
94    ( $path , $localize_file ) = make_paths();
95    add_to_buffer();
96    write_buffer( $path , $localize_file );
97}
98release_lock();
99exit( 0 );
100
101##########################################################################################
102sub acquire_lock
103{
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
111    {
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 );
117    }
118}
119sub release_lock
120{
121    print "Deleting lock file '$lock_file'\n";
122    unlink $lock_file, if( -e $lock_file );
123    $state = "none";
124}
125sub inthandler
126{
127    release_lock() , if( $state eq "locked" );
128    exit( -1 );
129}
130sub quithandler
131{
132    release_lock() , if( $state eq "locked" );
133    exit( 0 );
134}
135
136sub init
137{
138    foreach my $file ( @file_names )
139    {
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";
146    }
147}
148
149# get the next module/file
150sub getNextIdentifier
151{
152    my @sorted = sort {
153        return $a->module.$a->dir cmp $b->module.$b->dir;
154    } @current ;
155    return shift @sorted;
156}
157
158# update the obj with the next line
159sub getNextSdfObj
160{
161    my $obj             = shift;
162    my $line = readline ( $obj->FILEHANDLE );
163    if ( $line eq undef )
164    {
165        $obj->endoffile( "true" );
166    }
167    else
168    {
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 )
171        {
172            $obj->line  ( $1 );
173            $obj->module( $2 );
174            $obj->file  ( $3 );
175            $obj->dir   ( getDir( $3 ) );
176        }
177        else
178        {
179            $obj->line  ( "" );
180            $obj->module( "" );
181            $obj->file  ( "" );
182            $obj->dir   ( "" );
183        }
184    }
185    return $obj;
186}
187sub getNextSdfObjModule
188{
189    my $obj             = shift;
190    while( !$obj->endoffile )
191    {
192        my $line = readline ( $obj->FILEHANDLE );
193        if ( $line eq undef )
194        {
195            $obj->endoffile( "true" );
196        }
197        else
198        {
199            $line =~ /^(([^\t]*)\t([^\t]*).*)/o ;
200            if( defined $1 && defined $2 && defined $3 )
201            {
202                $obj->line  ( $1 );
203                $obj->module( $2 );
204                $obj->file  ( $3 );
205                $obj->dir   ( getDir( $3 ) );
206            }
207            else
208            {
209                $obj->line  ( "" );
210                $obj->module( "" );
211                $obj->file  ( "" );
212                $obj->dir   ( "" );
213            }
214            return $obj , if( $obj->module eq $module_name )
215        }
216    }
217    #return $obj;
218}
219sub getDir
220{
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;
227}
228
229sub hasLines
230{
231    my $hasLines  = "";
232    my @tmpfiles;
233    foreach ( @files )
234    {
235        push @tmpfiles , $_, if( !$_->endoffile );
236    }
237    @files = @tmpfiles;
238    return $#files+1;
239}
240
241sub make_paths
242{
243    my $localizeFile = $merge_dir."\\".$current[ 0 ]->module."\\".$current[ 0 ]->file;
244    my $path = getDir( $localizeFile );
245    $path =~ s/\\/\//g;
246
247    $localizeFile = $path."/localize.sdf";
248
249    return ( $path , $localizeFile );
250}
251sub write_lines
252{
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 = '';
261    }
262    else
263    {
264        return , if ( $#current+1 eq 0 );
265        my( $path , $localize_file ) = make_paths();
266        if( $path eq $last_path )
267        {
268            add_to_buffer();
269        }
270        else
271        {
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;
277        }
278    }
279}
280sub add_to_buffer
281{
282	my $plainline;
283	my $afile;
284	my $amodule;
285    foreach my $elem ( @current )
286    {
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 );
294    }
295}
296sub write_buffer
297{
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 )
304    {
305        print FILE $_."\n";
306    }
307    @buffer = ();
308}
309sub parse_options
310{
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 ) )
313    {
314        usage();
315        exit( -1 );
316    }
317}
318
319sub usage
320{
321    print "Usage: fast_merge -sdf_files <file containing sdf file names> -merge_dir <directory>\n" ;
322}
323
324sub read_sdf_file_names
325{
326    open FILE , "<$sdf_filename" or die "Can't open file '$sdf_filename'\n";
327    while (  <FILE> )
328    {
329        push @file_names , split " " , $_ ;
330    }
331    close ( FILE );
332}
333
334
335