]> git.donarmstrong.com Git - wannabuild.git/blobdiff - bin/wanna-build
Split the previous state and state change in 2 lines
[wannabuild.git] / bin / wanna-build
index 002d4521aae60fce335cf0dedb7c8365ce067591..a9a2f69bb5a969ea7deccf509e705b4f1267063f 100755 (executable)
@@ -24,9 +24,8 @@ package conf;
 $basedir ||= "/var/lib/debbuild";
 $dbbase ||= "build-db";
 $transactlog ||= "transactions.log";
-#$mailprog ||= "/usr/sbin/sendmail";
-$mailprog = "/bin/true";
-require "/etc/wanna-build.conf";
+$mailprog ||= "/usr/sbin/sendmail";
+require "/org/wanna-build/etc/wanna-build.conf";
 die "$conf::basedir is not a directory\n" if ! -d $conf::basedir;
 die "dbbase is empty\n" if ! $dbbase;
 die "transactlog is empty\n" if ! $transactlog;
@@ -40,11 +39,15 @@ use POSIX;
 use FileHandle;
 use File::Copy;
 use DBI;
+use lib '/org/wanna-build/bin';
 use WannaBuild;
+use YAML::Tiny;
+use Data::Dumper;
+use Hash::Merge qw ( merge );
 
 our ($verbose, $mail_logs, $list_order, $list_state,
     $curr_date, $op_mode, $user, $real_user, $distribution,
-    $fail_reason, $opt_override, $import_from, $opt_create_db,
+    $fail_reason, $opt_override, $import_from, $export_to, $opt_create_db,
     %prioval, %sectval,
     $info_all_dists, $arch,
     $category, %catval, %short_category,
@@ -53,6 +56,7 @@ our ($verbose, $mail_logs, $list_order, $list_state,
 
 # global vars
 $ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin:/org/wanna-build/bin/";
+$ENV{'LC_ALL'} = 'C';
 $verbose = 0;
 $mail_logs = "";
 @curr_time = gmtime;
@@ -118,6 +122,7 @@ my %options =
         "merge-sources" => { mode => "merge-sources" },
         "pretend-avail" => { short => "p", mode => "pretend-avail" },
         "merge-all"     => { mode => "merge-all" },
+        "merge-all-secondary" => { mode => "merge-all-secondary" },
         info                   => { short => "i", mode => "info" },
         'binNMU' => { mode => 'set-binary-nmu', arg => \$binNMUver, 
                             code => sub { die "Invalid binNMU version: $binNMUver\n"
@@ -155,7 +160,7 @@ my %options =
         { short => "O", arg => \$list_order,
           code => sub {
                   die "Bad ordering character\n"
-                          if $list_order !~ /^[PSpsncb]+$/;
+                          if $list_order !~ /^[PSpsncbCW]+$/;
           } },
         message        => { short => "m", arg => \$fail_reason },
         # database is deprecated, use arch instead.
@@ -173,15 +178,15 @@ my %options =
                                                 code => sub {
                                                         die "Argument of --min-age must be a non-zero number\n"
                                                                 if $list_min_age == 0;
-                                                        $list_min_age *= 24*60*60;
                                                 } },
         "max-age"      => { arg => \$list_min_age,
                                                 code => sub {
                                                         die "Argument of --max-age must be a non-zero number\n"
                                                                 if $list_min_age == 0;
-                                                        $list_min_age *= -24*60*60;
+                                                        $list_min_age *= -1;
                                                 } },
         # special actions
+        export         => { arg => \$export_to, mode => "export" },
         import         => { arg => \$import_from, mode => "import" },
         "manual-edit"  => { mode => "manual-edit" },
         );
@@ -251,7 +256,7 @@ if ($verbose) {
        print "wanna-build $version for $distribution on $arch\n";
 }
 
-if (!@ARGV && !isin( $op_mode, qw(list merge-quinn merge-partial-quinn import 
+if (!@ARGV && !isin( $op_mode, qw(list merge-quinn merge-partial-quinn import export
                                  merge-packages manual-edit
                                  merge-sources))) {
        warn "No packages given.\n";
@@ -293,6 +298,22 @@ if (!$fail_reason) {
        }
 }
 
+my $yamlmap = ();
+my $yamldir = "/org/wanna-build/etc/yaml";
+my @files = ('wanna-build.yaml');
+if ($user =~ /(buildd.*)-/) { push (@files, "$1.yaml") };
+push ( @files, "$user.yaml");
+foreach my $file (@files) {
+       if ($verbose >= 2) { print "Trying to read $file ...\n"; }
+       next unless -f $yamldir."/".$file;
+       if ($verbose >= 2) { print "Read $file ...\n"; }
+       my $m = YAML::Tiny->read( $yamldir."/".$file )->[0];
+       $yamlmap = merge($m, $yamlmap);
+}
+if (not $yamlmap) {
+       die "FATAL: no configuration found\n";
+}
+
 my $dbh;
 
 END {
@@ -302,8 +323,17 @@ END {
        }
 }
 
-$dbh = DBI->connect("DBI:Pg:database=wanna-build") || 
-       die "FATAL: Cannot open database: $DBI::errstr\n";
+my $schema_suffix = '';
+if (isin( $op_mode, qw(list info)) && $distribution !~ /security/ && !(not -t and $user =~ /-/)) {
+       $dbh = DBI->connect("DBI:Pg:service=wanna-build") || 
+               die "FATAL: Cannot open database: $DBI::errstr\n";
+       $schema_suffix = '_public';
+}
+else
+{
+       $dbh = DBI->connect("DBI:Pg:service=wanna-build-privileged") || 
+               die "FATAL: Cannot open database: $DBI::errstr\n";
+}
 
 # TODO: This shouldn't be needed, file a bug.
 $dbh->{pg_server_prepare} = 0;
@@ -355,6 +385,7 @@ sub process {
                        die "This operation is restricted to admin users\n"
                                if (defined @conf::admin_users and
                                    !isin( $real_user, @conf::admin_users));
+                       lock_table();
                        parse_quinn_diff(1);
                        last SWITCH;
                };
@@ -362,6 +393,7 @@ sub process {
                        die "This operation is restricted to admin users\n"
                                if (defined @conf::admin_users and
                                    !isin( $real_user, @conf::admin_users));
+                       lock_table();
                        parse_quinn_diff(0);
                        last SWITCH;
                };
@@ -369,13 +401,15 @@ sub process {
                        die "This operation is restricted to admin users\n"
                                if (defined @conf::admin_users and
                                    !isin( $real_user, @conf::admin_users));
-                       parse_packages();
+                       lock_table();
+                       parse_packages(0);
                        last SWITCH;
                };
                /^merge-sources/ && do {
                        die "This operation is restricted to admin users\n"
                                if (defined @conf::admin_users and
                                    !isin( $real_user, @conf::admin_users));
+                       lock_table();
                        parse_sources(0);
                        last SWITCH;
                };
@@ -383,13 +417,14 @@ sub process {
                        pretend_avail( @ARGV );
                        last SWITCH;
                };
-               /^merge-all/ && do {
+               /^merge-all$/ && do {
                        die "This operation is restricted to admin users\n"
                                if (defined @conf::admin_users and
                                    !isin( $real_user, @conf::admin_users));
+                       lock_table();
                        my @ARGS = @ARGV;
                        @ARGV = ( $ARGS[0] );
-                       my $pkgs = parse_packages();
+                       my $pkgs = parse_packages(0);
                        @ARGV = ( $ARGS[1] );
                        parse_quinn_diff(0);
                        @ARGV = ( $ARGS[2] );
@@ -397,6 +432,29 @@ sub process {
                        call_edos_depcheck( $ARGS[0], $srcs );
                        last SWITCH;
                };
+               /^merge-all-secondary/ && do {
+                       die "This operation is restricted to admin users\n"
+                               if (defined @conf::admin_users and
+                                   !isin( $real_user, @conf::admin_users));
+                       # This is in case the chroot has multiple unrelated
+                       # dist, for instance unstable and experimental.
+                       # This is not for stable and proposed-updates.
+                       # The second packages file contains a combination
+                       # of all Packages files known to the buildd, the
+                       # first only for the current dist.
+                       lock_table();
+                       my @ARGS = @ARGV;
+                       @ARGV = ( $ARGS[0] );
+                       my $pkgs = parse_packages(0);
+                       @ARGV = ( $ARGS[3] );
+                       my $pkgs = parse_packages(1);
+                       @ARGV = ( $ARGS[1] );
+                       parse_quinn_diff(0);
+                       @ARGV = ( $ARGS[2] );
+                       my $srcs = parse_sources(1);
+                       call_edos_depcheck( $ARGS[3], $srcs );
+                       last SWITCH;
+               };
                /^import/ && do {
                        die "This operation is restricted to admin users\n"
                                if (defined @conf::admin_users and
@@ -409,6 +467,10 @@ sub process {
                        read_db( $import_from );
                        last SWITCH;
                };
+               /^export/ && do {
+                       export_db( $export_to );
+                       last SWITCH;
+               };
 
                die "Unexpected operation mode $op_mode\n";
        }
@@ -630,7 +692,7 @@ sub add_one_attempted {
                return;
        }
 
-       if ($pkg->{'state'} ne "Building" ) {
+       if (($pkg->{'state'} ne "Building") && ($pkg->{'state'} ne "Build-Attempted")) {
                print "$name: not taken for building (state is $pkg->{'state'}). ",
                          "Skipping.\n";
                return;
@@ -662,7 +724,7 @@ sub add_one_built {
                return;
        }
 
-       if ($pkg->{'state'} ne "Building" ) {
+        if (($pkg->{'state'} ne "Building") && ($pkg->{'state'} ne "Build-Attempted")) {
                print "$name: not taken for building (state is $pkg->{'state'}). ",
                          "Skipping.\n";
                return;
@@ -928,7 +990,8 @@ sub add_one_needsbuild {
                }
        }
        if (defined ($pkg->{'builder'}) && $user ne $pkg->{'builder'} &&
-                   !($pkg->{'builder'} =~ /^(\w+)-\w+/ && $1 eq $user)) {
+               !($pkg->{'builder'} =~ /^(\w+)-\w+/ && $1 eq $user) &&
+               !$opt_override) {
                print "$name: not taken by you, but by ".
                          "$pkg->{'builder'}. Skipping.\n";
                return;
@@ -1007,8 +1070,14 @@ sub set_one_binnmu {
                return;
        }
 
-       change_state( \$pkg, 'BD-Uninstallable' );
-       $pkg->{'bd_problem'} = "Installability of build dependencies not tested yet";
+       if ($distribution eq "unstable") {
+               change_state( \$pkg, 'BD-Uninstallable' );
+               $pkg->{'bd_problem'} = "Installability of build dependencies not tested yet";
+       }
+       else
+       {
+               change_state( \$pkg, 'Needs-Build' );
+       }
        delete $pkg->{'builder'};
        delete $pkg->{'depends'};
        $pkg->{'binary_nmu_version'} = $binNMUver;
@@ -1137,6 +1206,8 @@ sub parse_sources {
        my $name;
        my $full = shift;
 
+       my $db = get_all_source_info();
+
        local($/) = ""; # read in paragraph mode
        while( <> ) {
                my( $version, $arch, $section, $priority, $builddep, $buildconf, $binaries );
@@ -1157,7 +1228,7 @@ sub parse_sources {
                $pkgs{$name}{'bin'} = $binaries;
                $pkgs{$name}{'dep'} = $builddep;
                $pkgs{$name}{'conf'} = $buildconf;
-               my $pkg = get_source_info($name);
+               my $pkg = $db->{$name};
 
                if (defined $pkg) {
                        my $change = 0;
@@ -1170,6 +1241,7 @@ sub parse_sources {
                                          "from database, because now Arch: all\n"
                                                  if $verbose;
                                del_source_info($name);
+                               delete $db->{$name};
                                next;
                        }
 
@@ -1197,7 +1269,6 @@ sub parse_sources {
         }
        # remove installed packages that no longer have source available
        # or binaries installed
-        my $db = get_all_source_info();
         foreach $name (keys %$db) {
                next if $name =~ /^_/;
                my $pkg = $db->{$name};
@@ -1209,6 +1280,7 @@ sub parse_sources {
                                  "not in Sources anymore\n"
                                          if $verbose;
                        del_source_info($name);
+                       delete $db->{$name};
                } else {
                        next if !isin( $pkg->{'state'}, qw(Installed) );
                        if ($full && not defined $merge_srcvers{$name}) {
@@ -1219,6 +1291,7 @@ sub parse_sources {
                                       "binaries don't exist anymore\n"
                                               if $verbose;
                            del_source_info($name);
+                           delete $db->{$name};
                         } elsif ($full && version_less( $merge_srcvers{$name}, $pkg->{'version'})) {
                             print "$name ($pkg->{'version'}): ".
                                       "package is Installed but binaries are from ".
@@ -1233,8 +1306,10 @@ sub parse_sources {
 # This function looks through a Packages file and sets the state of
 # packages to 'Installed'
 sub parse_packages {
+       my $depwait_only = shift;
        my $installed;
 
+       my $pkgs = get_all_source_info();
        local($/) = ""; # read in paragraph mode
        while( <> ) {
                my( $name, $version, $depends, $source, $sourcev, $architecture, $provides, $binaryv, $binnmu );
@@ -1250,6 +1325,7 @@ sub parse_packages {
                next if (defined ($installed->{$name}) and $installed->{$name}{'version'} ne "" and
                        version_lesseq( $version, $installed->{$name}{'version'} ));
                $installed->{$name}{'version'} = $version;
+               next if $depwait_only;
                $installed->{$name}{'depends'} = $depends;
                $installed->{$name}{'all'} = 1 if $architecture eq "all";
                undef $installed->{$name}{'Provider'};
@@ -1276,7 +1352,7 @@ sub parse_packages {
                next if defined($merge_srcvers{$name}) and $merge_srcvers{$name} eq $version;
                $merge_srcvers{$name} = $version;
 
-               my $pkg = get_source_info($name);
+               my $pkg = $pkgs->{$name};
 
                if (defined $pkg) {
                        if (isin( $pkg->{'state'}, qw(Not-For-Us)) ||
@@ -1425,6 +1501,8 @@ sub parse_quinn_diff {
        my %quinn_pkgs;
        my $dubious = "";
        
+       my $pkgs = get_all_source_info();
+
        while( <> ) {
                my $change = 0;
                next if !m,^([-\w\d/]*)/                        # section
@@ -1439,7 +1517,7 @@ sub parse_quinn_diff {
                $priority = "unknown" if $priority eq "-";
                $priority = "standard" if ($name eq "debian-installer");
 
-               my $pkg = get_source_info($name);
+               my $pkg = $pkgs->{$name};
 
                # Always update section and priority.
                if (defined($pkg)) {
@@ -1686,6 +1764,16 @@ sub sort_list_func {
 
        foreach $letter (split( "", $list_order )) {
          SWITCH: foreach ($letter) {
+                 /C/ && do {
+                       $x = $b->{'calprio'} <=> $a->{'calprio'};
+                       return $x if $x != 0;
+                       last SWITCH;
+                 };
+                 /W/ && do {
+                       $x = $b->{'state_days'} <=> $a->{'state_days'};
+                       return $x if $x != 0;
+                       last SWITCH;
+                 };
                  /P/ && do {
                        $x = $b->{'buildpri'} <=> $a->{'buildpri'};
                        return $x if $x != 0;
@@ -1743,6 +1831,27 @@ sub sort_list_func {
        return 0;
 }
 
+sub calculate_prio {
+       my $priomap = $yamlmap->{priority};
+       my $pkg = shift;
+       $pkg->{'calprio'} = 0;
+       foreach my $k (keys %{$priomap->{keys}}) {
+               $pkg->{'calprio'} += $priomap->{keys}->{$k}{$pkg->{$k}} if $pkg->{$k} and $priomap->{keys}->{$k}{$pkg->{$k}};
+       }
+
+       my $days = $pkg->{'state_days'};
+       $days = $priomap->{'waitingdays'}->{'min'} if $priomap->{'waitingdays'}->{'min'} and $days < $priomap->{'waitingdays'}->{'min'};
+       $days = $priomap->{'waitingdays'}->{'max'} if $priomap->{'waitingdays'}->{'max'} and $days > $priomap->{'waitingdays'}->{'max'};
+       my $scale = $priomap->{'waitingdays'}->{'scale'} || 1;
+       $pkg->{'calprio'} += $days * $scale;
+
+       $pkg->{'calprio'} += $pkg->{'permbuildpri'} if  $pkg->{'permbuildpri'};
+       $pkg->{'calprio'} += $pkg->{'buildpri'} if  $pkg->{'buildpri'};
+
+       return $pkg;
+}
+
+
 sub list_packages {
        my $state = shift;
        my( $name, $pkg, @list );
@@ -1750,19 +1859,10 @@ sub list_packages {
        my %scnt;
        my $ctime = time;
 
-       my $db = get_all_source_info();
+       my $db = get_all_source_info(state => $state, user => $user, category => $category, list_min_age => $list_min_age);
        foreach $name (keys %$db) {
                next if $name =~ /^_/;
-               $pkg = $db->{$name};
-               next if $state ne "all" && $pkg->{'state'} !~ /^\Q$state\E$/i;
-               next if $user && (lc($state) ne 'needs-build' and $pkg->{'builder'} ne $user);
-               next if $category && $pkg->{'state'} eq "Failed" &&
-                               $pkg->{'failed_category'} ne $category;
-               next if ($list_min_age > 0 &&
-                                ($ctime-parse_date($pkg->{'State-Change'})) < $list_min_age)||
-                               ($list_min_age < 0 &&
-                                ($ctime-parse_date($pkg->{'State-Change'})) > -$list_min_age);
-               push( @list, $pkg );
+               push @list, calculate_prio($db->{$name});
        }
 
        foreach $pkg (sort sort_list_func @list) {
@@ -1778,6 +1878,8 @@ sub list_packages {
                        if defined $pkg->{'buildpri'};
                print ":binNMU{" . $pkg->{'binary_nmu_version'} . "}"
                        if defined $pkg->{'binary_nmu_version'};
+               print ":calprio{". $pkg->{'calprio'}."}";
+               print ":days{". $pkg->{'state_days'}."}";
                print "]\n";
                print "  Reasons for failing:\n",
                          "    [Category: ",
@@ -1790,11 +1892,14 @@ sub list_packages {
                print "  Reasons for BD-Uninstallable:\n    ",
                          join("\n    ",split("\n",$pkg->{'bd_problem'})), "\n"
                        if $pkg->{'state'} eq "BD-Uninstallable";
-               print "  Previous state was $pkg->{'previous_state'} until ",
-                         "$pkg->{'State-Change'}\n"
+               print "  Previous state was $pkg->{'previous_state'}\n"
                        if $verbose && $pkg->{'previous_state'};
                print "  No previous state recorded\n"
                        if $verbose && !$pkg->{'previous_state'};
+               print "  State changed at $pkg->{'state_change'}\n"
+                       if $verbose && $pkg->{'state_change'};
+               print "  Previous state $pkg->{'previous_state'} left $pkg->{'state_time'} ago\n"
+                       if $verbose && $pkg->{'previous_state'};
                print "  Previous failing reasons:\n    ",
                      join("\n    ",split("\n",$pkg->{'old_failed'})), "\n"
                        if $verbose && $pkg->{'old_failed'};
@@ -1815,17 +1920,30 @@ sub info_packages {
        my @firstkeys = qw(package version builder state section priority
                                           installed_version previous_state state_change);
        my @dists = $info_all_dists ? keys %conf::distributions : ($distribution);
+       my %beautykeys = ( 'package' => 'Package', 'version' => 'Version', 'builder' => 'Builder',
+               'state' => 'State', 'section' => 'Section', 'priority' => 'Priority',
+               'installed_version' => 'Installed-Version', 'previous_state' => 'Previous-State',
+               'state_change' => 'State-Change',
+               'bd_problem' => 'BD-Problem', 
+               'binary_nmu_changelog' => 'Binary-NMU-Changelog', 'binary_nmu_version' => 'Binary-NMU-Version',
+               'buildpri' => 'BuildPri', 'depends' => 'Depends', 'failed' => 'Failed',
+               'failed_category' => 'Failed-Category', 'notes' => 'Notes',
+               'distribution' => 'Distribution', 'old_failed' => 'Old-Failed',
+               'permbuildpri' => 'PermBuildPri', 'rel' => 'Rel',
+               'calprio' => 'CalculatedPri', 'state_days' => 'State-Days'
+                        );
        
        foreach $name (@_) {
                $name =~ s/_.*$//; # strip version
                foreach $dist (@dists) {
                        my $pname = "$name" . ($info_all_dists ? "($dist)" : "");
                        
-                       $pkg = get_source_info($name);
+                       $pkg = get_readonly_source_info($name);
                        if (!defined( $pkg )) {
                                print "$pname: not registered\n";
                                next;
                        }
+                       $pkg = calculate_prio($pkg);
 
                        print "$pname:\n";
                        foreach $key (@firstkeys) {
@@ -1834,15 +1952,20 @@ sub info_packages {
                                chomp( $val );
                                $val = "\n$val" if isin( $key, qw(Failed Old-Failed));
                                $val =~ s/\n/\n    /g;
-                               printf "  %-20s: %s\n", $key, $val;
+                               my $print_key = $key;
+                               $print_key = $beautykeys{$print_key} if $beautykeys{$print_key};
+                               printf "  %-20s: %s\n", $print_key, $val;
                        }
                        foreach $key (sort keys %$pkg) {
                                next if isin( $key, @firstkeys );
                                my $val = $pkg->{$key};
+                               next if !defined($val);
                                chomp( $val );
                                $val = "\n$val" if isin( $key, qw(Failed Old-Failed));
                                $val =~ s/\n/\n    /g;
-                               printf "  %-20s: %s\n", $key, $val;
+                               my $print_key = $key;
+                               $print_key = $beautykeys{$print_key} if $beautykeys{$print_key};
+                               printf "  %-20s: %s\n", $print_key, $val;
                        }
                }
        }
@@ -1939,22 +2062,48 @@ sub check_entry {
                          join( "\n", map { "$_: $pkg->{$_}" } keys %$pkg ), "\n";
                die "Database entry lacks package or username field\n";
        }
-       if (!exists $pkg->{'version'}) {
-               die "Database entry for $pkg->{'package'} lacks Version: field\n";
-       }
        # if no State: field, generate one (for old db compat)
        if (!exists($pkg->{'state'})) {
                $pkg->{'state'} =
                        exists $pkg->{'failed'} ? 'Failed' : 'Building';
        }
+       if (!exists $pkg->{'version'} and $pkg->{'state'} ne 'Not-For-Us') {
+               die "Database entry for $pkg->{'package'} lacks Version: field\n";
+       }
        # check state field
        die "Bad state $pkg->{'state'} of package $pkg->{Package}\n"
                if !isin( $pkg->{'state'},
-                                 qw(Needs-Build Building Built Build-Attempted Uploaded Installed Dep-Wait
+                                 qw(Needs-Build Building Built Build-Attempted Uploaded Installed Dep-Wait Dep-Wait-Removed
                                         Failed Failed-Removed Not-For-Us BD-Uninstallable
                                         ) );
 }
 
+sub export_db {
+       my $file = shift;
+       my($name,$pkg,$key);
+
+       print "Writing ASCII database to $file..." if $verbose >= 1;
+       open( F, ">$file" ) or
+               die "Can't open export $file: $!\n";
+
+        my $db = get_all_source_info();
+        foreach $name (keys %$db) {
+               next if $name =~ /^_/;
+               my $pkg = $db->{$name};
+               foreach $key (keys %{$pkg}) {
+                       my $val = $pkg->{$key};
+                       next if !defined($val);
+                       $val =~ s/\n*$//;
+                       $val =~ s/^/ /mg;
+                       $val =~ s/^ +$/ ./mg;
+                       print F "$key: $val\n";
+               }
+               print F "\n";
+       }
+       close( F );
+       print "done\n" if $verbose >= 1;
+}
+
 sub change_state {
        my $pkgr = shift;
        my $pkg = $$pkgr;
@@ -1963,7 +2112,8 @@ sub change_state {
        
        return if defined($$state) and $$state eq $newstate;
         $pkg->{'previous_state'} = $$state if defined($$state);
-       $pkg->{'State-Change'} = $curr_date;
+       $pkg->{'state_change'} = $curr_date;
+       $pkg->{'do_state_change'} = 1;
 
        if (defined($$state) and $$state eq 'Failed') {
                $pkg->{'old_failed'} =
@@ -1991,13 +2141,13 @@ sub log_ta {
                   "changed from $prevstate to $pkg->{'state'} ".
                   "by $real_user as $user";
        
-       my $transactlog = db_transactlog();
-       if (!open( LOG, ">>$transactlog" )) {
-               warn "Can't open log file $transactlog: $!\n";
-               return;
-       }
-       print LOG "$curr_date: $str\n";
-       close( LOG );
+       $dbh->do('INSERT INTO ' . transactions_table_name() .
+                       ' (package, distribution, version, action, ' .
+                       ' prevstate, state, real_user, set_user, time) ' .
+                       ' values (?, ?, ?, ?, ?, ?, ?, ?, ?)',
+               undef, $pkg->{'package'}, $distribution,
+               $pkg->{'version'}, $action, $prevstate, $pkg->{'state'},
+               $real_user, $user, 'now()') or die $dbh->errstr;
 
        if (!($prevstate eq 'Failed' && $pkg->{'state'} eq 'Failed')) {
                $str .= " (with --override)"
@@ -2029,10 +2179,6 @@ sub send_mail {
        close( PIPE );
 }
 
-sub db_transactlog {
-       return "$conf::basedir/$arch/$conf::transactlog";
-}
-
 # for parsing input to dep-wait
 sub parse_deplist {
     my $deps = shift;
@@ -2069,59 +2215,6 @@ sub parse_deplist {
     return \%result;
 }
 
-# for parsing Build-Depends from Sources
-sub parse_srcdeplist {
-    my $pkg = shift;
-    my $deps = shift;
-    my $arch = shift;
-    my $dep;
-    my @results;
-    
-    foreach $dep (split( /\s*,\s*/, $deps )) {
-       my @alts = split( /\s*\|\s*/, $dep );
-        # Anything with an | is ignored, as it can be configured on a
-        # per-buildd basis what will be installed
-        next if $#alts != 0;
-       $_ = shift @alts;
-        if (!/^([^\s([]+)\s*(\(\s*([<=>]+)\s*(\S+)\s*\))?(\s*\[([^]]+)\])?/) {
-            warn( "parse_srcdeplist: bad dependency $_\n" );
-            next;
-        }
-        my($dep, $rel, $relv, $archlist) = ($1, $3, $4, $6);
-        if ($archlist) {
-            $archlist =~ s/^\s*(.*)\s*$/$1/;
-            my @archs = split( /\s+/, $archlist );
-            my ($use_it, $ignore_it, $include) = (0, 0, 0);
-            foreach (@archs) {
-                if (/^!/) {
-                    $ignore_it = 1 if substr($_, 1) eq $arch;
-                } else {
-                    $use_it = 1 if $_ eq $arch;
-                    $include = 1;
-                }
-            }
-            warn "Warning: inconsistent arch restriction on ",
-                 "$pkg: $dep depedency\n"
-                 if $ignore_it && $use_it;
-            next if $ignore_it || ($include && !$use_it);
-        }
-        my $neg = 0;
-        if ($dep =~ /^!/) {
-            $dep =~ s/^!\s*//;
-            $neg = 1;
-        }
-        my $result;
-        $result->{'package'} = $dep;
-        $result->{'Neg'} = $neg;
-        if ($rel && $relv) {
-            $result->{'rel'} = $rel;
-            $result->{'version'} = $relv;
-        }
-        push @results, $result;
-    }
-    return \@results;
-}
-
 sub build_deplist {
        my $list = shift;
        my($key, $result);
@@ -2135,83 +2228,6 @@ sub build_deplist {
        return $result;
 }
 
-sub get_unsatisfied_dep {
-    my $bd  = shift;
-    my $pkgs = shift;
-    my $dep = shift;
-    my $savedep = shift;
-
-    my $pkgname = $dep->{'package'};
-
-    if (defined $pkgs->{$pkgname}{'Provider'}) {
-        # provides.  leave them for buildd/sbuild.
-        return "";
-    }
-
-    # check cache
-    return $pkgs->{$pkgname}{'Unsatisfied'} if $savedep and defined($pkgs->{$pkgname}{'Unsatisfied'});
-
-    # Return unsatisfied deps to a higher caller to process
-    if ((!defined($pkgs->{$pkgname})) or
-        (defined($dep->{'rel'}) and !version_compare( $pkgs->{$pkgname}{'version'}, $dep->{'rel'}, $dep->{'Version'} ) ) ) {
-        my %deplist;
-        $deplist{$pkgname} = $dep;
-        my $deps = build_deplist(\%deplist);
-        $pkgs->{$pkgname}{'Unsatisfied'} = $deps if $savedep;
-        return $deps;
-    }
-
-    # set cache to "" to avoid infinite recursion
-    $pkgs->{$pkgname}{'Unsatisfied'} = "" if $savedep;
-
-    if (defined $pkgs->{$dep->{'package'}}{'depends'}) {
-        my $deps = parse_deplist( $pkgs->{$dep->{'package'}}{'depends'} );
-        foreach (keys %$deps) {
-            $dep = $$deps{$_};
-            # recur on dep.
-            my $ret = get_unsatisfied_dep($bd,$pkgs,$dep,1);
-            if ($ret ne "") {
-                my $retdep = parse_deplist( $ret );
-                foreach (keys %$retdep) {
-                    $dep = $$retdep{$_};
-
-                    $dep->{'rel'} = '>=' if defined($dep->{'rel'}) and $dep->{'rel'} =~ '^=';
-
-                    if (defined($dep->{'rel'}) and $dep->{'rel'} =~ '^>' and defined ($pkgs->{$dep->{'package'}}) and
-                        version_compare($bd->{$pkgs->{$dep->{'package'}}{'Source'}}{'ver'},'>>',$pkgs->{$dep->{'package'}}{'Sourcev'})) {
-                        if (not defined($merge_binsrc{$dep->{'package'}})) {
-                            # the uninstallable package doesn't exist in the new source; look for something else that does.
-                            delete $$retdep{$dep->{'package'}};
-                            foreach (sort (split( /\s*,\s*/, $bd->{$pkgs->{$dep->{'package'}}{'Source'}}{'bin'}))) {
-                                next if ($pkgs->{$_}{'all'} or not defined $pkgs->{$_}{'version'});
-                                $dep->{'package'} = $_;
-                                $dep->{'rel'} = '>>';
-                                $dep->{'version'} = $pkgs->{$_}{'Version'};
-                                $$retdep{$_} = $dep;
-                                last;
-                            }
-                        }
-                    } else {
-                        # sanity check to make sure the depending binary still exists, and the depended binary exists and dep-wait on a new version of it
-                        if ( defined($merge_binsrc{$pkgname}) and defined($pkgs->{$dep->{'package'}}{'version'}) ) {
-                            delete $$retdep{$dep->{'package'}};
-                            $dep->{'package'} = $pkgname;
-                            $dep->{'rel'} = '>>';
-                            $dep->{'version'} = $pkgs->{$pkgname}{'Version'};
-                            $$retdep{$pkgname} = $dep;
-                        }
-                        delete $$retdep{$dep->{'package'}} if (defined ($dep->{'rel'}) and $dep->{'rel'} =~ '^>');
-                    }
-                }
-                $ret = build_deplist($retdep);
-                $pkgs->{$pkgname}{'Unsatisfied'} = $ret if $savedep;
-                return $ret;
-            }
-        }
-    }
-    return "";
-}
-
 sub call_edos_depcheck {
     my $packagesfile = shift;
     my $srcs = shift;
@@ -2244,7 +2260,7 @@ sub call_edos_depcheck {
 
     open SOURCES, '>', $tmpfile or die "Could not open temporary file $tmpfile\n";
     for my $key (keys %interesting_packages) {
-       my $pkg = get_source_info($key);
+       my $pkg = $db->{$key};
        print SOURCES "Package: $key\n";
        print SOURCES "Version: $pkg->{'version'}\n";
        print SOURCES "Build-Depends: $srcs->{$key}{'dep'}\n" if $srcs->{$key}{'dep'};
@@ -2280,7 +2296,7 @@ sub call_edos_depcheck {
     unlink( $tmpfile );
 
     for my $key (keys %interesting_packages) {
-       my $pkg = get_source_info($key);
+       my $pkg = $db->{$key};
        my $change = 
            (defined $interesting_packages{$key} and $pkg->{'state'} eq 'Needs-Build') ||
            (not defined $interesting_packages{$key} and $pkg->{'state'} eq 'BD-Uninstallable');
@@ -2315,6 +2331,7 @@ sub usage {
 Usage: $prgname <options...> <package_version...>
 Options:
     -v, --verbose: Verbose execution.
+    -A arch: Architecture this operation is for.
     --take: Take package for building [default operation]
     -f, --failed: Record in database that a build failed due to
         deficiencies in the package (that aren't fixable without a new
@@ -2352,6 +2369,7 @@ Options:
         if -l is missing, set user name to be entered in db; usually
         automatically choosen
     --import FILE: Import database from a ASCII file FILE
+    --export FILE: Export database to a ASCII file FILE
 
 The remaining arguments (depending on operation) usually start with
 "name_version", the trailer is ignored. This allows to pass the names
@@ -2375,25 +2393,73 @@ sub pkg_version_eq {
 }
 
 sub table_name {
-       return $arch;
+       return '"' . $arch . $schema_suffix . '".packages';
 }
 
 sub user_table_name {
-       return $arch . '_users';
+       return '"' . $arch . $schema_suffix . '".users';
 }
 
-sub get_source_info {
+sub transactions_table_name {
+       return '"' . $arch . $schema_suffix . '".transactions';
+}
+
+sub get_readonly_source_info {
        my $name = shift;
-       my $pkg = $dbh->selectrow_hashref('SELECT * FROM ' . 
+       # SELECT FLOOR(EXTRACT('epoch' FROM age(localtimestamp, '2010-01-22  23:45')) / 86400) -- change to that?
+       my $pkg = $dbh->selectrow_hashref('SELECT *, extract(days from date_trunc(\'days\', now() - state_change::timestamp)) as state_days FROM ' . 
                table_name() . ' WHERE package = ? AND distribution = ?',
                undef, $name, $distribution);
        return $pkg;
 }
 
+sub get_source_info {
+       my $name = shift;
+       my $pkg = $dbh->selectrow_hashref('SELECT *, extract(days from date_trunc(\'days\', now() - state_change::timestamp)) as state_days FROM ' . 
+               table_name() . ' WHERE package = ? AND distribution = ?' .
+               ' FOR UPDATE',
+               undef, $name, $distribution);
+       return $pkg;
+}
+
 sub get_all_source_info {
-       my $db = $dbh->selectall_hashref('SELECT * FROM ' . table_name() .
-               ' WHERE distribution = ?',
-               'package', undef, $distribution);
+       my %options = @_;
+
+       my $q = 'SELECT *, '.
+               'extract(days from date_trunc(\'days\', now() - state_change::timestamp)) as state_days, '.
+               'date_trunc(\'seconds\', now() - state_change::timestamp) as state_time'.
+               ' FROM ' . table_name()
+               . ' WHERE distribution = ? ';
+       my @args = ($distribution);
+       if ($options{state} && uc($options{state}) ne "ALL") {
+               $q .= ' AND upper(state) = ? ';
+               push @args, uc($options{state});
+       }
+
+       if ($options{user}) {
+               #this basically means "this user, or no user at all":
+               $q .= ' AND (builder = ? OR upper(state) = ?)';
+               push @args, $options{user};
+               push @args, "NEEDS-BUILD";
+       }
+
+       if ($options{category}) {
+               $q .= ' AND failed_category <> ? AND upper(state) = ? ';
+               push @args, $options{category};
+               push @args, "FAILED";
+       }
+
+       if ($options{list_min_age} > 0) {
+               $q .= ' AND age(state_change::timestamp) > ? ';
+               push @args, $options{list_min_age} . " days";
+       }
+
+       if ($options{list_min_age} < 0) {
+               $q .= ' AND age(state_change::timestamp) < ? ';
+               push @args, -$options{list_min_age} . " days";
+       }
+
+       my $db = $dbh->selectall_hashref($q, 'package', undef, @args);
        return $db;
 }
 
@@ -2413,7 +2479,7 @@ sub update_source_info {
                        'priority = ?, ' .
                        'installed_version = ?, ' .
                        'previous_state = ?, ' .
-                       'state_change = ?, ' .
+                       (($pkg->{'do_state_change'}) ? "state_change = now()," : "").
                        'notes = ?, ' .
                        'builder = ?, ' .
                        'failed = ?, ' .
@@ -2434,7 +2500,6 @@ sub update_source_info {
                $pkg->{'priority'},
                $pkg->{'installed_version'},
                $pkg->{'previous_state'},
-               $pkg->{'state_change'},
                $pkg->{'notes'},
                $pkg->{'builder'},
                $pkg->{'failed'},
@@ -2492,3 +2557,9 @@ sub add_user_info {
                or die $dbh->errstr;
 }
 
+sub lock_table()
+{
+       $dbh->do('LOCK TABLE ' . table_name() .
+               ' IN EXCLUSIVE MODE', undef) or die $dbh->errstr;
+}
+