summaryrefslogtreecommitdiff
path: root/new-infra/tlsrc2tlp.pl
blob: 7e98fa0d8db0cff260a638938c0c4d8ba3451ff0 (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
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
#!/usr/bin/perl -w
#
# tlsrc2tlp.pl
# generate tlp files from tlsrc files
# (c) 2007 Norbert Preining
#
# This file is licensed under the GNU General Public Licence version 2
# (not later)

use strict;

my %tls;
my %tldb;
my $opt_debug = 0;
my %AllFiles;

my $tlpline;

&main(@ARGV);

1;




sub main {
	my (@tlsrc) = @_;
	foreach my $f (@tlsrc) {
		read_tlsrc_file($f);
	}
	initialize_all_files();
	foreach my $f (keys %tls) {
		generate_tlp_data($f);
	}
	foreach my $f (keys %tldb) {
		write_tlp_file($f);
	}
	foreach my $f (keys %AllFiles) {
		if ($AllFiles{$f} eq "") {
			print STDERR "Not covered file: $f\n";
		}
	}
}

sub read_tlsrc_file {
	my ($tlsrc) = @_;
	open(TMP,"<$tlsrc") || die("Cannot open $tlsrc: $!");
	my @lines = <TMP>;
	close(TMP);
	my $name = "";
	my $shortdesc = "";
	my $longdesc= "";
	my $catalogue = "";
	my (@executes, @depends);
	my (@runpatterns, @docpatterns, @binpatterns, @srcpatterns);
	my $started = 0;
	my $finished = 0;
	my $lastcmd = "";

	logit("Working on $tlsrc\n");
	foreach my $line (@lines) {
		$line =~ /^\s*#/ && next;          # skip comment lines
		if ($line =~ /^\s*$/) {
			if (!$started) { next; }
			if ($finished) { next; }
			die("No empty line allowed in tlsrc files!");
		}
		if ($line =~ /^ /) {
			if ( ($lastcmd eq "longdesc") ||
			     ($lastcmd eq "runpattern")  ||
			     ($lastcmd eq "binpattern")  ||
			     ($lastcmd eq "docpattern")  ||
			     ($lastcmd eq "srcpattern")  ||
			     ($lastcmd eq "execute")  ||
				 ($lastcmd eq "depend") ) {
				$line =~ s/^ /$lastcmd /;
			} else {
				die("Continuation of $lastcmd not allowed, please fix tlsrc!\n");
			}
		}
		if ($line =~ /^name\s*(\w+)$/) {
			logit("found name: $1\n");
			$name = "$1";
			$lastcmd = "name";
			$started && die("Cannot have two name directives!");
			$started = 1;
		} else {
			$started || die("First directive needs to be 'name'");
			if ($line =~ /^shortdesc\s*(.*)$/) {
				logit("found shortdesc: $1\n");
				$shortdesc .= "$1 ";
				$lastcmd = "shortdesc";
				next;
			} elsif ($line =~ /^longdesc\s*(.*)$/) {
				logit("found longdesc: $1\n");
				$longdesc .= "$1 ";
				$lastcmd = "longdesc";
				next;
			} elsif ($line =~ /^catalogue\s*(.*)$/) {
				logit("found catalogue: $1\n");
				$catalogue .= "$1 ";
				$lastcmd = "catalogue";
				next;
			} elsif ($line =~ /^runpattern\s*(.*)$/) {
				logit("found runpattern: $1\n");
				push @runpatterns, "$1";
				$lastcmd = "runpattern";
				next;
			} elsif ($line =~ /^srcpattern\s*(.*)$/) {
				logit("found srcpattern: $1\n");
				push @srcpatterns, "$1";
				$lastcmd = "srcpattern";
				next;
			} elsif ($line =~ /^docpattern\s*(.*)$/) {
				logit("found docpattern: $1\n");
				push @docpatterns, "$1";
				$lastcmd = "docpattern";
				next;
			} elsif ($line =~ /^binpattern\s*(.*)$/) {
				logit("found binpattern: $1\n");
				push @binpatterns, "$1";
				$lastcmd = "binpattern";
				next;
			} elsif ($line =~ /^execute\s*(.*)$/) {
				logit("found execute: $1\n");
				push @executes, "$1";
				$lastcmd = "execute";
				next;
			} elsif ($line =~ /^depend\s*(.*)$/) {
				logit("found depend: $1\n");
				push @depends, "$1";
				$lastcmd = "depend";
				next;
			} else {
				die("Unknown directive ...$line... in $tlsrc, please fix it!");
			}
		}
	}
	logit("Done reading the source file, filling the data structure!\n");
	if ($catalogue ne "") {
		$tls{$name}{'catalogue'} = "$catalogue";
	} else {
		$tls{$name}{'catalogue'} = "$name";
	}
	if ($shortdesc ne "") {
		$tls{$name}{'shortdesc'} = "$shortdesc";
	}
	if ($longdesc ne "") {
		$tls{$name}{'longdesc'} = "$longdesc";
	}
	if ($#srcpatterns >= 0) {
		$tls{$name}{'srcpatterns'} = [ @srcpatterns ];
	}
	if ($#runpatterns >= 0) {
		$tls{$name}{'runpatterns'} = [ @runpatterns ];
	}
	if ($#binpatterns >= 0) {
		$tls{$name}{'binpatterns'} = [ @binpatterns ];
	}
	if ($#docpatterns >= 0) {
		$tls{$name}{'docpatterns'} = [ @docpatterns ];
	}
	if ($#executes >= 0) {
		$tls{$name}{'executes'} = [ @executes ];
	}
	if ($#depends>= 0) {
		$tls{$name}{'depends'} = [ @depends ];
	}
}

sub initialize_all_files {
	foreach my $f (`find texmf texmf-dist texmf-doc -type f`) {
		chomp($f);
		$AllFiles{$f} = "";
	}
}

sub generate_tlp_data {
	my ($tlp) = @_;
	# predefined runpattern if all are missing
	if (!(defined($tls{$tlp}{'runpatterns'}))) {
		my $l = "d texmf-dist/.*/$tlp";
		@{$tls{$tlp}{'runpatterns'}} = ("$l");
	}
	$tldb{$tlp}{'name'} = $tls{$tlp};
	if (defined($tls{$tlp}{'shortdesc'})) {
		$tldb{$tlp}{'shortdesc'} = $tls{$tlp}{'shortdesc'};
	}
	if (defined($tls{$tlp}{'longdesc'})) {
		$tldb{$tlp}{'longdesc'} = $tls{$tlp}{'longdesc'};
	}
	if (defined($tls{$tlp}{'catalogue'})) {
		$tldb{$tlp}{'catalogue'} = $tls{$tlp}{'catalogue'};
	}
	if (defined($tls{$tlp}{'executes'})) {
		$tldb{$tlp}{'executes'} = $tls{$tlp}{'executes'};
	}
	if (defined($tls{$tlp}{'depends'})) {
		$tldb{$tlp}{'depends'} = $tls{$tlp}{'depends'};
	}
	$tldb{$tlp}{'runfiles'} = [ ];
	$tldb{$tlp}{'docfiles'} = [ ];
	$tldb{$tlp}{'binfiles'} = [ ];
	$tldb{$tlp}{'srcfiles'} = [ ];
	FILELABEL: foreach my $f (keys %AllFiles) {
		foreach my $p (@{$tls{$tlp}{'runpatterns'}}) {
			if (pattern_hit($f,$p,"run")) {
				$AllFiles{$f} = 1;
				logit("Hit\n");
				$tldb{$tlp}{'runfiles'} = [ @{$tldb{$tlp}{'runfiles'}}, $f ];
				next FILELABEL;
			}
		}
		foreach my $p (@{$tls{$tlp}{'docpatterns'}}) {
			if (pattern_hit($f,$p,"doc")) {
				$AllFiles{$f} = 1;
				logit("Hit\n");
				$tldb{$tlp}{'docfiles'} = [ @{$tldb{$tlp}{'docfiles'}}, $f ];
				next FILELABEL;
			}
		}
		foreach my $p (@{$tls{$tlp}{'binpatterns'}}) {
			if (pattern_hit($f,$p,"bin")) {
				$AllFiles{$f} = 1;
				logit("Hit\n");
				$tldb{$tlp}{'binfiles'} = [ @{$tldb{$tlp}{'binfiles'}}, $f ];
				next FILELABEL;
			}
		}
		foreach my $p (@{$tls{$tlp}{'srcpatterns'}}) {
			if (pattern_hit($f,$p,"src")) {
				$AllFiles{$f} = 1;
				logit("Hit\n");
				$tldb{$tlp}{'srcfiles'} = [ @{$tldb{$tlp}{'srcfiles'}}, $f ];
				next FILELABEL;
			}
		}
	}
}

sub write_tlp_file {
	my ($tlp) = @_;
	open(TLP,">tlp/$tlp.tlp") || die("Cannot open tlp/$tlp.tlp for writing: $!");
	print TLP "name $tlp\n";
	if (defined($tldb{$tlp}{'shortdesc'})) {
		$tlpline = "shortdesc $tldb{$tlp}{'shortdesc'}";
		write TLP;
	}
	if (defined($tldb{$tlp}{'longdesc'})) {
		$tlpline = "longdesc $tldb{$tlp}{'longdesc'}";
		write TLP;
	}
	if (defined($tldb{$tlp}{'catalogue'})) {
		print TLP "catalogue $tldb{$tlp}{'catalogue'}\n";
	}
	my @tmp;
	if (defined($tldb{$tlp}{'executes'})) {
		foreach (@{$tldb{$tlp}{'executes'}}) {
			print TLP "execute $_\n";
		}
	}
	if (defined($tldb{$tlp}{'depends'})) {
		foreach (@{$tldb{$tlp}{'depends'}}) {
			print TLP "depend $_\n";
		}
	}
	@tmp = @{$tldb{$tlp}{'docfiles'}};
	if ($#tmp >= 0) {
		print TLP "docfiles\n";
		foreach (@tmp) {
			print TLP " $_\n";
		}
	}
	@tmp = @{$tldb{$tlp}{'srcfiles'}};
	if ($#tmp >= 0) {
		print TLP "srcfiles\n";
		foreach (@tmp) {
			print TLP " $_\n";
		}
	}
	@tmp = @{$tldb{$tlp}{'binfiles'}};
	if ($#tmp >= 0) {
		print TLP "binfiles\n";
		foreach (@tmp) {
			print TLP " $_\n";
		}
	}
	@tmp = @{$tldb{$tlp}{'runfiles'}};
	if ($#tmp >= 0) {
		print TLP "runfiles\n";
		foreach (@tmp) {
			print TLP " $_\n";
		}
	}
	close(TLP);
}

sub logit {
	$opt_debug && print STDERR @_;
}

sub pattern_hit {
	my ($file, $pattern, $patterntype) = @_;
	$pattern =~ /^(d|f) (.*)$/;
	my $pattype = $1;
	my $patdata = $2;
	if ($pattype eq "d") {
		my $dirn = `dirname $file`;	# bad, should be rewritten
		chomp($dirn);
		logit("matching >>>$patdata<<< against >>>$dirn<<<\n");
		if ($dirn =~ /^$patdata$/) {
			return 1;
		}
	} elsif ($pattype eq "f") {
		logit("matching >>>$patdata<<< against >>>$file<<<\n");
		if ($file =~ /^$patdata$/) {
			return 1;
		}
	} else {
		die("Unknown pattern specification: $pattern!");
	}
}


###### Formats
format TLP =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$tlpline
 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
$tlpline
.


### Local Variables:
### perl-indent-level: 4
### tab-width: 4
### indent-tabs-mode: t
### End:
# vim:set tabstop=4: #