LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File lo-languagetool-gen-spec of Package libreoffice-languagetool (Project LibreOffice:Stable)

#!/usr/bin/perl -w

# This script changes the definite article of ProductName

use strict;
use File::Copy;

my $args = join ' ',  @ARGV;
# FIXME: we need a global variable to pass the data to the sort function
my $pdata_sort = undef;



sub init_locale_data($$$$)
{
    my ($pdata, $locale, $long_name, $level) = @_;
    
    % {$pdata->{'dict'}{$locale}} = ();
    my $pld = \% {$pdata->{'dict'}{$locale}};
    
    $pld->{'long_name'} = $long_name;
    $pld->{'level'} = $level;
    $pld->{'pack_suffix'} = "$locale";
    $pld->{'pack_suffix'} =~ s/_/-/g;
    
    return $pld;
}    

sub remember_licenses($$)
{
    my ($pdata, $list) = @_;

    foreach my $item ( split(/,\s*/, $list) ) {
	$pdata->{'license'}{"$item"} = 1;
    }
}


sub add_locale_data_list($$$$)
{
    my ($pdata, $pld, $tag, $value) = @_;

    foreach my $item ( split(/,\s*/, $value) ) {
	@ {$pld->{$tag}} = () unless (defined $pld->{$tag});
	push @ {$pld->{$tag}}, $item;
    }
    remember_licenses($pdata, $value) if ( $tag eq 'license' );
}

sub read_data($$)
{
    my ($pdata, $data_file) = @_;
    # pointer to locale data
    my $pld = undef;
    
    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*$/ ) {
	    $pld = init_locale_data($pdata, "$1", "$2", "$3");
	} elsif ( $line =~ m/^\s*\%lang\s+([\w-]+)\s+\"([\w\s]+)\"\s+(\d+)\s*$/ ) {
	    $pld = init_locale_data($pdata, "$1", "$2", "$3");
	} elsif ( $line =~ /^\s*\%license\s+(.*)$/ ) {
	    add_locale_data_list($pdata, $pld, 'license', "$1");
	} elsif ( $line =~ /^\s*\%license_libs\s+(.*)$/ ) {
	    remember_licenses($pdata, "$1");
	} elsif ( $line =~ /^\s*\%files\s+(.*)$/ ) {
	    push @ {$pld->{'files'}}, "$1";
	} elsif ( $line =~ /^\s*$/ ) {
	    # ignore empty line
	} else {
	    die "Synrax error in $data_file, line $.\n";
	}
    }
    close(DATA);
}


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

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

sub write_all_licenses_section($)
{
    my ($pdata) = @_;

    my @licenses = sort (keys %{$pdata->{'license'}});
    my $out = join " and ", @licenses;

    return $out;
}


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

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

    unless (defined $pld->{'license'}) {
	die "Error: License is not defined for the locale: $locale\n";
    }

    $out .= "%package -n libreoffice-languagetool-$pld->{'pack_suffix'}\n";
    $out .= "Summary:        $pld->{'long_name'} Dictionary for LibreOffice Language Tool\n";
    $out .= "License:        " . join (" and ", @{$pld->{'license'}}) . "\n";
    $out .= "Group:          Productivity/Office/Dictionary\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-languagetool = %{version}\n";
    $out .= "Provides:       locale(libreoffice-languagetool:$locale)\n";
    $out .= "# compat stuff\n";
    $out .= "Provides:       OpenOffice_org-LanguageTool-$pld->{'pack_suffix'} = %{version}\n";
    $out .= "Obsoletes:      OpenOffice_org-LanguageTool-$pld->{'pack_suffix'} <= %{version}\n";

    $out .= "\n";

    $out .= "%description -n libreoffice-languagetool-$pld->{'pack_suffix'}\n";
    $out .= "The $pld->{'long_name'} dictionary that can be used to check grammar and other more\n";
    $out .= "complex mistakes using the LibreOffice language tool extension.\n";
    $out .= "\n";

    return $out;
}

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

    $out .= "# $locale\n";
    $out .= "%posttrans -n libreoffice-languagetool-$pld->{'pack_suffix'}\n";
    $out .= "%{_datadir}/%ooo_home/link-to-ooo-home %{_datadir}/%ooo_home/files-langtool-$locale.txt || true\n";
    $out .= "\n";
    $out .= "%preun -n libreoffice-languagetool-$pld->{'pack_suffix'}\n";
    $out .= "cp %{_datadir}/%ooo_home/files-langtool-$locale.txt %{_datadir}/%ooo_home/files-langtool-$locale.txt.postun || true\n";
    $out .= "\n";
    $out .= "%postun -n libreoffice-languagetool-$pld->{'pack_suffix'}\n";
    $out .= "unlink_options=--unlink\n";
    $out .= "grep -q \"unlink-dirs\" %{_datadir}/%ooo_home/link-to-ooo-home && unlink_options=\"\$unlink_options --unlink-dirs\" || true\n";
    $out .= "%{_datadir}/%ooo_home/link-to-ooo-home \$unlink_options %{_datadir}/%ooo_home/files-langtool-$locale.txt.postun || true\n";
    $out .= "rm -f %{_datadir}/%ooo_home/files-langtool-$locale.txt.postun 2>/dev/null\n";
    $out .= "\n";

    return $out;
}

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

    $out .= "%files -f files-langtool-$locale.txt -n libreoffice-languagetool-$pld->{'pack_suffix'}\n";
    $out .= "%defattr(-,root,root)\n";

    foreach  my $line (@{$pld->{'files'}}) {
        $out .= "$line\n";
    }

    $out .= "\n";

    return $out;
}

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

