summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/PAR/Repository/Query.pm
blob: 7c0f48c49f81103cd92fab425bb1156fd03440a7 (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
405
406
407
408
409
410
package PAR::Repository::Query;

use 5.006;
use strict;
use warnings;

use Carp qw/croak/;

our $VERSION = '0.14';

=head1 NAME

PAR::Repository::Query - Implements repository queries

=head1 SYNOPSIS

  use PAR::Repository;
  # or:
  use PAR::Repository::Client;

=head1 DESCRIPTION

This module is for internal use by L<PAR::Repository> or
L<PAR::Repository::Client> only. Both modules inherit from this.
C<PAR::Repository::Query> implements a unified query interface for
both the server- and client-side components of PAR repositories.

If you decide to inherit from this class (for whatever reason),
you should provide at least two methods: C<modules_dbm> which returns
a L<DBM::Deep> object representing the modules DBM file.
(See L<PAR::Repository::DBM> for details.) And C<scripts_dbm> which is
the equivalent for the scripts DBM file.

=head2 EXPORT

None. But the methods are callable on C<PAR::Repository> and 
C<PAR::Repository::Client> objects.

=head1 METHODS

Following is a list of class and instance methods.
(Instance methods until otherwise mentioned.)

There is no C<PAR::Repository::Query> object.

=cut

=head2 query_module

Polls the repository for modules matching certain criteria.
Takes named arguments. Either a C<regex> or a C<name> parameter
must be present but not both.

Returns a reference to an array containing alternating distribution
file names and module versions. This method returns the following
structure

  [ 'Foo-Bar-0.01-any_arch-5.8.7.par', '0.01', ... ]

that means the module was found in the distribution
F<Foo-Bar-0.01-any_arch-5.8.7.par> and the copy in that file has version
0.01.

Parameters:

=over 2

=item B<name>

The name of the module to look for. This is used for an exact match.
If you want to find C<Foo> in C<Foo::Bar>, use the C<regex> parameter.
Only one of C<name> and C<regex> may be specified.

=item B<regex>

Same as C<name>, but interpreted as a regular expression.
Only one of C<name> and C<regex> may be specified.

=item B<arch>

Can be used to reduce the number of matches to a specific architecture.
Always interpreted as a regular expression.

=back

=cut

sub query_module {
    my $self = shift;
#    $self->verbose(2, "Entering query_module()");
    croak("query_module() called with uneven number of arguments.")
      if @_ % 2;
    my %args = @_;
    
    my $name = $args{name};
    my $regex = $args{regex};

    if (defined $name and defined $regex) {
        croak("query_module() accepts only one of 'name' and 'regex' parameters.");
    }
    elsif (not defined $name and not defined $regex) {
        croak("query_module() needs one of 'name' and 'regex' parameters.");
    }
    elsif (defined $name) {
        $regex = qr/^\Q$name\E$/;
    }
    else { # regex defined
        $regex = qr/$regex/ if not ref($regex) eq 'Regexp';
    }

    my ($modh, $modfile) = $self->modules_dbm
      or die("Could not get modules DBM.");

    my @modules;
    
    my $arch_regex = $args{arch};
    $arch_regex = qr/$arch_regex/
      if defined $arch_regex and not ref($arch_regex) eq 'Regexp';
    
    # iterate over all modules in the mod_dbm hash
    while (my ($mod_name, $dists) = each(%$modh)) {
        # skip non-matching
        next if $mod_name !~ $regex;
        
        if (defined $arch_regex) {
            while (my ($distname, $version) = each(%$dists)) {
                (undef, undef, my $arch, undef)
                  = PAR::Dist::parse_dist_name($distname);
                next if $arch !~ $arch_regex;
                push @modules, [$distname, $version];
            }
        }
        else {
            while (my ($distname, $version) = each(%$dists)) {
                push @modules, [$distname, $version];
            }
        }
    }
    
    my %seen;
    # sort return list alphabetically
    return [
        map  { @$_ }
        sort { $a->[0] cmp $b->[0] }
        grep { not $seen{$_->[0] . '|' . $_->[1]}++ }
        @modules
    ];
}

=head2 query_script

Note: Usually, you probably want to use C<query_script_hash()>
instead. The usage of both methods is very similar (and described
right below), but the data structure returned differes somewhat.

Polls the repository for scripts matching certain criteria.
Takes named arguments. Either a C<regex> or a C<name> parameter
must be present but not both.

Returns a reference to an array containing alternating distribution
file names and script versions. This method returns the following
structure

  [ 'Foo-Bar-0.01-any_arch-5.8.7.par', '0.01', ... ]

that means the script was found in the distribution
F<Foo-Bar-0.01-any_arch-5.8.7.par> and the copy in that file has version
0.01.

Parameters:

=over 2

=item B<name>

The name of the script to look for. This is used for an exact match.
If you want to find C<foo> in C<foobar>, use the C<regex> parameter.
Only one of C<name> and C<regex> may be specified.

=item B<regex>

Same as C<name>, but interpreted as a regular expression.
Only one of C<name> and C<regex> may be specified.

=item B<arch>

Can be used to reduce the number of matches to a specific architecture.
Always interpreted as a regular expression.

=back

=cut

# FIXME: factor out common code from query_script and query_module!
sub query_script {
    my $self = shift;
#    $self->verbose(2, "Entering query_script()");

    my $scripts = $self->query_script_hash(@_);

    my %seen;
    # sort return list alphabetically
    return [
        map  { @$_ }
        sort { $a->[0] cmp $b->[0] }
        grep { not $seen{$_->[0] . '|' . $_->[1]}++ }
        map  {
          my $scripthash = $scripts->{$_};
          map { [$_, $scripthash->{$_}] } keys %$scripthash;
        }
        keys %$scripts
    ];
}


=head2 query_script_hash

Works exactly the same as C<query_script> except it returns
a different resulting structure which includes the matching
script's name:

  { 'fooscript' => { 'Foo-Bar-0.01-any_arch-5.8.7.par' => '0.01', ... }, ... }

that means the script C<fooscript> was found in the distribution
F<Foo-Bar-0.01-any_arch-5.8.7.par> and the copy in that file has version
0.01.

Parameters are the same as for C<query_script>

=cut

# FIXME: factor out common code from query_script_hash and query_module!
sub query_script_hash {
    my $self = shift;
#    $self->verbose(2, "Entering query_script_hash()");
    croak("query_script() or query_script_hash() called with uneven number of arguments.")
      if @_ % 2;
    my %args = @_;
    
    my $name = $args{name};
    my $regex = $args{regex};

    if (defined $name and defined $regex) {
        croak("query_script() or query_script_hash() accepts only one of 'name' and 'regex' parameters.");
    }
    elsif (not defined $name and not defined $regex) {
        croak("query_script() or query_script_hash() needs one of 'name' and 'regex' parameters.");
    }
    elsif (defined $name) {
        $regex = qr/^\Q$name\E$/;
    }
    else { # regex defined
        $regex = qr/$regex/ if not ref($regex) eq 'Regexp';
    }

    my ($scrh, $scrfile) = $self->scripts_dbm
      or die("Could not get scripts DBM.");

    my %scripts;
    
    my $arch_regex = $args{arch};
    $arch_regex = qr/$arch_regex/
      if defined $arch_regex and not ref($arch_regex) eq 'Regexp';
    
    # iterate over all scripts in the scripts hash
    while (my ($scr_name, $dists) = each(%$scrh)) {
        # skip non-matching
        next if $scr_name !~ $regex;
        
        while (my ($distname, $version) = each(%$dists)) {
            if (defined $arch_regex) {
                (undef, undef, my $arch, undef)
                  = PAR::Dist::parse_dist_name($distname);
                next if $arch !~ $arch_regex;
            }
            $scripts{$scr_name} = {} if not exists $scripts{$scr_name};
            $scripts{$scr_name}{$distname} = $version; # distname => version
        }
    }

    return \%scripts;    
}



=head2 query_dist

Polls the repository for distributions matching certain criteria.
Takes named arguments. Either a C<regex> or a C<name> parameter
must be present but not both.

Returns a reference to an array containing alternating distribution
file names and hash references. The hashes contain module names
and associated versions in the distribution.
This method returns the following structure

  [
    'Foo-Bar-0.01-any_arch-5.8.7.par',
    {Foo::Bar => '0.01', Foo::Bar::Baz => '0.02'},
    ...
  ]

that means the distribution F<Foo-Bar-0.01-any_arch-5.8.7.par> matched and
that distribution contains the modules C<Foo::Bar> and C<Foo::Bar::Baz>
with versions 0.01 and 0.02 respectively.

Parameters:

=over 2

=item B<name>

The name of the distribution to look for. This is used for an exact match.
If you want to find C<Foo> in C<Foo-Bar-0.01-any_arch-5.8.8.par>,
use the C<regex> parameter.
Only one of C<name> and C<regex> may be specified.

=item B<regex>

Same as C<name>, but interpreted as a regular expression.
Only one of C<name> and C<regex> may be specified.

=item B<arch>

Can be used to reduce the number of matches to a specific architecture.
Always interpreted as a regular expression.

=back

=cut

sub query_dist {
    my $self = shift;
#    $self->verbose(2, "Entering query_dist()");
    croak("query_dist() called with uneven number of arguments.")
      if @_ % 2;
    my %args = @_;
    
    my $name = $args{name};
    my $regex = $args{regex};

    if (defined $name and defined $regex) {
        croak("query_dist() accepts only one of 'name' and 'regex' parameters.");
    }
    elsif (not defined $name and not defined $regex) {
        croak("query_dist() needs one of 'name' and 'regex' parameters.");
    }
    elsif (defined $name) {
        $regex = qr/^\Q$name\E$/;
    }
    else { # regex defined
        $regex = qr/$regex/ if not ref($regex) eq 'Regexp';
    }

    my ($modh, $modfile) = $self->modules_dbm
      or die("Could not get modules DBM.");

    my %dists;
    
    my $arch_regex = $args{arch};
    $arch_regex = qr/$arch_regex/
      if defined $arch_regex and not ref($arch_regex) eq 'Regexp';
    
    # iterate over all modules in the mod_dbm hash
    while (my ($mod_name, $this_dists) = each(%$modh)) {
        # get the distributions for the module
        my $this_dists = $modh->{$mod_name};
        
        while (my ($dist_name, $dist) = each(%$this_dists)) {
            # skip non-matching
            next if $dist_name !~ $regex;
            
            # skip non-matching archs
            if (defined $arch_regex) {
                (undef, undef, my $arch, undef)
                  = PAR::Dist::parse_dist_name($dist_name);
                next if $arch !~ $arch_regex;
            }
            
            $dists{$dist_name}{$mod_name} = $dist;
        }
    }
    
    # sort return list alphabetically
    return [
        map  { @$_ }
        sort { $a->[0] cmp $b->[0] }
        map  { [$_, $dists{$_}] }
        keys %dists
    ];
}



1;
__END__

=head1 AUTHOR

Steffen Müller, E<lt>smueller@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006-2009 by Steffen Müller

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.6 or,
at your option, any later version of Perl 5 you may have available.

=cut