LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File lo-help-gen-spec of Package libreoffice-icon-themes (Project LibreOffice:Stable)

#!/usr/bin/perl -w

# This script changes the definite article of ProductName

use strict;
use File::Copy;

my $args = join ' ',  @ARGV;
my $max_level = 2;


sub init_lang_data($$$$$)
{
    my ($pdata, $lang_code, $long_name, $level, $group) = @_;

    my $curLang;
    my $upstreamLang;
    # we support the syntax: package_lang_code[/lo_source_lang_code]
    if ($lang_code =~ m/([\w\-]+)\/?([\w\-]+)?/) {
	$curLang = "$1";
	if (defined $2) {
	    $upstreamLang = "$2";
	} else {
	    $upstreamLang = $curLang;
	}
    } else {
	die "Syntax error: unknown format of the language code: $lang_code, line $.";
    }

    % {$pdata->{'lang'}{$curLang}} = ();
    my $pld = \% {$pdata->{'lang'}{$curLang}};

    $pld->{'long_name'} = $long_name;
    $pld->{'level'} = $level;
    $pld->{'group'} = $group;
    $pld->{'locale'} = $upstreamLang;
    $pld->{'locale'} =~ s/-/_/g;
    $pld->{'package-help'} = ();

    $pdata->{'max_group'} = $group if ($group > $pdata->{'max_group'});

    ($level <= $max_level) || die "Error: Maximum allowed level is $max_level, line $.";
    foreach my $l ($level .. $max_level) {
	@ {$pdata->{'level_langs_list'}[$l]} = () unless (defined $pdata->{'level_langs_list'}[$l]);
	push @ {$pdata->{'level_langs_list'}[$l]}, $upstreamLang;
	$l++;
    }

    return $pld;
}

sub read_data($$)
{
    my ($data_file, $pdata) = @_;
    # pointer to locale data
    my $pld = undef;

    $pdata->{'max_group'} = 0;
    $pdata->{'level_langs_list'} = ();
    @ {$pdata->{'level_langs_list'}[$_]} = () foreach (0 .. $max_level);

    open DATA , "< $data_file" or die "Can't open '$data_file'\n";

    while( my $line = <DATA>){
	chomp $line;
	# ignore comments
	$line =~ s/#.*$//;
	if ( $line =~ m/^\s*\%lang\s+([\w\-\/]+)\s+(\w+)\s+(\d+)\s+(\d+)\s*$/ ) {
	    # long name without without quotation marks, e.g. %lang de German 1 1
	    # the language code might be different for the packages for upstream file list, e.g. %lang gu-IN/gu Gujarati 2 3
	    $pld = init_lang_data($pdata, "$1", "$2", "$3", "$4");
	} elsif ( $line =~ m/^\s*\%lang\s+([\w\-\/]+)\s+\"([\w\s]+)\"\s+(\d+)\s+(\d+)\s*$/ ) {
	    # long name with with quotation marks, e.g. %lang en-ZA "South Africa English" 2 2
	    $pld = init_lang_data($pdata, "$1", "$2", "$3", "$4");
	} elsif ( $line =~ /^\s*\%poor-help\s*$/ ) {
	    $pld->{'poor-help'} = 1;
	} elsif ( $line =~ /^\s*\%package\s+(.*)$/ ) {
	    push @ {$pld->{'package'}}, "$1";
	} elsif ( $line =~ /^\s*$/ ) {
	    # ignore empty line
	} else {
	    die "Synrax error in $data_file, line $.\n";
	}
    }
    close(DATA);
}

##################################
# conditional lines

sub write_level_begin($$$)
{
    my ($curLevel, $newLevel, $outp) = @_;

    if ($curLevel != $newLevel) {
	${$outp} .= "%endif\n" 				if ($curLevel > 0);
	${$outp} .= "%if %test_build_langs >= $newLevel\n" 	if ($newLevel > 0);
        ${$outp} .= "\n" 				if ($curLevel > 0);
    }

    return $newLevel;
}

sub write_level_end($$)
{
    my ($curLevel, $outp) = @_;

    if ($curLevel > 0) {
	${$outp} .= "%endif\n";
    }

    return 0;
}

##############################
# simple sections

sub write_do_not_edit_section($)
{
    my $out = "###################################################################\n" .
              "## DO NOT EDIT THIS SPEC FILE\n" .
              "## Generated by:\n" .
              "## perl lo-help-gen-spec $args\n" .
              "###################################################################";
    return $out;
}


##############################
# all langs sections

