summaryrefslogtreecommitdiff
path: root/specparser.pl
blob: f4d7d05408fa97c806315eb8817ba47c7421b9a6 (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
#!/usr/bin/perl
# $Id$
#
# USAGE: specparser.pl file.spec
#
# Output:
#   stdout:
#     list of:
#       <md5><space><url>\n
#     for given specfile.
#     except for lines prefixed with: ERROR:
#   exit status:
#     0 - normal
#     2 - cannot open spec file
#
use strict;
use warnings;

my %no_source;
my $spec;
my $base_spec;
my @spec;
my @sources;

sub next_spec($)
{
	$spec = shift;
	@spec = ();
	$base_spec = $spec;
	$base_spec =~ s|.*/||;
}

sub error($)
{
	print_once( "ERROR: $base_spec: $_[0]" );
}

sub warning($)
{
	print "ERROR: $base_spec: $_[0]\n";
}

sub trim_spaces($)
{
	my $v = shift;
	
	$v =~ s/\s+$//;
	$v =~ s/^\s+//;
	
	return $v;
}

# expand macros in string
sub expand($$) # {{{
{
	my $v = trim_spaces(shift);
	my $macrotree = shift;
	my $cnt = 20;

	while ($v =~ /\%\{([^\}]+)\}/) {
		my $value;
		if (defined $macrotree->{$1}) {
			$value = $macrotree->{$1};
		} else {
			error("undefined macro $1");
			$value = "UNDEFINED";
			return undef;
		}
		$v =~ s/\%\{([^\}]+)\}/$value/;

		return $v if (length $v > 1000 or $cnt-- <= 0)
	}

	while ($v =~ s/\%\(\s*echo\s+([^\|]+?)\s*\|\s*tr\s*(-d|)\s+([^\)]+?)\s*\)/\@\@tr-me\@\@/) {
		my ($what, $d_opt, $how) = ($1, $2, $3);
		my ($from, $to) = ($how, "");
		($from, $to) = ($1, $2) 
			if $how =~ /^([^\s]+)\s+([^\s]+)$/;
		if ($d_opt and $to ne "") {
			error("tr -d with second string)");
		} elsif (($from . $to) =~ /^[a-zA-Z0-9\+_\-\.]+$/) {
			if ($d_opt) {
				eval "\$what =~ tr/$from//d;";
			} else {
				eval "\$what =~ tr/$from/$to/;";
			}
		} else {
			error("illegal characters in tr string(s) '$from' '$to'");
		}
		$v =~ s/\@\@tr-me\@\@/$what/;
		
		return $v if (length $v > 1000 or $cnt-- <= 0)
	}

	error("unexpanded macros in $v")
		if ($v =~ /\%[^0-9]/);
	
	return $v;
} # }}}

