fix a package metadata parsing bug
[openwrt/staging/wigyori.git] / scripts / metadata.pm
1 package metadata;
2 use base 'Exporter';
3 use strict;
4 use warnings;
5 our @EXPORT = qw(%package %srcpackage %category %subdir %preconfig clear_packages parse_package_metadata get_multiline);
6
7 our %package;
8 our %preconfig;
9 our %srcpackage;
10 our %category;
11 our %subdir;
12
13 sub get_multiline {
14 my $fh = shift;
15 my $prefix = shift;
16 my $str;
17 while (<$fh>) {
18 last if /^@@/;
19 s/^\s*//g;
20 $str .= (($_ and $prefix) ? $prefix . $_ : $_);
21 }
22
23 return $str ? $str : "";
24 }
25
26 sub clear_packages() {
27 %subdir = ();
28 %preconfig = ();
29 %package = ();
30 %srcpackage = ();
31 %category = ();
32 }
33
34 sub parse_package_metadata($) {
35 my $file = shift;
36 my $pkg;
37 my $makefile;
38 my $preconfig;
39 my $subdir;
40 my $src;
41
42 open FILE, "<$file" or do {
43 warn "Cannot open '$file': $!\n";
44 return undef;
45 };
46 while (<FILE>) {
47 chomp;
48 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
49 $makefile = $1;
50 $subdir = $2;
51 $src = $3;
52 $subdir =~ s/^package\///;
53 $subdir{$src} = $subdir;
54 $srcpackage{$src} = [];
55 undef $pkg;
56 };
57 next unless $src;
58 /^Package:\s*(.+?)\s*$/ and do {
59 $pkg = {};
60 $pkg->{src} = $src;
61 $pkg->{makefile} = $makefile;
62 $pkg->{name} = $1;
63 $pkg->{default} = "m if ALL";
64 $pkg->{depends} = [];
65 $pkg->{builddepends} = [];
66 $pkg->{subdir} = $subdir;
67 $pkg->{tristate} = 1;
68 $package{$1} = $pkg;
69 push @{$srcpackage{$src}}, $pkg;
70 };
71 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
72 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
73 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
74 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
75 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
76 /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
77 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
78 /^Provides: \s*(.+)\s*$/ and do {
79 my @vpkg = split /\s+/, $1;
80 foreach my $vpkg (@vpkg) {
81 $package{$vpkg} or $package{$vpkg} = {
82 name => $vpkg,
83 vdepends => [],
84 src => $src,
85 subdir => $subdir,
86 makefile => $makefile
87 };
88 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
89 }
90 };
91 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
92 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
93 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
94 /^Category: \s*(.+)\s*$/ and do {
95 $pkg->{category} = $1;
96 defined $category{$1} or $category{$1} = {};
97 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
98 push @{$category{$1}->{$src}}, $pkg;
99 };
100 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
101 /^Type: \s*(.+)\s*$/ and do {
102 $pkg->{type} = [ split /\s+/, $1 ];
103 undef $pkg->{tristate};
104 foreach my $type (@{$pkg->{type}}) {
105 $type =~ /ipkg/ and $pkg->{tristate} = 1;
106 }
107 };
108 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
109 /^Prereq-Check:/ and $pkg->{prereq} = 1;
110 /^Preconfig:\s*(.+)\s*$/ and do {
111 my $pkgname = $pkg->{name};
112 $preconfig{$pkgname} or $preconfig{$pkgname} = {};
113 if (exists $preconfig{$pkgname}->{$1}) {
114 $preconfig = $preconfig{$pkgname}->{$1};
115 } else {
116 $preconfig = {
117 id => $1
118 };
119 $preconfig{$pkgname}->{$1} = $preconfig;
120 }
121 };
122 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
123 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
124 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
125 }
126 close FILE;
127 return 1;
128 }
129
130 1;