sub write_metadata_section($$)
{
    my ($pdata, $curLang) = @_;
    my $pld = \% {$pdata->{'lang'}{$curLang}};
    my $out = "";

    return $out if (defined $pld->{'poor-help'});

    my $oldPackage;
    if ("$curLang" eq "en-US") {
	$oldPackage = "OpenOffice_org";
    } else {
	$oldPackage = "OpenOffice_org-$curLang";
    }

    $out .= "%package -n libreoffice-help-$curLang\n";
    $out .= "Summary:        $pld->{'long_name'} Help Localization for LibreOffice\n";
    $out .= "Group:          Productivity/Office/Suite\n";
    $out .= "Provides:       locale(libreoffice:$pld->{'locale'})\n";
    $out .= "%if 0%{?suse_version} > 01120\n";
    $out .= "Requires(post):   coreutils\n";
    $out .= "Requires(post):   grep\n";
    $out .= "Requires(post):   libreoffice >= 3.5\n";
    $out .= "Requires(postun): coreutils\n";
    $out .= "Requires(postun): grep\n";
    $out .= "Requires(postun): libreoffice >= 3.5\n";
    $out .= "%endif\n";
    $out .= "%if 0%{?suse_version} && 0%{?suse_version} <= 01120\n";
    $out .= "PreReq:         coreutils\n";
    $out .= "PreReq:         grep\n";
    $out .= "PreReq:         libreoffice >= 3.5\n";
    $out .= "%endif\n";
    $out .= "Requires:       libreoffice = %{version}\n";
    foreach  my $line (@{$pld->{'package-help'}}) {
        $out .= "$line\n";
    }
    $out .= "# compat stuff\n";
    $out .= "Provides:       OpenOffice_org-help-$curLang = %version\n";
    $out .= "Obsoletes:      OpenOffice_org-help-$curLang <= %version\n";
    $out .= "Provides:       $oldPackage:%_prefix/ooo-2.0/help/$curLang/default.css\n";


    $out .= "\n";
    $out .= "%description -n libreoffice-help-$curLang\n";
    $out .= "$pld->{'long_name'} help localization for LibreOffice. The other localized stuff\n";
    $out .= "is in libreoffice-l10n-$curLang.\n";
    $out .= "\n";
    
    return $out;
}

sub write_install_scripts_section($$)
{
    my ($pdata, $curLang) = @_;
    my $pld = \% {$pdata->{'lang'}{$curLang}};
    my $out = "";

    return $out if (defined $pld->{'poor-help'});

    $out .= "# $curLang\n";
    $out .= "\n";
    $out .= "%posttrans -n libreoffice-help-$curLang\n";
    $out .= "%_datadir/%lo_home/link-to-ooo-home %_datadir/%lo_home/help_$pld->{'locale'}_list.txt || true\n";
    $out .= "\n";
    $out .= "%preun -n libreoffice-help-$curLang\n";
    $out .= "test \"\$1\" = \"0\" && cp %_datadir/%lo_home/help_$pld->{'locale'}_list.txt %_datadir/%lo_home/help_$pld->{'locale'}_list.txt.postun || true\n";
    $out .= "\n";
    $out .= "%postun -n libreoffice-help-$curLang\n";
    $out .= "test \"\$1\" = \"0\" && %_datadir/%lo_home/link-to-ooo-home --unlink %_datadir/%lo_home/help_$pld->{'locale'}_list.txt.postun || true\n";
    $out .= "rm -f %_datadir/%lo_home/help_$pld->{'locale'}_list.txt.postun 2>/dev/null\n";

    return $out;
}

sub write_files_section($$)
{
    my ($pdata, $curLang) = @_;
    my $pld = \% {$pdata->{'lang'}{$curLang}};
    my $out = "";

    return $out if (defined $pld->{'poor-help'});

    $out .= "%files -f file-lists/help_$pld->{'locale'}_list.txt -n libreoffice-help-$curLang\n";
    $out .= "%defattr(-,root,root)\n";
    $out .= "\n";

    return $out;
}

########################################################
# universal writing functions

sub write_level_langs($$$)
{
    my ($pdata, $group, $level) = @_;
    my $out = "";

    foreach my $curLang (sort keys %{$pdata->{'lang'}}) {
        my $pld = \% {$pdata->{'lang'}{$curLang}};
	if ( ($group == $pld->{'group'}) &&
	     ($level >= $pld->{'level'}) ) {
	    $out .= " $pld->{'locale'}";
	}
    }
    $out =~ s/^ //;
    $out =~ s/_/-/g;

    return $out;
}

# the text is not repeated for each locale
sub write_simple_section($$)
{
    my ($pdata, $write_section) = @_;

    return & {$write_section} ($pdata);
}


