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 confess("get_info_from_db not implemented without schema");
198 my %src_ver_packages;
200 my %bin_ver_packages;
201 # split packages into src/ver, bin/ver, src, and bin so we can select them
204 for my $pkg (@packages) {
206 if ($pkg->[0] =~ /^src:(.+)$/) {
207 for my $ver (@{$pkg}[1..$#{$pkg}]) {
208 $src_ver_packages{$1}{$ver} = 0;
211 for my $ver (@{$pkg}[1..$#{$pkg}]) {
212 $bin_ver_packages{$pkg->[0]}{$ver} = 0;
215 } elsif ($pkg =~ /^src:(.+)$/) {
216 $src_packages{$1} = 0;
218 $bin_packages{$pkg} = 0;
221 # calculate searches for packages where we want specific versions. We
222 # calculate this here so add_result_to_package can stomp over
223 # %src_ver_packages and %bin_ver_packages
225 for my $sp (keys %src_ver_packages) {
226 push @src_ver_search,
227 (-and => {'src_pkg.pkg' => $sp,
228 'me.ver' => [keys %{$src_ver_packages{$sp}}],
232 my @src_packages = keys %src_packages;
235 for my $sp (keys %bin_ver_packages) {
236 push @bin_ver_search,
237 (-and => {'bin_pkg.pkg' => $sp,
238 'me.ver' => [keys %{$bin_ver_packages{$sp}}],
242 my @bin_packages = keys %bin_packages;
244 sub _default_pkg_info {
245 return {name => $_[0],
246 type => $_[1]//'source',
248 valid_version_info => [],
249 invalid_versions => {},
252 sub add_result_to_package {
253 my ($pkgs,$rs,$svp,$bvp,$sp,$bp) = @_;
254 while (my $pkg = $rs->next) {
255 my $n = 'src:'.$pkg->{src_pkg};
256 if (not exists $pkgs->{$n}) {
258 _default_pkg_info($pkg->{src_pkg});
260 push @{$pkgs->{$n}{valid_version_info}},
262 $n = $pkg->{bin_pkg};
263 if (not exists $pkgs->{$n}) {
265 _default_pkg_info($pkg->{bin_pkg},'binary');
267 push @{$pkgs->{$n}{valid_version_info}},
269 # this is a package with a valid src_ver
270 $svp->{$pkg->{src_pkg}}{$pkg->{src_ver}}++;
271 $sp->{$pkg->{src_pkg}}++;
272 # this is a package with a valid bin_ver
273 $bvp->{$pkg->{bin_pkg}}{$pkg->{bin_ver}}++;
274 $bp->{$pkg->{bin_pkg}}++;
278 my $src_rs = $s->resultset('SrcVer')->
279 search({-or => [-and => {'src_pkg.pkg' => [@src_packages],
280 -or => {'suite.codename' => $common_dists,
281 'suite.suite_name' => $common_dists,
289 'src_associations' => 'suite'},
291 'bin_vers' => ['bin_pkg','arch']},
294 'select' => [qw(src_pkg.pkg),
296 qw(suite.suite_name),
297 qw(src_associations.modified),
299 q(CONCAT(src_pkg.pkg,'/',me.ver)),
300 qw(bin_vers.ver bin_pkg.pkg arch.arch),
303 'as' => [qw(src_pkg codename suite_name),
304 qw(modified_time src_ver src_pkg_ver),
305 qw(bin_ver bin_pkg arch maintainer),
307 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
310 add_result_to_package($packages,$src_rs,
319 $s->resultset('BinAssociation')->
320 search({-and => {'bin_pkg.pkg' => [@bin_packages],
321 -or => {'suite.codename' => $common_dists,
322 'suite.suite_name' => $common_dists,
326 [{'src_ver' => ['src_pkg',
333 'select' => [qw(src_pkg.pkg),
335 qw(suite.suite_name),
338 q(CONCAT(src_pkg.pkg,'/',src_ver.ver)),
339 qw(bin.ver bin_pkg.pkg arch.arch),
342 'as' => [qw(src_pkg codename suite_name),
343 qw(modified_time src_ver src_pkg_ver),
344 qw(bin_ver bin_pkg arch maintainer),
346 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
349 add_result_to_package($packages,$bin_assoc_rs,
356 if (@bin_ver_search) {
357 my $bin_rs = $s->resultset('BinVer')->
358 search({-or => [@bin_ver_search,
363 'bin_associations' => 'suite'},
364 {'src_ver' => ['src_pkg',
369 'select' => [qw(src_pkg.pkg),
371 qw(suite.suite_name),
372 qw(bin_associations.modified),
374 q(CONCAT(src_pkg.pkg,'/',src_ver.ver)),
375 qw(me.ver bin_pkg.pkg arch.arch),
378 'as' => [qw(src_pkg codename suite_name),
379 qw(modified_time src_ver src_pkg_ver),
380 qw(bin_ver bin_pkg arch maintainer),
382 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
385 add_result_to_package($packages,$bin_rs,
392 for my $sp (keys %src_ver_packages) {
393 if (not exists $packages->{'src:'.$sp}) {
394 $packages->{'src:'.$sp} =
395 _default_pkg_info($sp,'source',0);
397 for my $sv (keys %{$src_ver_packages{$sp}}) {
398 next if $src_ver_packages{$sp}{$sv} > 0;
399 $packages->{'src:'.$sp}{invalid_versions}{$sv} = 1;
402 for my $bp (keys %bin_ver_packages) {
403 if (not exists $packages->{$bp}) {
405 _default_pkg_info($bp,'binary',0);
407 for my $bv (keys %{$bin_ver_packages{$bp}}) {
408 next if $bin_ver_packages{$bp}{$bv} > 0;
409 $packages->{$bp}{invalid_versions}{$bv} = 1;
412 for my $sp (keys %src_packages) {
413 next if $src_packages{$sp} > 0;
414 $packages->{'src:'.$sp} =
415 _default_pkg_info($sp,'source',0);
417 for my $bp (keys %bin_packages) {
418 next if $bin_packages{$bp} > 0;
420 _default_pkg_info($bp,'binary',0);
425 has 'source_version_to_info' =>
426 (is => 'bare', isa => 'HashRef',
429 builder => '_build_source_version_to_info',
430 handles => {_get_source_version_to_info => 'get',
434 sub _build_source_version_to_info {
438 for my $v ($self->_valid_version_info) {
439 push @{$info->{$v->{src_ver}}}, $i;
445 has 'binary_version_to_info' =>
446 (is => 'bare', isa => 'HashRef',
449 builder => '_build_binary_version_to_info',
450 handles => {_get_binary_version_to_info => 'get',
454 sub _build_binary_version_to_info {
458 for my $v ($self->_valid_version_info) {
459 push @{$info->{$v->{bin_ver}}}, $i;
465 has 'dist_to_info' =>
466 (is => 'bare', isa => 'HashRef',
469 builder => '_build_dist_to_info',
470 handles => {_get_dist_to_info => 'get',
473 sub _build_dist_to_info {
477 for my $v ($self->_valid_version_info) {
478 next unless defined $v->{suite_name} and length($v->{suite_name});
479 push @{$info->{$v->{suite_name}}}, $i;
485 # this is a hashref of versions that we know are invalid
486 has 'invalid_versions' =>
487 (is => 'bare',isa => 'HashRef[Bool]',
490 clearer => '_clear_invalid_versions',
492 handles => {_invalid_version => 'exists',
493 _mark_invalid_version => 'set',
497 has 'binaries' => (is => 'ro',
498 isa => 'Debbugs::Collection::Package',
500 builder => '_build_binaries',
501 predicate => '_has_binaries',
504 sub _build_binaries {
506 if ($self->is_binary) {
507 return $self->package_collection->limit($self->name);
509 # OK, walk through the valid_versions for this package
511 uniq map {$_->{bin_pkg}} $self->_valid_version_info;
512 return $self->package_collection->limit(@binaries);
515 has 'sources' => (is => 'ro',
516 isa => 'Debbugs::Collection::Package',
518 builder => '_build_sources',
519 predicate => '_has_sources',
524 return $self->package_collection->limit($self->source_names);
530 if ($self->is_source) {
533 return uniq map {'src:'.$_->{src_pkg}} $self->_valid_version_info;
538 L<Debbugs::Collection::Correspondent> of the maintainer(s) of the current package
542 has maintainers => (is => 'ro',
543 isa => 'Debbugs::Collection::Correspondent',
545 builder => '_build_maintainers',
546 predicate => '_has_maintainers',
549 sub _build_maintainers {
552 for my $v ($self->_valid_version_info) {
553 next unless length($v->{suite_name}) and length($v->{maintainer});
554 push @maintainers,$v->{maintainer};
558 return $self->correspondent_collection->limit(@maintainers);
561 has 'versions' => (is => 'bare',
562 isa => 'HashRef[Debbugs::Version]',
564 handles => {_exists_version => 'exists',
565 _get_version => 'get',
566 _set_version => 'set',
569 builder => '_build_versions',
572 sub _build_versions {
584 $self->_set_version(@set);
587 sub get_source_version_distribution {
590 my %src_pkg_vers = @_;
594 $self->_get_dist_to_info($dist);
596 _get_valid_version_info(@ver_loc)) {
597 $src_pkg_vers{$v->{src_pkg_ver}} = 1;
600 return $self->package_collection->
601 get_source_versions(keys %src_pkg_vers)->members;
604 # returns the source version(s) corresponding to the version of *this* package; the
605 # version passed may be binary or source, depending.
606 sub get_source_version {
608 if ($self->is_source) {
609 return $self->get_version(@_);
616 ($ver,@archs) = @{$ver};
617 @archs{@archs} = (1) x @archs;
620 @{$self->_get_binary_version_to_info($ver)//[]};
621 next unless @ver_loc;
622 my @vers = map {$self->
623 _get_valid_version_info($_)}
627 next unless exists $archs{$v->{arch}};
629 $src_pkg_vers{$v->{src_pkg_ver}} = 1;
632 return $self->package_collection->
633 get_source_versions(keys %src_pkg_vers)->members;
640 if ($self->_exists_version($v)) {
641 push @ret,$self->_get_version($v);
644 $self->_create_version($v);
650 sub _create_version {
653 if ($self->is_source) {
657 Debbugs::Version::Source->
660 package_collection => $self->package_collection,
661 $self->schema_argument,
668 Debbugs::Version::Binary->
671 package_collection => $self->package_collection,
672 $self->schema_argument,
676 $self->_set_version(@versions);
679 =head2 package_collection
681 L<Debbugs::Collection::Package> to get additional packages required
685 # gets used to retrieve packages
686 has 'package_collection' => (is => 'ro',
687 isa => 'Debbugs::Collection::Package',
688 builder => '_build_package_collection',
692 sub _build_package_collection {
694 return Debbugs::Collection::Package->new($self->schema_argument)
697 =head2 correspondent_collection
699 L<Debbugs::Collection::Correspondent> to get additional maintainers required
703 has 'correspondent_collection' => (is => 'ro',
704 isa => 'Debbugs::Collection::Correspondent',
705 builder => '_build_correspondent_collection',
709 sub _build_correspondent_collection {
711 return Debbugs::Collection::Correspondent->new($self->schema_argument)
716 return 'Debbugs::Package={package='.$self->qualified_name.'}';
719 __PACKAGE__->meta->make_immutable;
727 # indent-tabs-mode: nil
728 # cperl-indent-level: 4