build-gnome2
author dermotm
Mon, 24 Jul 2006 12:30:39 +0000
branchgnome-2-10
changeset 19949 94f49bf19351
parent 19482 b3bd7a1d14e0
permissions -rwxr-xr-x
access control test #4

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long qw(:config gnu_getopt no_auto_abbrev);
use rpm_spec;

# --------- defaults -------------------------------------------------------
my $default_spec_dir = `dirname $0`;
$default_spec_dir =~ s/\s//g;

# use "rpmbuild" (RPM v4) if present or "rpm" (RPM v3) if not
my $RPM_BUILD = 'rpmbuild';
my $cmd_found = `which rpmbuild | wc -l`;
if ($cmd_found == 0) {
	$RPM_BUILD = 'rpm';
}

my $default_log_dir = '/tmp';
my $default_tarball_dir = "/sgnome/tarballs/gnome2.10";
my $default_source_dirs = $default_spec_dir . "/ext-sources" .
			":" . $default_spec_dir . "/manpages/man1" .
			":" . $default_spec_dir . "/manpages/man3" .
			":" . $default_spec_dir . "/manpages/man4" .
			":" . $default_spec_dir . "/manpages/man5" .
                        ":" . "/sgnome/tarballs/moz-evo";
my $default_patch_dir = $default_spec_dir . "/patches";
my $default_sys_rpm_dir = "/usr/src/packages";
my $default_nightly_date_format = '%y%m%d';

# --------- global vars ----------------------------------------------------
# config settings
my $the_spec_dir = $default_spec_dir;
my $the_log_dir = $default_log_dir;
my $the_summary_log;
my $the_summary_title;
my $the_logdir_url;
my $the_rpm_url;
my $the_srpm_url;
my $the_source_dirs = $default_source_dirs;
my @the_source_dirlist;
my $the_tarball_dir = $default_tarball_dir;
my $the_good_build_dir;
my $the_good_rpms_copy_dir;
my $the_patch_dir = $default_patch_dir;
my $the_sys_rpm_dir = $default_sys_rpm_dir;
my $rpm_target = "i586";

my $check_deps = 1;
my $live_summary = 0;

my $build_command;
my $build_and_install = 1;
my $prep_only = 0;

my $exit_val = 0;
my $verbose = 1;
my $debug_level = 0;
my $full_path = 0;
my $halt_on_errors = 0;
my $mail_errors_file;
my $mail_errors_to;
my $mail_errors_cc;
my $prodname = "unnamed";
my $is_nightly = 0;
my $the_nightly_date_format = $default_nightly_date_format;
my $is_nodeps = 0;

# counter used as an id for the spec files
my $spec_id = 0;

# array of spec objects
my @specs_to_build = ();
my @build_status;
my @status_details;
my @remove_list = ();

my %provider;
my %warned_about;

# --------- utility functions ----------------------------------------------
our $opt_specdir = $default_spec_dir;

# return the name of the log file given the id of the spec file
sub get_log_name ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];
    my $spec_file = $spec->get_param ('file_name');
    my $base_name = $spec_file;
    $base_name =~ s/^.*\/([^\/]+)/$1/;
    my $log_name = "$base_name";
    $log_name =~ s/(\.spec|)$/.log/;

    return $log_name;
}

my $current_log;

sub open_log ($) {
    my $log_filename = shift;

    if (not defined ($log_filename)) {
	return;
    }

    if (defined ($current_log)) {
	if ($current_log ne $log_filename) {
	    close LOG_FILE;
	}
    }

    $current_log = undef;

    if (! open LOG_FILE, ">>$log_filename") {
	msg_warning (0, "Failed to open log file $log_filename");
	return;
    }

    $current_log = $log_filename;

    msg_log ("--- log starts --- " . `date`);
}

sub close_log () {
    msg_log ("--- log ends --- " . `date`);
    close LOG_FILE;
    $current_log = undef;
}

sub print_message ($$) {
    my $min_verbose = shift;
    my $message = shift;

    chomp $message;

    if ($verbose > $min_verbose) {
	print "$message\n";
    }
    if (defined ($current_log)) {
	print LOG_FILE "$message\n";
    }
}

sub msg_error ($) {
    my $message = shift;

    print_message (0, "ERROR: $message");
    if ($halt_on_errors > 0) {
	print "ERROR: Exiting...\n";
	exit (255);
    }
}

sub msg_warning ($$) {
    my $min_verbose = shift;
    my $message = shift;

    print_message ($min_verbose, "WARNING: $message");
}

sub msg_info ($$) {
    my $min_verbose = shift;
    my $message = shift;

    print_message ($min_verbose, "INFO: $message");
}

sub msg_debug ($$) {
    my $min_debug = shift;
    my $message = shift;

    if ($debug_level > $min_debug) {
	print "DEBUG: $message\n";
    }
}

sub msg_log ($) {
    my $message = shift;

    if (defined ($current_log)) {
	print LOG_FILE "$message\n";
    }
}

