+ if (defined $pkg->{'binary_nmu_version'}) and
+ version_compare(binNMU_version($pkg->{'version'},
+ $pkg->{'binary_nmu_version'}),'=', $version);
+ return version_compare( $pkg->{'version'}, "=", $version );
+}
+
+sub table_name {
+ return '"' . $arch . $schema_suffix . '".packages';
+}
+
+sub user_table_name {
+ return '"' . $arch . $schema_suffix . '".users';
+}
+
+sub transactions_table_name {
+ return '"' . $arch . $schema_suffix . '".transactions';
+}
+
+sub pkg_history_table_name {
+ return '"' . $arch . $schema_suffix . '".pkg_history';
+}
+
+sub get_readonly_source_info {
+ my $name = shift;
+ # SELECT FLOOR(EXTRACT('epoch' FROM age(localtimestamp, '2010-01-22 23:45')) / 86400) -- change to that?
+ my $q = "SELECT rel, priority, state_change, permbuildpri, section, buildpri, failed, state, binary_nmu_changelog, bd_problem, version, package, distribution, installed_version, notes, failed_category, builder, old_failed, previous_state, binary_nmu_version, depends, extract(days from date_trunc('days', now() - state_change)) as state_days, floor(extract(epoch from now()) - extract(epoch from state_change)) as state_time"
+ . ", (SELECT max(build_time) FROM ".pkg_history_table_name()." WHERE pkg_history.package = packages.package AND pkg_history.distribution = packages.distribution AND result = 'successful') AS successtime"
+ . ", (SELECT max(build_time) FROM ".pkg_history_table_name()." WHERE pkg_history.package = packages.package AND pkg_history.distribution = packages.distribution ) AS anytime"
+ . ", extra_depends, extra_conflicts, build_arch_all"
+ . " FROM " . table_name()
+ . ' WHERE package = ? AND distribution = ?';
+ my $pkg = $dbh->selectrow_hashref( $q,
+ undef, $name, $distribution);
+ return $pkg;
+}
+
+sub get_source_info {
+ my $name = shift;
+ return get_readonly_source_info($name) if $simulate;
+ my $pkg = $dbh->selectrow_hashref('SELECT *, extract(days from date_trunc(\'days\', now() - state_change)) as state_days, floor(extract(epoch from now()) - extract(epoch from state_change)) as state_time FROM ' .
+ table_name() . ' WHERE package = ? AND distribution = ?' .
+ ' FOR UPDATE',
+ undef, $name, $distribution);
+ return $pkg;
+}
+
+sub get_all_source_info {
+ my %options = @_;
+
+ my $q = "SELECT rel, priority, state_change, permbuildpri, section, buildpri, failed, state, binary_nmu_changelog, bd_problem, version, package, distribution, installed_version, notes, failed_category, builder, old_failed, previous_state, binary_nmu_version, depends, extract(days from date_trunc('days', now() - state_change)) as state_days, floor(extract(epoch from now()) - extract(epoch from state_change)) as state_time"
+# . ", (SELECT max(build_time) FROM ".pkg_history_table_name()." WHERE pkg_history.package = packages.package AND pkg_history.distribution = packages.distribution AND result = 'successful') AS successtime"
+# . ", (SELECT max(build_time) FROM ".pkg_history_table_name()." WHERE pkg_history.package = packages.package AND pkg_history.distribution = packages.distribution ) AS anytime"
+ . ", successtime.build_time as successtime, anytime.build_time as anytime, extra_depends, extra_conflicts"
+ . " FROM " . table_name()
+ . " left join ( "
+ . "select distinct on (package, distribution) build_time, package, distribution from ".pkg_history_table_name()." where result = 'successful' order by package, distribution, timestamp "
+ . " ) as successtime using (package, distribution) "
+ . " left join ( "
+ . "select distinct on (package, distribution) build_time, package, distribution from ".pkg_history_table_name()." order by package, distribution, timestamp desc"
+ . " ) as anytime using (package, distribution) "
+ . " WHERE TRUE ";
+ my @args = ();
+ if ($distribution) {
+ my @dists = split(/[, ]+/, $distribution);
+ $q .= ' AND ( distribution = ? '.(' OR distribution = ? ' x $#dists).' )';
+ foreach my $d ( @dists ) {
+ push @args, ($d);
+ }
+ }
+ if ($options{state} && uc($options{state}) ne "ALL") {
+ $q .= ' AND upper(state) = ? ';
+ push @args, uc($options{state});
+ }
+
+ if ($options{user} && uc($options{state}) ne "NEEDS-BUILD") { # if it's NEEDS-BUILD, we don't look at users
+ #this basically means "this user, or no user at all":
+ $q .= " AND (builder = ? OR upper(state) = 'NEEDS-BUILD')";
+ push @args, $options{user};
+ }
+
+ if ($options{category}) {
+ $q .= ' AND failed_category <> ? AND upper(state) = ? ';
+ push @args, $options{category};
+ push @args, "FAILED";
+ }
+
+ if ($options{list_min_age} && $options{list_min_age} > 0) {
+ $q .= ' AND age(state_change) > ? ';
+ push @args, $options{list_min_age} . " days";
+ }
+
+ if ($options{list_min_age} && $options{list_min_age} < 0) {
+ $q .= ' AND age(state_change) < ? ';
+ push @args, -$options{list_min_age} . " days";
+ }
+
+ my $db = $dbh->selectall_hashref($q, 'package', undef, @args);
+ return $db;
+}
+
+sub show_distribution_architectures {
+ my $q = 'SELECT distribution, spacecat_all(architecture) AS architectures '.
+ 'FROM distribution_architectures '.
+ 'GROUP BY distribution';
+ my $rows = $dbh->selectall_hashref($q, 'distribution');
+ foreach my $name (keys %$rows) {
+ print $name.': '.$rows->{$name}->{'architectures'}."\n";
+ }
+}
+
+sub show_distribution_aliases {
+ foreach my $alias (keys %distribution_aliases) {
+ print $alias.': '.$distribution_aliases{$alias}."\n";
+ }
+}
+
+sub update_source_info {
+ my $pkg = shift;
+ $pkg->{'extra_depends'} = $extra_depends if defined $extra_depends;
+ undef $pkg->{'extra_depends'} unless $pkg->{'extra_depends'};
+ $pkg->{'extra_conflicts'} = $extra_conflicts if defined $extra_conflicts;
+ undef $pkg->{'extra_conflicts'} unless $pkg->{'extra_conflicts'};
+ print Dumper $pkg if $verbose and $simulate;
+ return if $simulate;
+
+ my $pkg2 = get_source_info($pkg->{'package'});
+ if (! defined $pkg2)
+ {
+ add_source_info($pkg);
+ }
+
+ $dbh->do('UPDATE ' . table_name() . ' SET ' .
+ 'version = ?, ' .
+ 'state = ?, ' .
+ 'section = ?, ' .
+ 'priority = ?, ' .
+ 'installed_version = ?, ' .
+ 'previous_state = ?, ' .
+ (($pkg->{'do_state_change'}) ? "state_change = now()," : "").
+ 'notes = ?, ' .
+ 'builder = ?, ' .
+ 'failed = ?, ' .
+ 'old_failed = ?, ' .
+ 'binary_nmu_version = ?, ' .
+ 'binary_nmu_changelog = ?, ' .
+ 'failed_category = ?, ' .
+ 'permbuildpri = ?, ' .
+ 'buildpri = ?, ' .
+ 'depends = ?, ' .
+ 'rel = ?, ' .
+ 'extra_depends = ?, ' .
+ 'extra_conflicts = ?, ' .
+ 'bd_problem = ? ' .
+ 'WHERE package = ? AND distribution = ?',
+ undef,
+ $pkg->{'version'},
+ $pkg->{'state'},
+ $pkg->{'section'},
+ $pkg->{'priority'},
+ $pkg->{'installed_version'},
+ $pkg->{'previous_state'},
+ $pkg->{'notes'},
+ $pkg->{'builder'},
+ $pkg->{'failed'},
+ $pkg->{'old_failed'},
+ $pkg->{'binary_nmu_version'},
+ $pkg->{'binary_nmu_changelog'},
+ $pkg->{'failed_category'},
+ $pkg->{'permbuildpri'},
+ $pkg->{'buildpri'},
+ $pkg->{'depends'},
+ $pkg->{'rel'},
+ $pkg->{'extra_depends'},
+ $pkg->{'extra_conflicts'},
+ $pkg->{'bd_problem'},
+ $pkg->{'package'},
+ $distribution) or die $dbh->errstr;
+}
+
+sub add_source_info {
+ return if $simulate;
+ my $pkg = shift;
+ $dbh->do('INSERT INTO ' . table_name() .
+ ' (package, distribution) values (?, ?)',
+ undef, $pkg->{'package'}, $distribution) or die $dbh->errstr;
+}
+
+sub del_source_info {
+ return if $simulate;
+ my $name = shift;
+ $dbh->do('DELETE FROM ' . table_name() .
+ ' WHERE package = ? AND distribution = ?',
+ undef, $name, $distribution) or die $dbh->errstr;
+}
+
+sub get_user_info {
+ my $name = shift;
+ my $user = $dbh->selectrow_hashref('SELECT * FROM ' .
+ user_table_name() . ' WHERE username = ? AND distribution = ?',
+ undef, $name, $distribution);
+ return $user;
+}
+
+sub update_user_info {
+ return if $simulate;
+ my $user = shift;
+ $dbh->do('UPDATE ' . user_table_name() .
+ ' SET last_seen = now() WHERE username = ?' .
+ ' AND distribution = ?',
+ undef, $user, $distribution)
+ or die $dbh->errstr;
+}
+
+
+sub add_user_info {
+ return if $simulate;
+ my $user = shift;
+ $dbh->do('INSERT INTO ' . user_table_name() .
+ ' (username, distribution, last_seen)' .
+ ' values (?, ?, now())',
+ undef, $user, $distribution)
+ or die $dbh->errstr;
+}
+
+sub lock_table {
+ return if $simulate;
+ $dbh->do('LOCK TABLE ' . table_name() .
+ ' IN EXCLUSIVE MODE', undef) or die $dbh->errstr;
+}
+
+sub parse_argv {
+# parts the array $_[0] and $_[1] and returns the sub-array (modifies the original one)
+ my @ret = ();
+ my $args = shift;
+ my $separator = shift;
+ while($args->[0] && $args->[0] ne $separator) {
+ push @ret, shift @$args;
+ }
+ shift @$args if @$args;
+ return @ret;
+}
+
+sub parse_all_v3 {
+ my $srcs = shift;
+ my $vars = shift;
+ my $db = get_all_source_info();
+ my $binary = $srcs->{'_binary'};
+
+ SRCS:
+ foreach my $name (keys %$srcs) {
+ next if $name eq '_binary';
+
+ # state = installed, out-of-date, uncompiled, not-for-us, auto-not-for-us
+ my $pkgs = $srcs->{$name};
+ my $pkg = $db->{$name};
+
+ unless ($pkg) {
+ next SRCS if $pkgs->{'status'} eq 'not-for-us';
+ my $logstr = sprintf("merge-v3 %s %s_%s (%s, %s):", $vars->{'time'}, $name, $pkgs->{'version'}, $vars->{'arch'}, $vars->{'suite'});
+
+ # does at least one binary exist in the database and is more recent - if so, we're probably just outdated, ignore the source package
+ for my $bin (@{$pkgs->{'binary'}}) {
+ if ($binary->{$bin} and vercmp($pkgs->{'version'}, $binary->{$bin}->{'version'}) < 0) {
+ print "$logstr skipped because binaries (assumed to be) overwritten\n" if $verbose || $simulate;
+ next SRCS;
+ }
+ }
+ $pkg->{'package'} = $name;
+ }
+ my $logstr = sprintf("merge-v3 %s %s_%s", $vars->{'time'}, $name, $pkgs->{'version'}).
+ ($pkgs->{'binnmu'} ? ";b".$pkgs->{'binnmu'} : "").
+ sprintf(" (%s, %s, previous: %s", $vars->{'arch'}, $vars->{'suite'}, $pkg->{'version'}//"").
+ ($pkg->{'binary_nmu_version'} ? ";b".$pkg->{'binary_nmu_version'} : "").
+ ", $pkg->{'state'}):";
+
+ if (isin($pkgs->{'status'}, qw (installed related)) && $pkgs->{'version'} eq $pkg->{'version'} && $pkgs->{'binnmu'}//0 < int($pkg->{'binary_nmu_version'}//0)) {
+ $pkgs->{'status'} = 'out-of-date';
+ }
+ if (isin($pkgs->{'status'}, qw (installed related))) {
+ my $change = 0;
+ if ($pkg->{'state'} ne 'Installed') {
+ change_state( \$pkg, 'Installed');
+ delete $pkg->{'depends'};
+ delete $pkg->{'extra_depends'};
+ delete $pkg->{'extra_conflicts'};
+ $change++;
+ }
+ my $attrs = { 'version' => 'version', 'installed_version' => 'version', 'binary_nmu_version' => 'binnmu', 'section' => 'section', 'priority' => 'priority' };
+ foreach my $k (keys %$attrs) {
+ if (($pkg->{$k}//"") ne ($pkgs->{$attrs->{$k}}//"")) {
+ $pkg->{$k} = $pkgs->{$attrs->{$k}};
+ $change++;
+ }
+ }
+ if (isin($pkgs->{'status'}, qw (related)) and $pkg->{'notes'} ne "related") {
+ $pkg->{'notes'} = "related";
+ $change++;
+ }
+ if ($change) {
+ print "$logstr set to installed/".($pkg->{'notes'}//"")."\n" if $verbose || $simulate;
+ log_ta( $pkg, "--merge-v3: installed" ) unless $simulate;
+ update_source_info($pkg) unless $simulate;
+ }
+ next;
+ }
+
+ if ($pkgs->{'status'} eq 'not-for-us') {
+ next if isin( $pkg->{'state'}, qw(Not-For-Us Installed Failed-Removed));
+
+ if (isin( $pkg->{'state'}, qw(Failed Build-Attempted Built))) {
+ change_state( \$pkg, "Failed-Removed" );
+ log_ta( $pkg, "--merge-v3: Failed-Removed" ) unless $simulate;
+ update_source_info($pkg) unless $simulate;
+ print "$logstr (virtually) deleted from database\n" if $verbose || $simulate;
+ next;
+ }
+
+ print "$logstr should delete (not-for-us according to P-a-s)\n" if $verbose || $simulate || 1; # not implemented yet on purpose
+ next;
+ }
+
+ if ($pkgs->{'status'} eq 'auto-not-for-us') {
+ next if isin( $pkg->{'state'}, qw(Not-For-Us Failed Failed-Removed Dep-Wait Dep-Wait-Removed Auto-Not-For-Us));
+ # if the package is currently current, the status is Installed, not not-for-us
+
+ change_state( \$pkg, "Auto-Not-For-Us" );
+ log_ta( $pkg, "--merge-v3: Auto-Not-For-Us" ) unless $simulate;
+ update_source_info($pkg) unless $simulate;
+ print "$logstr set to auto-not-for-us\n" if $verbose || $simulate;
+ next SRCS;
+ }
+
+ # only uncompiled / out-of-date are left, so check if anything new
+ if (!(isin($pkgs->{'status'}, qw (uncompiled out-of-date)))) {
+ print "$logstr package in unknown state: $pkgs->{'status'}\n";
+ next SRCS;
+ }
+ next if $pkgs->{'version'} eq $pkg->{'version'} and $pkgs->{'binnmu'}//0 >= int($pkg->{'binary_nmu_version'}//0);
+ next if $pkgs->{'version'} eq $pkg->{'version'} and !isin( $pkg->{'state'}, qw(Installed));
+ next if isin( $pkg->{'state'}, qw(Not-For-Us Failed-Removed));
+
+ if (defined( $pkg->{'state'} ) && isin( $pkg->{'state'}, qw(Building Built Build-Attempted))) {
+ send_mail( $pkg->{'builder'},
+ "new version of $name (dist=$distribution)",
+ "As far as I'm informed, you're currently building the package $name\n".
+ "in version $pkg->{'version'}.\n\n".
+ "Now there's a new source version $pkgs->{'version'}. If you haven't finished\n".
+ "compiling $name yet, you can stop it to save some work.\n".
+ "Just to inform you...\n".
+ "(This is an automated message)\n" ) unless $simulate;
+ print "$logstr new version while building $pkg->{'version'} -- sending mail to builder ($pkg->{'builder'})\n"
+ if $verbose || $simulate;
+ }
+ change_state( \$pkg, 'Needs-Build');
+ $pkg->{'notes'} = $pkgs->{'status'};
+ $pkg->{'version'} = $pkgs->{'version'};
+ $pkg->{'section'} = $pkgs->{'section'};
+ $pkg->{'priority'} = $pkgs->{'priority'};
+ $pkg->{'dep'} = $pkgs->{'depends'};
+ $pkg->{'conf'} = $pkgs->{'conflicts'};
+ delete $pkg->{'builder'};
+ delete $pkg->{'binary_nmu_version'} unless $pkgs->{'binnmu'};
+ delete $pkg->{'binary_nmu_changelog'} unless $pkgs->{'binnmu'};
+ log_ta( $pkg, "--merge-v3: needs-build" ) unless $simulate;
+ update_source_info($pkg) unless $simulate;
+ print "$logstr set to needs-builds\n" if $simulate || $verbose;
+ }
+
+ foreach my $name (keys %$db) {
+ next if $srcs->{$name};
+ my $pkg = $db->{$name};
+ my $logstr = "merge-v3 $vars->{'time'} ".$name."_$pkg->{'version'} ($vars->{'arch'}, $vars->{'suite'}, previous: $pkg->{'state'}):";
+ # package disappeared - delete
+ change_state( \$pkg, 'deleted' );
+ log_ta( $pkg, "--merge-v3: deleted" ) unless $simulate;
+ print "$logstr deleted from database\n" if $verbose || $simulate;
+ del_source_info($name) unless $simulate;
+ delete $db->{$name};
+ }