Debian Codegraph @

debconf-mergetemplate

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 debconf-mergetemplate - merge together multiple debconf template files
6
7 =cut
8
9 use warnings;
10 use strict;
11 use Debconf::Template::Transient;
12 use Debconf::Config;
13 use Debconf::Gettext;
14
15 print STDERR gettext("debconf-mergetemplate: This utility is deprecated. You should switch to using po-debconf's po2debconf program.")."\n";
16
17 =head1 SYNOPSIS
18
19 debconf-mergetemplate [options] [templates.ll ...] templates
20
21 =head1 DESCRIPTION
22
23 Note: This utility is deprecated. You should switch to using po-debconf's
24 po2debconf program.
25
26 This program is useful if you have multiple debconf templates files which
27 you want to merge together into one big file. All the specified files will
28 be read in, merged, and output to standard output.
29
30 This can be especially useful if you are dealing with translated template
31 files. In this case, you might have your main template file, plus several
32 other files provided by the translators. These files will have translated
33 fields in them, and maybe the translators left in the english versions of
34 the fields they translated, for their reference.
35
36 So, you want to merge together all the translated templates files
37 with your main templates file. Any fields that are unique to the translated
38 files need to be added in to the correct templates, but any fields they have
39 in common should be superseded by the fields in the main file (which might be
40 more up-to-date).
41
42 This program handles that case properly, just list each of the translated
43 templates files, and then your main templates file last.
44
45 =head1 OPTIONS
46
47 =over 4
48
49 =item --outdated
50
51 Merge in even outdated translations. The default is to drop them with a
52 warning message.
53
54 =item --drop-old-templates
55
56 If a translation has an entire template that is not in the master file (and
57 thus is probably an old template), drop that entire template.
58
59 =back
60
61 =head1 SEE ALSO
62
63 L<debconf-getlang(1)>
64
65 =cut
66
67 my $usage=gettext("Usage: debconf-mergetemplate [options] [templates.ll ...] templates");
68
69 my $outdated=0;
70 my $dropold=0;
71 Debconf::Config->getopt($usage.
72 gettext(qq{
73 --outdated Merge in even outdated translations.
74 --drop-old-templates Drop entire outdated templates.}),
75 "outdated" => \$outdated,
76 "drop-old-templates" => \$dropold,
77 );
78
79 if (! @ARGV) {
80 die $usage."\n";
81 }
82
83 # Ignore the user's locale settings.
84 Debconf::Template::Transient->i18n(0);
85
86 sub is_fuzzy {
87 my $a=defuzz(shift);
88 my $b=shift;
89 }
90
91 sub defuzz {
92 my $value=shift;
93 # Ignore leading/trailing whitespace,
94 # and collapse other whitespace.
95 $value=~s/^\s+//gm;
96 $value=~s/\s+$//gm;
97 $value=~tr/ \t\n/ /s;
98 return $value;
99 }
100
101 my %templates = map { $_->template => $_ } Debconf::Template::Transient->load(pop @ARGV);
102
103 foreach my $template (map { Debconf::Template::Transient->load($_) } @ARGV) {
104 if (exists $templates{$template->template}) {
105 my $master=$templates{$template->template};
106 foreach my $field (grep { /.+-.+/ } $template->fields) {
107 next if $field =~ /^extended_/;
108 if ($field =~ /(.+?)-(.+)/) {
109 my $basefield = $1;
110 my $lang = $2;
111
112 # Get the english versions, including
113 # extended field if any.
114 my $t_val = $template->$basefield;
115 my $m_val = $master->$basefield;
116
117 if (! defined $t_val) {
118 if ($outdated) {
119 warn "debconf-mergetemplate: ".$template->template." ".
120 sprintf(gettext("%s is missing"), $basefield)."\n";
121 }
122 else {
123 warn "debconf-mergetemplate: ".$template->template." ".
124 sprintf(gettext("%s is missing; dropping %s"), $basefield, $field)."\n";
125 next;
126 }
127 }
128
129 my $extbasefield = "extended_$basefield";
130 $t_val .= "\n".$template->$extbasefield
131 if defined $template->$extbasefield;
132 $m_val .= "\n".$master->$extbasefield
133 if defined $master->$extbasefield;
134
135 my ($df_t, $df_m) = (defuzz($t_val), defuzz($m_val));
136 if ($df_t ne $df_m) {
137 my $diff_p = 0;
138
139 $diff_p++
140 while substr($t_val, 0, $diff_p) eq substr($m_val, 0, $diff_p);
141 my $diff_t = "'".
142 ($diff_p ? '...' : '').defuzz(substr($t_val, $diff_p-1, 8))."' != '".
143 ($diff_p ? '...' : '').defuzz(substr($m_val, $diff_p-1, 8))."'";
144
145 if ($outdated) {
146 warn "debconf-mergetemplate: ".$template->template." ".
147 sprintf(gettext("%s is fuzzy at byte %s: %s"),
148 $field, $diff_p, $diff_t)."\n";
149 }
150 else {
151 warn "debconf-mergetemplate: ".$template->template." ".
152 sprintf(gettext("%s is fuzzy at byte %s: %s; dropping it"),
153 $field, $diff_p, $diff_t)."\n";
154 next;
155 }
156 }
157 }
158
159 $master->$field($template->$field);
160 # If the other template has no extended part of the field,
161 # coply in nothing.
162 $field="extended_$field";
163 $master->$field($template->$field);
164 }
165 }
166 else {
167 if (! $dropold) {
168 warn "debconf-mergetemplate: ".
169 sprintf(gettext("%s is outdated"), $template->template)."\n";
170 $templates{$template->template}=$template;
171 }
172 else {
173 warn "debconf-mergetemplate: ".
174 sprintf(gettext("%s is outdated; dropping whole template!"), $template->template)."\n";
175 }
176 }
177 }
178
179 print Debconf::Template::Transient->stringify(values %templates);
180
181 =head1 AUTHOR
182
183 Joey Hess <[email protected]>
184
185 =cut