]> git.pld-linux.org Git - projects/distfiles.git/blob - specparser.pl
- initial
[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 #   stderr:
11 #     diagnostics
12 #   exit status:
13 #     0 - normal
14 #     2 - cannot open spec file
15 #
16
17 %macro = ();
18
19 # expand macros in string
20 sub expand($)
21 {
22   my $v = shift;
23   my $cnt = 20;
24   
25   while ($cnt-- > 0 && $v =~ /\%\{([^\{]+)\}/) {
26     my $value;
27     if (defined $macro{$1}) {
28       $value = $macro{$1};
29     } else {
30       $value = "\@undef:$1\@";
31     }
32     $v =~ s/\%\{([^\{]+)\}/$value/;
33     last if (length $v > 1000);
34   }
35   return $v;
36 }
37
38 # define given macro
39 sub define($$)
40 {
41   my ($n, $v) = @_;
42   $macro{$n} = expand($v);
43 }
44
45 # sets hash of macros defined with %define or %global
46 # also define %{name}, %{version} and %{source_N}
47 sub parse_defines($)
48 {
49   open(F, "< $_[0]") or die;
50   while (<F>) {
51     chomp;
52     if (/^\s*\%(define|global)\s+([^\s]+)\s+([^\s].*)$/) {
53      define($2, $3);
54     } elsif (/^Version\s*:\s*(.*)/i) {
55       define("version", $1);
56     } elsif (/^Name\s*:\s*(.*)/i) {
57       define("name", $1);
58     } elsif (/^Source(\d+)\s*:\s*(.*)/i) {
59       define("source_$1", $2);
60     }
61   }
62   close(F);
63 }
64
65 sub print_md5($)
66 {
67   open(F, "< $_[0]") or die;
68   while (<F>) {
69     chomp;
70     if (/^\s*#\s*source(\d+)-md5\s*:\s*([a-f0-9]{32})/i) {
71       if (defined $macro{"source_$1"}) {
72         my $s = $macro{"source_$1"};
73         my $md5 = $2;
74         if ($s =~ / / || $s =~ /^\%\{/ || $s =~ /\@undef/) {
75           print STDERR "$_[0]: source url $s is ill-formatted\n";
76         } else {
77           print "$md5 $s\n";
78         }
79       } else {
80         print STDERR "$_[0]: source $1 not defined\n";
81       }
82     }
83   }
84   close(F);
85 }
86
87 $spec = shift;
88
89 parse_defines($spec);
90 print_md5($spec);
91
92 exit(0);
This page took 0.066558 seconds and 4 git commands to generate.