sub find_file ($) {
    my $glob = shift;

    my @files = `find $glob 2>&1`;
    if ($? == 0) {
	my $file = $files[$#files];
	chomp $file;
	return ($file);
    }

    return (undef);
}

sub mail_log ($) {
    my $spec_id = shift;
    my $address;
    my $cc;
    my $log_name = get_log_name ($spec_id);
    my $spec_name = $specs_to_build[$spec_id]->{name};
    my $base_name = $log_name;
    $log_name =~ s/\.log$//;

    if (defined ($mail_errors_file)) {
	$address = get_address_from_file ($mail_errors_file, $base_name);
    }
    
    if (not defined ($address)) {
	$address = $mail_errors_to;
    }

    if (not defined ($address)) {
	$address = $mail_errors_cc;
    } else {
	$cc = $mail_errors_cc;
    }

    if (not defined ($address)) {
	return;
    }

    my $log_file = get_log_name ($spec_id);
    msg_info (1, "mailing the build log to $address");

    my $subject;

    my $log_pointer;
    if (defined ($the_logdir_url)) {
	$log_pointer = "$the_logdir_url/${log_name}.log";
    } else {
	$log_pointer = "$the_log_dir/$log_file";
    }

    if (defined ($prodname)) {
	$subject = "BUILD FAILED ($prodname): $spec_name";
    } else {
	$subject = "BUILD FAILED: $spec_name";	
    }

    if (defined ($cc)) {
	`( echo "Full log: $log_pointer" ; echo; echo "--- tail of the log follows ---"; echo; tail -100 $the_log_dir/$log_file ) | mailx -s "$subject" -c "$cc" $address`;
    } else {
	`( echo "Full log: $log_pointer" ; echo; echo "--- tail of the log follows ---"; echo; tail -100 $the_log_dir/$log_file ) | mailx -s "$subject" $address`;
    }
}

sub get_address_from_file ($$) {
    my $fname = shift;
    my $pkg = shift;

    $pkg =~ s/\.spec$//;

    if (! open ADDR_FILE, "<$fname") {
        msg_warning (0, "Could not open file $fname");
	return undef;
    }
    my @lines = <ADDR_FILE>;
    my @addresses = grep /^$pkg:/, @lines;
    my $address = $addresses[0];
    if (not defined ($address)) {
	return undef;
    }
    $address =~ s/^$pkg://;
    $address =~ s/\s//g;
    close ADDR_FILE;
    return $address;
}

# --------- functions to process the command line args ---------------------
sub add_spec ($) {
    my $spec_name = shift;

    my $spec;

    if (-f $spec_name) {
	$spec = rpm_spec->new ($spec_name, $rpm_target);
    } else {
	if (not $spec_name =~ /\.spec$/) {
	    $spec_name = "${opt_specdir}/${spec_name}.spec";
	}
	$spec = rpm_spec->new ("$spec_name", $rpm_target);
    }

    if (not defined ($spec)) {
	msg_error ("$spec_name not found\n");
    } else {
	if ($is_nightly) {
	    $spec->add_define ("nightly", "1");
	}
	my $this_spec_id = $spec_id ++;
	$specs_to_build[$this_spec_id] = $spec;
	my @packages = $spec->get_param_array ('packages');

	$build_status[$this_spec_id] = 'NOT_BUILT';
	$status_details[$this_spec_id] = '';

	foreach my $pkg (@packages) {
	    my @provides = $spec->get_pkg_param_array ($pkg, 'provides');
	    foreach my $prov (@provides) {
		if (not defined $prov) {
		    next;
		}
		$prov =~ s/ .*//;
		$provider{$prov} = $this_spec_id;
	    }
	}
    }
}

sub set_quiet {
    $verbose = 0;
}

sub process_args {
    my $arg = shift;

    if (not defined ($build_command)) {
	if (($arg ne "build") and ($arg ne "build-order") and
	    ($arg ne "uninstall-pkgs") and
	    ($arg ne "build-install") and ($arg ne "build-only") and
	    ($arg ne "prep") and
	    ($arg ne "install-order")) {
	    msg_error ("unknown command: $arg");
	    usage (1);
	}
	$build_command = $arg;
    } else {
	add_spec ($arg);
    }
}

sub process_options {
    
  Getopt::Long::Configure ("bundling");
    
    our $opt_logdir = $default_log_dir;
    our $opt_sourcedirs = $default_source_dirs;
    our $opt_tarballdir = $default_tarball_dir;
    our $opt_good_build_dir;
    our $opt_good_rpms_copy_dir;
    our $opt_patchdir = $default_patch_dir;
    our $opt_rpmdir = $default_sys_rpm_dir;
    our $opt_deps = 1;
    our $opt_live_summary = 0;
    our $opt_summary_log;
    our $opt_summary_title;
    our $opt_logdir_url;
    our $opt_rpm_url;
    our $opt_srpm_url;
    our $opt_date = $default_nightly_date_format;
    
    GetOptions ('v|verbose+' => \$verbose,
		'debug=n' => \$debug_level,
		'q|quiet' => \&set_quiet,
		'specdir|spec|S=s',
		'halt-on-errors!', \$halt_on_errors,
		'mail-errors-to=s' => \$mail_errors_to,
		'mail-errors-cc=s' => \$mail_errors_cc,
		'mail-errors-file=s' => \$mail_errors_file,
		'prodname=s' => \$prodname,
		'sourcedirs|sourcedir|src|srcdirs|srcdir|sources|source|s=s',
		'logdir|log|l=s',
		'summary-log=s',
		'live-summary|live!',
		'summary-title=s',
		'logdir-url=s',
		'rpm-url=s',
		'srpm-url=s',
		'target=s' => \$rpm_target,
		'deps!',
		'tarballdir|tar|tarball|tardir|t=s',
		'good-build-dir=s',
		'good-rpms-copy-dir=s',
		'nightly!' => \$is_nightly,
		'date|date-format|nightly-date-format=s',
		'patchdir|patch|p=s',
		'rpmdir|rpm|r=s',
		'full-path' => \$full_path,
		'nodeps!' => \$is_nodeps,
		'help' => \&usage,
		'<>' => \&process_args);
    
    $the_spec_dir = $opt_specdir;
    $the_log_dir = $opt_logdir;
    $the_summary_log = $opt_summary_log;
    $the_summary_title = $opt_summary_title;
    $the_logdir_url = $opt_logdir_url;
    $the_rpm_url = $opt_rpm_url;
    $the_srpm_url = $opt_srpm_url;
    $the_source_dirs = $opt_sourcedirs;
    $the_tarball_dir = $opt_tarballdir;
    $the_patch_dir = $opt_patchdir;
    $the_sys_rpm_dir = $opt_rpmdir;
    $the_good_build_dir = $opt_good_build_dir;
    $the_good_rpms_copy_dir = $opt_good_rpms_copy_dir;
    $check_deps = $opt_deps;
    $live_summary = $opt_live_summary;
    $the_nightly_date_format = $opt_date;

    if ($is_nodeps) {
        $check_deps = 0; 
    }

    @the_source_dirlist = split /:/, $the_source_dirs;
}

sub usage (;$) {
    my $retval = shift;
    if (not defined ($retval)) {
	$retval = 0;
    }

    print << "EOF";
build-gnome2 [options] [command] specs...

Options:

    -v|--verbose:    
                  Increase verbosity: the more -v's the more diag messages

    -q|--quiet:
                  Silent operation.

    --specdir=dir, --spec=dir:
                  Use dir as the default directory for spec files.
                  Default: $default_spec_dir

    --tarballdir=dir, --tardir=dir, --tar=dir:
                  Search for source tarballs in dir.
                  Default: $default_tarball_dir

    --sourcedir=dir, --srcdir=dir, --src=dir:
                  Location of the additional sources referenced in the spec
                  files. Default: $default_spec_dir

    --patchdir=dir, --patch=dir:
                  Look for the patches referenced in the spec files in dir.
                  Default: $default_patch_dir
 
    --rpmdir=dir --rpm=dir:
                  Use dir as the rpm base directory (where the SPECS,
                  SOURCES, RPMS, SRPMS, BUILD directories are found).
                  Default: $default_sys_rpm_dir

    --logdir=dir, --log=dir:
                  Write build logs to dir. Default: $default_log_dir
                  
    --nodeps, --deps:
                  Ignore/verify dependencies before building a component.
                  Default: --deps

    --target=arch
		  If used, the same option will be passed to rpm when
		  building the package(s)

    --halt-on-errors:
                  Halt on the first build error, do not attempt to continue.

    --full-path:
                  Print the full path to the rpm when running install-order.

    --nightly, --nonightly:
                  Suffix/Don't suffix the rpm Release with the current date.
                  Default: --nonightly;

    --date-format=format, --date=format:
                  Use "date +format" to generate the date suffix for
                  the nightly builds. Default: $default_nightly_date_format


Commands:

    build          Build and install the specs listed on the command line

    build-only     Build the specs listed on the command line, don't install
                   them.

    prep           run the %prep section of the spec files listed on the
                   command line

    build-order    Print the build order of the specs listed on the
                   command line.

    install-order  Print the rpms in the order they should be installed
	
    install-pkgs   (not implemented yet): install the packages defined
                   by the spec files listed on the command line from
                   the PKGS directory.
	
    uninstall-pkgs Remove all rpms defined in the spec files listed
	           on the command line. (runs rpm --erase --nodeps)

specs...

    List of spec files to build. Either full path names or names of spec
    files in the default spec directory (with or without the .spec suffix).
EOF
    exit $retval;
}

# --------- print reports --------------------------------------------------
# plain text report on stdout and
# html report if summary log file name
# was specified on the command line
sub print_status {
    print_status_text ();
    print_status_html ();
}

sub print_live_status {
    if ($live_summary) {
	print_status_html ();
    }
}

sub print_status_text {
    my $spec_name;
    print "\nSummary:\n\n";
    printf "%20s | %10s | %s\n", "package", "status", "details";
    print "---------------------+------------+------------------------------------------\n";
    for (my $i = 0; $i <= $#specs_to_build; $i++) {
	$spec_name=$specs_to_build[$i]->{name};
	printf "%20s | %10s | %s\n", $spec_name, $build_status[$i], 
		$status_details[$i];
    }
}

sub print_status_html {
    my $spec_name;

    if (not defined ($the_summary_log)) {
	return;
    }

    if (! open SUM_LOG, ">$the_summary_log") {
	msg_warning (0, "Failed to open file $the_summary_log for writing");
	return;
    }
	
    print SUM_LOG "<HTML>\n<HEAD>\n<TITLE>Build report</TITLE>\n";
    print SUM_LOG "</HEAD>\n<BODY BGCOLOR=#FFFFFF>\n";
    if (defined ($the_summary_title)) {
	print SUM_LOG "<P><H3>$the_summary_title</H3><P>\n";
    }
    print SUM_LOG "<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5 WIDTH=90%>\n";
    print SUM_LOG " <TR>\n";
    print SUM_LOG "  <TD BGCOLOR=#666699><B>package</B></TD>\n";
    print SUM_LOG "  <TD BGCOLOR=#666699><B>version</B></TD>\n";
    print SUM_LOG "  <TD BGCOLOR=#666699><B>release</B></TD>\n";
    print SUM_LOG "  <TD BGCOLOR=#666699><B>status</B></TD>";
    print SUM_LOG "  <TD BGCOLOR=#666699><B>details</B></TD>\n";
    print SUM_LOG " </TR>\n";
    for (my $i = 0; $i <= $#specs_to_build; $i++) {
	my $log_name = get_log_name ($i);
	$spec_name=$specs_to_build[$i]->{name};
	my $version = $specs_to_build[$i]->{version};
	my $release = $specs_to_build[$i]->{release};
	print SUM_LOG " <TR>\n";
	if (defined ($the_srpm_url)) {
	    if (($build_status[$i] eq "PASSED") and defined ($the_srpm_url)) {
		print SUM_LOG "  <TD><A HREF=\"$the_srpm_url/$spec_name-$version-$release.src.rpm\">$spec_name</A></TD>\n";
	    } else {
		print SUM_LOG "  <TD>$spec_name</TD>\n";
	    }
	} else {
	    print SUM_LOG "  <TD>$spec_name</TD>\n";
	}
	print SUM_LOG "  <TD>", $version, "</TD>\n";
	print SUM_LOG "  <TD>", $release, "</TD>\n";
	my $color_start = "";
	my $color_end = "";
	if ($build_status[$i] eq "PASSED") {
	    $color_start = "<FONT COLOR=#11AA11>";
	    $color_end = "</FONT>";
	} elsif ($build_status[$i] eq "BEING_BUILT") {
	    $color_start = "<FONT COLOR=#FFA500>";
	    $color_end = "</FONT>";
	} elsif ($build_status[$i] eq "DEP") {
	    $color_start = "<FONT COLOR=#FFA500>";
	    $color_end = "</FONT>";
	} elsif ($build_status[$i] eq "NOT_BUILT") {
	    $color_start = "";
	    $color_end = "";
	} else {
	    $color_start = "<FONT COLOR=#CC1111>";
	    $color_end = "</FONT>";
	}
	if (defined ($the_logdir_url) and ($build_status[$i] ne "NOT_BUILT")) {
	    print SUM_LOG "  <TD><A HREF=\"$the_logdir_url/$log_name\">",
	    $color_start, $build_status[$i], $color_end,
	    "</A></TD>\n";
	} else {
	    print SUM_LOG "  <TD>", 
	    $color_start, $build_status[$i], $color_end,
	    "</A></TD>\n";
	}
	if ($build_status[$i] eq "PASSED") {
	    if (defined ($the_rpm_url)) {
		print SUM_LOG "  <TD>RPMS: \n";
		my @rpms = $specs_to_build[$i]-> get_param_array ('rpms');
		my $ctr = 1;
		for my $rpm (@rpms) {
		    print SUM_LOG "    <A HREF=\"$the_rpm_url/$rpm\">[$ctr]</a> \n";
		    $ctr++;
		}
		print SUM_LOG "  </TD>\n";
	    } else {
		print SUM_LOG "  <TD>&nbsp;</TD>\n";
	    }
	} else {
	    print SUM_LOG "  <TD><PRE>", $status_details[$i], "</PRE></TD>\n";
	}
	print SUM_LOG " </TR>\n";
    }
    print SUM_LOG "</TABLE><P>\n";
    print SUM_LOG "<SMALL>", `date`, "</SMALL>\n";
    print SUM_LOG "</BODY>\n</HTML>\n";
    close SUM_LOG;
}

# --------- rpm utility functions ------------------------------------------
sub is_provided ($) {
    my $capability = shift;

    $capability =~ s/\s.*//;
    `sh -c "rpm -q --whatprovides $capability" >/dev/null 2>&1`;
    my $result = (! $?);
    `sh -c "rpm -q $capability" >/dev/null 2>&1`;
    $result = ($result or (! $?));
    
    return ($result);
}

sub what_provides ($) {
    my $capability = shift;

    $capability =~ s/ .*//;
    my $rpm=`sh -c "rpm -q --whatprovides $capability" 2>&1`;
    if ($?) {
	$rpm=`sh -c "rpm -q $capability" 2>&1`;
    }
    chomp $rpm;
    return ($rpm);
}

sub spec_id_of ($) {
    my $name = shift;

    for (my $i = 0; $i <= $#specs_to_build; $i++) {
	if ($specs_to_build[$i]->{name} eq $name) {
	    return $i;
	}
    }

    return undef;
}

sub install_good_rpms ($) {
    my $spec_id = shift;

    return if not defined($spec_id);
    my $spec = $specs_to_build[$spec_id];

    my @rpms =  $spec->get_param_array ('rpms');
    map $_ =~ s/$spec->{version}-$spec->{release}.$spec->{buildarchitecture}.rpm/[0-9]*-[0-9]*.*.rpm/, @rpms;
    map $_ =~ s/^/$the_good_build_dir\//, @rpms;

    for (my $i = 0; $i <= $#rpms; $i++) {
	my $current_rpm = $rpms[$i];
	msg_info (1, "Looking for last known good rpm as $current_rpm");
	$rpms[$i] = find_file ($current_rpm);
	if (not defined ($rpms[$i])) {
	    msg_error ("No file matches $current_rpm");
	    return 0;
	} else {
	    msg_info (1, "Found $rpms[$i]");
	}
    }

    my $command = "rpm --upgrade -v @rpms";

    if ($verbose > 0) {
	map msg_info (0, "Installing last known good rpm: $_\n"), @rpms;
    }

    my $msg=`$command 2>&1`;

    if ($? > 0) {
	msg_error "failed to install last known good rpm: $msg";
	$status_details[$spec_id] = $status_details[$spec_id] . 
	    "; Failed to install last known good rpm: " . $msg;
	return 0;
    }

    if (defined ($the_good_rpms_copy_dir)) {
	`mkdir -p $the_good_rpms_copy_dir`;
	`cp @rpms $the_good_rpms_copy_dir`;
    }

    return 1;
}

sub install_rpms ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];

    my @rpms =  $spec->get_param_array ('rpm_paths');
    map $_ =~ s/^/$the_sys_rpm_dir\//, @rpms;
    my $command = "rpm --upgrade -v @rpms";

    if ($verbose > 0) {
	map msg_info (0, "Installing $_\n"), @rpms;
    }

    my $msg=`$command 2>&1`;
    if ($? > 0) {
	msg_error "failed to install rpm: $msg";
	$build_status[$spec_id] = 'FAILED';
	$status_details[$spec_id] = $msg;
	return 0;
    }

    return 1;
}