sub sort_dictionaries()
{
    # sort the dictionaries by the package name
    $pdata_sort->{'dict'}{$a}->{'pack_suffix'} cmp $pdata_sort->{'dict'}{$b}->{'pack_suffix'};
}

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

    $pdata_sort = $pdata;
    foreach my $locale (sort sort_dictionaries keys %{$pdata->{'dict'}}) {
	$out .= & {$write_section} ($pdata, $locale);
    }
    return $out;
}

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

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

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

    my $all_licenses = write_simple_section ($pdata, \&write_all_licenses_section);
    my $do_not_edit = write_simple_section ($pdata, \&write_do_not_edit_section);
    my $metadata = write_locale_sections ($pdata, \&write_metadata_section);
    my $install_scripts = write_locale_sections ($pdata, \&write_install_scripts_section);
    my $files = write_locale_sections ($pdata, \&write_files_section);

    my $spec = $spec_template;
    $spec =~ s/.in$//;

    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>) {
	$line =~ s/\@ALL_LICENSES\@/$all_licenses/;
	$line =~ s/\@DO_NOT_EDIT_COMMENT\@/$do_not_edit/;
	$line =~ s/\@METADATA\@/$metadata/;
	$line =~ s/\@INSTALL_SCRIPTS\@/$install_scripts/;
	$line =~ s/\@FILES\@/$files/;

	print SPEC $line;
    }

    close(TEMPLATE);
    close(SPEC);
}

############################################################
# main stuff
############################################################

sub usage()
{
    print "This tool generates the LanguageTool spec file\n\n" .

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


# info about data structure
# it is a hash, keys introduce perl-like structure items:
#	'dict'		... hash; key is the primary locale for the given dictioanry, e.g. "en_US"
#			    the value is hash that store an information about ech dictionary using
#			    tags; the known tags are:
#	      'long_name' 	hash(*) of string; long name of the language, e.g. "American"
#	      'level' 	 	hash(*) of integer; level in whih it should get build, e.g. '1'
#					   it is the level in the rpm spec to do a reduced build
#	      'pack_suffix'	hash(*) of string; like the locale but used hash instead of underscore, e.g en-us
#	      'license'         hash(*) of array of strings; defines the license of the package
#	      'files' 		hash(*) of array of strings; libes that should be added to the %files section
#	'license'	... hash; key is the license used for a dictionary, e.g. LGPLv2; it is used to generate
#				licenses for the source package

#
# hash(*) of ...   - key is the condition under which the tag is defined; it is usually used to define a special tag
#		     for another distro; the key 'default' defines tags for the default distro, ...
my %data;
my $spec_template;
my $data_file;

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, $data_file);
write_spec(\%data, $spec_template);