summaryrefslogtreecommitdiff
path: root/extract_template.pl
blob: c11137fce126a70857b9f63ce71b6d7d75df9cb9 (plain)
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
#!/usr/bin/perl
#
# extract_template.pl
#
# Create a list of all the debconf template settings contained in a
# given Debian release
#
# Copright 2020 Steve McIntyre <steve@einval.com>
#
# GPLv2+

use strict;
use warnings;
use File::Basename;
use File::stat;
use Getopt::Long;
Getopt::Long::Configure ('no_ignore_case');
Getopt::Long::Configure ('no_auto_abbrev');

my $mirror = "/srv/mirror/debian";
my $suite = "buster";
my $component = "main";
my $arch = "amd64";
my $outfile = "";
my $verbose = 0;
my $verbose_number = 1000;
my $out_fh;
my $result = GetOptions("m|mirror=s"    => \$mirror,
			"suite|s=s"     => \$suite,
			"c|component=s" => \$component,
			"a|arch=s"      => \$arch,
			"o|out=s"       => \$outfile,
			"v|verbose+"    => \$verbose
    ) or die ("Error parsing command line");
my $deb;

if ($outfile eq "") {
    $out_fh = *STDOUT;
}

sub dump_entry {
    my $out_fh = shift;
    my $base = shift;
    my $desc = shift;
    my $tmp = shift;
    my @long_desc = @$tmp;
    my $tmpl_name = shift;
    my $type = shift;
    my $default = shift;
    my $choices = shift;

    if (!defined($default)) {
	if (defined($type)) {
	    if ($type eq "string") {
		$default = "<string>";
	    } elsif ($type eq "select") {
		$default = "<choice>";
	    } elsif ($type eq "multiselect") {
		$default = "<choice(s)>";
	    } elsif ($type eq "password") {
		$default = "<password>";
	    } else {
		$default = "<choice>";
	    }
	} else {
	    print $out_fh "# Unknown type for $base $desc, $default\n";
	    print $out_fh "# Maybe broken template? Guessing...\n";
	    $type = "<choice>";
	    $default = "<choice>";
	}
    }
    if (!defined($desc)) {
	print $out_fh "$base: unknown desc\n";
    }
    if ($type ne "text" and
	$type ne "error" and
	$type ne "title" and
	$type ne "note") {
	print $out_fh "\n### $desc\n";
	foreach my $dl (@long_desc) {
	    print $out_fh "#   $dl\n";
	}
	if (!defined($tmpl_name)) {
	    print $out_fh "# Unknown tmpl_name for $base $desc, $type, $default\n";
	}
	print $out_fh "# d-i $tmpl_name $type $default\n";
	if (defined($choices)) {
	    print $out_fh "# Possible choices: $choices\n";
	}
    }
}

sub process_deb {
    my $out_fh = shift;
    my $deb = shift;
    my $template;
    $template = `dpkg --ctrl-tarfile $deb | tar tf - | grep templates`;
    chomp $template;
    if ($template) {
	my $base = basename($deb);
	print $out_fh "\n############################\n";
	print $out_fh "#### $base\n";
	print $out_fh "############################\n";

	open (TMPL, "dpkg --ctrl-tarfile $deb | tar xf - $template -O |")
	    or die "Failed to extra template from $deb: $!\n";

	my $in_template = 0;
	my $in_descr = 0;
	my $tmpl_name;
	my $type;
	my $default;
	my $desc;
	my $choices;
	my @long_desc;

	while (defined (my $line = <TMPL>)) {
	    chomp $line;
	    if ($line =~ /^Template:\s*(.*)/i) {
		$in_template = 1;
		$tmpl_name = $1;
		$in_descr = 0;
		next;
	    }
	    if ($line =~ /^Type:\s*(.*)/i) {
		$type = $1;
		$in_descr = 0;
		next;
	    }
	    if ($line =~ /^Default:\s*(.*)/i) {
		$default = $1;
		$in_descr = 0;
		next;
	    }
	    if ($line =~ /^Choices:\s*(.*)/i) {
		$choices = $1;
		$in_descr = 0;
		next;
	    }
	    if ($line =~ /^(Description:\s*.*)/i) {
		$in_descr = 1;
		$desc = $1;
		next;
	    }
	    if ($line =~ /^ (.*)/i && $in_descr) {
		push(@long_desc, $1);
		next;
	    }
	    if ($line =~ /^Description-.*/i) {
		$in_descr = 0;
		next;
	    }

	    if ($line =~ /^$/ and $in_template) {
		dump_entry($out_fh, $base, $desc, \@long_desc, $tmpl_name, $type, $default, $choices);
		undef $desc;
		undef @long_desc;
		undef $tmpl_name;
		undef $type;
		undef $default;
		undef $choices;
		$in_template = 0;
	    }
	}
	close TMPL;
	if ($in_template and defined($desc)) {
	    dump_entry($out_fh, $base, $desc, \@long_desc, $tmpl_name, $type, $default, $choices);
	    $in_template = 0;
	}
    }
}

my $num_debs = 0;

if (scalar (@ARGV)) {
    # Ignore the other bits, just use the specified debs
    while ($deb = shift) {
#    print "Processing $deb\n";
#    print "Found control tarball as $control\n";
	process_deb(*STDOUT, $deb);
	$num_debs++;
	if ($verbose && $num_debs % $verbose_number == 0) {
	    print STDERR "Processed $num_debs packages OK\n";
	}
    }
} else {
    my @inputs = ("$mirror/dists/$suite/$component/debian-installer/binary-$arch/Packages.gz",
		  "$mirror/dists/$suite/$component/binary-$arch/Packages.gz");
    my $should_run = 0;

    # If we have an output file, ONLY run if one of the input files is
    # newer
    if ($outfile eq "" || ! -f $outfile) {
	$should_run = 1;
    } else {
	my $sb_out = stat($outfile);
	foreach my $file (@inputs) {
	    if (-f $file) {
		my $sb_in = stat($file);
		if ($sb_in->mtime > $sb_out->mtime)
		{
		    $should_run = 1;
		}
	    }
	}
    }

    if ($should_run) {
	if ($outfile ne "") {
	    open($out_fh, ">", "$outfile.new")
		or die "Can't open $outfile.new for writing: $!\n";
	}
	foreach my $file (@inputs) {
	    if ($verbose) {
		print STDERR "Checking for debs in $file\n";
	    }
	    if (-f $file) {
		open(PACKAGES, "zcat -f $file |")
		    or die "Can't read Packages file $file: $!\n";
		while (defined (my $line = <PACKAGES>)) {
		    chomp $line;
		    if ($line =~ m/^Filename: (.*)/) {
			process_deb($out_fh, "/$mirror/$1");
			$num_debs++;
			if ($verbose >= 2) {
			    print STDERR "Processed $1\n";
			} elsif ($verbose && $num_debs % $verbose_number == 0) {
			    print STDERR "Processed $num_debs packages OK\n";
			}
		    }
		}
		close PACKAGES;
		if ($verbose) {
		    print STDERR "Processed $num_debs packages OK\n";
		}
	    }
	}
	if ($outfile ne "") {
	    close $out_fh;
	    rename "$outfile.new", "$outfile";
	}
    } else {
	if ($verbose) {
	    print STDERR "Output file $outfile is newer than input files, not running\n";
	}
    }
}