sub print_rpms ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];

    if ($full_path) {
	my @rpms =  $spec->get_param_array ('rpm_paths');
	map print("$the_sys_rpm_dir\/$_\n"), @rpms;
    } else {
	my @rpms =  $spec->get_param_array ('rpms');
	map print("$_\n"), @rpms;
    }
}

sub print_spec ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];

    print "$spec->{file_name}\n";
}

sub push_to_remove_list ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];

    my @rpms =  $spec->get_param_array ('packages');
    map unshift (@remove_list, $_), @rpms;
}

# --------- dependency checking code ---------------------------------------
sub warn_always ($$$) {
    my $spec_name = shift;
    my $dep = shift;
    my $reason = shift;

    if ($reason eq "DEP_FAILED") {
	msg_warning (1, "$spec_name requires $dep");
	msg_warning (1, "but that failed to build");
	msg_warning (1, "Can't build $spec_name");
    } elsif ($reason eq "NOT_FOUND") {
	msg_warning (0, "no spec file or installed package provides $dep");
	msg_warning (0, "can't build $spec_name");
    }
}

sub warn_once ($$$) {
    my $spec_name = shift;
    my $dep = shift;
    my $reason = shift;

    if ($reason eq "DEP_FAILED") {
	# should not happen
	msg_error ("assertion failed: warn_once / DEP_FAILED");
    } elsif ($reason eq "NOT_FOUND") {
	if (not defined ($warned_about{$dep})) {
	    msg_warning (1, "$dep is required but not found");
	    $warned_about{$dep}=1;
	}
    }
}

