]> git.pld-linux.org Git - projects/distfiles.git/blob - specparser.pl
1709c8e4464ce3b389ffa4ca64c5c61714712303
[projects/distfiles.git] / specparser.pl
1 #!/usr/bin/perl
2 #
3 # USAGE: specparser.pl file.spec
4 #
5 # Output:
6 #   stdout:
7 #     list of:
8 #       <md5><space><url>\n
9 #     for given specfile.
10 #     except for lines prefixed with: ERROR:
11 #   exit status:
12 #     0 - normal
13 #     2 - cannot open spec file
14 #
15 use strict;
16 use warnings;
17
18 my %no_source;
19 my $spec;
20 my $base_spec;
21 my @spec;
22 my @sources;
23 my $sources;
24 my %patchset;
25
26 sub print_source($$$);
27
28 sub next_spec($)
29 {
30         $spec = shift;
31         @spec = ();
32         $base_spec = $spec;
33         $base_spec =~ s|.*/||;
34 }
35
36 sub error($)
37 {
38         print_once( "ERROR: $base_spec: $_[0]" );
39 }
40
41 sub warning($)
42 {
43         print "ERROR: $base_spec: $_[0]\n";
44 }
45
46 sub trim_spaces($)
47 {
48         my $v = shift;
49
50         $v =~ s/\s+$//;
51         $v =~ s/^\s+//;
52
53         return $v;
54 }
55
56 # expand macros in string
57 sub expand($$) # {{{
58 {
59         my $v = trim_spaces(shift);
60         my $macrotree = shift;
61         my $cnt = 20;
62
63         while ($v =~ /\%\{([^\}]+)\}/) {
64                 my $value;
65                 if (defined $macrotree->{$1}) {
66                         $value = $macrotree->{$1};
67                 } else {
68                         error("undefined macro $1");
69                         $value = "UNDEFINED";
70                         return undef;
71                 }
72                 $v =~ s/\%\{([^\}]+)\}/$value/;
73
74                 return $v if (length $v > 1000 or $cnt-- <= 0)
75         }
76
77         while ($v =~ s/\%\(\s*echo\s+([^\|]+?)\s*\|\s*tr\s*(-d|)\s+([^\)]+?)\s*\)/\@\@tr-me\@\@/) {
78                 my ($what, $d_opt, $how) = ($1, $2, $3);
79                 my ($from, $to) = ($how, "");
80                 ($from, $to) = ($1, $2)
81                         if $how =~ /^([^\s]+)\s+([^\s]+)$/;
82                 if ($d_opt and $to ne "") {
83                         error("tr -d with second string)");
84                 } elsif (($from . $to) =~ /^[a-zA-Z0-9\+_\-\.]+$/) {
85                         if ($d_opt) {
86                                 eval "\$what =~ tr/$from//d;";
87                         } else {
88                                 eval "\$what =~ tr/$from/$to/;";
89                         }
90                 } else {
91                         error("illegal characters in tr string(s) '$from' '$to'");
92                 }
93                 $v =~ s/\@\@tr-me\@\@/$what/;
94
95                 return $v if (length $v > 1000 or $cnt-- <= 0)
96         }
97
98         error("unexpanded macros in $v")
99                 if ($v =~ /\%[^0-9]/);
100
101         return $v;
102 } # }}}
103
104 sub preparse_spec($) # {{{
105 {
106         @spec = ("");
107
108         open(F, "< $_[0]") or die("failed opening: " . $_[0]);
109         while (<F>) {
110                 chomp;
111                 if (/^\s*(\%(description|package|prep|install|pre|post|files)|BuildRoot|URL)/) {
112                         last;
113                 } elsif (/^\s*(\%if.*|\%else|\%endif|\%define\s+.*|Version.*|Name.*)\s*$/) {
114                         $_ = $1;
115                         if ($spec[$#spec] =~ /\%if/) {
116                                 if (/\%else/) {
117                                         next; # don't include empty %if-%else
118                                 } elsif (/\%endif/) {
119                                         # remove empty %if-%endif
120                                         pop @spec;
121                                         next;
122                                 }
123                         }
124                         push @spec, $_;
125                 } elsif (/^NoSource\s*:\s*(\d+)\s*$/i) {
126                         $no_source{ "source" . $1 } = 1;
127                 } elsif (my ($patchset_pattern, $patchset_start, $patchset_end) = /^%patchset_source.+-f\s+(\S+)\s+(\S+)\s+(\S+)/i) {
128                         %patchset = (
129                                 pattern => $patchset_pattern,
130                                 start => $patchset_start,
131                                 end => $patchset_end
132                         );
133                 }
134         }
135         close(F);
136
137         shift @spec;
138 } # }}}
139
140 sub process_patchset($) {
141         my $macros = shift;
142         return unless %patchset or $sources;
143
144         # parse sources file
145         my %files;
146         open(my $fh, '<', $sources) or die $!;
147         while (<$fh>) {
148                 chomp;
149                 next unless my ($hash, $filename) = /^([a-f0-9]{32})\s+\*?(.+)$/;
150                 $files{$filename} = $hash;
151         }
152
153         # print out patchset entries which source md5 is present in source file
154         my $start = expand($patchset{start}, $macros);
155         my $end = expand($patchset{end}, $macros);
156         my $pattern = expand($patchset{pattern}, $macros);
157         for (my $i = $start; $i <= $end; $i++) {
158                 my $url = sprintf($pattern, $i);
159                 my ($basename) = $url =~ m{/([^/]+$)};
160                 my $hash = $files{$basename} or next;
161                 print_source "patchset $i", $hash, $url;
162         }
163 }
164
165
166 my $total = 0;
167
168 sub cont($$);
169 sub cont($$) # {{{
170 {
171         my ($spec, $macros) = @_;
172         local $_;
173         while ($_ = shift @{$spec}) {
174                 if (0 <= index $_, '%if') { # if, ifarch, ifos
175
176                         # split spec parsing
177                         my @speccopy = @{$spec};
178                         my %macroscopy = %{$macros};
179                         cont(\@speccopy, \%macroscopy);
180
181                         my $level = 0;
182                         while ($_ = shift @{$spec}) {
183                                 if ($level <= 0 and ($_ eq '%else' or $_ eq '%endif')) {
184                                         last;
185                                 } elsif (0 <= index $_, '%if') {
186                                         $level++;
187                                 } elsif ($_ eq '%endif') {
188                                         $level--;
189                                 }
190                         }
191
192                         # continue parsing
193                 } elsif ($_ eq '%else') {
194
195                         # %else happens only when %if was interpreted
196                         # so skip until %endif
197
198                         my $level = 0;
199                         while ($_ = shift @{$spec}) {
200                                 if ($level <= 0 and $_ eq '%endif') {
201                                         last;
202                                 } elsif (0 <= index $_, '%if') {
203                                         $level++;
204                                 } elsif ($_ eq '%endif') {
205                                         $level--;
206                                 }
207                         }
208
209                 } elsif (/^\s*\%(define|global)\s+([^\s]+)\s+([^\s].*?)\s*$/) {
210                         $macros->{$2} = $3;
211
212                 } elsif (/^Version\s*:\s*(.*?)\s*$/i) {
213                         $macros->{"version"} = $1;
214                 } elsif (/^Name\s*:\s*(.*?)\s*$/i) {
215                         $macros->{"name"} = $1;
216                 } elsif (!/\%endif/) {
217                         warn "unrecognised line: $_\n";
218                 }
219         }
220
221         # so we have macros now, can parse patchset source
222         process_patchset($macros);
223
224         # the end, yuppie !
225         foreach my $s (@sources) {
226                 my $src = expand( $s->[2], $macros );
227                 if (defined $src) {
228                         our %tried;
229                         unless (exists $tried{$src}) {
230                                 print_source( $s->[0], $s->[1], $src );
231                                 $tried{$src} = 1;
232                         }
233                 }
234         }
235
236         if (++$total > 10000) {
237                 error("maximum number of bcond posibilities exceeded");
238                 exit 0;
239         }
240 } # }}}
241
242 sub print_once($)
243 {
244         our %printed;
245         my $l = shift;
246         unless (exists $printed{$l}) {
247                 print $l . "\n";
248                 $printed{$l} = 1;
249         }
250 }
251
252 sub print_source($$$) # {{{
253 {
254         my ($no, $md5, $s) = @_;
255
256         if ($s =~ /^([a-z0-9A-Z;:\=\?&\@\+\~\.,\-\/_]|\%[0-9])+(#\/[a-zA-Z0-9\._-]+)?$/) {
257                 if ($s =~ /^(ftp|http|https):\/\//) {
258                         if ($s =~ /\/$/) {
259                                 error("source $no ($s) is directory");
260                         } else {
261                                 if ($s =~ /:\/\/distfiles\.pld-linux\.org\/src/) {
262                                         $s =~ s|.*/||;
263                                         print_once( "$md5 no-url-copy://$s" );
264                                 } else {
265                                         print_once( "$md5 $s" );
266                                 }
267                         }
268                 } else {
269                         $s =~ s|.*/||;
270                         print_once( "$md5 no-url://$s");
271                 }
272         } else {
273                 error("source $no url $s is ill-formatted");
274         }
275 } # }}}
276
277 sub add_md5_to_print($) # {{{
278 {
279         open(F, "< $_[0]") or die("failed opening: " . $_[0]);
280         my %sourcemap = ();
281         while (<F>) {
282                 chomp;
283                 if (/^((?:Source|Patch)\d+)\s*:\s*(.*)/i) {
284                         my $sourceno = lc $1;
285                         my $source = $2;
286                         # master.dl is outdated currently
287                         # $source =~ s/dl.sourceforge.net/master.dl.sourceforge.net/;
288                         $sourcemap{ $sourceno } = $source;
289                 } elsif (/^\s*#\s*((?:source|patch)\d+)-md5\s*:\s*([a-f0-9]{32})/i) {
290                         my $no = lc $1;
291                         my $md5 = $2;
292                         if (defined $no_source{$no}) {
293                                 error("both NoSource: $no and md5 given");
294                         } elsif (defined $sourcemap{ $no} ) {
295                                 my $source = $sourcemap{ $no };
296                                 push @sources, [$no, $md5, $source];
297                         } else {
298                                 error("source $no not defined (# SourceN-md5: has to be placed just after SourceN:)");
299                         }
300                 } elsif (/^\s*BuildRequires:\s*digest\(%((?:SOURCE|PATCH)\d+)\)\s*=\s*([a-f0-9]{32})/i) {
301                         my $no = lc $1;
302                         my $md5 = $2;
303                         if (defined $no_source{ $no }) {
304                                 error("both NoSource: $no and md5 given");
305                         } elsif (defined $sourcemap{ $no }) {
306                                 my $source = $sourcemap{ $no };
307                                 push @sources, [$no, $md5, $source];
308                         } else {
309                                 error("source $no not defined (# Source digest has to be placed after SourceN:)");
310                         }
311                 }
312         }
313         close(F);
314 } # }}}
315
316 next_spec(shift);
317 $sources = shift if @ARGV;
318 undef $sources if $sources and not -e $sources;
319 preparse_spec($spec);
320 add_md5_to_print($spec);
321 cont( \@spec, { "nil" => "" } );
322
323 exit(0);
324
325 # vim: ts=4:sw=4:fdm=marker
This page took 0.055169 seconds and 3 git commands to generate.