sub preparse_spec($) # {{{
{
	@spec = ("");

	open(F, "< $_[0]") or die;
	while (<F>) {
		chomp;
		if (/^\s*(\%(description|package|prep|install|pre|post|files)|BuildRoot|URL)/) {
			last;
		} elsif (/^\s*(\%if.*|\%else|\%endif|\%define\s+.*|Version.*|Name.*)\s*$/) {
			$_ = $1;
			if ($spec[$#spec] =~ /\%if/) {
				if (/\%else/) {
					next; # don't include empty %if-%else
				} elsif (/\%endif/) {
					# remove empty %if-%endif
					pop @spec;
					next;
				}
			}
			push @spec, $_;
		} elsif (/^NoSource\s*:\s*(\d+)\s*$/i) {
			$no_source{$1} = 1;
		}
	}
	close(F);

	shift @spec;
} # }}}


my $total = 0;

sub cont($$);
sub cont($$) # {{{
{
	my ($spec, $macros) = @_;
	local $_;
	while ($_ = shift @{$spec}) {
		if (0 <= index $_, '%if') { # if, ifarch, ifos

			# split spec parsing
			my @speccopy = @{$spec};
			my %macroscopy = %{$macros};
			cont(\@speccopy, \%macroscopy);

			my $level = 0;
			while ($_ = shift @{$spec}) {
				if ($level <= 0 and ($_ eq '%else' or $_ eq '%endif')) {
					last;
				} elsif (0 <= index $_, '%if') {
					$level++;
				} elsif ($_ eq '%endif') {
					$level--;
				}
			}

			# continue parsing
			
		} elsif ($_ eq '%else') {

			# %else happens only when %if was interpreted
			# so skip until %endif
			
			my $level = 0;
			while ($_ = shift @{$spec}) {
				if ($level <= 0 and $_ eq '%endif') {
					last;
				} elsif (0 <= index $_, '%if') {
					$level++;
				} elsif ($_ eq '%endif') {
					$level--;
				}
			}

		} elsif (/^\s*\%(define|global)\s+([^\s]+)\s+([^\s].*?)\s*$/) {
			$macros->{$2} = $3;

		} elsif (/^Version\s*:\s*(.*?)\s*$/i) {
			$macros->{"version"} = $1;
		} elsif (/^Name\s*:\s*(.*?)\s*$/i) {
			$macros->{"name"} = $1;
		} elsif (!/\%endif/) {
			warn "unrecognised line: $_\n";
		}
	}

	# the end, yuppie !
	foreach my $s (@sources) {
		my $src = expand( $s->[2], $macros );
		if (defined $src) {
			our %tried;
			unless (exists $tried{$src}) {
				print_source( $s->[0], $s->[1], $src );
				$tried{$src} = 1;
			}
		}
	}
	
	if (++$total > 10000) {
		error("maximum number of bcond posibilities exceeded");
		exit 0;
	}
} # }}}

sub print_once($)
{
	our %printed;
	my $l = shift;
	unless (exists $printed{$l}) {
		print $l . "\n";
		$printed{$l} = 1;
	}
}

sub print_source($$$) # {{{
{
	my ($no, $md5, $s) = @_;

	if ($s =~ /^([a-z0-9A-Z:\=\?\@\+\~\.,\-\/_]|\%[0-9])+$/) {
		if ($s =~ /^(ftp|http|https):\/\//) {
			if ($s =~ /\/$/) {
				error("source $no ($s) is directory");
			} else {
				if ($s =~ /:\/\/distfiles\.pld-linux\.org\/src/) {
					$s =~ s|.*/||;
					print_once( "$md5 no-url-copy://$s" );
				} else {
					print_once( "$md5 $s" );
				}
			}
		} else {
			$s =~ s|.*/||;
			print_once( "$md5 no-url://$s");
		}
	} else {
		error("source $no url $s is ill-formatted");
	}
} # }}}

sub add_md5_to_print($) # {{{
{
	open(F, "< $_[0]") or die;
	my @sourcemap = ();
	while (<F>) {
		chomp;
		if (/^Source(\d+)\s*:\s*(.*)/i) {
			my $sourceno = $1;
			my $source = $2;
			$sourcemap[$sourceno] = $source;
		} elsif (/^\s*#\s*source(\d+)-md5\s*:\s*([a-f0-9]{32})/i) {
			my $no = $1;
			my $md5 = $2;
			if (defined $no_source{$no}) {
				error("both NoSource: $no and md5 given");
			} if (defined $sourcemap[$no]) {
				my $source = $sourcemap[$no];
				push @sources, [$no, $md5, $source];
			} else {
				error("source $no not defined (# SourceN-md5: has to be placed just after SourceN:)");
			}
		} elsif (/^\s*BuildRequires:\s*digest\(%SOURCE(\d+)\)\s*=\s*([a-f0-9]{32})/i) {
			my $no = $1;
			my $md5 = $2;
			if (defined $no_source{$no}) {
				error("both NoSource: $no and md5 given");
			} if (defined $sourcemap[$no]) {
				my $source = $sourcemap[$no];
				push @sources, [$no, $md5, $source];
			} else {
				error("source $no not defined (# Source digest has to be placed after SourceN:)");
			}
		}
	}
	close(F);
} # }}}

next_spec(shift);
preparse_spec($spec);
add_md5_to_print($spec);
cont( \@spec, { "nil" => "" } );

exit(0);

# vim: ts=4:sw=4:fdm=marker