sub get_dependencies ($@) {
    my $spec_id = shift;
    my @packages = @_;
    my $spec = $specs_to_build[$spec_id];

    my @dependencies = ();
    my @this_pkg_requires;
    foreach my $pkg (@packages) {
	@this_pkg_requires = $spec->get_pkg_param_array ($pkg, 'requires');
	msg_debug (3, "adding \"@this_pkg_requires\" to the dependencies of $spec->{name}");
	push (@dependencies, @this_pkg_requires);
    }

    return @dependencies;
}

sub check_dependency ($$&&@) {
    my $spec_id = shift;
    my $capability = shift;
    my $recursive_callback = shift;
    my $warning_callback = shift;
    my @rec_cb_opts = @_;
    my $spec_name = $specs_to_build[$spec_id]->{name};

    my $result = 1;

    if (not defined ($capability)) {
	return 1;
    }

    if ((defined ($provider{$capability})) and
	($provider{$capability} == $spec_id)) {
	return 1;
    }

    if (defined $provider{$capability}) {
	if ($build_status[$provider{$capability}] eq "PASSED") {
	    return 1;
	} elsif ($build_status[$provider{$capability}] ne "NOT_BUILT") {
	    if (!is_provided ($capability)) {
		&$warning_callback ($spec_name, $capability, "DEP_FAILED");
		return 0;
	    }
	    return 1;
	}

	msg_info (1, "need to build $capability first");
	my $save_log_name = $current_log;
	my $result = &$recursive_callback ($provider{$capability}, @rec_cb_opts);
	open_log ($save_log_name);
	return $result;
    } elsif (!is_provided ($capability)) {
	&$warning_callback ($spec_name, $capability, "NOT_FOUND");
	return 0;
    } else {
	return 1;
    }

    # should not happen
    msg_error("Assertion failed: check_dependency: return 0");
    return 0;
}

