aboutsummaryrefslogtreecommitdiffstats
path: root/Perl/Debian/L10n/Db.pm
blob: 662c28973721af7d275d097511a7d5443e1a1d91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/bin/perl -w

##  Copyright (C) 2001-2004  Denis Barbier <barbier@debian.org>
##  Copyright (C) 2004       Martin Quinson <martin.quinson@tuxfamily.org>
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.

=head1 NAME

Debian::L10n::Db - handle database of debian l10n stuff

=head1 SYNOPSIS

 use Debian::L10n::Db;
 my $l10n_db = Debian::L10n::Db->new();
 $l10n_db->read("../data/unstable");
 foreach ($l10n_db->list_packages()) {
         print "Package $_ ".$l10n_db->version($_)."\n";
 }

=head1 DESCRIPTION

This module is an interface to the database files used in several places of
the debian localisation infrastructure, such as the webpages under
C<webwml/E<lt>languageE<gt>/international/l10n/>.

=head1 METHODS

=over 4

=cut

package Debian::L10n::Db;
use strict;
use Time::localtime;
use File::Path;
use Data::Dumper;

#   Do not use ``our'' to be compatible with Perl 5.005
use vars (qw($AUTOLOAD));

# Define data that are used in various places:
# stattrans.pl, english/international/l10n/scripts/gen-files.pl

use Exporter;
our @ISA=('Exporter');
our @EXPORT_OK=('%LanguageList');

our %LanguageList = (
	AR    => 'arabic',
	CA    => 'catalan',
	CS    => 'czech',
	DA    => 'danish',
	DE    => 'german',
	IT    => 'italian',
	RU    => 'russian',
# Used by the Smith project, not for translations
#       EN    => 'english',
	ES    => 'spanish',
	FR    => 'french',
	GL    => 'galician',
# Not supported yet by the robot. Not all messages are sent ot the list
#	NL    => 'dutch',
	PT_BR => 'portuguese',
	RO    => 'romanian',
	SK    => 'slovak',
	SV    => 'swedish',
# Has not used pseudo-urls recently
#	TR    => 'turkish',
);

=item new

This is the constructor, it only performs some initialization.

   my $l10n_db = Debian::L10n::Db->new();

=cut

sub new {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self  = {
                data    => {},
	        #   Fields below constitute the header of the files. they are written 
	        #   as fields of a package called '' (that's the same trick than in po files)
	        
	        # Language Year Month Message are for the spider
	        headers => [qw{Date Language Year Month Message Page}],
                #   Fields below are written into file in the same order
                #   Package must always be the first field
	    
                #   Switch is used temporarily to detect packages which
                #   depend on debconf and did not switch to using po-debconf.
                scalar  => [qw(Package Version Section Priority Maintainer PoolDir Type Upstream 
		               Switch )],
                array1  => [qw(Errors Catgets Gettext)],
                array2  => [qw(NLS PO TEMPLATES PODEBCONF PO4A MENU DESKTOP MAN STATUS)],
        };
        $self->{methods} = {};
        foreach (@{$self->{scalar}}) {
                $self->{fields}->{$_} = '$';
        }
        foreach (@{$self->{array1}}) {
                $self->{fields}->{$_} = '@';
        }
        foreach (@{$self->{array2}}) {
                $self->{fields}->{$_} = '@@';
        }
        foreach (keys %{$self->{fields}}) {
                $self->{methods}->{lc $_} = $_;
        }
        bless ($self, $class);
        return $self;
}

sub AUTOLOAD {
        my $self = shift;
        my $type = ref($self) or die "$self is not an object";
        my $pkg  = shift;

        my $name = $AUTOLOAD;
        $name =~ s/.*://;   # strip fully-qualified portion

        return defined($self->{data}->{$pkg}) if $name eq 'has_package';

        #   Add a new package into database
        $self->{data}->{$pkg} = {} if $name eq 'package';

        if (! defined $self->{data}->{$pkg}) {
                warn __PACKAGE__.": Package $pkg does not exist, method \`$name' skipped\n";
                return;
        }
        my $has = "";
        my $add = "";
        if ($name =~ s/^has_//) {
                $has = "has_";
        } elsif ($name =~ s/^add_//) {
                $add = "add_";
        }

        die "Can't access \`$has$name' method in class $type"
                unless defined($self->{methods}->{$name});

        my $field = $self->{methods}->{$name};

        if ($has) {
                return defined($self->{data}->{$pkg}->{$field});
        } else {
                if ($#_ == -1) {
                        if ($self->{fields}->{$field} =~ m/@/) {
                                return $self->{data}->{$pkg}->{$field} || [];
                        }
                        return $self->{data}->{$pkg}->{$field};
                }
                if ($self->{fields}->{$field} eq '$') {
                        $self->{data}->{$pkg}->{$field} = $_[0];
                } elsif ($self->{fields}->{$field} eq '@') {
                        $self->{data}->{$pkg}->{$field} = []
                                unless defined($self->{data}->{$pkg}->{$field})
                                        || !$add;
                        push (@{$self->{data}->{$pkg}->{$field}}, @_);
                } elsif ($self->{fields}->{$field} eq '@@') {
                        $self->{data}->{$pkg}->{$field} = []
                                unless defined($self->{data}->{$pkg}->{$field})
                                        || !$add;
                        my @list = @_;
                        push (@{$self->{data}->{$pkg}->{$field}}, \@list);
                } else {
                        die __PACKAGE__.":internal error: unknown data type:".$self->{fields}->{$field}."\n";
                }
        }
}

