]> git.donarmstrong.com Git - wannabuild.git/blobdiff - bin/wanna-build
Don't requests all the packages again in the same function
[wannabuild.git] / bin / wanna-build
index f929b46fb338955e179ceb3dc521df431d95a05e..63416591d29f282ee54508259c971a0794a96a7b 100755 (executable)
@@ -25,7 +25,7 @@ $basedir ||= "/var/lib/debbuild";
 $dbbase ||= "build-db";
 $transactlog ||= "transactions.log";
 $mailprog ||= "/usr/sbin/sendmail";
-require "/etc/wanna-build.conf";
+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;
@@ -39,11 +39,12 @@ use POSIX;
 use FileHandle;
 use File::Copy;
 use DBI;
+use lib '/org/wanna-build/bin';
 use WannaBuild;
 
 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,
@@ -172,15 +173,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" },
         );
@@ -250,7 +251,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";
@@ -302,8 +303,7 @@ END {
 }
 
 my $schema_suffix = '';
-# TODO: Base this on something else, like an option that is passed.
-if ($real_user eq 'nobody') {
+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';
@@ -364,6 +364,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;
                };
@@ -371,6 +372,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;
                };
@@ -378,6 +380,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_packages();
                        last SWITCH;
                };
@@ -385,6 +388,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_sources(0);
                        last SWITCH;
                };
@@ -396,6 +400,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();
                        my @ARGS = @ARGV;
                        @ARGV = ( $ARGS[0] );
                        my $pkgs = parse_packages();
@@ -418,6 +423,10 @@ sub process {
                        read_db( $import_from );
                        last SWITCH;
                };
+               /^export/ && do {
+                       export_db( $export_to );
+                       last SWITCH;
+               };
 
                die "Unexpected operation mode $op_mode\n";
        }
@@ -937,7 +946,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;
@@ -1016,8 +1026,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;
@@ -1146,6 +1162,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 );
@@ -1166,7 +1184,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;
@@ -1179,6 +1197,7 @@ sub parse_sources {
                                          "from database, because now Arch: all\n"
                                                  if $verbose;
                                del_source_info($name);
+                               delete $db->{$name};
                                next;
                        }
 
@@ -1206,7 +1225,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};
@@ -1218,6 +1236,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}) {
@@ -1228,6 +1247,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 ".
@@ -1244,6 +1264,7 @@ sub parse_sources {
 sub parse_packages {
        my $installed;
 
+       my $pkgs = get_all_source_info();
        local($/) = ""; # read in paragraph mode
        while( <> ) {
                my( $name, $version, $depends, $source, $sourcev, $architecture, $provides, $binaryv, $binnmu );
@@ -1285,7 +1306,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)) ||
@@ -1434,6 +1455,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
@@ -1448,7 +1471,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)) {
@@ -1759,19 +1782,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, $db->{$name};
        }
 
        foreach $pkg (sort sort_list_func @list) {
@@ -1830,7 +1844,7 @@ sub info_packages {
                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;
@@ -1988,6 +2002,32 @@ sub check_entry {
                                         ) );
 }
 
+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;
@@ -2024,13 +2064,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)"
@@ -2062,10 +2102,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;
@@ -2277,7 +2313,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'};
@@ -2313,7 +2349,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');
@@ -2348,6 +2384,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
@@ -2385,6 +2422,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
@@ -2415,7 +2453,11 @@ sub user_table_name {
        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 ' . 
                table_name() . ' WHERE package = ? AND distribution = ?',
@@ -2423,10 +2465,50 @@ sub get_source_info {
        return $pkg;
 }
 
+sub get_source_info {
+       my $name = shift;
+       my $pkg = $dbh->selectrow_hashref('SELECT * 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 * 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;
 }
 
@@ -2525,3 +2607,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;
+}
+