# --------- copy build spec files, tarballs, patches -----------------------
sub copy_spec ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];
    my $spec_file = $spec->get_param ('file_name');
    my $base_name = $spec_file;
    $base_name =~ s/^.*\/([^\/]+)/$1/;
    my $target = "$the_sys_rpm_dir/SPECS/$base_name";

    msg_info (1, "copying spec file $base_name to the SPECS dir");

    if ($is_nightly) {
	my $the_nightly_date = `date "+$the_nightly_date_format"`;
	if ($? > 0) {
	    msg_error ("incorrect date format: $the_nightly_date_format");
	    $build_status[$spec_id] = 'ERROR';
	    $status_details[$spec_id] = "incorrect nightly date format: $the_nightly_date";
	    return 0;
	}

	open SPEC_OUT, ">$target";
	my $msg=`cp $spec_file /tmp/.gnome_build.tmp.$$ 2>&1`;
	if ($? > 0) {
	    msg_error ("failed to copy $spec_file");
	    msg_error ("can't continue with $spec->{name}");
	    $build_status[$spec_id] = 'FAILED';
	    $status_details[$spec_id] = "cound not copy spec file: $msg";
	    return 0;
	}
	open SPEC_IN, "</tmp/.gnome_build.tmp.$$";
	my $line;
	while (1) {
	    $line = <SPEC_IN>;
	    if (not defined ($line)) {
		last;
	    }
	    if ($line =~ /^Release\s*:/) {
		$line =~ s/\s*$/.$the_nightly_date/;
	    }
	    print SPEC_OUT $line;
	}
	close SPEC_OUT;
	close SPEC_IN;

	`rm -f /tmp/.gnome_build.tmp.$$`;
	my $s = rpm_spec->new ($target, $rpm_target);
	$specs_to_build[$spec_id] = $s;
	$s->add_define ("nightly", "1");
    } else {
	`cmp -s $spec_file $target`;
	if ($? != 0) {  # the files differ
	    my $msg=`cp $spec_file $target 2>&1`;
	    if ($? > 0) {
		msg_error "failed to copy $spec_file";
		msg_error "dude getting here 1 $target $spec_file different";
		$build_status[$spec_id] = 'FAILED';
		$status_details[$spec_id] = "failed to copy spec file: $msg";
		return 0;
	    }
	} else {
	    msg_info (3, "   $spec_file and");
	    msg_info (3, "   $target");
	    msg_info (3, "   are the same; not copying");
	}
    }
    
    return 1;
}