#    Perl 5.6.1 complains when it does not find this routine
sub DESTROY {
}

=item read

Read database from a given file.  Returns 1 on success and otherwise 0.

   $l10n_db->read("foo");

=cut

sub read {
        my $self = shift;
        my $file  = shift;
        my $check  = shift;
        $check = 1 unless defined $check;
    
        if ($file =~ m/\.gz$/) {
                open (DB,"gzip -dc $file |") || return 0;
        } else {
                open (DB,"< $file") || return 0;
        }

        MAIN: while (1) {
                my $entry = {};
                my $desc = '';
                my $last_item = 0;
                my $text;

                while (<DB>) {
                        last if m/^\s*$/;
                        $desc .= $_;
                }
	        if ($desc =~ m/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) { # Parse old format date
		    $self->set_date($_);
		    next MAIN;
		}
                if (!defined($_)) {
                        last unless $desc =~ m/\S/;
                        $last_item = 1;
                }

                # Leading tabs are illegal, but handle them anyway
                $desc =~ s/^\t/ \t/mg;

                foreach (@{$self->{scalar}}) {
                        if ($desc =~ m/^$_: ?(.*)$/m) {
 			        if ($_ eq 'Package' && defined $self->{data}->{$1} && length($1)) {
                                        $entry = $self->{data}->{$1};
                                } elsif ($_ eq 'Package' && length($1) == 0) {
				    foreach (@{$self->{headers}}) {
					if ($desc =~ m/^$_: (.*)$/m) {
					    $self->set_header($_,$1);
					}
				    }
				    next MAIN;
				} else {
                                        $entry->{$_} = $1;
                                }
                        } elsif ($check && $_ ne 'Switch' && $_ ne 'STATUS') {
			        $desc =~ s/^/  /mg;
                                warn "Parse error when reading $file: Package ".(defined($entry->{Package}) ? $entry->{Package} : "<unknown>").": missing \`$_' field\nDescription follows:\n$desc\n";
#                                next MAIN;
                        }
                }
                foreach (@{$self->{array1}}) {
                        if ($desc =~ m/(^|\n)$_:\n(.+?)(\n\S|$)/s) {
                                $text = $2;
                                $text =~ s/^ //mg;
                                my @list = split(/\n\./, $text);
                                $entry->{$_} = \@list;
                        }
                }
                foreach (@{$self->{array2}}) {
                        if ($desc =~ m/(^|\n)$_:\n(.+?)(\n\S|$)/s) {
                                $text = $2;
                                $text =~ s/^ //mg;
                                my @list = ();
                                foreach my $line (split(/\n/, $text)) {
                                        my @list2 = split('!', $line);
                                        push(@list, \@list2);
                                }
                                $entry->{$_} = \@list;
                        }
                }
                $self->{data}->{$entry->{Package}} = $entry;
                last if $last_item;
        }
        close DB;
        return defined($self->{data}->{''}->{Date});
}

=item write

Write database into file.

   $l10n_db->write("foo");

=cut

