From 9d50e7424d7e4809ba1197ae276bf68899e78df9 Mon Sep 17 00:00:00 2001 From: Marc Espie Date: Mon, 16 Nov 2009 12:53:28 +0000 Subject: pull more common code in a "framework" routine. --- usr.sbin/pkg_add/OpenBSD/AddDelete.pm | 54 ++++++--- usr.sbin/pkg_add/pkg_add | 217 ++++++++++++++++------------------ usr.sbin/pkg_add/pkg_delete | 116 +++++++++--------- 3 files changed, 196 insertions(+), 191 deletions(-) (limited to 'usr.sbin') diff --git a/usr.sbin/pkg_add/OpenBSD/AddDelete.pm b/usr.sbin/pkg_add/OpenBSD/AddDelete.pm index 75b245e5920..5d01755239f 100644 --- a/usr.sbin/pkg_add/OpenBSD/AddDelete.pm +++ b/usr.sbin/pkg_add/OpenBSD/AddDelete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: AddDelete.pm,v 1.1 2009/11/16 12:20:32 espie Exp $ +# $OpenBSD: AddDelete.pm,v 1.2 2009/11/16 12:53:27 espie Exp $ # # Copyright (c) 2007-2009 Marc Espie # @@ -25,14 +25,14 @@ $opt_v = 0; sub setup_state { - lock_db($opt_n) unless $state->{defines}->{nolock}; - $state->setup_progressmeter($opt_x); - $state->check_root; } sub handle_options { - my ($opt_string, $hash) = @_; + my ($opt_string, $hash, @usage) = @_; + + set_usage(@usage); + $state = OpenBSD::State->new; $hash->{h} = sub { Usage(); }; $hash->{f} = $hash->{F} = sub { for my $o (split /\,/o, shift) { @@ -83,18 +83,44 @@ sub do_the_main_work eval { &$code; }; } my $dielater = $@; - # cleanup various things - $state->{recorder}->cleanup($state); - OpenBSD::PackingElement::Lib::ensure_ldconfig($state); - OpenBSD::PackingElement::Fontdir::finish_fontdirs($state); - if ($state->{beverbose}) { - OpenBSD::Vstat::tally(); - } - $state->progress->clear; - $state->log->dump; return $dielater; } +sub framework +{ + my $code = shift; + try { + lock_db($opt_n) unless $state->{defines}->{nolock}; + $state->setup_progressmeter($opt_x); + $state->check_root; + process_parameters(); + my $dielater = do_the_main_work($code); + # cleanup various things + $state->{recorder}->cleanup($state); + OpenBSD::PackingElement::Lib::ensure_ldconfig($state); + OpenBSD::PackingElement::Fontdir::finish_fontdirs($state); + if ($state->{beverbose}) { + OpenBSD::Vstat::tally(); + } + $state->progress->clear; + $state->log->dump; + finish_display(); + # show any error, and show why we died... + rethrow $dielater; + } catch { + print STDERR "$0: $_\n"; + if ($_ =~ m/^Caught SIG(\w+)/o) { + kill $1, $$; + } + exit(1); + }; + + if ($bad) { + exit(1); + } +} + + package OpenBSD::SharedItemsRecorder; sub new { diff --git a/usr.sbin/pkg_add/pkg_add b/usr.sbin/pkg_add/pkg_add index b88f469dbf2..fcd933ad4b7 100644 --- a/usr.sbin/pkg_add/pkg_add +++ b/usr.sbin/pkg_add/pkg_add @@ -1,7 +1,7 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: pkg_add,v 1.384 2009/11/16 12:32:16 espie Exp $ +# $OpenBSD: pkg_add,v 1.385 2009/11/16 12:53:27 espie Exp $ # # Copyright (c) 2003-2009 Marc Espie # @@ -62,7 +62,7 @@ sub has_new_sig return $plist->{new_sig}; } -package OpenBSD::pkg_add::State; +package OpenBSD::State; our @ISA=(qw(OpenBSD::UI)); # one-level dependencies tree, for nicer printouts @@ -585,16 +585,12 @@ sub find_truelocations } } -set_usage('pkg_add [-acIinqruvxz] [-A arch] [-B pkg-destdir] [-F keywords]', -'[-L localbase] [-l file] [-P type] [-Q quick-destdir] pkg-name [...]'); - -$state = OpenBSD::pkg_add::State->new; - our ($opt_a, $opt_A, $opt_P, $opt_Q, $opt_r, $opt_u, $opt_l, $opt_z); -handle_options('aqchruzl:A:P:Q:'); +handle_options('aqchruzl:A:P:Q:', {}, + 'pkg_add [-acIinqruvxz] [-A arch] [-B pkg-destdir] [-F keywords]', + '[-L localbase] [-l file] [-P type] [-Q quick-destdir] pkg-name [...]'); -try { $state->{cache} = {}; $state->{do_faked} = 0; $state->{arch} = $opt_A; @@ -644,105 +640,129 @@ if ($opt_l && @ARGV != 0) { Usage "No pkgnames after -l"; } -setup_state(); # Here we create the list of packages to install # actually, an updateset list (@todo2), and we hope to do this lazily # later for the most part... my @todo2 = (); -if ($opt_l) { - open my $f, '<', $opt_l or die "$!: bad list $opt_l"; - my $_; - while (<$f>) { - chomp; - s/\s.*//; - push(@ARGV, $_); +sub process_parameters +{ + if ($opt_l) { + open my $f, '<', $opt_l or die "$!: bad list $opt_l"; + my $_; + while (<$f>) { + chomp; + s/\s.*//; + push(@ARGV, $_); + } } -} -# three cases: -# 1/ match fuzzily against a list -if ($opt_l || $opt_z) { + # three cases: + # 1/ match fuzzily against a list + if ($opt_l || $opt_z) { - $state->progress->set_header("Looking for packages"); - for my $_ (@ARGV) { - my $set = OpenBSD::UpdateSet->new->add_hints($_); - if ($state->updater->process_hint($set, $_, $state)) { - push(@todo2, $set); + $state->progress->set_header("Looking for packages"); + for my $_ (@ARGV) { + my $set = OpenBSD::UpdateSet->new->add_hints($_); + if ($state->updater->process_hint($set, $_, $state)) { + push(@todo2, $set); + } } - } -# 2/ update existing stuff -} elsif ($opt_u) { - require OpenBSD::PackageRepository::Installed; - - if (@ARGV == 0) { - @ARGV = sort(installed_packages()); - $state->{full_update} = 1; - } - my $inst = OpenBSD::PackageRepository::Installed->new; - $state->progress->set_header("Looking for updates"); - for my $pkgname (@ARGV) { - my $l; + # 2/ update existing stuff + } elsif ($opt_u) { + require OpenBSD::PackageRepository::Installed; - if (OpenBSD::PackageName::is_stem($pkgname)) { - $l = stem2location($inst, $pkgname, $state); - } else { - $l = $inst->find($pkgname, $state->{arch}); + if (@ARGV == 0) { + @ARGV = sort(installed_packages()); + $state->{full_update} = 1; } - if (!defined $l) { - $state->say("Problem finding $pkgname"); - } else { - my $set = OpenBSD::UpdateSet->new->add_older(OpenBSD::Handle->from_location($l)); - my $problem = 0; - for my $l ($set->older) { - (defined $state->updater->process_handle($set, $l, $state)) or $problem = 1; + my $inst = OpenBSD::PackageRepository::Installed->new; + $state->progress->set_header("Looking for updates"); + for my $pkgname (@ARGV) { + my $l; + + if (OpenBSD::PackageName::is_stem($pkgname)) { + $l = stem2location($inst, $pkgname, $state); + } else { + $l = $inst->find($pkgname, $state->{arch}); } - if ($set->newer > 0) { - push(@todo2, $set); + if (!defined $l) { + $state->say("Problem finding $pkgname"); } else { - $state->tracker->mark_uptodate($set); + my $set = OpenBSD::UpdateSet->new->add_older(OpenBSD::Handle->from_location($l)); + my $problem = 0; + for my $l ($set->older) { + (defined $state->updater->process_handle($set, $l, $state)) or $problem = 1; + } + if ($set->newer > 0) { + push(@todo2, $set); + } else { + $state->tracker->mark_uptodate($set); + } + if ($problem) { + $state->tracker->mark_cant_update($set); + } } - if ($problem) { - $state->tracker->mark_cant_update($set); + } + $state->progress->next; + + my @cantupdate = $state->tracker->cant_update; + if (@cantupdate > 0) { + $state->print("Cannot find updates for ", join(', ', (@cantupdate > 50 ? @cantupdate[1..50] : @cantupdate))); + print " and more" if @cantupdate > 50; + print "\n"; + if (@todo2 == 0) { + print "No updates found\n"; + exit(1); } + if ($state->{defines}->{alwaysupdate} || + $state->{full_update} && !$state->{interactive}) { + print "Proceeding anyways\n"; + } else { + if (!OpenBSD::Interactive::confirm("Proceed", + $state->{interactive}, 0)) { + exit(1); + } + } + } + if (defined $state->{issues}) { + $state->say("There are some ambiguities. ", + "Please run in interactive mode again."); } + $state->say("Running update ", + join(',', map {$_->short_print} @todo2)); + } else { + # 3/ actual names + find_truelocations(\@ARGV, \@todo2, $state); + OpenBSD::Add::tag_user_packages(@todo2); } - $state->progress->next; +} - my @cantupdate = $state->tracker->cant_update; - if (@cantupdate > 0) { - $state->print("Cannot find updates for ", join(', ', (@cantupdate > 50 ? @cantupdate[1..50] : @cantupdate))); - print " and more" if @cantupdate > 50; - print "\n"; - if (@todo2 == 0) { - print "No updates found\n"; - exit(1); +sub finish_display +{ + OpenBSD::Add::manpages_index($state); + + + # and display delayed thingies. + if ($state->{packages_with_sig}) { + $state->print("Packages with signatures: ", + $state->{packages_with_sig}); + if ($state->{packages_without_sig}) { + print ". UNSIGNED PACKAGES: ", + join(', ', keys %{$state->{packages_without_sig}}); } - if ($state->{defines}->{alwaysupdate} || - $state->{full_update} && !$state->{interactive}) { - print "Proceeding anyways\n"; - } else { - if (!OpenBSD::Interactive::confirm("Proceed", - $state->{interactive}, 0)) { - exit(1); - } - } + print "\n"; } - if (defined $state->{issues}) { - $state->say("There are some ambiguities. ", - "Please run in interactive mode again."); + if (defined $state->{updatedepends} && %{$state->{updatedepends}}) { + print "Forced updates, bogus dependencies for ", + join(' ', sort(keys %{$state->{updatedepends}})), + " may remain\n"; } - $state->say("Running update ", - join(',', map {$_->short_print} @todo2)); -} else { -# 3/ actual names - find_truelocations(\@ARGV, \@todo2, $state); - OpenBSD::Add::tag_user_packages(@todo2); } -my $dielater = do_the_main_work( +framework( sub { # This is the actual very small loop that adds all packages $state->tracker->add_sets(@todo2); @@ -751,34 +771,3 @@ sub { } }); -OpenBSD::Add::manpages_index($state); - - -# and display delayed thingies. -if ($state->{packages_with_sig}) { - $state->print("Packages with signatures: ", $state->{packages_with_sig}); - if ($state->{packages_without_sig}) { - print ". UNSIGNED PACKAGES: ", join(', ', keys %{$state->{packages_without_sig}}); - } - print "\n"; -} -if (defined $state->{updatedepends} && %{$state->{updatedepends}}) { - print "Forced updates, bogus dependencies for ", - join(' ', sort(keys %{$state->{updatedepends}})), - " may remain\n"; -} - -# show any error, and show why we died... -rethrow $dielater; -} catch { - print STDERR "$0: $_\n"; - if ($_ =~ m/^Caught SIG(\w+)/o) { - kill $1, $$; - } - exit(1); -}; - -if ($bad) { - exit(1); -} - diff --git a/usr.sbin/pkg_add/pkg_delete b/usr.sbin/pkg_add/pkg_delete index 4b5c8506c34..36dbc0427bf 100644 --- a/usr.sbin/pkg_add/pkg_delete +++ b/usr.sbin/pkg_add/pkg_delete @@ -1,6 +1,6 @@ #!/usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: pkg_delete,v 1.122 2009/11/16 12:32:16 espie Exp $ +# $OpenBSD: pkg_delete,v 1.123 2009/11/16 12:53:27 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie # @@ -21,7 +21,7 @@ use warnings; use OpenBSD::AddDelete; -package OpenBSD::pkg_delete::State; +package OpenBSD::State; our @ISA=(qw(OpenBSD::UI)); package OpenBSD::AddDelete; @@ -35,11 +35,10 @@ use OpenBSD::PackageInfo; use OpenBSD::Vstat; use OpenBSD::UpdateSet; -set_usage('pkg_delete [-cIinqvx] [-B pkg-destdir] [-F keywords] pkg-name [...]'); -$state = OpenBSD::pkg_delete::State->new; our ($opt_d, $opt_p, $opt_i); -handle_options('chixDdnqpS:'); +handle_options('chixDdnqpS:', {}, + 'pkg_delete [-cIinqvx] [-B pkg-destdir] [-F keywords] pkg-name [...]'); $opt_B = $ENV{'PKG_DESTDIR'} unless defined $opt_B; $opt_B = '' unless defined $opt_B; @@ -55,9 +54,6 @@ if (defined $opt_d) { Usage "Option d is obsolete"; } -my %done; -my $removed; - $state->{destdir} = $opt_B; if ($opt_B eq '') { $state->{destdirname} = ''; @@ -67,56 +63,64 @@ if ($opt_B eq '') { $ENV{'PKG_DELETE_EXTRA'} = $state->{extra} ? "Yes" : "No"; -try { -setup_state(); -# First, resolve pkg names +my %done; +my $removed; +# Resolve pkg names my @realnames; - -OpenBSD::PackageInfo::solve_installed_names(\@ARGV, \@realnames, - "(removing them all)", $state); - -my @todo = OpenBSD::RequiredBy->compute_closure(@realnames); - -if (@todo > @realnames) { - my $details = $state->{very_verbose} || $defines{verbosedeps}; - my $show = sub { - my ($p, $d) = @_; - $state->say("Can't remove ", join(' ', @$p), - " without also removing:\n", - join(' ', @$d)); - }; - if ($state->{interactive} || !$details) { - my %deps = map {($_, 1)} @todo; - for my $p (@realnames) { - delete $deps{$p}; +my @todo; + +sub process_parameters +{ + OpenBSD::PackageInfo::solve_installed_names(\@ARGV, \@realnames, + "(removing them all)", $state); + + @todo = OpenBSD::RequiredBy->compute_closure(@realnames); + + if (@todo > @realnames) { + my $details = $state->{very_verbose} || $defines{verbosedeps}; + my $show = sub { + my ($p, $d) = @_; + $state->say("Can't remove ", join(' ', @$p), + " without also removing:\n", + join(' ', @$d)); + }; + if ($state->{interactive} || !$details) { + my %deps = map {($_, 1)} @todo; + for my $p (@realnames) { + delete $deps{$p}; + } + &$show([@realnames], [keys %deps]); + if (@realnames > 1 && (keys %deps) > 1 && + OpenBSD::Interactive::confirm( + "Do you want details", $state->{interactive}, 1)) { + $details = 1; + } } - &$show([@realnames], [keys %deps]); - if (@realnames > 1 && (keys %deps) > 1 && - OpenBSD::Interactive::confirm( - "Do you want details", $state->{interactive}, 1)) { - $details = 1; + if ($details) { + for my $pkg (@realnames) { + my @deps = OpenBSD::RequiredBy->compute_closure($pkg); + next unless @deps > 1; + @deps = grep {$_ ne $pkg} @deps; + &$show([$pkg], [@deps]); + } } - } - if ($details) { - for my $pkg (@realnames) { - my @deps = OpenBSD::RequiredBy->compute_closure($pkg); - next unless @deps > 1; - @deps = grep {$_ ne $pkg} @deps; - &$show([$pkg], [@deps]); + my $them = @todo > 1 ? 'them' : 'it'; + if ($defines{dependencies} or + OpenBSD::Interactive::confirm("Do you want to remove $them as well", $state->{interactive}, 0)) { + $state->say("(removing $them as well)"); + } else { + $bad = 1; } } - my $them = @todo > 1 ? 'them' : 'it'; - if ($defines{dependencies} or - OpenBSD::Interactive::confirm("Do you want to remove $them as well", $state->{interactive}, 0)) { - $state->say("(removing $them as well)"); - } else { - $bad = 1; - } } -my $dielater = do_the_main_work( +sub finish_display +{ +} + +framework( sub { # and finally, handle the removal do { @@ -155,17 +159,3 @@ sub { } } while ($removed); }); - -rethrow $dielater; -} catch { - print STDERR "$0: $_\n"; - if ($_ =~ m/^Caught SIG(\w+)/o) { - kill $1, $$; - } - exit(1); -}; - -if ($bad) { - exit(1); -} - -- cgit v1.2.3