aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Repository/PLF.pm
blob: c26037911e54994ea39ec320b7463364462a75ef (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# $Id: /local/youri/soft/trunk/lib/Youri/Upload/Action/RSS.pm 857 2006-01-29T10:15:43.298856Z guillaume  $
package Youri::Repository::PLF;

=head1 NAME

Youri::Repository::PLF - PLF repository implementation

=head1 DESCRIPTION

This module implements PLF repository.

=cut

use warnings;
use strict;
use Carp;
use Memoize;
use base qw/Youri::Repository/;
use constant {
    PACKAGE_CLASS   => 'Youri::Package::URPM',
    PACKAGE_CHARSET => 'utf8'
};

memoize('_get_section');


sub _init {
    my $self   = shift;
    my %options = (
        module => 'SPECS', # CVS module
        noarch => 'noarch', # noarch packages policy
        @_
    );

    $self->{_module} = $options{module};
    $self->{_noarch} = $options{noarch};
}

sub get_package_class {
    return PACKAGE_CLASS;
}

sub get_package_charset {
    return PACKAGE_CHARSET;
}

sub get_install_path {
    my ($self, $package, $target, $define) = @_;

    return $self->_get_path($package, $target, $define);
}

sub get_archive_path {
    my ($self, $package, $target, $define) = @_;

    return $self->_get_path($package, $target, $define);
}

sub _get_path {
    my ($self, $package, $target, $define) = @_;

    my $section = $self->_get_section($package, $target, $define);

    my $subpath = $self->_get_subpath($package, $target);

    return "$section/$subpath";
}


sub get_version_path {
    my ($self, $package, $target, $define) = @_;

    my $section = $self->_get_section($package, $target, $define);

    return "$self->{_module}/$section";
}

=head2 get_replaced_packages($package, $target, $define)

Overrides parent method to add libified packages.

=cut

sub get_replaced_packages {
    my ($self, $package, $target, $define) = @_;
    croak "Not a class method" unless ref $self;

    my @replaced_packages = 
        $self->SUPER::get_replaced_packages($package, $target, $define);

    # mandriva lib policy:
    # library package names change with revision, making mandatory to
    # duplicate older revisions search with a custom pattern
    my $name = $package->get_name();
    if ($name =~ /^(lib\w+[a-zA-Z_])[\d_\.]+([-\w]*)$/) {
        push(@replaced_packages,
            grep { $package->compare($_) > 0 }
            map { PACKAGE_CLASS->new(file => $_) }
            $self->get_files(
                $self->{_install_root},
                $self->get_install_path($package, $target, $define),
                PACKAGE_CLASS->get_pattern(
                    $1 . '[\d_\.]+' . $2, # custom name pattern
                    undef,
                    undef,
                    $package->get_arch()
                ),
            )
        );
    }

    return @replaced_packages;
       
}

sub _get_section {
    my ($self, $package, $target, $define) = @_;

    my $section;

    # try to find section automatically
    my $arch = $package->get_arch();

    my $source_pattern = PACKAGE_CLASS->get_pattern(
        $package->get_canonical_name(),
        undef,
        undef,
        'src'
    );

    my $binary_pattern = PACKAGE_CLASS->get_pattern(
        $package->get_name(),
        undef,
        undef,
        $arch
    );

    my $source_subpath = $self->_get_subpath($package, $target, 'src');
    my $binary_subpath = $self->_get_subpath($package, $target, $arch);

    # for each potential section, try to match
    # a suitable source patten in source directory
    # a suitable binary patten in binary directory
    foreach my $dir (qw/free non-free/) {
        next unless
            $self->get_files(
                $self->{_install_root},
                "$dir/$source_subpath",
                $source_pattern
            ) || $self->get_files(
                $self->{_install_root},
                "$dir/$binary_subpath",
                $binary_pattern
            );
        $section = $dir;
        last;
    }

    # use defined section if not found
    $section = $define->{section} unless $section;

    die "Can't guess destination: section missing" unless $section;

    return $section;
}

sub _get_subpath {
    my ($self, $package, $target, $arch) = @_;

    my $subpath;

    # use package arch if not specified
    $arch = $package->get_arch() unless $arch;

    if ($arch eq 'src') {
        $subpath = 'src';
    } else {
        if ($arch eq 'noarch') {
            $subpath = "$target/$self->{_noarch}";
        } else {
            $subpath = "$target/$arch";
        }
    }

    return $subpath;
}

=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;