1 # This module is part of debbugs, and
2 # is released under the terms of the GPL version 3, or any later
3 # version (at your option). See the file README and COPYING for more
5 # Copyright 2018 by Don Armstrong <don@donarmstrong.com>.
7 package Debbugs::Package;
11 Debbugs::Package -- OO interface to packages
16 Debbugs::Package->new(schema => $s,binaries => [qw(foo)],sources => [qw(bar)]);
26 use v5.10; # for state
27 use namespace::autoclean;
29 use List::AllUtils qw(uniq pairmap);
30 use Debbugs::Config qw(:config);
31 use Debbugs::Version::Source;
32 use Debbugs::Version::Binary;
34 extends 'Debbugs::OOBase';
42 name if binary, name prefixed with C<src:> if source
46 has name => (is => 'ro', isa => 'Str',
53 # src: if source, nothing if binary
54 ($self->_type eq 'source' ? 'src:':'') .
61 Type of the package; either C<binary> or C<source>
65 has type => (is => 'bare', isa => 'Str',
67 builder => '_build_type',
68 clearer => '_clear_type',
70 writer => '_set_type',
75 if ($self->name !~ /^src:/) {
88 return $config{web_domain}.'/'.$self->qualified_name;
91 around BUILDARGS => sub {
95 if (@_==1 and ref($_[0]) eq 'HASH') {
100 $args{name} //= '(unknown)';
101 if ($args{name} =~ /src:(.+)/) {
103 $args{type} = 'source';
105 $args{type} = 'binary' unless
108 return $class->$orig(%args);
113 true if the package is a source package
117 true if the package is a binary package
122 return $_[0]->_type eq 'source'
126 return $_[0]->_type eq 'binary'
129 =head2 valid -- true if the package has any valid versions
133 has valid => (is => 'ro', isa => 'Bool',
135 builder => '_build_valid',
136 writer => '_set_valid',
141 if ($self->valid_version_info_count> 0) {
147 # this contains source name, source version, binary name, binary version, arch,
148 # and dist which have been selected from the database. It is used to build
149 # versions and anything else which are known as required.
150 has 'valid_version_info' =>
151 (is => 'bare', isa => 'ArrayRef',
154 builder => '_build_valid_version_info',
155 predicate => '_has_valid_version_info',
156 clearer => '_clear_valid_version_info',
157 handles => {'_get_valid_version_info' => 'get',
158 'valid_version_info_grep' => 'grep',
159 '_valid_version_info' => 'elements',
160 'valid_version_info_count' => 'count',
164 sub _build_valid_version_info {
166 my $pkgs = $self->_get_valid_version_info_from_db;
167 for my $invalid_version (@{$pkgs->{$self->qualified_name}->{invalid_versions}}) {
168 $self->_mark_invalid_version($invalid_version,1);
170 return $pkgs->{$self->qualified_name}->{valid_version_info} // [];
173 state $common_dists = [@{$config{distributions}}];
174 sub _get_valid_version_info_from_db {
176 if ((@_ % 2) == 1 and
184 if ($self->has_schema) {
189 @packages = $self->qualified_name;
192 @packages = @{$args{packages}};
194 if (not defined $s) {
195 # FIXME: Implement equivalent loader when there isn't a schema
196 confess("get_info_from_db not implemented without schema");
199 my %src_ver_packages;
201 my %bin_ver_packages;
202 # split packages into src/ver, bin/ver, src, and bin so we can select them
205 for my $pkg (@packages) {
207 if ($pkg->[0] =~ /^src:(.+)$/) {
208 for my $ver (@{$pkg}[1..$#{$pkg}]) {
209 $src_ver_packages{$1}{$ver} = 0;
212 for my $ver (@{$pkg}[1..$#{$pkg}]) {
213 $bin_ver_packages{$pkg->[0]}{$ver} = 0;
216 } elsif ($pkg =~ /^src:(.+)$/) {
217 $src_packages{$1} = 0;
219 $bin_packages{$pkg} = 0;
222 # calculate searches for packages where we want specific versions. We
223 # calculate this here so add_result_to_package can stomp over
224 # %src_ver_packages and %bin_ver_packages
226 for my $sp (keys %src_ver_packages) {
227 push @src_ver_search,
228 (-and => {'src_pkg.pkg' => $sp,
229 'me.ver' => [keys %{$src_ver_packages{$sp}}],
233 my @src_packages = keys %src_packages;
236 for my $sp (keys %bin_ver_packages) {
237 push @bin_ver_search,
238 (-and => {'bin_pkg.pkg' => $sp,
239 'me.ver' => [keys %{$bin_ver_packages{$sp}}],
243 my @bin_packages = keys %bin_packages;
245 sub _default_pkg_info {
246 return {name => $_[0],
247 type => $_[1]//'source',
249 valid_version_info => [],
250 invalid_versions => {},
253 sub add_result_to_package {
254 my ($pkgs,$rs,$svp,$bvp,$sp,$bp) = @_;
255 while (my $pkg = $rs->next) {
256 my $n = 'src:'.$pkg->{src_pkg};
257 if (not exists $pkgs->{$n}) {
259 _default_pkg_info($pkg->{src_pkg});
261 push @{$pkgs->{$n}{valid_version_info}},
263 $n = $pkg->{bin_pkg};
264 if (not exists $pkgs->{$n}) {
266 _default_pkg_info($pkg->{bin_pkg},'binary');
268 push @{$pkgs->{$n}{valid_version_info}},
270 # this is a package with a valid src_ver
271 $svp->{$pkg->{src_pkg}}{$pkg->{src_ver}}++;
272 $sp->{$pkg->{src_pkg}}++;
273 # this is a package with a valid bin_ver
274 $bvp->{$pkg->{bin_pkg}}{$pkg->{bin_ver}}++;
275 $bp->{$pkg->{bin_pkg}}++;
279 my $src_rs = $s->resultset('SrcVer')->
280 search({-or => [-and => {'src_pkg.pkg' => [@src_packages],
281 -or => {'suite.codename' => $common_dists,
282 'suite.suite_name' => $common_dists,
290 'src_associations' => 'suite'},
292 'bin_vers' => ['bin_pkg','arch']},
295 'select' => [qw(src_pkg.pkg),
297 qw(suite.suite_name),
298 qw(src_associations.modified),
300 q(CONCAT(src_pkg.pkg,'/',me.ver)),
301 qw(bin_vers.ver bin_pkg.pkg arch.arch),
304 'as' => [qw(src_pkg codename suite_name),
305 qw(modified_time src_ver src_pkg_ver),
306 qw(bin_ver bin_pkg arch maintainer),
308 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
311 add_result_to_package($packages,$src_rs,
320 $s->resultset('BinAssociation')->
321 search({-and => {'bin_pkg.pkg' => [@bin_packages],
322 -or => {'suite.codename' => $common_dists,
323 'suite.suite_name' => $common_dists,
327 [{'src_ver' => ['src_pkg',
334 'select' => [qw(src_pkg.pkg),
336 qw(suite.suite_name),
339 q(CONCAT(src_pkg.pkg,'/',src_ver.ver)),
340 qw(bin.ver bin_pkg.pkg arch.arch),
343 'as' => [qw(src_pkg codename suite_name),
344 qw(modified_time src_ver src_pkg_ver),
345 qw(bin_ver bin_pkg arch maintainer),
347 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
350 add_result_to_package($packages,$bin_assoc_rs,
357 if (@bin_ver_search) {
358 my $bin_rs = $s->resultset('BinVer')->
359 search({-or => [@bin_ver_search,
364 'bin_associations' => 'suite'},
365 {'src_ver' => ['src_pkg',
370 'select' => [qw(src_pkg.pkg),
372 qw(suite.suite_name),
373 qw(bin_associations.modified),
375 q(CONCAT(src_pkg.pkg,'/',src_ver.ver)),
376 qw(me.ver bin_pkg.pkg arch.arch),
379 'as' => [qw(src_pkg codename suite_name),
380 qw(modified_time src_ver src_pkg_ver),
381 qw(bin_ver bin_pkg arch maintainer),
383 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
386 add_result_to_package($packages,$bin_rs,
393 for my $sp (keys %src_ver_packages) {
394 if (not exists $packages->{'src:'.$sp}) {
395 $packages->{'src:'.$sp} =
396 _default_pkg_info($sp,'source',0);
398 for my $sv (keys %{$src_ver_packages{$sp}}) {
399 next if $src_ver_packages{$sp}{$sv} > 0;
400 $packages->{'src:'.$sp}{invalid_versions}{$sv} = 1;
403 for my $bp (keys %bin_ver_packages) {
404 if (not exists $packages->{$bp}) {
406 _default_pkg_info($bp,'binary',0);
408 for my $bv (keys %{$bin_ver_packages{$bp}}) {
409 next if $bin_ver_packages{$bp}{$bv} > 0;
410 $packages->{$bp}{invalid_versions}{$bv} = 1;
413 for my $sp (keys %src_packages) {
414 next if $src_packages{$sp} > 0;
415 $packages->{'src:'.$sp} =
416 _default_pkg_info($sp,'source',0);
418 for my $bp (keys %bin_packages) {
419 next if $bin_packages{$bp} > 0;
421 _default_pkg_info($bp,'binary',0);
426 has 'source_version_to_info' =>
427 (is => 'bare', isa => 'HashRef',
430 builder => '_build_source_version_to_info',
431 handles => {_get_source_version_to_info => 'get',
435 sub _build_source_version_to_info {
439 for my $v ($self->_valid_version_info) {
440 push @{$info->{$v->{src_ver}}}, $i;
446 has 'binary_version_to_info' =>
447 (is => 'bare', isa => 'HashRef',
450 builder => '_build_binary_version_to_info',
451 handles => {_get_binary_version_to_info => 'get',
455 sub _build_binary_version_to_info {
459 for my $v ($self->_valid_version_info) {
460 push @{$info->{$v->{bin_ver}}}, $i;
466 has 'dist_to_info' =>
467 (is => 'bare', isa => 'HashRef',
470 builder => '_build_dist_to_info',
471 handles => {_get_dist_to_info => 'get',
474 sub _build_dist_to_info {
478 for my $v ($self->_valid_version_info) {
479 next unless defined $v->{suite_name} and length($v->{suite_name});
480 push @{$info->{$v->{suite_name}}}, $i;
486 # this is a hashref of versions that we know are invalid
487 has 'invalid_versions' =>
488 (is => 'bare',isa => 'HashRef[Bool]',
491 clearer => '_clear_invalid_versions',
493 handles => {_invalid_version => 'exists',
494 _mark_invalid_version => 'set',
498 has 'binaries' => (is => 'ro',
499 isa => 'Debbugs::Collection::Package',
501 builder => '_build_binaries',
502 predicate => '_has_binaries',
505 sub _build_binaries {
507 if ($self->is_binary) {
508 return $self->package_collection->limit($self->name);
510 # OK, walk through the valid_versions for this package
512 uniq map {$_->{bin_pkg}} $self->_valid_version_info;
513 return $self->package_collection->limit(@binaries);
516 has 'sources' => (is => 'ro',
517 isa => 'Debbugs::Collection::Package',
519 builder => '_build_sources',
520 predicate => '_has_sources',
525 return $self->package_collection->limit($self->source_names);
531 if ($self->is_source) {
534 return uniq map {'src:'.$_->{src_pkg}} $self->_valid_version_info;
539 L<Debbugs::Collection::Correspondent> of the maintainer(s) of the current package
543 has maintainers => (is => 'ro',
544 isa => 'Debbugs::Collection::Correspondent',
546 builder => '_build_maintainers',
547 predicate => '_has_maintainers',
550 sub _build_maintainers {
553 for my $v ($self->_valid_version_info) {
554 next unless length($v->{suite_name}) and length($v->{maintainer});
555 push @maintainers,$v->{maintainer};
559 return $self->correspondent_collection->limit(@maintainers);
562 has 'versions' => (is => 'bare',
563 isa => 'HashRef[Debbugs::Version]',
565 handles => {_exists_version => 'exists',
566 _get_version => 'get',
567 _set_version => 'set',
570 builder => '_build_versions',
573 sub _build_versions {
585 $self->_set_version(@set);
588 sub get_source_version_distribution {
591 my %src_pkg_vers = @_;
595 $self->_get_dist_to_info($dist);
597 _get_valid_version_info(@ver_loc)) {
598 $src_pkg_vers{$v->{src_pkg_ver}} = 1;
601 return $self->package_collection->
602 get_source_versions(keys %src_pkg_vers)->members;
605 # returns the source version(s) corresponding to the version of *this* package; the
606 # version passed may be binary or source, depending.
607 sub get_source_version {
609 if ($self->is_source) {
610 return $self->get_version(@_);
617 ($ver,@archs) = @{$ver};
618 @archs{@archs} = (1) x @archs;
621 @{$self->_get_binary_version_to_info($ver)//[]};
622 next unless @ver_loc;
623 my @vers = map {$self->
624 _get_valid_version_info($_)}
628 next unless exists $archs{$v->{arch}};
630 $src_pkg_vers{$v->{src_pkg_ver}} = 1;
633 return $self->package_collection->
634 get_source_versions(keys %src_pkg_vers)->members;
641 if ($self->_exists_version($v)) {
642 push @ret,$self->_get_version($v);
645 $self->_create_version($v);
651 sub _create_version {
654 if ($self->is_source) {
658 Debbugs::Version::Source->
661 package_collection => $self->package_collection,
662 $self->schema_argument,
669 Debbugs::Version::Binary->
672 package_collection => $self->package_collection,
673 $self->schema_argument,
677 $self->_set_version(@versions);
680 =head2 package_collection
682 L<Debbugs::Collection::Package> to get additional packages required
686 # gets used to retrieve packages
687 has 'package_collection' => (is => 'ro',
688 isa => 'Debbugs::Collection::Package',
689 builder => '_build_package_collection',
693 sub _build_package_collection {
695 return Debbugs::Collection::Package->new($self->schema_argument)
698 =head2 correspondent_collection
700 L<Debbugs::Collection::Correspondent> to get additional maintainers required
704 has 'correspondent_collection' => (is => 'ro',
705 isa => 'Debbugs::Collection::Correspondent',
706 builder => '_build_correspondent_collection',
710 sub _build_correspondent_collection {
712 return Debbugs::Collection::Correspondent->new($self->schema_argument)
717 return 'Debbugs::Package={package='.$self->qualified_name.'}';
720 __PACKAGE__->meta->make_immutable;
728 # indent-tabs-mode: nil
729 # cperl-indent-level: 4