xref: /aoo42x/main/l10ntools/scripts/fast_merge.pl (revision cdf0e10c)
1*cdf0e10cSrcweir:
2*cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}'
3*cdf0e10cSrcweir    if 0;
4*cdf0e10cSrcweir#*************************************************************************
5*cdf0e10cSrcweir#
6*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7*cdf0e10cSrcweir#
8*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
9*cdf0e10cSrcweir#
10*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
11*cdf0e10cSrcweir#
12*cdf0e10cSrcweir# This file is part of OpenOffice.org.
13*cdf0e10cSrcweir#
14*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
15*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
16*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
17*cdf0e10cSrcweir#
18*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
19*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
20*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
22*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
23*cdf0e10cSrcweir#
24*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
25*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
26*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
27*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
28*cdf0e10cSrcweir#
29*cdf0e10cSrcweir#*************************************************************************
30*cdf0e10cSrcweir
31*cdf0e10cSrcweiruse strict;
32*cdf0e10cSrcweiruse Class::Struct;
33*cdf0e10cSrcweiruse Getopt::Long;
34*cdf0e10cSrcweiruse File::Temp;
35*cdf0e10cSrcweiruse File::Path;
36*cdf0e10cSrcweir
37*cdf0e10cSrcweirmy @files;
38*cdf0e10cSrcweirmy @file_names;
39*cdf0e10cSrcweirmy $module_name = '';
40*cdf0e10cSrcweirmy @current;
41*cdf0e10cSrcweirmy @buffer;
42*cdf0e10cSrcweirmy $last_file;
43*cdf0e10cSrcweirmy $last_path;
44*cdf0e10cSrcweirmy $last_localize_file;
45*cdf0e10cSrcweirmy $first_run = "1";
46*cdf0e10cSrcweirmy $sdf_filename;
47*cdf0e10cSrcweirmy $merge_dir;
48*cdf0e10cSrcweirmy $state = "none";
49*cdf0e10cSrcweir
50*cdf0e10cSrcweir$SIG{INT}  = 'inthandler';
51*cdf0e10cSrcweir$SIG{QUIT} = 'quithandler';
52*cdf0e10cSrcweir
53*cdf0e10cSrcweirstruct ( sdf_obj =>
54*cdf0e10cSrcweir{
55*cdf0e10cSrcweir    module      => '$',
56*cdf0e10cSrcweir    file        => '$',
57*cdf0e10cSrcweir    dir         => '$',
58*cdf0e10cSrcweir    FILEHANDLE  => '$',
59*cdf0e10cSrcweir    line        => '$',
60*cdf0e10cSrcweir    endoffile   => '$'
61*cdf0e10cSrcweir}
62*cdf0e10cSrcweir);
63*cdf0e10cSrcweir
64*cdf0e10cSrcweirparse_options();
65*cdf0e10cSrcweirmy $lock_file   = $merge_dir."/lock.mk";
66*cdf0e10cSrcweiracquire_lock();
67*cdf0e10cSrcweirread_sdf_file_names();
68*cdf0e10cSrcweirinit();
69*cdf0e10cSrcweirmy $reference;
70*cdf0e10cSrcweirmy $path ;
71*cdf0e10cSrcweirmy $localize_file;
72*cdf0e10cSrcweirwhile( hasLines() )
73*cdf0e10cSrcweir{
74*cdf0e10cSrcweir    @current = ();
75*cdf0e10cSrcweir    foreach ( @files )
76*cdf0e10cSrcweir    {
77*cdf0e10cSrcweir        push @current , $_;
78*cdf0e10cSrcweir    }
79*cdf0e10cSrcweir
80*cdf0e10cSrcweir    $reference = getNextIdentifier( );
81*cdf0e10cSrcweir
82*cdf0e10cSrcweir    @current = ();
83*cdf0e10cSrcweir    foreach ( @files )
84*cdf0e10cSrcweir    {
85*cdf0e10cSrcweir        if( $_->module eq $reference->module && $_->dir eq $reference->dir )
86*cdf0e10cSrcweir        {
87*cdf0e10cSrcweir            push @current , $_ ;
88*cdf0e10cSrcweir        }
89*cdf0e10cSrcweir    }
90*cdf0e10cSrcweir    write_lines();
91*cdf0e10cSrcweir}
92*cdf0e10cSrcweirif( $#current+1 ne 0 )
93*cdf0e10cSrcweir{
94*cdf0e10cSrcweir    ( $path , $localize_file ) = make_paths();
95*cdf0e10cSrcweir    add_to_buffer();
96*cdf0e10cSrcweir    write_buffer( $path , $localize_file );
97*cdf0e10cSrcweir}
98*cdf0e10cSrcweirrelease_lock();
99*cdf0e10cSrcweirexit( 0 );
100*cdf0e10cSrcweir
101*cdf0e10cSrcweir##########################################################################################
102*cdf0e10cSrcweirsub acquire_lock
103*cdf0e10cSrcweir{
104*cdf0e10cSrcweir    if( -e $lock_file ){
105*cdf0e10cSrcweir        $state = "blocked";
106*cdf0e10cSrcweir        print "WARNING: Lock file '$lock_file' 'found, waiting ....\n";
107*cdf0e10cSrcweir        my $cnt = 0;
108*cdf0e10cSrcweir        sleep 10 , while( -e $lock_file && $cnt++ < 180 );
109*cdf0e10cSrcweir        exit( 0 );
110*cdf0e10cSrcweir    }else
111*cdf0e10cSrcweir    {
112*cdf0e10cSrcweir        $state = "locked";
113*cdf0e10cSrcweir        print "Writing lock file '$lock_file'\n";
114*cdf0e10cSrcweir        open FILE, ">$lock_file" or die "Can't create lock file '$lock_file'";
115*cdf0e10cSrcweir        print FILE "L10N_LOCK=YES" ;
116*cdf0e10cSrcweir        close ( FILE );
117*cdf0e10cSrcweir    }
118*cdf0e10cSrcweir}
119*cdf0e10cSrcweirsub release_lock
120*cdf0e10cSrcweir{
121*cdf0e10cSrcweir    print "Deleting lock file '$lock_file'\n";
122*cdf0e10cSrcweir    unlink $lock_file, if( -e $lock_file );
123*cdf0e10cSrcweir    $state = "none";
124*cdf0e10cSrcweir}
125*cdf0e10cSrcweirsub inthandler
126*cdf0e10cSrcweir{
127*cdf0e10cSrcweir    release_lock() , if( $state eq "locked" );
128*cdf0e10cSrcweir    exit( -1 );
129*cdf0e10cSrcweir}
130*cdf0e10cSrcweirsub quithandler
131*cdf0e10cSrcweir{
132*cdf0e10cSrcweir    release_lock() , if( $state eq "locked" );
133*cdf0e10cSrcweir    exit( 0 );
134*cdf0e10cSrcweir}
135*cdf0e10cSrcweir
136*cdf0e10cSrcweirsub init
137*cdf0e10cSrcweir{
138*cdf0e10cSrcweir    foreach my $file ( @file_names )
139*cdf0e10cSrcweir    {
140*cdf0e10cSrcweir        my $obj = new sdf_obj;
141*cdf0e10cSrcweir        open my $FILEHANDLE , "<$file" or die "Can't open file '$file'";
142*cdf0e10cSrcweir        $obj->FILEHANDLE ( $FILEHANDLE ) ;
143*cdf0e10cSrcweir        getNextSdfObj( $obj );
144*cdf0e10cSrcweir        push @files, $obj ;
145*cdf0e10cSrcweir        print "Open file '$file'\n";
146*cdf0e10cSrcweir    }
147*cdf0e10cSrcweir}
148*cdf0e10cSrcweir
149*cdf0e10cSrcweir# get the next module/file
150*cdf0e10cSrcweirsub getNextIdentifier
151*cdf0e10cSrcweir{
152*cdf0e10cSrcweir    my @sorted = sort {
153*cdf0e10cSrcweir        return $a->module.$a->dir cmp $b->module.$b->dir;
154*cdf0e10cSrcweir    } @current ;
155*cdf0e10cSrcweir    return shift @sorted;
156*cdf0e10cSrcweir}
157*cdf0e10cSrcweir
158*cdf0e10cSrcweir# update the obj with the next line
159*cdf0e10cSrcweirsub getNextSdfObj
160*cdf0e10cSrcweir{
161*cdf0e10cSrcweir    my $obj             = shift;
162*cdf0e10cSrcweir    my $line = readline ( $obj->FILEHANDLE );
163*cdf0e10cSrcweir    if ( $line eq undef )
164*cdf0e10cSrcweir    {
165*cdf0e10cSrcweir        $obj->endoffile( "true" );
166*cdf0e10cSrcweir    }
167*cdf0e10cSrcweir    else
168*cdf0e10cSrcweir    {
169*cdf0e10cSrcweir        $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*cdf0e10cSrcweir        if( defined $1 && defined $2 && defined $3 )
171*cdf0e10cSrcweir        {
172*cdf0e10cSrcweir            $obj->line  ( $1 );
173*cdf0e10cSrcweir            $obj->module( $2 );
174*cdf0e10cSrcweir            $obj->file  ( $3 );
175*cdf0e10cSrcweir            $obj->dir   ( getDir( $3 ) );
176*cdf0e10cSrcweir        }
177*cdf0e10cSrcweir        else
178*cdf0e10cSrcweir        {
179*cdf0e10cSrcweir            $obj->line  ( "" );
180*cdf0e10cSrcweir            $obj->module( "" );
181*cdf0e10cSrcweir            $obj->file  ( "" );
182*cdf0e10cSrcweir            $obj->dir   ( "" );
183*cdf0e10cSrcweir        }
184*cdf0e10cSrcweir    }
185*cdf0e10cSrcweir    return $obj;
186*cdf0e10cSrcweir}
187*cdf0e10cSrcweirsub getNextSdfObjModule
188*cdf0e10cSrcweir{
189*cdf0e10cSrcweir    my $obj             = shift;
190*cdf0e10cSrcweir    while( !$obj->endoffile )
191*cdf0e10cSrcweir    {
192*cdf0e10cSrcweir        my $line = readline ( $obj->FILEHANDLE );
193*cdf0e10cSrcweir        if ( $line eq undef )
194*cdf0e10cSrcweir        {
195*cdf0e10cSrcweir            $obj->endoffile( "true" );
196*cdf0e10cSrcweir        }
197*cdf0e10cSrcweir        else
198*cdf0e10cSrcweir        {
199*cdf0e10cSrcweir            $line =~ /^(([^\t]*)\t([^\t]*).*)/o ;
200*cdf0e10cSrcweir            if( defined $1 && defined $2 && defined $3 )
201*cdf0e10cSrcweir            {
202*cdf0e10cSrcweir                $obj->line  ( $1 );
203*cdf0e10cSrcweir                $obj->module( $2 );
204*cdf0e10cSrcweir                $obj->file  ( $3 );
205*cdf0e10cSrcweir                $obj->dir   ( getDir( $3 ) );
206*cdf0e10cSrcweir            }
207*cdf0e10cSrcweir            else
208*cdf0e10cSrcweir            {
209*cdf0e10cSrcweir                $obj->line  ( "" );
210*cdf0e10cSrcweir                $obj->module( "" );
211*cdf0e10cSrcweir                $obj->file  ( "" );
212*cdf0e10cSrcweir                $obj->dir   ( "" );
213*cdf0e10cSrcweir            }
214*cdf0e10cSrcweir            return $obj , if( $obj->module eq $module_name )
215*cdf0e10cSrcweir        }
216*cdf0e10cSrcweir    }
217*cdf0e10cSrcweir    #return $obj;
218*cdf0e10cSrcweir}
219*cdf0e10cSrcweirsub getDir
220*cdf0e10cSrcweir{
221*cdf0e10cSrcweir    my $path     = shift ;
222*cdf0e10cSrcweir    $path        =~ s/\//\\/g;
223*cdf0e10cSrcweir    my @tmp_path = split /\\/ , $path;
224*cdf0e10cSrcweir    pop @tmp_path;
225*cdf0e10cSrcweir    $path        = join  '\\' , @tmp_path;
226*cdf0e10cSrcweir    return $path;
227*cdf0e10cSrcweir}
228*cdf0e10cSrcweir
229*cdf0e10cSrcweirsub hasLines
230*cdf0e10cSrcweir{
231*cdf0e10cSrcweir    my $hasLines  = "";
232*cdf0e10cSrcweir    my @tmpfiles;
233*cdf0e10cSrcweir    foreach ( @files )
234*cdf0e10cSrcweir    {
235*cdf0e10cSrcweir        push @tmpfiles , $_, if( !$_->endoffile );
236*cdf0e10cSrcweir    }
237*cdf0e10cSrcweir    @files = @tmpfiles;
238*cdf0e10cSrcweir    return $#files+1;
239*cdf0e10cSrcweir}
240*cdf0e10cSrcweir
241*cdf0e10cSrcweirsub make_paths
242*cdf0e10cSrcweir{
243*cdf0e10cSrcweir    my $localizeFile = $merge_dir."\\".$current[ 0 ]->module."\\".$current[ 0 ]->file;
244*cdf0e10cSrcweir    my $path = getDir( $localizeFile );
245*cdf0e10cSrcweir    $path =~ s/\\/\//g;
246*cdf0e10cSrcweir
247*cdf0e10cSrcweir    $localizeFile = $path."/localize.sdf";
248*cdf0e10cSrcweir
249*cdf0e10cSrcweir    return ( $path , $localizeFile );
250*cdf0e10cSrcweir}
251*cdf0e10cSrcweirsub write_lines
252*cdf0e10cSrcweir{
253*cdf0e10cSrcweir    if( $first_run ){
254*cdf0e10cSrcweir        add_to_buffer();
255*cdf0e10cSrcweir        my( $path , $localize_file ) = make_paths();
256*cdf0e10cSrcweir        $last_path = $path;
257*cdf0e10cSrcweir        $last_localize_file = $localize_file;
258*cdf0e10cSrcweir        mkpath $path;
259*cdf0e10cSrcweir        write_buffer( $path , $localize_file );
260*cdf0e10cSrcweir        $first_run = '';
261*cdf0e10cSrcweir    }
262*cdf0e10cSrcweir    else
263*cdf0e10cSrcweir    {
264*cdf0e10cSrcweir        return , if ( $#current+1 eq 0 );
265*cdf0e10cSrcweir        my( $path , $localize_file ) = make_paths();
266*cdf0e10cSrcweir        if( $path eq $last_path )
267*cdf0e10cSrcweir        {
268*cdf0e10cSrcweir            add_to_buffer();
269*cdf0e10cSrcweir        }
270*cdf0e10cSrcweir        else
271*cdf0e10cSrcweir        {
272*cdf0e10cSrcweir            mkpath $path;
273*cdf0e10cSrcweir            write_buffer( $last_path , $last_localize_file );
274*cdf0e10cSrcweir            add_to_buffer();
275*cdf0e10cSrcweir            $last_path = $path;
276*cdf0e10cSrcweir            $last_localize_file = $localize_file;
277*cdf0e10cSrcweir        }
278*cdf0e10cSrcweir    }
279*cdf0e10cSrcweir}
280*cdf0e10cSrcweirsub add_to_buffer
281*cdf0e10cSrcweir{
282*cdf0e10cSrcweir	my $plainline;
283*cdf0e10cSrcweir	my $afile;
284*cdf0e10cSrcweir	my $amodule;
285*cdf0e10cSrcweir    foreach my $elem ( @current )
286*cdf0e10cSrcweir    {
287*cdf0e10cSrcweir		do {
288*cdf0e10cSrcweir        $amodule=$elem->module;
289*cdf0e10cSrcweir        $afile=$elem->file;
290*cdf0e10cSrcweir		$plainline=$elem->line;
291*cdf0e10cSrcweir        push @buffer, $plainline;
292*cdf0e10cSrcweir        getNextSdfObj( $elem );
293*cdf0e10cSrcweir		} while ( !$elem->endoffile && $amodule eq $elem->module && $afile eq $elem->file );
294*cdf0e10cSrcweir    }
295*cdf0e10cSrcweir}
296*cdf0e10cSrcweirsub write_buffer
297*cdf0e10cSrcweir{
298*cdf0e10cSrcweir    my $path            = shift;
299*cdf0e10cSrcweir    my $localize_file   = shift;
300*cdf0e10cSrcweir    my $cnt             = $#buffer+1;
301*cdf0e10cSrcweir    print "Write to $path $cnt lines\n";
302*cdf0e10cSrcweir    open FILE , ">>$localize_file" or die "Can't open file '$localize_file'\n";
303*cdf0e10cSrcweir    foreach ( @buffer )
304*cdf0e10cSrcweir    {
305*cdf0e10cSrcweir        print FILE $_."\n";
306*cdf0e10cSrcweir    }
307*cdf0e10cSrcweir    @buffer = ();
308*cdf0e10cSrcweir}
309*cdf0e10cSrcweirsub parse_options
310*cdf0e10cSrcweir{
311*cdf0e10cSrcweir    my $success = GetOptions( 'sdf_files=s' => \$sdf_filename , 'merge_dir=s' => \$merge_dir ); #, 'module=s' => \$module_name );
312*cdf0e10cSrcweir    if( ! ( $sdf_filename && $merge_dir && $success ) )
313*cdf0e10cSrcweir    {
314*cdf0e10cSrcweir        usage();
315*cdf0e10cSrcweir        exit( -1 );
316*cdf0e10cSrcweir    }
317*cdf0e10cSrcweir}
318*cdf0e10cSrcweir
319*cdf0e10cSrcweirsub usage
320*cdf0e10cSrcweir{
321*cdf0e10cSrcweir    print "Usage: fast_merge -sdf_files <file containing sdf file names> -merge_dir <directory>\n" ;
322*cdf0e10cSrcweir}
323*cdf0e10cSrcweir
324*cdf0e10cSrcweirsub read_sdf_file_names
325*cdf0e10cSrcweir{
326*cdf0e10cSrcweir    open FILE , "<$sdf_filename" or die "Can't open file '$sdf_filename'\n";
327*cdf0e10cSrcweir    while (  <FILE> )
328*cdf0e10cSrcweir    {
329*cdf0e10cSrcweir        push @file_names , split " " , $_ ;
330*cdf0e10cSrcweir    }
331*cdf0e10cSrcweir    close ( FILE );
332*cdf0e10cSrcweir}
333*cdf0e10cSrcweir
334*cdf0e10cSrcweir
335