aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Check/Input/Dependencies.pm
blob: f2b96d13e9f33b76850bb1cd374f5137558a00a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# $Id$
package Youri::Check::Input::Dependencies;

=head1 NAME

Youri::Check::Input::Dependencies - Check dependencies consistency

=head1 DESCRIPTION

This class checks dependencies consistency.

=cut

use warnings;
use strict;
use Carp;
use Youri::Package;
use base 'Youri::Check::Input';

use constant MEDIA => 0;
use constant RANGE => 1;

sub columns {
    return qw/
        arch
        file
        error
        level
    /;
}

sub links {
    return qw//;
}

sub prepare {
    my ($self, @medias) = @_;
    croak "Not a class method" unless ref $self;

    foreach my $media (@medias) {
        my $media_id = $media->get_id();
        $self->{_medias}->{$media_id} = 1;
        print STDERR "Indexing media $media_id dependencies\n"
            if $self->{_verbose};

        my $index = sub {
            my ($package) = @_;

            # index provides
            foreach my $provide ($package->get_provides()) {
                push(
                    @{$self->{_provides}->{$provide->[Youri::Package::DEPENDENCY_NAME]}},
                    [ $media_id, $provide->[Youri::Package::DEPENDENCY_RANGE] ]
                );
            }

            # index files
            foreach my $file ($package->get_files()) {
                push(
                    @{$self->{_files}->{$file->[Youri::Package::FILE_NAME]}},
                    [ $media_id, undef ]
                );
            }
        };
        $media->traverse_headers($index);
    }
}

sub run {
    my ($self, $media, $resultset) = @_;
    croak "Not a class method" unless ref $self;

    my @allowed_ids = $media->allow_deps();

    # abort unless all allowed medias are present
    foreach my $id (@allowed_ids) {
        unless ($self->{_medias}->{$id}) {
            carp "Missing media $id, aborting";
            return;
        }
    }

    # index allowed medias
    my %allowed_ids = map { $_ => 1 } @allowed_ids;
    my $allowed_ids = join(",", @allowed_ids);

    my $class = $media->get_package_class();

    my $check = sub {
        my ($package) = @_;

        my $arch = $package->get_arch();
        my $name = $package->get_name();

        foreach my $require ($package->get_requires()) {

            my $found =
                substr($require->[Youri::Package::DEPENDENCY_NAME], 0, 1) eq '/' ?
                    $self->{_files}->{$require->[Youri::Package::DEPENDENCY_NAME]} :
                    $self->{_provides}->{$require->[Youri::Package::DEPENDENCY_NAME]};

            my @found = $found ? @$found : ();

            if (!@found) {
                $resultset->add_result($self->{_id}, $media, $package, {
                    arch  => $arch,
                    file  => $name,
                    error => "$require->[Youri::Package::DEPENDENCY_NAME] not found",
                    level => Youri::Check::Input::ERROR
                });
                next;
            }

            my @found_in_media =
                grep { $allowed_ids{$_->[MEDIA]} }
                @found;

            if (!@found_in_media) {
                $resultset->add_result($self->{_id}, $media, $package, {
                    arch  => $arch,
                    file  => $name,
                    error => "$require->[Youri::Package::DEPENDENCY_NAME] found in incorrect media $_->[MEDIA] (allowed $allowed_ids)",
                    level => Youri::Check::Input::ERROR
                }) foreach @found;
                next;
            }

            next unless $require->[Youri::Package::DEPENDENCY_RANGE];

            my @found_in_range =
                grep {
                    !$_->[RANGE] ||
                    $class->compare_ranges(
                        $require->[Youri::Package::DEPENDENCY_RANGE],
                        $_->[RANGE]
                    )
                } @found_in_media;

            if (!@found_in_range) {
                $resultset->add_result($self->{_id}, $media, $package, {
                    arch  => $arch,
                    file  => $name,
                    error => "$require->[Youri::Package::DEPENDENCY_NAME] found with incorrect range $_->[RANGE] (needed $require->[Youri::Package::DEPENDENCY_RANGE])",
                    level => Youri::Check::Input::ERROR
                }) foreach @found_in_media;
                next;
            }
        }
    };

    $media->traverse_headers($check);
}

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1;