]> git.donarmstrong.com Git - wannabuild.git/blob - lib/WB/QD.pm
Newer versions of Dpkg::Version export version_compare instead of vercmp.
[wannabuild.git] / lib / WB / QD.pm
1 package WB::QD;
2
3 use strict;
4 use IO::Uncompress::AnyInflate qw(anyinflate);
5 use Dpkg::Version (); # import nothing
6 if ( defined $Dpkg::Version::VERSION ) {
7     *vercmp = \&Dpkg::Version::version_compare;
8 } else {
9     *vercmp = \&Dpkg::Version::vercmp;
10 }
11 use Dpkg::Arch qw(debarch_is);
12 use Data::Dumper;
13
14 sub readsourcebins {
15     my $arch = shift;
16     my $pasfile = shift;
17     my $SRC = shift;
18     my $BIN = shift;
19     my $binary = {};
20
21     my $pas = {};
22     local($/) = "\n";
23     open(my $pasf, '<', $pasfile);
24     while(<$pasf>) {
25         chomp;
26         s,\s*#.*,,;
27         next unless $_;
28         my ($p, $c) = split(/:\s*/);
29         $pas->{$p} = { arch => [ split(/\s+/, $c) ], mode => substr($c, 0, 1) ne '!' };
30     }
31     close $pasf;
32
33     my $srcs = {};
34     local($/) = ""; # read in paragraph mode
35
36     foreach my $s (@$SRC) {
37         my $S = new IO::Uncompress::AnyInflate($s) || return "WB::QD::SRC can't open $s";
38         while(<$S>) {
39             my $p={};
40             /^Package:\s*(\S+)$/mi and $p->{'name'} = $1;
41             /^Version:\s*(\S+)$/mi and $p->{'version'} = $1;
42             /^Binary:\s*(.*)$/mi and $p->{'binary'} = $1;
43             /^Architecture:\s*(.+)$/mi and $p->{'arch'} = $1;
44             /^Priority:\s*(\S+)$/mi and $p->{'priority'} = $1;
45             /^Section:\s*(\S+)$/mi and $p->{'section'} = $1;
46             /^Build-Depends:\s*(.*)$/mi and $p->{'depends'} = $1;
47             /^Build-Conflicts:\s*(.*)$/mi and $p->{'conflicts'} = $1;
48
49             next unless $p->{'name'} and $p->{'version'};
50             next if $p->{'arch'} eq 'all';
51             foreach my $tarch (split(/\s+/, $p->{'arch'})) {
52                 $p->{'for-us'} = 1 if debarch_is($arch, $tarch);
53             }
54             delete $p->{'arch'};
55
56             # ignore if package already exists with higher version
57             if ($srcs->{$p->{'name'}}) {
58                 next if (vercmp($srcs->{$p->{'name'}}->{'version'}, $p->{'version'}) > 0);
59             }
60             if ($p->{'binary'}) {
61                 $p->{'binary'} = [ split(/,? /, $p->{'binary'}) ];
62             }
63             $srcs->{$p->{'name'}} = $p;
64         }
65         close $S;
66     }
67
68     foreach my $p (@$BIN) {
69         my $P = new IO::Uncompress::AnyInflate($p) || return "WB::QD::PKGS can't open $p";
70         while(<$P>) {
71             my $p;
72             /^Version:\s*(\S+)$/mi and $p->{'version'} = $1;
73             /^Version:\s*(\S+)\+b([0-9]+)$/mi and $p->{'version'} = $1 and $p->{'binnmu'} = $2;
74             /^Architecture:\s*(\S+)$/mi and $p->{'arch'} = $1;
75             /^Package:\s*(\S+)$/mi and $p->{'binary'} = $1;
76             /^Package:\s*(\S+)$/mi and $p->{'source'} = $1;
77             /^Source:\s*(\S+)$/mi and $p->{'source'} = $1;
78             /^Source:\s*(\S+)\s+\((\S+)\)$/mi and $p->{'source'} = $1 and $p->{'version'} = $2;
79
80             next unless $p->{'arch'} eq 'all' || $p->{'arch'} eq ${arch};
81             $binary->{$p->{'binary'}} = { 'version' => $p->{'version'}, 'arch' => $p->{'arch'}} unless $binary->{$p->{'binary'}} and vercmp($binary->{$p->{'binary'}}->{'version'}, $p->{'version'}) < 0;
82
83             #next if $pas->{$p->{'binary'}} && pasignore($pas->{$p->{'binary'}}, $arch);
84             next if $p->{'arch'} eq 'all';
85             next unless $srcs->{$p->{'source'}};
86             $srcs->{$p->{'source'}}->{'compiled'} = 1;
87             next unless $srcs->{$p->{'source'}}->{'version'} eq $p->{'version'};
88             $srcs->{$p->{'source'}}->{'installed'} = 1;
89             next unless $p->{'binnmu'};
90             next if ($srcs->{$p->{'source'}}->{'binnmu'}) && ($srcs->{$p->{'source'}}->{'binnmu'} > $p->{'binnmu'});
91             $srcs->{$p->{'source'}}->{'binnmu'} = $p->{'binnmu'};
92         }
93         close $P;
94     }
95
96     SRCS:
97     for my $k (keys %$srcs) {
98         if ($srcs->{$k}->{'installed'}) {
99             $srcs->{$k}->{'status'} = 'installed';
100             delete $srcs->{$k}->{'installed'};
101         } elsif ($srcs->{$k}->{'compiled'}) {
102             $srcs->{$k}->{'status'} = 'out-of-date';
103         } else {
104             $srcs->{$k}->{'status'} = 'uncompiled';
105         }
106         delete $srcs->{$k}->{'compiled'};
107         $srcs->{$k}->{'status'} = 'installed' if $srcs->{$k}->{'arch'} && $srcs->{$k}->{'arch'} eq 'all';
108         
109         if (!$srcs->{$k}->{'for-us'} && $srcs->{$k}->{'status'} ne 'installed') {
110             $srcs->{$k}->{'status'} = 'auto-not-for-us';
111         }
112         delete $srcs->{$k}->{'for-us'};
113
114         #my $p = $pas->{$k};
115         #$p ||= $pas->{'%'.$k};
116         #$srcs->{$k}->{'status'} = 'not-for-us' if pasignore($p, $arch);
117         if (pasignore($pas->{'%'.$k}, $arch)) {
118             $srcs->{$k}->{'status'} = 'not-for-us';
119             next;
120         }
121         for my $bin (@{$srcs->{$k}->{'binary'}}) {
122             next if pasignore($pas->{$bin}, $arch);
123             next if $binary->{$bin} and $binary->{$bin}->{'arch'} eq 'all';
124             next SRCS;
125         }
126         $srcs->{$k}->{'status'} = 'not-for-us';
127     }
128     $srcs->{'_binary'} = $binary;
129     local($/) = "\n";
130
131     return \$srcs;
132 }
133
134 sub pasignore {
135     my $p = shift;
136     my $arch = shift;
137     if ($p && $p->{'mode'}) {
138         return 1 unless grep { $_ eq $arch } @{$p->{'arch'}};
139     }
140     if ($p && not $p->{'mode'}) {
141         return 1 if grep /^!$arch$/, @{$p->{'arch'}};
142     }
143     return 0;
144 }
145
146 1;