sub find_source ($$) {
    my $spec_id = shift;
    my $src = shift;
    my $is_tarball = 0;
    my $src_path;

    if ($src =~ /\.(tar\.gz|tgz|tar\.bz2|tar\.bzip2)$/) {
	$is_tarball = 1;

	$src_path = "$the_tarball_dir/$src";
	if (! -f "$src_path") {
	    msg_info (3, "   $src not found in $the_tarball_dir");
	} else {
	    msg_info (3, "   found in $the_tarball_dir");
	    return $src_path;
	}
    }
    foreach my $extsrcdir (@the_source_dirlist) {
	$src_path = "$extsrcdir/$src";
	if (! -f "$src_path") {
	    msg_info (3, "   $src not found in $extsrcdir");
	} else {
	    msg_info (3, "   found in $extsrcdir");
	    return "$src_path";
	}
    }

    if (!$is_tarball) {
	$src_path = "$the_tarball_dir/$src";
	if (! -f "$src_path") {
	    msg_info (3, "   trying the tarball directory");
	    msg_info (3, "   $src not found in $the_tarball_dir");
	} else {
	    msg_info (3, "   found in $the_tarball_dir");
	    msg_warning (1, "$src is not expected to be in the tarball dir");
	    return $src_path;
	}
    }

    $build_status[$spec_id] = 'FAILED';
    $status_details[$spec_id] = "Source $src not found";
    msg_error ("Source file $src not found");
    return undef;
}

sub copy_sources ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];

    my @sources = $spec->get_param_array ('sources');
    my $src_path;
    my $target;

    msg_info (1, "copying sources to $the_sys_rpm_dir/SOURCES");

    foreach my $src (@sources) {
	if (not defined ($src)) {
	    next;
	}
        $src_path = find_source ($spec_id, $src);
	if (not defined ($src_path)) {
	    return 0;
	}

	msg_info (2, "   copying $src");

	$target = "$the_sys_rpm_dir/SOURCES/$src";

	`cmp -s $src_path $target`;

	if ($? != 0) {  # the files differ
	    my $msg=`cp $src_path $target 2>&1`;
	    if ($? > 0) {
		msg_error ("failed to copy $src_path");
		msg_error "dude getting here 2";
		$build_status[$spec_id] = 'ERROR';
		$status_details[$spec_id] = $msg;
		return 0;
	    }
	}
    
    }

    return 1;
}

