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