aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Repository.pm
blob: 255d2172a6c24ef697b084d002c1ce89e08144d0 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# $Id: Base.pm 631 2006-01-26 22:22:23Z guillomovitch $
package Youri::Repository;

=head1 NAME

Youri::Repository - Abstract repository

=head1 DESCRIPTION

This abstract class defines Youri::Repository interface.

=cut

use warnings;
use strict;
use Carp;
use File::Basename;
use Youri::Package;

=head1 CLASS METHODS

=head2 get_package_class()

Return package class for this repository.

=head2 get_package_charset()

Return package charset for this repository.

=head2 new(%args)

Creates and returns a new Youri::Repository object.

No generic parameters (subclasses may define additional ones).

Warning: do not call directly, call subclass constructor instead.

=cut

sub new {
    my $class = shift;
    croak "Abstract class" if $class eq __PACKAGE__;

    my %options = (
        install_root  => '', # path to top-level directory
        archive_root  => '', # path to top-level directory
        version_root  => '', # path to top-level directory
        test          => 0,  # test mode
        verbose       => 0,  # verbose mode
        @_
    );


    croak "no install root" unless $options{install_root};
    croak "invalid install root" unless -d $options{install_root};

    my $self = bless {
        _install_root  => $options{install_root},
        _archive_root  => $options{archive_root},
        _version_root  => $options{version_root},
        _test          => $options{test},
        _verbose       => $options{verbose},
    }, $class;

    $self->_init(%options);

    return $self;
}

sub _init {
    # do nothing
}

=head1 INSTANCE METHODS

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

Get all older revisions from a package found in its installation directory, as a
list of L<Youri::Package> objects.

=cut

sub get_older_revisions {
    my ($self, $package, $target, $define) = @_;
    croak "Not a class method" unless ref $self;
    print "Looking for package $package older revisions for $target\n"
        if $self->{_verbose} > 0;

    return $self->get_revisions(
        $package,
        $target,
        $define,
        sub { return $package->compare($_[0]) > 0 }
    );
}

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

Get last older revision from a package found in its installation directory, as a
single L<Youri::Package> object.

=cut

sub get_last_older_revision {
    my ($self, $package, $target, $define) = @_;
    croak "Not a class method" unless ref $self;
    print "Looking for package $package last older revision for $target\n"
        if $self->{_verbose} > 0;

    return ($self->get_older_revisions($package, $target, $define))[0];
}

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

Get all newer revisions from a package found in its installation directory, as a
list of L<Youri::Package> objects.

=cut

sub get_newer_revisions {
    my ($self, $package, $target, $define) = @_;
    croak "Not a class method" unless ref $self;
    print "Looking for package $package newer revisions for $target\n"
        if $self->{_verbose} > 0;

    return $self->get_revisions(
        $package,
        $target,
        $define,
        sub { return $_[0]->compare($package) >= 0 }
    );
}


=head2 get_revisions($package, $target, $define, $filter)

Get all revisions from a package found in its installation directory, using an
optional filter, as a list of L<Youri::Package> objects.

=cut

sub get_revisions {
    my ($self, $package, $target, $define, $filter) = @_;
    croak "Not a class method" unless ref $self;
    print "Looking for package $package revisions for $target\n"
        if $self->{_verbose} > 0;

    my @packages = 
        map { $self->get_package_class()->new(file => $_) }
        $self->get_files(
            $self->{_install_root},
            $self->get_install_path($package, $target, $define),
            $self->get_package_class()->get_pattern(
                $package->get_name(),
                undef,
                undef,
                $package->get_arch(),
            )
        );

    @packages = grep { $filter->($_) } @packages if $filter;

    return
        sort { $b->compare($a) } # sort by revision order
        @packages;
}

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

Get all packages obsoleted by given one, as a list of L<Youri::Package>
objects.

=cut

sub get_obsoleted_packages {
    my ($self, $package, $target, $define) = @_;
    croak "Not a class method" unless ref $self;
    print "Looking for packages obsoleted by $package for $target\n"
        if $self->{_verbose} > 0;

    my @packages;
    foreach my $obsolete ($package->get_obsoletes()) {
        my $pattern = $self->get_package_class()->get_pattern($obsolete->[Youri::Package::DEPENDENCY_NAME]);
        push(@packages,
            map { $self->get_package_class()->new(file => $_) }
            $self->get_files(
                $self->{_install_root},
                $self->get_install_path($package, $target, $define),
                $pattern
            )
        );
    }

    return @packages;
}

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

Get all packages replaced by given one, as a list of L<Youri::Package>
objects.

=cut

sub get_replaced_packages {
    my ($self, $package, $target, $define) = @_;
    croak "Not a class method" unless ref $self;
    print "Looking for packages replaced by $package for $target\n"
        if $self->{_verbose} > 0;

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

=head2 get_files($path, $pattern)

Get all files found in a directory, using an optional filtering pattern
(applied to the whole file name), as a list of files.

=cut

sub get_files {
    my ($self, $root, $path, $pattern) = @_;
    croak "Not a class method" unless ref $self;
    print "Looking for files matching $pattern in $root/$path\n"
        if $self->{_verbose} > 1;

    my @files =
        grep { -f }
        glob "$root/$path/*";

    @files =
        grep { basename($_) =~ /^$pattern$/ }
        @files
        if $pattern;

    return @files;
}

=head2 get_install_root()

Returns installation root

=cut

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

    return $self->{_install_root};
}

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

Returns install destination directory for given L<Youri::Package> object
and given target.

=cut

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

    return
        $self->{_install_root} .
        '/' .
        $self->get_install_path($package, $target, $define);
}

=head2 get_archive_root()

Returns archiving root

=cut

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

    return $self->{_archive_root};
}

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

Returns archiving destination directory for given L<Youri::Package> object
and given target.

=cut

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

    return
        $self->{_archive_root} .
        '/' .
        $self->get_archive_path($package, $target, $define);
}

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

Returns rejecting destination directory for given L<Youri::Package> object
and given target.

=cut

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

    return
        $self->get_reject_path($package, $target, $define);
}
=head2 get_version_root()

Returns versionning root

=cut

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

    return $self->{_version_root};
}

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

Returns versioning destination directory for given L<Youri::Package>
object and given target.

=cut

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

    return
        $self->{_version_root} .
        '/' .
        $self->get_version_path($package, $target, $define);
}

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

Returns install destination file for given L<Youri::Package> object and
given target.

=cut

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

    return 
        $self->get_install_dir($package, $target, $define) .
        '/' .
        $package->get_file_name();
}

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

Returns installation destination path (relative to repository root) for given
L<Youri::Package> object and given target.

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

Returns archiving destination path (relative to repository root) for given
L<Youri::Package> object and given target.

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

Returns reject destination path for given
L<Youri::Package> object and given target.


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

Returns versioning destination path (relative to repository root) for given
L<Youri::Package> object and given target.

=head1 SUBCLASSING

The following methods have to be implemented:

=over

=item get_install_path

=item get_archive_path

=item get_version_path

=back

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