sub copy_patches ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];

    my @patches = $spec->get_param_array ('patches');
    my $patch_path;
    my $target;
    foreach my $patch (@patches) {
	if (not defined ($patch)) {
	    next;
	}

	msg_info (2, "copying patch $patch");

	$patch_path = "$the_patch_dir/$patch";

	if (! -f "$patch_path") {
	    $build_status[$spec_id] = 'ERROR';
	    $status_details[$spec_id] = "Patch $patch_path not found";
	    msg_error ("Patch $patch_path not found");
	    return 0;
	}

	$target = "$the_sys_rpm_dir/SOURCES/$patch";

	`cmp -s $patch_path $target`;

	if ($? != 0) {  # the files differ
	    my $msg=`cp $patch_path $target 2>&1`;
	    if ($? > 0) {
		msg_error "failed to copy $patch_path";
		$build_status[$spec_id] = 'ERROR';
		$status_details[$spec_id] = $msg;
		return 0;
	    }
	} else {
	    msg_info (3, "   $patch and");
	    msg_info (3, "   $the_sys_rpm_dir/SOURCES/$patch");
	    msg_info (3, "   are the same; not copying");
	}
    
    }

    return 1;
}

# --------- build command --------------------------------------------------
sub do_build () {
    print_live_status;

    for (my $i = 0; $i <= $#specs_to_build; $i++) {
	if ($build_status[$i] eq "NOT_BUILT") {
	    if (! build_spec ($i)) {
		if (defined ($the_good_build_dir)) {
		    msg_info (0, "Attempting to use a known good rpm");
		    install_good_rpms ($i);
		}
	    }
	    print_live_status;
	}
	if ($build_status[$i] ne "PASSED") {
	    if ($build_status[$i] ne "DEP_FAILED") {
		mail_log ($i);
	    }
	    $exit_val++;
	}
    }

    if ($verbose > 0) {
	print_status;
    }
}

sub build_spec ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];
    my $logname = $the_log_dir . "/" . get_log_name ($spec_id);

    msg_debug (0, "Trying to build $spec->{name}");
    open_log ($logname);

    if ($build_status[$spec_id] ne "NOT_BUILT") {
	if ($build_status[$spec_id] eq "DEP") {
	    $build_status[$spec_id]="ERROR";
	    $status_details[$spec_id]="Circular dependency detected";
	    msg_error ("Circular dependency detected " .
		"while trying to build $spec->{name}");
	}
	return 0;
    }

    my @packages = $spec->get_param_array ('packages');
    msg_debug (3, "packages: $spec->{file_name} defines: @packages");

    foreach my $pkg (@packages) {
	if (not defined ($pkg)) {
	    next;
	}
	if ($build_and_install) {
	    msg_info (2, "Checking if $pkg is installed");
	    if (is_provided ($pkg) and $check_deps) {
		my $provider = what_provides ($pkg);
		msg_warning (0, "$pkg is already provided by $provider");
		msg_warning (0, "not building $pkg");
		$build_status[$spec_id]='SKIPPED';
		$status_details[$spec_id]="$pkg already installed";
		
		return 0;
	    }
	}
    }

    if ($check_deps) {
	my @dependencies = get_dependencies ($spec_id, @packages);

	my $this_result;
	my $result = 1;

	$build_status[$spec_id] = "DEP";
	$status_details[$spec_id] = "building dependencies first";
	
	msg_info (1, "Checking dependencies of $spec->{name}");

	foreach my $dep (@dependencies) {
	    $dep =~ s/\s.*//;
	    
	    $this_result = check_dependency ($spec_id, $dep,
					     \&build_spec, \&warn_always, ());
	    if (!$this_result and $build_and_install) {
		if (defined ($the_good_build_dir)) {
		    msg_info (0, "Attempting to use a known good rpm");
		    $this_result = install_good_rpms (spec_id_of ($dep));
		}
		if (! $this_result) {
		    msg_warning (1, "$spec->{name} requires $dep");
		}
	    }
	    $result = ($this_result and $result);
	}
	
	if (! $result) {
	    $build_status[$spec_id]="DEP_FAILED";
	    $status_details[$spec_id]="Dependency check failed";
		
	    msg_warning (0, "Dependency check for $spec->{name} failed");

	    return 0;
	}
    }

    copy_spec ($spec_id) || return 0;
    copy_sources ($spec_id) || return 0;
    copy_patches ($spec_id) || return 0;

    if ($live_summary) {
	$build_status[$spec_id] = 'BEING_BUILT';
	$status_details[$spec_id] = "$RPM_BUILD running";
	print_live_status;
    }
    run_rpmbuild_ba ($spec_id) || return 0;

    if ($build_and_install) {
	install_rpms ($spec_id) || return 0;
    }

    $build_status[$spec_id] = "PASSED";
    $status_details[$spec_id] = "";

    close_log;

    return 1;

}