# the text is repeated for each locale
# it is used on all distributions
sub write_locale_sections($$$)
{
    my ($pdata, $group, $write_single_lang_section) = @_;
    my $curLevel = 0;
    my $out = "";

    foreach my $curLang (sort keys %{$pdata->{'lang'}}) {
        my $pld = \% {$pdata->{'lang'}{$curLang}};
	if ($pld->{'group'} == $group) {
	    $curLevel = write_level_begin($curLevel, $pld->{'level'}, \$out);
	    $out .= & {$write_single_lang_section} ($pdata, $curLang);
	}
    }

    $curLevel = write_level_end($curLevel, \$out);
    
    return $out;
}

########################################################
# main write function

sub write_specs($$)
{
    my ($pdata, $spec_template) = @_;

    my $do_not_edit = write_simple_section ($pdata, \&write_do_not_edit_section);
    foreach my $group (0 .. $pdata->{'max_group'}) {
        my $metadata = write_locale_sections ($pdata, $group, \&write_metadata_section);
        my $install_scripts = write_locale_sections ($pdata, $group, \&write_install_scripts_section);
        my $files = write_locale_sections ($pdata, $group, \&write_files_section);

        if ($metadata || $install_scripts || $files) {
	    my $spec = $spec_template;
	    $spec =~ s/.in$//;
	    $spec =~ s/groupX/group$group/;
	
	    print "Generating $spec...\n";
	
	    open TEMPLATE , "< $spec_template" or die "Can't open '$spec_template'\n";
	    open SPEC , "> $spec" or die "Can't open '$spec for writing'\n";

	    while( my $line = <TEMPLATE>) {
	        foreach my $level (0..2) {
		    my $level_langs = write_level_langs($pdata, $group, $level);
		    $line =~ s/\@OOO_LANGS_MIN_$level\@/$level_langs/;
	        }
	        $line =~ s/\@DO_NOT_EDIT_COMMENT\@/$do_not_edit/;
	        $line =~ s/\@GROUP_NUMBER\@/$group/;
	        $line =~ s/\@SUBPACKAGES_METAINFO\@/$metadata/;
	        $line =~ s/\@SUBPACKAGES_SCRIPTS\@/$install_scripts/;
	        $line =~ s/\@SUBPACKAGES_FILELIST\@/$files/;

	        print SPEC $line;
	    }
	    close(TEMPLATE);
	    close(SPEC);
        }
        $group++;
    }
}

sub usage()
{
    print "This tool generates the help spec files\n\n" .

          "Usage:\n".
	  "\tlo-l10n-gen-spec [--help] spec_template.in data_file\n\n";
}


# info about localizations
# it is a hash, keys introduce perl-like structure items:
#	'max_group'		... integer; defined the maximum group number
#       'level_langs_list'      ... array, index is the level number, value is an array of
#				    the localizations defined for the givel level
#       'lang'			...the key is the lang id, e.g. "en-US", the value is:
#	  			   a hash, keys introduce perl-like structure items:
#	      'long_name' 	 	... string, long name of the language, e.g. "American"
#	      'level' 	 		... integer, level in whih it should get build, e.g. '1'
#					    it is the level in the rpm spec to do a reduced build
#	      'group' 	 		... integer, defines the target spec file; '0' is used for the en-US package
#	      'locale' 	 		... string, it is the code used by the upstream sources;
#					    it is usually the lang id with undersore instead of dash,
#					    e.g. "en_US"; ot it might differ from the package lang code if
#					    upstream renamed it later, for example, "gu-IN" was renamed to
#					    "gu" in ooo320-m5
#	      'poor-help' 	 	... integer, is defined when the help is poor and the help package
#					    is not created
#	      'package-help' 		... array of strings that should be added to the %package section for
#                                           the given help package
#	      'package-help' 		... array of strings that should be added to the %files section for
#                                           the given help package
my %data;
my $spec_template;
my $data_file;
my $help;



for my $arg (@ARGV) {
    if ($arg eq '--help' || $arg eq '-h') {
	usage;
	exit 0;
    } else {
	-f $arg || die "Error: The file does not exist: $arg\n";
	if (! defined $spec_template) {
	    $spec_template = $arg;
	} elsif (! defined $data_file) {
	    $data_file = $arg;
	} else {
	    die "Error: Too many arguments!\n";
	}
    }
}

die "Error: Spec file template is not defined, try --help" unless (defined $spec_template);
die "Error: Data file is not defined, try --help" unless (defined $data_file);

read_data($data_file, \%data);
write_specs(\%data, $spec_template);