]> git.pld-linux.org Git - projects/distfiles.git/blame - specparser.pl
- opt-in not opt-out url validation
[projects/distfiles.git] / specparser.pl
CommitLineData
0db58532
MM
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
20sub 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
39sub 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}
47sub 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
65sub 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;
7882011d 74 if ($s =~ /^([a-z0-9A-Z:\+\~\.\-\/_]|\%[0-9])+$/) {
0db58532 75 print "$md5 $s\n";
7882011d
MM
76 } else {
77 print STDERR "$_[0]: source url $s is ill-formatted\n";
0db58532
MM
78 }
79 } else {
80 print STDERR "$_[0]: source $1 not defined\n";
81 }
82 }
83 }
84 close(F);
85}
86
87$spec = shift;
88
89parse_defines($spec);
90print_md5($spec);
91
92exit(0);
This page took 0.09576 seconds and 4 git commands to generate.