sub run_rpmbuild_ba ($) {
    my $spec_id = shift;
    my $spec = $specs_to_build[$spec_id];
    my $spec_file = $spec->get_param ('file_name');
    my $base_name = $spec_file;
    $base_name =~ s/^.*\/([^\/]+)/$1/;
    my $log_name = "$base_name";
    $log_name = get_log_name ($spec_id);

    msg_info (0, "Running rpm build of $spec->{name} ($base_name)");
    msg_info (1, "Log file: $the_log_dir/$log_name");

    my $save_log_name = $current_log;
    msg_log ("INFO: Starting $RPM_BUILD at " . `date`);
    close_log;
    my $tempfile = "/tmp/rpm.out.$$";
    my $defnightly = "";
    if ($is_nightly) {
	$defnightly = " --define \"nightly 1\""
    }
    my $rpmopt = "-ba";
    if ($prep_only) {
	$rpmopt = "-bp";
    }
    if (!$check_deps) {
	$rpmopt = "$rpmopt --nodeps";
    }
    if (defined ($the_sys_rpm_dir) and $the_sys_rpm_dir ne "/usr/src/packages") {
	$rpmopt = "$rpmopt --define \"_topdir $the_sys_rpm_dir\"";
    }
    if (defined($rpm_target) and ($rpm_target eq $spec->{buildarchitecture})) {
        system ("$RPM_BUILD --target $rpm_target $rpmopt $the_sys_rpm_dir/SPECS/$base_name $defnightly > $tempfile 2>&1");
    } else {
        system ("$RPM_BUILD $rpmopt $the_sys_rpm_dir/SPECS/$base_name $defnightly > $tempfile 2>&1");
    }
    my $rpm_result = $?;
    system ("sed -e 's/^/RPM: /' $tempfile >> $the_log_dir/$log_name 2>&1; rm -f $tempfile");
    open_log ($save_log_name);
    msg_log ("INFO: $RPM_BUILD finished at " . `date`);

    if ($rpm_result) {
	msg_error ("Build of $spec->{name} FAILED");
	$build_status[$spec_id] = "FAILED";
	$status_details[$spec_id] = 'RPM build failed';
	return 0;
    }

    msg_info (0, "$spec->{name} PASSED");
    return 1;
}

# --------- build-order and install-order commands -------------------------
sub do_build_order () {
    for (my $i = 0; $i <= $#specs_to_build; $i++) {
	print_order ($i, \&print_spec);
    }
}

sub do_install_order () {
    for (my $i = 0; $i <= $#specs_to_build; $i++) {
	print_order ($i, \&print_rpms);
    }
}

sub print_order ($&) {
    my $spec_id = shift;
    my $print_command = shift;
    my $spec = $specs_to_build[$spec_id];

    if ($build_status[$spec_id] ne "NOT_BUILT") {
	if ($build_status[$spec_id] eq "DEP") {
	    $build_status[$spec_id]="ERROR";
	    $status_details[$spec_id]="Circular dependency detected";
	    msg_error ("Circular dependency detected " .
		"while checking dependencies of $spec->{name}");
	}
	return 0;
    }

    if ($check_deps) {
	my @packages = $spec->get_param_array ('packages');
	my @dependencies = get_dependencies ($spec_id, @packages);

	$build_status[$spec_id] = "DEP";
	
	msg_info (1, "Checking dependencies of $spec->{name}");

	foreach my $dep (@dependencies) {
	    $dep =~ s/\s.*//;	
    
	    check_dependency ($spec_id, $dep, \&print_order, \&warn_once, ($print_command));	}
    }
    
    copy_spec ($spec_id) || return 0;

    $build_status[$spec_id] = "PASSED";
    &$print_command ($spec_id);
}

# --------- uninstall-pkgs command -----------------------------------------
sub do_uninstall_pkgs () {
    for (my $i = 0; $i <= $#specs_to_build; $i++) {
	print_order ($i, \&push_to_remove_list);
    }

    foreach my $pkg_to_remove (@remove_list) {
	msg_info (0, "Removing $pkg_to_remove");
	my $cmd_out = `rpm -v --erase --nodeps $pkg_to_remove 2>&1`;
	if ($? > 0) {
	    $exit_val++;
	}
	$cmd_out =~ s/^/INFO: rpm: /;
	if ($verbose > 0) {
	    print $cmd_out . "\n";
	}
    }
}

# --------- main program ---------------------------------------------------
sub main {
    process_options;

    if (not defined ($build_command)) {
	usage (1);
    }
    
    if (not defined ($specs_to_build[0])) {
	msg_info (0, "Nothing to do.");
	exit (0);
    }

    if ($build_command eq "build") {
	do_build;
    } if ($build_command eq "build-install") {
	do_build;
    } if ($build_command eq "build-only") {
	$build_and_install = 0;
	do_build;
    } if ($build_command eq "prep") {
	$build_and_install = 0;
	$prep_only = 1;
	do_build;
    } elsif ($build_command eq "uninstall-pkgs") {
	do_uninstall_pkgs;
    } elsif ($build_command eq "build-order") {
	do_build_order;
    } elsif ($build_command eq "install-order") {
	do_install_order;
    }

    exit ($exit_val);
}

main;