+
+if (not $check_everything and not $packages_file and not $schedule->count) {
+ v_msg("No packages selected.");
+ exit $exit_code;
+}
+
+# Check to make sure there are packages to check.
+sub set_value {
+ my ($f,$target,$field,$source,$required) = @_;
+ use YAML;
+# print map { Dump($_) } @_;
+ if ($required and not $source->{$field}) {
+ print Dump($f)."\n";
+ fail("description file $f does not define required tag $field");
+ }
+ $target->{$field} = $source->{$field};
+ delete $source->{$field};
+}
+
+opendir(COLLDIR, "$MAEMIAN_ROOT/collection")
+ or fail("cannot read directory $MAEMIAN_ROOT/collection");
+
+for my $f (readdir COLLDIR) {
+ next if $f =~ /^\./;
+ next unless $f =~ /\.desc$/;
+
+ debug_msg(2, "Reading collector description file $f ...");
+ my @secs = read_dpkg_control("$MAEMIAN_ROOT/collection/$f");
+ my $script;
+ ($#secs+1 == 1)
+ or fail("syntax error in description file $f: too many sections");
+
+ ($script = $secs[0]->{'collector-script'})
+ or fail("error in description file $f: `Collector-Script:' not defined");
+
+ delete $secs[0]->{'collector-script'};
+ $collection_info{$script}->{'script'} = $script;
+ my $p = $collection_info{$script};
+
+ set_value($f, $p,'type',$secs[0],1);
+ # convert Type:
+ my ($b,$s,$u) = ( "", "", "" );;
+ for (split(/\s*,\s*/o,$p->{'type'})) {
+ if ($_ eq 'binary') {
+ $b = 'b';
+ } elsif ($_ eq 'source') {
+ $s = 's';
+ } elsif ($_ eq 'udeb') {
+ $u = 'u';
+ } else {
+ fail("unknown type $_ specified in description file $f");
+ }
+ }
+ $p->{'type'} = "$s$b$u";
+
+ set_value($f,$p,'unpack-level',$secs[0],1);
+ set_value($f,$p,'order',$secs[0],1);
+ set_value($f,$p,'version',$secs[0],1);
+
+ if (exists $secs[0]->{'needs-info'} && defined $secs[0]->{'needs-info'}) {
+ for (split(/\s*,\s*/o,$secs[0]->{'needs-info'})) {
+ $p->{$_} = 1;
+ }
+ delete $secs[0]->{'needs-info'};
+ }
+
+ # ignore Info: and other fields for now
+ delete $secs[0]->{'info'};
+ delete $secs[0]->{'author'};
+
+ for (keys %{$secs[0]}) {
+ warning("unused tag $_ in description file $f");
+ }
+
+ debug_msg(2, map( { "$_: $p->{$_}" } sort keys %$p ));
+}
+
+closedir(COLLDIR);
+# }}}
+
+# {{{ Now we're ready to load info about checks & tags
+
+# load information about checker scripts
+opendir(CHECKDIR, "$MAEMIAN_ROOT/checks")
+ or fail("cannot read directory $MAEMIAN_ROOT/checks");
+
+for my $f (readdir CHECKDIR) {
+ next if $f =~ /^\./;
+ next unless $f =~ /\.desc$/;
+ debug_msg(2, "Reading checker description file $f ...");
+
+ my @secs = read_dpkg_control("$MAEMIAN_ROOT/checks/$f");
+ my $script;
+ ($script = $secs[0]->{'check-script'})
+ or fail("error in description file $f: `Check-Script:' not defined");
+
+ # ignore check `maemian' (this check is a special case and contains the
+ # tag info for the maemian frontend--this script here)
+ next if $script eq 'maemian';
+
+ delete $secs[0]->{'check-script'};
+ $check_info{$script}->{'script'} = $script;
+ my $p = $check_info{$script};
+
+ set_value($f,$p,'type',$secs[0],1);
+ # convert Type:
+ my ($b,$s,$u) = ( "", "", "" );
+ for (split(/\s*,\s*/o,$p->{'type'})) {
+ if ($_ eq 'binary') {
+ $b = 'b';
+ } elsif ($_ eq 'source') {
+ $s = 's';
+ } elsif ($_ eq 'udeb') {
+ $u = 'u';
+ } else {
+ fail("unknown type $_ specified in description file $f");
+ }
+ }
+ $p->{'type'} = "$s$b$u";
+
+ set_value($f,$p,'unpack-level',$secs[0],1);
+ set_value($f,$p,'abbrev',$secs[0],1);
+
+ if (exists $secs[0]->{'needs-info'} && defined $secs[0]->{'needs-info'}) {
+ for (split(/\s*,\s*/o,$secs[0]->{'needs-info'})) {
+ $p->{$_} = 1;
+ }
+ delete $secs[0]->{'needs-info'};
+ }
+
+ # ignore Info: and other fields for now...
+ delete $secs[0]->{'info'};
+ delete $secs[0]->{'standards-version'};
+ delete $secs[0]->{'author'};
+
+ for (keys %{$secs[0]}) {
+ warning("unused tag $_ in description file $f");
+ }
+
+ debug_msg(2, map( { "$_: $p->{$_}" } sort keys %$p ));
+
+ shift(@secs);
+ $p->{'requested-tags'} = 0;
+ foreach my $tag (@secs) {
+ $tag->{'script'} = $script;
+ Tags::add_tag($tag);
+ $p->{'requested-tags'}++ if Tags::display_tag($tag);
+ }
+}
+
+closedir(CHECKDIR);
+
+# }}}
+
+# {{{ Again some lone code the author just dumped where his cursor just happened to be
+if ($unpack_info) {
+ # determine which info has been requested
+ for my $i (split(/,/,$unpack_info)) {
+ unless ($collection_info{$i}) {
+ fail("unknown info specified: $i");
+ }
+ $unpack_infos{$i} = 1;
+ }
+}
+
+# create check_abbrev hash
+for my $c (keys %check_info) {
+ $check_abbrev{$check_info{$c}->{'abbrev'}} = $c;
+}
+
+# }}}
+
+# {{{ determine which checks have been requested
+if ($action eq 'check') {
+ if ($check_tags) {
+ foreach my $t (split(/,/, $check_tags)) {
+ my $info = Tags::get_tag_info($t);
+
+ fail("unknown tag specified: $t") unless defined($info);
+ my $script = $info->{'script'};
+ next if $script eq 'maemian';
+ if ($check_info{$script}) {
+ $checks{$script} = 1;
+ } else {
+ # should never happen
+ fail("no info for script $script");
+ }
+ }
+ } else {
+ my %dont_check = map { $_ => 1 } (split m/,/, ($dont_check || ""));
+ $checks or ($checks = join(',',keys %check_info));
+ for my $c (split(/,/,$checks)) {
+ if ($check_info{$c}) {
+ if ($dont_check{$c}
+ || ($check_info{$c}->{'abbrev'}
+ && $dont_check{$check_info{$c}->{'abbrev'}})) {
+ #user requested not to run this check
+ } elsif ($check_info{$c}->{'requested-tags'} == 0) {
+ #no need to run this check, no tags will be issued
+ } else {
+ $checks{$c} = 1;
+ }
+ } elsif (exists $check_abbrev{$c}) {
+ #abbrevs only used when -C is given, so we don't need %dont_check
+ $checks{$check_abbrev{$c}} = 1;
+ } else {
+ fail("unknown check specified: $c");
+ }
+ }
+ }
+
+ # determine which info is needed by the checks
+ for my $c (keys %checks) {
+ for my $i (keys %collection_info) {
+ # required by $c ?
+ if ($check_info{$c}->{$i}) {
+ $unpack_infos{$i} = 1;
+ }
+ }
+ }
+}
+
+# }}}
+
+# {{{ determine which info is needed by the collection scripts
+for my $c (keys %unpack_infos) {
+ for my $i (keys %collection_info) {
+ # required by $c ?
+ if ($collection_info{$c}->{$i}) {
+ $unpack_infos{$i} = 1;
+ }
+ }
+}
+# }}}
+
+# {{{ process all packages in the archive?
+if ($check_everything) {
+ # make sure package info is available
+ read_src_list("$MAEMIAN_LAB/info/source-packages", 0);
+ read_bin_list("$MAEMIAN_LAB/info/binary-packages", 0);
+ read_udeb_list("$MAEMIAN_LAB/info/udeb-packages", 0);
+
+ debug_msg(2, "pkg_mode = $pkg_mode");
+
+ if (($pkg_mode eq 'a') or ($pkg_mode eq 's')) {
+ for my $arg (sort keys %source_info) {
+ debug_msg(1, "doing stuff with $MAEMIAN_ARCHIVEDIR/$source_info{$arg}->{'file'}");
+ $schedule->add_file('s', "$MAEMIAN_ARCHIVEDIR/$source_info{$arg}->{'file'}",
+ %{$source_info{$arg}});
+ }
+ }
+ if (($pkg_mode eq 'a') or ($pkg_mode eq 'b')) {
+ for my $arg (sort keys %binary_info) {
+ debug_msg(1, "doing stuff with $MAEMIAN_ARCHIVEDIR/$binary_info{$arg}->{'file'}");
+ $schedule->add_file('b', "$MAEMIAN_ARCHIVEDIR/$binary_info{$arg}->{'file'}",
+ %{$binary_info{$arg}});
+ }
+ }
+ if (($pkg_mode eq 'a') or ($pkg_mode eq 'u')) {
+ for my $arg (sort keys %udeb_info) {
+ debug_msg(1, "doing stuff with $MAEMIAN_ARCHIVEDIR/$udeb_info{$arg}->{'file'}");
+ $schedule->add_file('u', "$MAEMIAN_ARCHIVEDIR/$udeb_info{$arg}->{'file'}",
+ %{$udeb_info{$arg}});
+ }
+ }
+
+ # package list still empty?
+ unless ($schedule->count) {
+ warning("no packages found in distribution directory");
+ }
+} elsif ($packages_file) { # process all packages listed in packages file?
+ $schedule->add_pkg_list($packages_file);
+}
+# }}}
+
+# {{{ Some silent exit
+unless ($schedule->count) {
+ v_msg("No packages selected.");
+ exit 0;
+}
+# }}}
+
+# {{{ Okay, now really processing the packages in one huge loop
+$unpack_infos{ "override-file" } = 1 unless $no_override;
+v_msg(sprintf("Processing %d packages...", $schedule->count));
+debug_msg(1,
+ "Selected action: $action",
+ "Requested unpack level: $unpack_level",
+ sprintf("Requested data to collect: %s", join(',',keys %unpack_infos)),
+ sprintf("Selected checks: %s", join(',',keys %checks)),
+ );
+
+require Checker;
+require Maemian::Collect;
+
+my %overrides;
+my @pending_jobs;
+PACKAGE:
+foreach my $pkg_info ($schedule->get_all) {
+ my ($type, $pkg, $ver, $arch, $file) =
+ @$pkg_info{qw(type package version architecture file)};
+ my $long_type = ($type eq 'b' ? 'binary' :
+ ($type eq 's' ? 'source' : 'udeb' ));
+
+ Tags::set_pkg( $file, $pkg, $ver, $arch, $long_type );
+
+ # Kill pending jobs, if any
+ Maemian::Command::kill(@pending_jobs);
+ undef @pending_jobs;
+
+ # determine base directory
+ my $base = "$MAEMIAN_LAB/$long_type/$pkg";
+ unless ($base =~ m,^/,) {
+ $base = "$cwd/$base";
+ }
+ debug_msg(1, "Base directory in lab: $base");
+
+ my $act_unpack_level = 0;
+
+ # unpacked package up-to-date?
+ if (-d $base) {
+ my $remove_basedir = 0;
+
+ # there's a base dir, so we assume that at least
+ # one level of unpacking has been done
+ $act_unpack_level = 1;
+
+ # maemian status file exists?
+ unless (-f "$base/.maemian-status") {
+ v_msg("No maemian status file found (removing old directory in lab)");
+ $remove_basedir = 1;
+ goto REMOVE_BASEDIR;
+ }
+
+ # read unpack status -- catch any possible errors
+ my $data;
+ eval { ($data) = read_dpkg_control("$base/.maemian-status"); };
+ if ($@) { # error!
+ v_msg($@);
+ $remove_basedir = 1;
+ goto REMOVE_BASEDIR;
+ }
+
+ # compatible maemian version?
+ if (not exists $data->{'lab-format'} or ($data->{'lab-format'} < $LAB_FORMAT)) {
+ v_msg("Lab directory was created by incompatible maemian version");
+ $remove_basedir = 1;
+ goto REMOVE_BASEDIR;
+ }
+
+ # version up to date?
+ if (not exists $data->{'version'} or ($data->{'version'} ne $ver)) {
+ debug_msg(1, "Removing package in lab (newer version exists) ...");
+ $remove_basedir = 1;
+ goto REMOVE_BASEDIR;
+ }
+
+ # unpack level defined?
+ unless (exists $data->{'unpack-level'}) {
+ warning("cannot determine unpack-level of package");
+ $remove_basedir = 1;
+ goto REMOVE_BASEDIR;
+ } else {
+ $act_unpack_level = $data->{'unpack-level'};
+ }
+
+ # file modified?
+ my $timestamp;
+ my @stat;
+ unless (@stat = stat $file) {
+ warning("cannot stat file $file: $!");
+ } else {
+ $timestamp = $stat[9];
+ }
+ if ((not defined $timestamp) or (not exists $data->{'timestamp'}) or ($data->{'timestamp'} != $timestamp)) {
+ debug_msg(1, "Removing package in lab (package has been changed) ...");
+ $remove_basedir = 1;
+ goto REMOVE_BASEDIR;
+ }
+
+ REMOVE_BASEDIR:
+ if ($remove_basedir) {
+ v_msg("Removing $pkg");
+ unless (remove_pkg($base)) {
+ warning("skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+ $act_unpack_level = 0;
+ }
+ }
+
+ # unpack to requested unpack level
+ $act_unpack_level = unpack_pkg($type,$base,$file,$act_unpack_level,
+ $unpack_level);
+ if ($act_unpack_level == -1) {
+ warning("could not unpack package to desired level",
+ "skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+
+ if (($action eq 'unpack') or ($action eq 'check')) { # collect info
+ my $current_order = -1;
+ for my $coll (sort by_collection_order keys %unpack_infos) {
+ my $ci = $collection_info{$coll};
+ my %run_opts = ('description' => $coll);
+
+ # current type?
+ next unless ($ci->{'type'} =~ m/$type/);
+
+ # If a file named .SCRIPT-VERSION already exists, we've already
+ # collected this information and we can skip it. Otherwise,
+ # remove any .SCRIPT-* files (which are old version information).
+ next if (-f "$base/.${coll}-$ci->{'version'}");
+ opendir(BASE, $base)
+ or fail("cannot read directory $base: $!");
+ for my $file (readdir BASE) {
+ if ($file =~ /^\.\Q$coll-/) {
+ unlink("$base/$file");
+ }
+ }
+ closedir(BASE);
+
+ # unpack to desired unpack level (if necessary)
+ $act_unpack_level = unpack_pkg($type,$base,$file,$act_unpack_level,$ci->{'unpack-level'});
+ if ($act_unpack_level == -1) {
+ warning("could not unpack package to desired level",
+ "skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+
+ # chdir to base directory
+ unless (chdir($base)) {
+ warning("could not chdir into directory $base: $!",
+ "skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+
+ $current_order = $ci->{'order'} if ($current_order == -1);
+ if ($current_order != $ci->{'order'}) {
+ debug_msg(1, "Waiting for jobs from order $current_order ...");
+ unless (reap_collect_jobs($pkg, $base, @pending_jobs)) {
+ warning("skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+ undef @pending_jobs;
+ $current_order = $ci->{'order'};
+ }
+
+ # collect info
+ remove_status_file($base);
+ debug_msg(1, "Collecting info: $coll ...");
+ my $script = "$MAEMIAN_ROOT/collection/$ci->{'script'}";
+ unless (spawn(\%run_opts, [ $script, $pkg, $long_type, '&' ])) {
+ warning("collect info $coll about package $pkg failed",
+ "skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+ push @pending_jobs, \%run_opts;
+ }
+
+ # wait until all the jobs finish and skip this package if any of them
+ # failed.
+ debug_msg(1, "Waiting for jobs from order $current_order ...");
+ unless (reap_collect_jobs($pkg, $base, @pending_jobs)) {
+ warning("skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+ undef @pending_jobs;
+ }
+
+ if ($action eq 'check') { # read override file
+
+ unless ($no_override) {
+ Tags::add_overrides("$base/override", $pkg, $long_type)
+ if (-f "$base/override")
+ }
+
+ # perform checks
+ my $info = Maemian::Collect->new($pkg, $long_type);
+ for my $check (keys %checks) {
+ my $ci = $check_info{$check};
+
+ # current type?
+ next unless ($ci->{'type'} =~ m/$type/);
+
+ # unpack to desired unpack level (if necessary)
+ $act_unpack_level = unpack_pkg($type,$base,$file,$act_unpack_level,$ci->{'unpack-level'});
+ if ($act_unpack_level == -1) {
+ warning("could not unpack package to desired level",
+ "skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+
+ # chdir to base directory
+ unless (chdir($base)) {
+ warning("could not chdir into directory $base: $!",
+ "skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+
+ my $returnvalue = Checker::runcheck($pkg, $long_type, $info, $check);
+ # Set exit_code correctly if there was not yet an exit code
+ $exit_code = $returnvalue unless $exit_code;
+
+ if ($returnvalue == 2) {
+ warning("skipping $action of $long_type package $pkg");
+ next PACKAGE;
+ }
+
+ }
+ unless ($exit_code) {
+ my $stats = Tags::get_stats( $file );
+ if ($stats->{types}{E}) {
+ $exit_code = 1;
+ } elsif ($fail_on_warnings && $stats->{types}{W}) {
+ $exit_code = 1;
+ }
+ }
+
+ # report unused overrides
+ if (not $no_override) {
+ my $overrides = Tags::get_overrides( $file );
+
+ for my $tag (sort keys %$overrides) {
+ my $taginfo = Tags::get_tag_info{$tag};
+ if (defined $taginfo) {
+ # Did we run the check script containing the tag?
+ next unless $checks{$taginfo->{'script'}};
+
+ # If only checking specific tags, is this one of them?
+ next unless (scalar keys %Tags::only_issue_tags == 0)
+ or exists $Tags::only_issue_tags{$tag};
+ }
+
+ for my $extra (sort keys %{$overrides->{$tag}}) {
+ next if $overrides->{$tag}{$extra};
+
+ tag( "unused-override", $tag, $extra );
+ }
+ }
+ }
+
+ # Report override statistics.
+ if (not $no_override and not $show_overrides) {
+ my $stats = Tags::get_stats($file);
+ my $short = $file;
+ $short =~ s%.*/%%;
+ my $errors = $stats->{overrides}{types}{E} || 0;
+ my $warnings = $stats->{overrides}{types}{W} || 0;
+ my $info = $stats->{overrides}{types}{I} || 0;
+ $overrides{errors} += $errors;
+ $overrides{warnings} += $warnings;
+ $overrides{info} += $info;
+ }
+ }
+
+ # chdir to maemian root directory (to unlock $base so it can be removed below)
+ unless (chdir($MAEMIAN_ROOT)) {
+ warning("could not chdir into directory $MAEMIAN_ROOT: $!",
+ "skipping $action of $long_type package $pkg");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+
+ # clean up
+ if ($act_unpack_level > $unpack_level) {
+ $act_unpack_level = clean_pkg($type,$base,$file,$act_unpack_level,$unpack_level);
+ if ($act_unpack_level == -1) {
+ warning("could not clean up laboratory for package $pkg: $!",
+ "skipping clean up");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+ }
+
+ # create Maemian status file
+ if (($act_unpack_level > 0) and (not -f "$base/.maemian-status")) {
+ my @stat;
+ unless (@stat = stat $file) {
+ warning("cannot stat file $file: $!",
+ "skipping creation of status file");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+ my $timestamp = $stat[9];
+
+ unless (open(STATUS, '>', "$base/.maemian-status")) {
+ warning("could not create status file $base/.maemian-status for package $pkg: $!");
+ $exit_code = 2;
+ next PACKAGE;
+ }
+
+ print STATUS "Maemian-Version: $MAEMIAN_VERSION\n";
+ print STATUS "Lab-Format: $LAB_FORMAT\n";
+ print STATUS "Package: $pkg\n";
+ print STATUS "Version: $ver\n";
+ print STATUS "Type: $type\n";
+ print STATUS "Unpack-Level: $act_unpack_level\n";
+ print STATUS "Timestamp: $timestamp\n";
+ close(STATUS);
+ }
+}
+Tags::reset_pkg();
+if ($action eq 'check' and not $no_override and not $show_overrides) {
+ my $errors = $overrides{errors} || 0;
+ my $warnings = $overrides{warnings} || 0;
+ my $info = $overrides{info} || 0;
+ my $total = $errors + $warnings + $info;
+ if ($total > 0) {
+ my $total = ($total == 1)
+ ? "$total tag overridden"
+ : "$total tags overridden";
+ my @output;
+ if ($errors) {
+ push (@output, ($errors == 1) ? "$errors error" : "$errors errors");
+ }
+ if ($warnings) {
+ push (@output, ($warnings == 1) ? "$warnings warning" : "$warnings warnings");
+ }
+ if ($info) {
+ push (@output, "$info info");
+ }
+ msg("$total (". join (', ', @output). ")");
+ }
+}
+
+# }}}
+
+exit $exit_code;
+
+# {{{ Some subroutines
+
+sub unpack_pkg {
+ my ($type,$base,$file,$cur_level,$new_level) = @_;
+
+ debug_msg(1, sprintf("Current unpack level is %d",$cur_level));
+
+ return $cur_level if $cur_level == $new_level;
+
+ # remove .maemian-status file
+ remove_status_file($base);
+
+ if ( ($cur_level == 0) and (-d $base) ) {
+ # We were lied to, there's something already there - clean it up first
+ remove_pkg($base) or return -1;
+ }
+
+ if ( ($new_level >= 1) and
+ (not defined ($cur_level) or ($cur_level < 1)) ) {
+ # create new directory
+ debug_msg(1, "Unpacking package to level 1 ...");
+ if (($type eq 'b') || ($type eq 'u')) {
+ spawn({}, ["$MAEMIAN_ROOT/unpack/unpack-binpkg-l1", $base, $file])
+ or return -1;
+ } else {
+ spawn({}, ["$MAEMIAN_ROOT/unpack/unpack-srcpkg-l1", $base, $file])
+ or return -1;
+ }
+ $cur_level = 1;
+ }
+
+ if ( ($new_level >= 2) and
+ (not defined ($cur_level) or ($cur_level < 2)) ) {
+ # unpack package contents
+ debug_msg(1, "Unpacking package to level 2 ...");
+ if (($type eq 'b') || ($type eq 'u')) {
+ spawn({}, ["$MAEMIAN_ROOT/unpack/unpack-binpkg-l2", $base])
+ or return -1;
+ } else {
+ debug_msg(1, "$MAEMIAN_ROOT/unpack/unpack-srcpkg-l2 $base");
+ spawn({}, ["$MAEMIAN_ROOT/unpack/unpack-srcpkg-l2", $base])
+ or return -1;
+ }
+ $cur_level = 2;
+ }
+
+ return $cur_level;
+}
+
+# Given a list of jobs corresponding to collect scripts, reap each of the
+# jobs. For each successful job, record that it was successful by creating
+# the corresponding version marker file in the lab. For each unsuccessful
+# job, warn that it was unsuccessful.
+#
+# Takes the current package, base directory, and the list of pending jobs.
+# Return true if all jobs were successful, false otherwise.
+sub reap_collect_jobs {
+ my ($pkg, $base, @pending_jobs) = @_;
+ my $status = reap(@pending_jobs);
+ for my $job (@pending_jobs) {
+ my $coll = $job->{'description'};
+ if ($job->{success}) {
+ my $ci = $collection_info{$coll};
+ open(VERSION, '>', "$base/.${coll}-$ci->{'version'}")
+ or fail("cannot create $base/.${coll}-$ci->{'version'}: $!");
+ print VERSION "Maemian-Version: $MAEMIAN_VERSION\n"
+ . "Timestamp: " . time . "\n";
+ close(VERSION);
+ } else {
+ warning("collect info $coll about package $pkg failed");
+ }
+ }
+ return $status;
+}
+
+# TODO: is this the best way to clean dirs in perl?
+# no, look at File::Path module
+sub clean_pkg {
+ my ($type,$base,$file,$cur_level,$new_level) = @_;
+
+ return $cur_level if $cur_level == $new_level;
+
+ if ($new_level < 1) {
+ # remove base directory
+ remove_pkg($base) or return -1;
+ return 0;
+ }
+
+ if ( ($new_level < 2) and defined ($cur_level) and ($cur_level >= 2) ) {
+ # remove .maemian-status file
+ remove_status_file($base);
+
+ # remove unpacked/ directory
+ debug_msg(1, "Decreasing unpack level to 1 (removing files) ...");
+ if ( -l "$base/unpacked" ) {
+ delete_dir("$base/".readlink("$base/unpacked"))
+ or return -1;
+ delete_dir("$base/unpacked") or return -1;
+ } else {
+ delete_dir("$base/unpacked") or return -1;
+ }
+
+ $cur_level = 1;
+ }
+
+ return $cur_level;
+}
+
+# this function removes a package's base directory in the lab completely
+sub remove_pkg {
+ my ($base) = @_;
+
+ debug_msg(1, "Removing package in lab ...");
+ unless (delete_dir($base)) {
+ warning("cannot remove directory $base: $!");
+ return 0;
+ }
+
+ return 1;
+}
+
+sub remove_status_file {
+ my ($base) = @_;
+
+ # status file exists?
+ if (not -e "$base/.maemian-status") {
+ return 1;
+ }
+
+ if (not unlink("$base/.maemian-status")) {
+ warning("cannot remove status file $base/.maemian-status: $!");
+ return 0;
+ }
+
+ return 1;
+}
+
+# get package name, version, and file name from the lab
+sub get_bin_info_from_lab {
+ my ($base_dir) = @_;
+ my ($pkg,$ver,$arch,$file);
+
+ ($pkg = read_file("$base_dir/fields/package"))
+ or fail("cannot read file $base_dir/fields/package: $!");
+
+ ($ver = read_file("$base_dir/fields/version"))
+ or fail("cannot read file $base_dir/fields/version: $!");
+
+ ($arch = read_file("$base_dir/fields/architecture"))
+ or fail("cannot read file $base_dir/fields/architecture: $!");
+
+ ($file = readlink("$base_dir/deb"))
+ or fail("cannot read link $base_dir/deb: $!");
+
+ return ($file, package => $pkg, version => $ver, architecture => $arch);
+}
+
+# get package name, version, and file name from the lab
+sub get_src_info_from_lab {
+ my ($base_dir) = @_;
+ my ($pkg,$ver,$file);
+
+ ($pkg = read_file("$base_dir/fields/source"))
+ or fail("cannot read file $base_dir/fields/source: $!");
+
+ ($ver = read_file("$base_dir/fields/version"))
+ or fail("cannot read file $base_dir/fields/version: $!");
+
+ ($file = readlink("$base_dir/dsc"))
+ or fail("cannot read link $base_dir/dsc: $!");
+
+ return ($file, source => $pkg, version => $ver);
+}
+
+# read first line of a file
+sub read_file {
+ my $first_line;
+
+ open(T, '<', $_[0]) or return;
+ chop($first_line = <T>);
+ close(T) or return;
+
+ return $first_line;
+}
+
+# sort collection list by `order'
+sub by_collection_order {
+ $collection_info{$a}->{'order'} <=> $collection_info{$b}->{'order'};
+}
+
+sub END {
+ # Prevent Lab::delete from affecting the exit code.
+ local $?;
+
+ $SIG{'INT'} = 'DEFAULT';
+ $SIG{'QUIT'} = 'DEFAULT';
+
+ $LAB->delete() if $LAB and not $keep_lab;
+}
+
+sub interrupted {
+ $SIG{$_[0]} = 'DEFAULT';
+ die "N: Interrupted.\n";
+}
+
+1;
+__END__