Debian Codegraph @

podebconf-display-po

raw
1 #!/usr/bin/perl -w
2
3 use strict;
4 use File::Temp;
5 use Getopt::Long;
6
7 sub usage {
8 my $rc = shift;
9 print "Usage: podebconf-display-po [-h] [-f frontend] file.po\n";
10 exit($rc);
11 }
12
13 my $help = 0;
14 my $frontend = '';
15 Getopt::Long::Configure('bundling');
16 Getopt::Long::GetOptions(
17 "h|help" => \$help,
18 "f|frontend=s" => \$frontend,
19 ) || usage(1);
20 usage(0) if $help;
21 usage(1) if $#ARGV != 0;
22
23 sub unescapechar {
24 my $char = shift;
25 if ($char eq 'n') {
26 # Add a space to conform to debconf templates format
27 return "\n ";
28 } elsif ($char eq 't') {
29 return "\t";
30 } elsif ($char eq 'r') {
31 return "\r";
32 } else {
33 # Fallback also works for \\ and \"
34 return $char;
35 }
36 }
37 sub unescapestr {
38 my $text = shift;
39 my $out = '';
40 my $last = 0;
41 while ($text =~ m/\G([^\\]*)\\([ntr"\\])/g) {
42 $last = pos($text);
43 $out .= $1 . unescapechar($2);
44 }
45 $out .= substr($text, $last);
46 return $out;
47 }
48 # Format 1: No "Type:" in comments
49 # Format 2: Only one reference per string and file, line numbers
50 # match the ones in templates files
51 # Format 3: All references are written into PO files; first template
52 # is numbered from 1001, 2nd from 2001 etc.
53 sub guessFormat {
54 my $text = shift;
55 return 1 unless $text =~ m/^#\. Type:/;
56 while ($text =~ s/#:\s*([^:\s]+):(\d+)/#:/) {
57 my ($file,$line) = ($1, $2);
58 return 2 if $line < 1000;
59 }
60 return 3;
61 }
62
63 # 1. Read the PO file and write a fake templates file
64
65 my $fields = {};
66 my $rfc822Format = 0;
67
68 # Recode PO files into UTF-8 to avoid stateful encodings
69 {
70 open(PO, "msgconv -t UTF-8 $ARGV[0] |")
71 or die "'msgconv -t UTF-8 $ARGV[0]' failed: $!\n";
72 local $/ = "\n\n";
73 while (<PO>) {
74 next unless m/^msgid/m;
75 s/"\n"//g;
76 # Skip header entry
77 next if m/^msgid ""$/m;
78
79 my $type = '';
80 my $field = '';
81 m/^msgid "(.*)"/m;
82 my $msgid = unescapestr($1);
83 m/^msgstr "(.*)"/m;
84 my $msgstr = unescapestr($1);
85 # Escape dollar signs to prevent variable substitutions
86 $msgid =~ s/\$/\${dollarsign}/g;
87 $msgstr =~ s/\$/\${dollarsign}/g;
88 $rfc822Format = guessFormat($_) if $rfc822Format == 0;
89 while (s/#:\s*([^:\s]+):(\d+)/#:/) {
90 my ($file,$line) = ($1, $2);
91 my $tmp = $_;
92 if ($rfc822Format == 1) {
93 # Old format
94 1 while ($tmp =~ s/^#\.\s+(.*)//m);
95 $field = $1;
96 } else {
97 # New format
98 if ($tmp =~ s/^#\. Type:\s*(\S+)\n//) {
99 $type = lc($1);
100 }
101 # The entropy-text type is a plugin
102 $type = "text" if $type eq "entropy-text";
103 if ($tmp =~ s/^#\. (\S+)\n//) {
104 $field = $1;
105 }
106 }
107 $field = ucfirst($field);
108 $fields->{$file} = {} unless defined($fields->{$file});
109 if (defined $fields->{$file}->{$line}) {
110 if ($field eq 'Choices') {
111 if ($msgstr =~ m/,/) {
112 warn "The translation of Choices ".
113 "should not contain commas. ".
114 "Please fix the translation ".
115 "of '$msgid'.";
116 }
117 $fields->{$file}->{$line}->{msgid} .= ", ".$msgid;
118 $fields->{$file}->{$line}->{msgstr} .= ", ".$msgstr;
119 } else {
120 $fields->{$file}->{$line}->{msgid} .= "\n ".($fields->{$file}->{$line}->{msgid} =~ m/\n/ ? ".\n " : '').$msgid;
121 $fields->{$file}->{$line}->{msgstr} .= "\n ".($fields->{$file}->{$line}->{msgstr} =~ m/\n/ ? ".\n " : '').$msgstr;
122 }
123 } else {
124 $fields->{$file}->{$line} = {
125 type => $type,
126 field => $field,
127 msgid => $msgid,
128 msgstr => $msgstr,
129 line => $line,
130 };
131 }
132 }
133 }
134 close(PO);
135 }
136
137 # Translated fields in the generated templates file will be written
138 # with an extension which is suitable for current user's environment.
139 # Of course, there will be trouble if there is some encoding mismatch.
140 my $lang = $ENV{LANGUAGE} || $ENV{LC_ALL} || $ENV{LC_MESSAGES} || $ENV{LANG} || $ARGV[0];
141 # For LANGUAGE
142 $lang =~ s/:.*//;
143 # For filename
144 $lang =~ s{.*/}{};
145 $lang =~ s/\.po$//;
146 # For locale variables. Charset and modifiers have to be stripped.
147 $lang =~ s/[.@].*//;
148
149 my $count = 0;
150 my $new = 1;
151 my $choices = 0;
152 my @tempfiles = ();
153 my ($fh, $template) = File::Temp::tempfile()
154 or die "Unable to write temporary files";
155 push (@tempfiles, $template);
156 $SIG{INT} = sub { unlink (@tempfiles); exit(1); };
157
158 my $titles = {};
159
160 # TODO: Refactor this code
161 if ($rfc822Format < 3) {
162 for my $file (keys %$fields) {
163 for (sort { $a <=> $b } keys %{$fields->{$file}}) {
164 $titles->{$count} = 1 if ($fields->{$file}->{$_}->{type} =~ m/title/);
165 print $fh "Template: foo/bar$count\n" if $new;
166 if ($fields->{$file}->{$_}->{field} eq 'Description') {
167 print $fh "Type: ".
168 (length($fields->{$file}->{$_}->{type}) ?
169 $fields->{$file}->{$_}->{type} :
170 ($choices ? "select" : "string"))."\n";
171 } elsif ($fields->{$file}->{$_}->{field} eq 'Choices') {
172 $choices = 1;
173 } elsif ($fields->{$file}->{$_}->{field} eq 'DefaultChoice') {
174 $fields->{$file}->{$_}->{field} = 'Default';
175 }
176 if ($fields->{$file}->{$_}->{field} eq 'Description' && $choices == 0 && $fields->{$file}->{$_}->{type} =~ m/select/)
177 {
178 # Dummy Choices field, needed to display the
179 # question.
180 print $fh "Choices: dummy1, dummy2\n";
181 }
182 print $fh "$fields->{$file}->{$_}->{field}: $fields->{$file}->{$_}->{msgid}\n";
183 print $fh "$fields->{$file}->{$_}->{field}-$lang.UTF-8: $fields->{$file}->{$_}->{msgstr}\n";
184 $new = 0;
185 if ($fields->{$file}->{$_}->{field} eq 'Description') {
186 $count++;
187 $new = 1;
188 $choices = 0;
189 print $fh "\n";
190 }
191 }
192 }
193 }
194 else
195 {
196 my $text = "";
197 my %last = ();
198 for my $file (keys %$fields) {
199 my $oldTemplateNr = 0;
200 for (sort { $a <=> $b } keys %{$fields->{$file}}) {
201 $titles->{$count} = 1 if ($fields->{$file}->{$_}->{type} =~ m/title/);
202 my $newTemplateNr = sprintf("%d", $_ / 1000);
203 if ($newTemplateNr != $oldTemplateNr) {
204 if ($count > 0) {
205 printf $fh "\nTemplate: foo/bar%d\n", $count - 1;
206 print $fh "Type: ".
207 (length($last{type}) ? $last{type} :
208 ($choices ? "select" : "string"))."\n";
209 if ($choices == 0 && $last{type} =~ m/select/)
210 {
211 # Dummy Choices field, needed to display the
212 # question.
213 print $fh "Choices: dummy1, dummy2\n";
214 }
215 print $fh "$text\n";
216 }
217 $count++;
218 $choices = 0;
219 $text = "";
220 $oldTemplateNr = $newTemplateNr;
221 %last = %{$fields->{$file}->{$_}};
222 }
223 if ($fields->{$file}->{$_}->{field} eq 'Choices') {
224 $choices = 1;
225 } elsif ($fields->{$file}->{$_}->{field} eq 'DefaultChoice') {
226 $fields->{$file}->{$_}->{field} = 'Default';
227 }
228 # debconf is kind enough to not display a Select question if there
229 # is only one choice, so add a dummy item
230 if ($fields->{$file}->{$_}->{field} eq 'Choices' && $fields->{$file}->{$_}->{type} eq 'select' && $fields->{$file}->{$_}->{msgid} !~ /,/) {
231 $fields->{$file}->{$_}->{msgid} .= ", dummy2";
232 $fields->{$file}->{$_}->{msgstr} .= ", dummy2";
233 }
234 $text .= "$fields->{$file}->{$_}->{field}: $fields->{$file}->{$_}->{msgid}\n";
235 $text .= "$fields->{$file}->{$_}->{field}-$lang.UTF-8: $fields->{$file}->{$_}->{msgstr}\n";
236 }
237 }
238 if ($text ne "") {
239 printf $fh "Template: foo/bar%d\n", $count - 1;
240 print $fh "Type: ".
241 (length($last{type}) ? $last{type} :
242 ($choices ? "select" : "string"))."\n";
243 if ($choices == 0 && $last{type} =~ m/select/)
244 {
245 # Dummy Choices field, needed to display the
246 # question.
247 print $fh "Choices: dummy1, dummy2\n";
248 }
249 print $fh "$text";
250 }
251 }
252 close($fh);
253
254 # 2. Load the templates file in debconf DB
255
256 use Debconf::Db;
257 use Debconf::AutoSelect qw(:all);
258 use Debconf::Config;
259
260 my ($dbth, $dbt) = File::Temp::tempfile()
261 or die "Unable to write temporary files";
262 my ($dbch, $dbc) = File::Temp::tempfile()
263 or die "Unable to write temporary files";
264 push (@tempfiles, $dbt, $dbc);
265
266 my %hashconfig = (
267 driver => "File",
268 mode => '0600',
269 name => "config",
270 backup => 0,
271 filename => $dbc
272 );
273 my %hashtemplates = (
274 driver => "File",
275 mode => '0600',
276 name => "templates",
277 backup => 0,
278 filename => $dbt
279 );
280
281 $Debconf::Db::config=Debconf::Db->makedriver(%hashconfig);
282 $Debconf::Db::templates=Debconf::Db->makedriver(%hashtemplates);
283 Debconf::Template->load($template, 'display');
284 Debconf::Db->save;
285 unlink $template;
286
287 # 3. Display questions
288
289 $Debconf::Db::config=Debconf::Db->makedriver(%hashconfig);
290 $Debconf::Db::templates=Debconf::Db->makedriver(%hashtemplates);
291 Debconf::Config->frontend($frontend) if length($frontend);
292 my $dc_frontend=make_frontend();
293 my $dc_confmodule=make_confmodule();
294 my $code=127;
295
296 my $cnt = 0;
297 while (1) {
298 $_="RESET foo/bar$cnt\n";
299 my $ret=$dc_confmodule->process_command($_);
300 if ($titles->{$cnt}) {
301 $_="SETTITLE foo/bar$cnt\n";
302 $ret=$dc_confmodule->process_command($_);
303 $cnt++;
304 next;
305 }
306 $_="SUBST foo/bar$cnt dollarsign \$\n";
307 $ret=$dc_confmodule->process_command($_);
308 $_="INPUT high foo/bar$cnt\n";
309 $ret=$dc_confmodule->process_command($_);
310 ($code, undef)=split(/ /, $ret, 2);
311 last if $code ne 0 && $code ne 30;
312 $_="GO\n";
313 $ret=$dc_confmodule->process_command($_);
314 ($code, undef)=split(/ /, $ret, 2);
315 $cnt++;
316 }
317
318 $dc_frontend->shutdown;
319 $dc_confmodule->finish;
320 Debconf::Db->save;
321 unlink $dbt, $dbc;
322
323 1;