Debian Codegraph @

debconf-set-selections

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 debconf-set-selections - insert new values into the debconf database
6
7 =head1 SYNOPSIS
8
9 debconf-set-selections file
10 debconf-get-selections | ssh newhost debconf-set-selections
11
12 =head1 DESCRIPTION
13
14 B<debconf-set-selections> can be used to pre-seed the debconf database with
15 answers, or to change answers in the database. Each question will be marked
16 as seen to prevent debconf from asking the question interactively.
17
18 Reads from a file if a filename is given, otherwise from stdin.
19
20 =head1 WARNING
21
22 Only use this command to seed debconf values for packages that will be or
23 are installed. Otherwise you can end up with values in the database for
24 uninstalled packages that will not go away, or with worse problems
25 involving shared values. It is recommended that this only be used to seed
26 the database if the originating machine has an identical install.
27
28 =head1 DATA FORMAT
29
30 The data is a series of lines. Lines beginning with a # character are
31 comments. Blank lines are ignored. All other lines set the value of one
32 question, and should contain four values, each separated by one character
33 of whitespace. The first value is the name of the package that owns the
34 question. The second is the name of the question, the third value is the
35 type of this question, and the fourth value (through the end of the line)
36 is the value to use for the answer of the question.
37
38 Alternatively, the third value can be "seen"; then the preseed line only
39 controls whether the question is marked as seen in debconf's database. Note
40 that preseeding a question's value defaults to marking that question as
41 seen, so to override the default value without marking a question seen, you
42 need two lines.
43
44 Lines can be continued to the next line by ending them with a "\"
45 character.
46
47 =head1 EXAMPLES
48
49 # Force debconf priority to critical.
50 debconf debconf/priority select critical
51
52 # Override default frontend to readline, but allow user to select.
53 debconf debconf/frontend select readline
54 debconf debconf/frontend seen false
55
56 =head1 OPTIONS
57
58 =over 4
59
60 =item B<--verbose>, B<-v>
61
62 verbose output
63
64 =item B<--checkonly>, B<-c>
65
66 only check the input file format, do not save changes to database
67
68 =back
69
70 =head1 SEE ALSO
71
72 L<debconf-get-selections(1)>
73 (available in the debconf-utils package)
74
75 =cut
76
77 use warnings;
78 use strict;
79 use Debconf::Db;
80 use Debconf::Template;
81 use Getopt::Long;
82
83 use vars qw(%opts $filename $debug $error $checkonly $unseen);
84
85 sub usage {
86 print STDERR <<EOF;
87 Usage: debconf-set-selections [-vcu] [file]
88 -v, --verbose verbose output
89 -c, --checkonly only check the input file format
90 -u, --unseen do not set the 'seen' flag when preseeding values
91 EOF
92 exit(1);
93 }
94
95 sub info {
96 my $msg = shift;
97 print STDERR "info: $msg\n" if $debug;
98 }
99
100 sub warning {
101 my $msg = shift;
102 print STDERR "warning: $msg\n";
103 }
104
105 sub error {
106 my $msg = shift;
107 print STDERR "error: $msg\n";
108 $error++
109 }
110
111 sub load_answer {
112 my ($owner, $label, $type, $content) = @_;
113
114 info "Loading answer for '$label'";
115
116 # Set up the template.
117 my $template=Debconf::Template->get($label);
118 if (! $template) {
119 $template=Debconf::Template->new($label, $owner, $type);
120 $template->description("Dummy template");
121 $template->extended_description("This is a fake template used to pre-seed the debconf database. If you are seeing this, something is probably wrong.");
122 }
123 $template->type($type);
124
125 # The question should already exist, it was created along with the
126 # template. Set it up.
127 my $question=Debconf::Question->get($label);
128 if (! $question) {
129 error("Cannot find a question for $label");
130 return;
131 }
132 $question->addowner($owner, $type);
133 $question->value($content);
134 if (! $unseen) {
135 $question->flag("seen", "true");
136 }
137 }
138
139 sub set_flag {
140 my ($owner, $label, $flag, $content) = @_;
141
142 info "Setting $flag flag";
143
144 my $question=Debconf::Question->get($label);
145 if (! $question) {
146 error("Cannot find a question for $label");
147 return;
148 }
149 $question->flag($flag, $content);
150 }
151
152 my @knowntypes = qw(select boolean string multiselect note password text title);
153 my @knownflags = qw(seen);
154
155 sub ok_format {
156 my ($owner, $label, $type, $content) = @_;
157 if (! defined $owner || ! defined $label || ! defined $content) {
158 error "parse error on line $.: '$_'";
159 return;
160 }
161 elsif (! grep { $_ eq $type } @knowntypes, @knownflags) {
162 warning "Unknown type $type, skipping line $.";
163 return;
164 }
165 else {
166 return 1;
167 }
168 }
169
170 sub mungeline ($) {
171 my $line=shift;
172 chomp $line;
173 $line=~s/\r$//;
174 return $line;
175 }
176
177
178 GetOptions(
179 "verbose|v" => \$debug,
180 "checkonly|c" => \$checkonly,
181 "unseen|u" => \$unseen,
182 ) || usage();
183
184 {
185 local $ENV{DEBCONF_NOWARNINGS} = 'yes' if $checkonly;
186 Debconf::Db->load;
187 }
188
189 $error = 0;
190
191 unshift @ARGV, '-' unless @ARGV;
192 foreach my $filename (@ARGV) {
193 my $input;
194 if ($filename eq '-') {
195 $input = \*STDIN;
196 } elsif (not open $input, '<', $filename) {
197 warn $!;
198 next;
199 }
200 while (<$input>) {
201 $_=mungeline($_);
202 while (/\\$/ && ! eof) {
203 s/\\$//;
204 $_.=mungeline(<$input>);
205 }
206 next if /^\s*$/ || /^\s*\#/;
207 # Allow multiple spaces between all values except the last one.
208 # Extra whitespace in the content is significant.
209 my ($owner, $label, $type, $content) = /^\s*(\S+)\s+(\S+)\s+(\S+)(?:\s(.*))?/;
210 if (! defined $content) {
211 $content='';
212 }
213 if (ok_format($owner, $label, $type, $content)) {
214 if (grep { $_ eq $type } @knownflags) {
215 info "Trying to set '$type' flag to '$content'";
216 set_flag($owner, $label, $type, $content);
217 }
218 else {
219 info "Trying to set '$label' [$type] to '$content'";
220 load_answer($owner, $label, $type, $content);
221 }
222 }
223 }
224 }
225
226 if (! $checkonly) {
227 Debconf::Db->save;
228 }
229
230 if ($error) {
231 exit 1;
232 }
233
234 =head1 AUTHOR
235
236 Petter Reinholdtsen <[email protected]>
237
238 =cut