sub write {
        my $self = shift;
        my $file = shift;
        my ($text, $line);

        my $dir  = $file;
        File::Path::mkpath($dir, 0, 0755)
           if ($dir  =~ s#/+[^/]*$## && !-d $dir);
    
        if ($file =~ m/\.gz$/) {
                open (DB,"| gzip -c > $file")
                        || die "Unable to write to $file: $!\n";
        } else {
                open (DB,"> $file")
                        || die "Unable to write to $file: $!\n";
        }

        $self->set_date(sprintf "%d-%02d-%02d", 
	                        Time::localtime::localtime->year() + 1900, 
	                        Time::localtime::localtime->mon() + 1, 
	                        Time::localtime::localtime->mday);
        print DB "Package:\n";
        foreach (@{$self->{headers}}) {
	    next unless defined($self->{data}->{''}->{$_});
	    print DB $_.": ".$self->{data}->{''}->{$_}."\n";
	}
        print DB "\n";
        foreach my $pkg (sort keys %{$self->{data}}) {
	        next if $pkg eq ''; # skip headers
                foreach (@{$self->{scalar}}) {
                        next unless defined($self->{data}->{$pkg}->{$_});
                        print DB $_.": ".$self->{data}->{$pkg}->{$_}."\n";
                }
                foreach (@{$self->{array1}}) {
                        next unless defined($self->{data}->{$pkg}->{$_});
                        $text = join("\n\.\n", @{$self->{data}->{$pkg}->{$_}})."\n";
                        $text =~ s/\n\n/\n/g;
                        $text =~ s/\n+$//s;
                        $text =~ s/^/ /mg;
                        print DB $_.":\n".$text."\n";
                }
                foreach (@{$self->{array2}}) {
                        next unless defined($self->{data}->{$pkg}->{$_});
                        $text = '';
                        foreach $line (@{$self->{data}->{$pkg}->{$_}}) {
                                $text .= ' '.join('!', @{$line})."\n";
                        }
                        print DB $_.":\n".$text;
                }
                print DB "\n";
        }
        close (DB) || die "Unable to close $file: $!\n";
}

=item list_packages

Returns an array with the list of package names

=cut

sub list_packages {
        my $self = shift;
        return keys %{$self->{data}};
}

=item clear_pkg

Reset info for a given package

   $l10n_db->clear_pkg("foo");

=cut

sub clear_pkg {
        my $self = shift;
        my $pkg  = shift;

        delete $self->{data}->{$pkg};
}

=item set_status

Change the status for the category specified as second argument.

=cut

sub set_status {
    my ($db,$pkg,$type,$file,$date,$status,$translator,$list,$url,$bug_nb) = @_;

    foreach my $line (@{$db->{data}->{$pkg}->{STATUS}}) {
	if (${$line}[0] eq $type
	 && ${$line}[1] eq $file) {
	    ${$line}[2] = $date;
	    ${$line}[3] = $status;
	    ${$line}[4] = $translator;
	    ${$line}[5] = $list;
	    ${$line}[6] = $url;
	    ${$line}[7] = $bug_nb;
	    return
	}
    }
    $db->add_status($pkg,$type,$file,$date,$status,$translator,$list,$url,$bug_nb);
}

=item del_status

Del the package if there was only one status line. 
If a reference to a statusline is provided, it removes the first found
It should remove the right line from the DB, and empty the package if nothing else is left.

=cut

sub del_status {
    my ($db,$pkg,$type,$statusline) = @_;
    if (scalar @{$db->{data}->{$pkg}->{STATUS}} == 1) {
	$db->clear_pkg($pkg);
    } elsif ($statusline) {
	my $ok;
	for (my $i=0; $i < @{$db->{data}->{$pkg}->{STATUS}}; $i++) {
	    my @a = @$statusline;
	    my @b = @{$db->{data}->{$pkg}->{STATUS}->[$i]};
	    $ok = 1;
	    while (scalar @a) {
		next if (shift(@a) eq shift(@b));
		$ok = 0;
		last;
	    }
	    next unless $ok;
	    splice @{$db->{data}->{$pkg}->{STATUS}}, $i, 1;
	    last;
	}
	print "Cannot del_status, statusline not found\n" unless $ok;
    } else {
	print "Ups, sorry, cannot del_status when there is more than one status field in the pkg\n";
    }
#    foreach my $line (@{$db->{data}->{$pkg}->{STATUS}}) {
#	if (${$line}[0] eq $type) {
#	    print "Do not remove $type from $pkg since it's not implemented yet\n";
#        }
#    }
}


=item get_header

Returns the value of the specified header

=cut

sub get_header {
#    print "get $_[1] -> ".($_[0]->{data}->{''}->{$_[1]})."\n";
    return $_[0]->{data}->{''}->{$_[1]};
}

=item set_header

Sets the specified header to the specified value

=cut

sub set_header {
#    print "set $_[1] -> $_[2]\n";
    $_[0]->{data}->{''}->{$_[1]} = $_[2];
}


=item get_date

Returns date of generation

=cut

sub get_date {
    return get_header($_[0],'Date');
}

=item set_date

Sets the date of generation

=cut

sub set_date {
    set_header($_[0],'Date',$_[1]);
}


=back

=head2 DATA MANIPULATION

Data about packages can be classified within scalar values (C<package>,
C<version>, C<section>, C<priority>, C<maintainer>, C<pooldir>, C<type>,
C<upstream>), arrays (C<errors>, C<catgets>, C<gettext>), and arrays of
arrays (C<nls>, C<po>, C<po4a>, C<templates>, C<podebconf>, C<man>, C<menu>
and C<desktop>).
Each field has a method with the same name to get and set it, e.g.

   $section = $l10n_db->section($pkg);
   $l10n_db->section($pkg, "libs");

The first line get the section associated with the package in C<$pkg>,
whereas the second set it to C<libs>.

Two other methods are also defined to access those data, by prefixing
field name by C<has_> and C<add_>.  The former is used to ask whether
this field is defined in database, and the latter appends values for
arrays or arrays of arrays.

   if ($l10n_db->has_templates($pkg)) {
           print "Package $pkg has Debconf templates\n";
   }
   $l10n_db->add_po($pkg, 'po/fr.po', 'fr', '42t0f0u', 'po/adduser_3.42_po_fr.po');

=head1 AUTHOR

Copyright (C) 2001-2004  Denis Barbier <barbier@debian.org>
Copyright (C) 2004       Martin Quinson <enough@spam>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=cut

1;

© 2014-2024 Faster IT GmbH | imprint | privacy policy