]> git.donarmstrong.com Git - wannabuild.git/blob - lib/WB/QD.pm
WB::QD: Comment the code a bit more.
[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             foreach my $tarch (split(/\s+/, $p->{'arch'})) {
51                 $p->{'for-us'} = 1 if debarch_is($arch, $tarch);
52             }
53
54             # ignore if package already exists with higher version
55             if ($srcs->{$p->{'name'}}) {
56                 next if (vercmp($srcs->{$p->{'name'}}->{'version'}, $p->{'version'}) > 0);
57             }
58             if ($p->{'binary'}) {
59                 $p->{'binary'} = [ split(/,? /, $p->{'binary'}) ];
60             }
61             $srcs->{$p->{'name'}} = $p;
62         }
63         close $S;
64     }
65
66     foreach my $p (@$BIN) {
67         my $P = new IO::Uncompress::AnyInflate($p) || return "WB::QD::PKGS can't open $p";
68         while(<$P>) {
69             my $p;
70             /^Version:\s*(\S+)$/mi and $p->{'version'} = $1;
71             /^Version:\s*(\S+)\+b([0-9]+)$/mi and $p->{'version'} = $1 and $p->{'binnmu'} = $2;
72             /^Architecture:\s*(\S+)$/mi and $p->{'arch'} = $1;
73             /^Package:\s*(\S+)$/mi and $p->{'binary'} = $1;
74             /^Package:\s*(\S+)$/mi and $p->{'source'} = $1;
75             /^Source:\s*(\S+)$/mi and $p->{'source'} = $1;
76             /^Source:\s*(\S+)\s+\((\S+)\)$/mi and $p->{'source'} = $1 and $p->{'version'} = $2;
77
78             # consider packages as non-existant if it's all but outdated
79             # arch:all and ver(binary) < ver(source) => skip
80             next if $p->{'arch'} eq 'all' && $srcs->{$p->{'source'}} && $srcs->{$p->{'source'}}->{'version'} && vercmp($srcs->{$p->{'source'}}->{'version'}, $p->{'version'}) > 0;
81             # not for the current architecture or arch:all => skip
82             next unless $p->{'arch'} eq 'all' || $p->{'arch'} eq ${arch};
83             # register the binary if there isn't a newer one in the hash yet
84             $binary->{$p->{'binary'}} = { 'version' => $p->{'version'}, 'arch' => $p->{'arch'}}
85                 unless $binary->{$p->{'binary'}} and vercmp($binary->{$p->{'binary'}}->{'version'}, $p->{'version'}) < 0;
86
87             #next if $pas->{$p->{'binary'}} && pasignore($pas->{$p->{'binary'}}, $arch);
88
89             # only continue if it's arch-specific...
90             next if $p->{'arch'} eq 'all';
91
92             # annotate the source package if present, continue otherwise
93             next unless $srcs->{$p->{'source'}};
94
95             $srcs->{$p->{'source'}}->{'compiled'} = 1;
96
97             # TODO: ???
98             next unless $srcs->{$p->{'source'}}->{'version'} eq $p->{'version'};
99             $srcs->{$p->{'source'}}->{'installed'} = 1;
100
101             next unless $p->{'binnmu'};
102             next if ($srcs->{$p->{'source'}}->{'binnmu'}) && ($srcs->{$p->{'source'}}->{'binnmu'} > $p->{'binnmu'});
103             $srcs->{$p->{'source'}}->{'binnmu'} = $p->{'binnmu'};
104         }
105         close $P;
106     }
107
108     SRCS:
109     for my $k (keys %$srcs) {
110         if ($srcs->{$k}->{'installed'}) {
111             $srcs->{$k}->{'status'} = 'installed';
112             delete $srcs->{$k}->{'installed'};
113         } elsif ($srcs->{$k}->{'compiled'}) {
114             $srcs->{$k}->{'status'} = 'out-of-date';
115         } else {
116             $srcs->{$k}->{'status'} = 'uncompiled';
117         }
118         delete $srcs->{$k}->{'compiled'};
119         $srcs->{$k}->{'status'} = 'installed' if $srcs->{$k}->{'arch'} && $srcs->{$k}->{'arch'} eq 'all';
120         
121         if (!$srcs->{$k}->{'for-us'} && $srcs->{$k}->{'status'} ne 'installed') {
122             $srcs->{$k}->{'status'} = 'arch-not-in-arch-list';
123         }
124         delete $srcs->{$k}->{'for-us'};
125
126         if ($srcs->{$k}->{'arch'} eq 'all') {
127             $srcs->{$k}->{'status'} = 'arch-all-only';
128             delete $srcs->{$k}->{'arch'};
129             next;
130         }
131         delete $srcs->{$k}->{'arch'};
132         
133         if (pasignore($pas->{'%'.$k}, $arch)) {
134             $srcs->{$k}->{'status'} = 'packages-arch-specific';
135             next;
136         }
137         for my $bin (@{$srcs->{$k}->{'binary'}}) {
138             $srcs->{$k}->{'pas'} = 1 if pasignore($pas->{$bin}, $arch);
139             next if pasignore($pas->{$bin}, $arch);
140             next if $binary->{$bin} and $binary->{$bin}->{'arch'} eq 'all';
141             next SRCS;
142         }
143         if ($srcs->{$k}->{'pas'}) {
144             $srcs->{$k}->{'status'} = 'packages-arch-specific';
145         } else {
146             $srcs->{$k}->{'status'} = 'overwritten-by-arch-all';
147         }
148         delete $srcs->{$k}->{'pas'};
149     }
150     $srcs->{'_binary'} = $binary;
151     local($/) = "\n";
152
153     return \$srcs;
154 }
155
156 sub pasignore {
157     my $p = shift;
158     my $arch = shift;
159     if ($p && $p->{'mode'}) {
160         return 1 unless grep { $_ eq $arch } @{$p->{'arch'}};
161     }
162     if ($p && not $p->{'mode'}) {
163         return 1 if grep /^!$arch$/, @{$p->{'arch'}};
164     }
165     return 0;
166 }
167
168 1;