libreoffice/lo-help-gen-spec

325 lines
11 KiB
Perl

#!/usr/bin/perl -w
# This script changes the definite article of ProductName
use strict;
use File::Copy;
my $max_level = 2;
sub init_lang_data($$$$$)
{
my ($datap, $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 $.";
}
$datap->{'lang'}{$curLang}{'long_name'} = $long_name;
$datap->{'lang'}{$curLang}{'level'} = $level;
$datap->{'lang'}{$curLang}{'group'} = $group;
$datap->{'lang'}{$curLang}{'locale'} = $upstreamLang;
$datap->{'lang'}{$curLang}{'locale'} =~ s/-/_/g;
$datap->{'lang'}{$curLang}{'package-help'} = ();
$datap->{'max_group'} = $group if ($group > $datap->{'max_group'});
($level <= $max_level) || die "Error: Maximum allowed level is $max_level, line $.";
foreach my $l ($level .. $max_level) {
@ {$datap->{'level_langs_list'}[$l]} = () unless (defined $datap->{'level_langs_list'}[$l]);
push @ {$datap->{'level_langs_list'}[$l]}, $upstreamLang;
$l++;
}
return $curLang;
}
sub read_data($$)
{
my ($data_file, $datap) = @_;
my $curLang;
$datap->{'max_group'} = 0;
$datap->{'level_langs_list'} = ();
@ {$datap->{'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
$curLang = init_lang_data($datap, "$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
$curLang = init_lang_data($datap, "$1", "$2", "$3", "$4");
} elsif ( $line =~ /^\s*\%poor-help\s*$/ ) {
$datap->{'lang'}{$curLang}{'poor-help'} = 1;
} elsif ( $line =~ /^\s*\%package-help\s+(.*)$/ ) {
push @ {$datap->{'lang'}{$curLang}{'package-help'}}, "$1";
} elsif ( $line =~ /^\s*\%files-help\s+(.*)$/ ) {
push @ {$datap->{'lang'}{$curLang}{'files-help'}}, "$1";
} elsif ( $line =~ /^\s*$/ ) {
# ignore empty line
} else {
die "Synrax error in $data_file, line $.\n";
}
}
close(DATA);
}
sub write_level_begin($$$)
{
my ($curLevel, $newLevel, $outp) = @_;
if ($curLevel != $newLevel) {
${$outp} .= "\n";
${$outp} .= "%endif\n" if ($curLevel > 0);
${$outp} .= "%if %test_build_langs >= $newLevel\n" if ($newLevel > 0);
}
return $newLevel;
}
sub write_level_end($$)
{
my ($curLevel, $outp) = @_;
if ($curLevel > 0) {
${$outp} .= "%endif\n";
}
return 0;
}
sub write_help_package_section($$$)
{
my ($datap, $curLang, $outp) = @_;
my $oldPackage;
return if (defined $datap->{'lang'}{$curLang}{'poor-help'});
if ("$curLang" eq "en-US") {
$oldPackage = "OpenOffice_org";
} else {
$oldPackage = "OpenOffice_org-$curLang";
}
${$outp} .= "\n";
${$outp} .= "%package -n libreoffice-help-$curLang\n";
${$outp} .= "License: LGPLv3\n";
${$outp} .= "Summary: $datap->{'lang'}{$curLang}{'long_name'} Help Localization for LibreOffice\n";
${$outp} .= "Group: Productivity/Office/Suite\n";
${$outp} .= "Provides: locale(libreoffice:$datap->{'lang'}{$curLang}{'locale'})\n";
${$outp} .= "PreReq: libreoffice = %version\n";
${$outp} .= "%if 0%{?suse_version}\n";
${$outp} .= "Supplements: libreoffice-l10n-$curLang\n";
${$outp} .= "%endif\n";
${$outp} .= "# compat stuff\n";
${$outp} .= "Provides: OpenOffice_org-help-$curLang = %version\n";
${$outp} .= "Obsoletes: OpenOffice_org-help-$curLang <= %version\n";
${$outp} .= "Provides: $oldPackage:%_prefix/ooo-2.0/help/$curLang/default.css\n";
foreach my $line (@{$datap->{'lang'}{$curLang}{'package-help'}}) {
${$outp} .= "$line\n";
}
${$outp} .= "\n";
${$outp} .= "%description -n libreoffice-help-$curLang\n";
${$outp} .= "$datap->{'lang'}{$curLang}{'long_name'} help localization for LibreOffice. The other localized stuff\n";
${$outp} .= "is in libreoffice-l10n-$curLang.\n";
${$outp} .= "\n";
${$outp} .= "\n";
}
sub write_help_scripts_section($$$)
{
my ($datap, $curLang, $outp) = @_;
return if (defined $datap->{'lang'}{$curLang}{'poor-help'});
${$outp} .= "# $curLang\n";
${$outp} .= "%posttrans -n libreoffice-help-$curLang\n";
${$outp} .= "%_datadir/%lo_home/link-to-ooo-home %_datadir/%lo_home/help_$datap->{'lang'}{$curLang}{'locale'}_list.txt || true\n";
${$outp} .= "\n";
${$outp} .= "%preun -n libreoffice-help-$curLang\n";
${$outp} .= "test \"\$1\" = \"0\" && cp %_datadir/%lo_home/help_$datap->{'lang'}{$curLang}{'locale'}_list.txt %_datadir/%lo_home/help_$datap->{'lang'}{$curLang}{'locale'}_list.txt.postun || true\n";
${$outp} .= "\n";
${$outp} .= "%postun -n libreoffice-help-$curLang\n";
${$outp} .= "test \"\$1\" = \"0\" && %_datadir/%lo_home/link-to-ooo-home --unlink %_datadir/%lo_home/help_$datap->{'lang'}{$curLang}{'locale'}_list.txt.postun || true\n";
${$outp} .= "rm -f %_datadir/%lo_home/help_$datap->{'lang'}{$curLang}{'locale'}_list.txt.postun 2>/dev/null\n";
}
sub write_help_files_section($$$$)
{
my ($datap, $curLang, $outp) = @_;
return if (defined $datap->{'lang'}{$curLang}{'poor-help'});
${$outp} .= "\n";
${$outp} .= "%files -f file-lists/help_$datap->{'lang'}{$curLang}{'locale'}_list.txt -n libreoffice-help-$curLang\n";
${$outp} .= "%defattr(-,root,root)\n";
foreach my $line (@{$datap->{'lang'}{$curLang}{'files-help'}}) {
${$outp} .= "$line\n";
}
}
sub write_section($$$$)
{
my ($datap, $group, $write_single_lang_section, $outp) = @_;
my $curLevel = 0;
foreach my $curLang (sort keys %{$datap->{'lang'}}) {
if ($datap->{'lang'}{$curLang}{'group'} == $group) {
$curLevel = write_level_begin($curLevel, $datap->{'lang'}{$curLang}{'level'}, $outp);
& {$write_single_lang_section} ($datap, $curLang, $outp);
}
}
$curLevel = write_level_end($curLevel, $outp);
}
sub write_level_langs($$$)
{
my ($datap, $group, $level) = @_;
my $level_langs = "";
foreach my $curLang (sort keys %{$datap->{'lang'}}) {
if ( ($group == $datap->{'lang'}{$curLang}{'group'}) &&
($level >= $datap->{'lang'}{$curLang}{'level'}) ) {
$level_langs .= " $datap->{'lang'}{$curLang}{'locale'}";
}
}
$level_langs =~ s/^ //;
$level_langs =~ s/_/-/g;
return $level_langs;
}
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;
my $args = join ' ', @ARGV;
my $do_not_edit = "###################################################################\n" .
"## DO NOT EDIT THIS SPEC FILE\n" .
"## Generated by:\n" .
"## perl lo-help-gen-spec $args\n" .
"###################################################################";
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);
foreach my $group (0 .. $data{'max_group'}) {
my $metainfo = "";
my $scripts = "";
my $filelist = "";
write_section (\%data, $group, \&write_help_package_section, \$metainfo);
write_section (\%data, $group, \&write_help_scripts_section, \$scripts);
write_section (\%data, $group, \&write_help_files_section, \$filelist);
if ($metainfo || $scripts || $filelist) {
my $spec = $spec_template;
$spec =~ s/.in$//;
$spec =~ s/groupX/group$group/;
if (-f $spec && ! -f "$spec.old") {
print "Saving $spec.old...\n";
copy("$spec","$spec.old");
}
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(\%data, $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\@/$metainfo/;
$line =~ s/\@SUBPACKAGES_SCRIPTS\@/$scripts/;
$line =~ s/\@SUBPACKAGES_FILELIST\@/$filelist/;
print SPEC $line;
}
close(TEMPLATE);
close(SPEC);
}
$group++;
}