summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2007-06-01 14:58:30 +0000
committerMarc Espie <espie@cvs.openbsd.org>2007-06-01 14:58:30 +0000
commit34efd479fa8bad945ccf900ff7b2ea62520b5ab9 (patch)
tree45fa87d5e13357c168aceffa96b66199b0fd125e
parent323bb16b2bd94d254e8211d526fa04629fb1aec7 (diff)
refactor ProgressMeter code to create objects, put these into state.
Create a common state class for delete and add, put the progressmeter creation in there. neat effect: we don't have a progressmeter, we use a stub class instead and *never* pull the code in at all.
-rw-r--r--usr.sbin/pkg_add/OpenBSD/Add.pm4
-rw-r--r--usr.sbin/pkg_add/OpenBSD/Delete.pm8
-rw-r--r--usr.sbin/pkg_add/OpenBSD/PackageInfo.pm5
-rw-r--r--usr.sbin/pkg_add/OpenBSD/ProgressMeter.pm28
-rw-r--r--usr.sbin/pkg_add/OpenBSD/Replace.pm4
-rw-r--r--usr.sbin/pkg_add/OpenBSD/SharedItems.pm22
-rw-r--r--usr.sbin/pkg_add/OpenBSD/Update.pm15
-rw-r--r--usr.sbin/pkg_add/OpenBSD/Vstat.pm51
-rw-r--r--usr.sbin/pkg_add/pkg_add31
-rw-r--r--usr.sbin/pkg_add/pkg_delete20
10 files changed, 105 insertions, 83 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/Add.pm b/usr.sbin/pkg_add/OpenBSD/Add.pm
index 17944b11e3d..d22e2a76361 100644
--- a/usr.sbin/pkg_add/OpenBSD/Add.pm
+++ b/usr.sbin/pkg_add/OpenBSD/Add.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: Add.pm,v 1.68 2007/05/31 11:05:41 espie Exp $
+# $OpenBSD: Add.pm,v 1.69 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
#
@@ -128,7 +128,7 @@ sub install_and_progress
if ($state->{interrupted}) {
die "Interrupted";
}
- $self->mark_progress($donesize, $totsize);
+ $self->mark_progress($state->progress, $donesize, $totsize);
}
sub install
diff --git a/usr.sbin/pkg_add/OpenBSD/Delete.pm b/usr.sbin/pkg_add/OpenBSD/Delete.pm
index 202312001ca..492d0e9819f 100644
--- a/usr.sbin/pkg_add/OpenBSD/Delete.pm
+++ b/usr.sbin/pkg_add/OpenBSD/Delete.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: Delete.pm,v 1.57 2007/05/31 13:33:17 espie Exp $
+# $OpenBSD: Delete.pm,v 1.58 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
#
@@ -107,7 +107,7 @@ sub remove_packing_info
sub delete_package
{
my ($pkgname, $state) = @_;
- OpenBSD::ProgressMeter::message("reading plist");
+ $state->progress->message("reading plist");
my $plist = OpenBSD::PackingList->from_installation($pkgname) or
Fatal "Bad package";
if (!defined $plist->pkgname) {
@@ -138,7 +138,7 @@ sub delete_plist
$plist->register_manpage($state);
manpages_unindex($state);
$plist->delete_and_progress($state, \$donesize, $totsize);
- OpenBSD::ProgressMeter::next();
+ $state->progress->next;
if ($plist->has(UNDISPLAY)) {
$plist->get(UNDISPLAY)->prepare($state);
}
@@ -181,7 +181,7 @@ sub delete_and_progress
{
my ($self, $state, $donesize, $totsize) = @_;
$self->delete($state);
- $self->mark_progress($donesize, $totsize);
+ $self->mark_progress($state->progress, $donesize, $totsize);
}
sub record_shared
diff --git a/usr.sbin/pkg_add/OpenBSD/PackageInfo.pm b/usr.sbin/pkg_add/OpenBSD/PackageInfo.pm
index be7fff8d12b..5525a9cda66 100644
--- a/usr.sbin/pkg_add/OpenBSD/PackageInfo.pm
+++ b/usr.sbin/pkg_add/OpenBSD/PackageInfo.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: PackageInfo.pm,v 1.31 2007/05/31 13:11:21 espie Exp $
+# $OpenBSD: PackageInfo.pm,v 1.32 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
#
@@ -238,8 +238,7 @@ sub solve_installed_names
}
} else {
if ($state->{interactive}) {
- require OpenBSD::ProgressMeter;
-
+ require OpenBSD::Interactive;
my $result = OpenBSD::Interactive::ask_list('Choose one package', 1, ("<None>", sort @l));
push(@$new, $result) if $result ne '<None>';
$seen->{$result} = 1;
diff --git a/usr.sbin/pkg_add/OpenBSD/ProgressMeter.pm b/usr.sbin/pkg_add/OpenBSD/ProgressMeter.pm
index ba793b3cd3d..0f9249bf659 100644
--- a/usr.sbin/pkg_add/OpenBSD/ProgressMeter.pm
+++ b/usr.sbin/pkg_add/OpenBSD/ProgressMeter.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: ProgressMeter.pm,v 1.10 2007/04/15 10:17:29 espie Exp $
+# $OpenBSD: ProgressMeter.pm,v 1.11 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2004-2007 Marc Espie <espie@openbsd.org>
#
@@ -23,7 +23,6 @@ package OpenBSD::ProgressMeter;
my $header;
my $lastdisplay = '';
my $isatty;
-my $enabled = 0;
my $continued = 0;
# unless we know better
@@ -33,6 +32,13 @@ my $playfield;
my $wsz_format = 'SSSS';
our %sizeof;
+sub new
+{
+ my $class = shift;
+ $isatty = -t STDERR;
+ return bless {}, $class;
+}
+
sub find_window_size
{
return if defined $width;
@@ -59,20 +65,10 @@ sub compute_playfield
}
}
-sub enable
-{
- $enabled = 1;
-}
-
sub set_header
{
+ my $self = shift;
$header = shift;
- if (!$enabled) {
- $isatty = 0;
- }
- if (!defined $isatty) {
- $isatty = -t STDERR;
- }
if ($isatty) {
find_window_size();
compute_playfield();
@@ -90,6 +86,7 @@ sub set_header
sub message
{
+ my $self = shift;
return unless $isatty;
my $message = shift;
my $d;
@@ -109,6 +106,7 @@ sub message
sub show
{
+ my $self = shift;
return unless $isatty;
my ($current, $total) = @_;
my $d;
@@ -127,14 +125,16 @@ sub show
sub clear
{
+ my $self = shift;
return unless $isatty;
print STDERR ' 'x length($lastdisplay), "\r";
}
sub next
{
+ my $self = shift;
return unless $isatty;
- clear;
+ $self->clear;
print STDERR"$header: complete\n";
}
diff --git a/usr.sbin/pkg_add/OpenBSD/Replace.pm b/usr.sbin/pkg_add/OpenBSD/Replace.pm
index 4cd72ec9e2f..1344612b02b 100644
--- a/usr.sbin/pkg_add/OpenBSD/Replace.pm
+++ b/usr.sbin/pkg_add/OpenBSD/Replace.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: Replace.pm,v 1.26 2007/06/01 12:19:00 espie Exp $
+# $OpenBSD: Replace.pm,v 1.27 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2004-2006 Marc Espie <espie@openbsd.org>
#
@@ -61,7 +61,7 @@ sub extract_and_progress
if ($state->{interrupted}) {
die "Interrupted";
}
- $self->mark_progress($donesize, $totsize);
+ $self->mark_progress($state->progress, $donesize, $totsize);
}
package OpenBSD::PackingElement::FileBase;
diff --git a/usr.sbin/pkg_add/OpenBSD/SharedItems.pm b/usr.sbin/pkg_add/OpenBSD/SharedItems.pm
index 35373c232d2..d722b7f62c4 100644
--- a/usr.sbin/pkg_add/OpenBSD/SharedItems.pm
+++ b/usr.sbin/pkg_add/OpenBSD/SharedItems.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: SharedItems.pm,v 1.10 2007/05/22 10:11:59 espie Exp $
+# $OpenBSD: SharedItems.pm,v 1.11 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2004-2006 Marc Espie <espie@openbsd.org>
#
@@ -19,20 +19,20 @@ use strict;
use warnings;
package OpenBSD::SharedItems;
-use OpenBSD::ProgressMeter;
use OpenBSD::Error;
use OpenBSD::PackageInfo;
use OpenBSD::PackingList;
sub find_items_in_installed_packages
{
+ my $progress = shift;
my $db = OpenBSD::SharedItemsRecorder->new;
my @list = installed_packages();
my $total = @list;
- OpenBSD::ProgressMeter::set_header("Read shared items");
+ $progress->set_header("Read shared items");
my $done = 0;
for my $e (@list) {
- OpenBSD::ProgressMeter::show($done, $total);
+ $progress->show($done, $total);
my $plist = OpenBSD::PackingList->from_installation($e,
\&OpenBSD::PackingList::SharedItemsOnly) or next;
$plist->record_shared($db, $e);
@@ -45,10 +45,10 @@ sub cleanup
{
my ($recorder, $state) = @_;
- my $remaining = find_items_in_installed_packages();
+ my $remaining = find_items_in_installed_packages($state->progress);
- OpenBSD::ProgressMeter::clear();
- OpenBSD::ProgressMeter::set_header("Clean shared items");
+ $state->progress->clear;
+ $state->progress->set_header("Clean shared items");
my $h = $recorder->{dirs};
my $u = $recorder->{users};
my $g = $recorder->{groups};
@@ -60,7 +60,7 @@ sub cleanup
if (defined $h) {
for my $d (sort {$b cmp $a} keys %$h) {
- OpenBSD::ProgressMeter::show($done, $total);
+ $state->progress->show($done, $total);
my $realname = $state->{destdir}.$d;
if ($remaining->{dirs}->{$realname}) {
for my $i (@{$h->{$d}}) {
@@ -82,7 +82,7 @@ sub cleanup
}
if (defined $u) {
while (my ($user, $pkgname) = each %$u) {
- OpenBSD::ProgressMeter::show($done, $total);
+ $state->progress->show($done, $total);
next if $remaining->{users}->{$user};
if ($state->{extra}) {
System("/usr/sbin/userdel", $user);
@@ -95,7 +95,7 @@ sub cleanup
}
if (defined $g) {
while (my ($group, $pkgname) = each %$g) {
- OpenBSD::ProgressMeter::show($done, $total);
+ $state->progress->show($done, $total);
next if $remaining->{groups}->{$group};
if ($state->{extra}) {
System("/usr/sbin/groupdel", $group);
@@ -106,7 +106,7 @@ sub cleanup
$done++;
}
}
- OpenBSD::ProgressMeter::next();
+ $state->progress->next;
}
package OpenBSD::PackingElement;
diff --git a/usr.sbin/pkg_add/OpenBSD/Update.pm b/usr.sbin/pkg_add/OpenBSD/Update.pm
index 023fd165487..61d7d1212f5 100644
--- a/usr.sbin/pkg_add/OpenBSD/Update.pm
+++ b/usr.sbin/pkg_add/OpenBSD/Update.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: Update.pm,v 1.77 2007/05/14 11:22:00 espie Exp $
+# $OpenBSD: Update.pm,v 1.78 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2004-2006 Marc Espie <espie@openbsd.org>
#
@@ -18,7 +18,6 @@ use strict;
use warnings;
package OpenBSD::Update;
-use OpenBSD::ProgressMeter;
use OpenBSD::Interactive;
use OpenBSD::PackageInfo;
use OpenBSD::PackageLocator;
@@ -59,7 +58,7 @@ sub process_package
{
my ($self, $pkgname, $state) = @_;
if ($pkgname =~ m/^(?:\.libs|partial)\-/) {
- OpenBSD::ProgressMeter::clear();
+ $state->progress->clear;
print "Not updating $pkgname, remember to clean it\n";
return;
}
@@ -128,7 +127,7 @@ sub process_package
}
if (@l == 1) {
if ($state->{forced}->{pkgpath}) {
- OpenBSD::ProgressMeter::clear();
+ $state->progress->clear;
print "Directly updating $pkgname -> ", $l[0], "\n";
$self->add2updates($l[0]);
return;
@@ -136,13 +135,13 @@ sub process_package
if (defined $found && $found eq $l[0] &&
!$plist->uses_old_libs) {
my $msg = "No need to update $pkgname";
- OpenBSD::ProgressMeter::message($msg);
+ $state->progress->message($msg);
print "$msg\n" if $state->{beverbose};
return;
}
}
- OpenBSD::ProgressMeter::clear();
+ $state->progress->clear;
print "Candidates for updating $pkgname -> ", join(' ', @l), "\n";
if (@l == 1) {
@@ -175,11 +174,11 @@ sub process
@list = OpenBSD::Requiring->compute_closure(@list);
}
- OpenBSD::ProgressMeter::set_header("Looking for updates");
+ $state->progress->set_header("Looking for updates");
for my $pkgname (@list) {
$self->process_package($pkgname, $state);
}
- OpenBSD::ProgressMeter::next();
+ $state->progress->next;
}
1;
diff --git a/usr.sbin/pkg_add/OpenBSD/Vstat.pm b/usr.sbin/pkg_add/OpenBSD/Vstat.pm
index 961d19c6cd0..8d805644cc6 100644
--- a/usr.sbin/pkg_add/OpenBSD/Vstat.pm
+++ b/usr.sbin/pkg_add/OpenBSD/Vstat.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: Vstat.pm,v 1.30 2007/05/30 14:04:51 espie Exp $
+# $OpenBSD: Vstat.pm,v 1.31 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
#
@@ -267,6 +267,50 @@ sub cleanup
OpenBSD::SharedItems::cleanup($self, $state);
}
+package OpenBSD::pkg_foo::State;
+use OpenBSD::Error;
+our @ISA=(qw(OpenBSD::Error));
+
+sub progress
+{
+ my $self = shift;
+ return $self->{progressmeter};
+}
+
+sub setup_progressmeter
+{
+ my ($self, $opt_x) = @_;
+ if (!$opt_x && !$self->{beverbose}) {
+ require OpenBSD::ProgressMeter;
+ $self->{progressmeter} = OpenBSD::ProgressMeter->new;
+ } else {
+ $self->{progressmeter} = bless {}, "OpenBSD::StubProgress";
+ }
+}
+
+sub check_root
+{
+ my $state = shift;
+ if ($< && !$state->{forced}->{nonroot}) {
+ if ($state->{not}) {
+ Warn "$0 should be run as root\n";
+ } else {
+ Fatal "$0 must be run as root";
+ }
+ }
+}
+
+package OpenBSD::StubProgress;
+sub clear {}
+
+sub show {}
+
+sub message {}
+
+sub next {}
+
+sub set_header {}
+
# fairly non-descriptive name. Used to store various package information
# during installs and updates.
package OpenBSD::Handle;
@@ -406,11 +450,10 @@ sub mark_progress
package OpenBSD::PackingElement::FileBase;
sub mark_progress
{
- my ($self, $donesize, $totsize) = @_;
+ my ($self, $progress, $donesize, $totsize) = @_;
return unless defined $self->{size};
- require OpenBSD::ProgressMeter;
$$donesize += $self->{size};
- OpenBSD::ProgressMeter::show($$donesize, $totsize);
+ $progress->show($$donesize, $totsize);
}
1;
diff --git a/usr.sbin/pkg_add/pkg_add b/usr.sbin/pkg_add/pkg_add
index 4fd61cf6b4a..0a8bd46f2ee 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.299 2007/05/31 16:50:43 espie Exp $
+# $OpenBSD: pkg_add,v 1.300 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
#
@@ -29,7 +29,6 @@ use OpenBSD::PkgCfl;
use OpenBSD::Vstat;
use OpenBSD::Getopt;
use OpenBSD::Error;
-use OpenBSD::ProgressMeter;
use OpenBSD::Interactive;
use OpenBSD::Add;
use OpenBSD::SharedLibs;
@@ -41,7 +40,7 @@ our %forced = ();
our $not;
package OpenBSD::pkg_add::State;
-our @ISA=(qw(OpenBSD::Error));
+our @ISA=(qw(OpenBSD::pkg_foo::State));
# one-level dependencies tree, for nicer printouts
sub build_deptree
@@ -113,7 +112,7 @@ sub setup_header
if (@toreplace) {
$header.=" (replacing ". join(', ', (map {$_->{pkgname}} @toreplace)). ")";
}
- if (!OpenBSD::ProgressMeter::set_header($header)) {
+ if (!$state->progress->set_header($header)) {
print $state->{not} ? "Pretending to add " : "Adding ";
print $header;
if ($state->{do_faked}) {
@@ -371,7 +370,7 @@ sub really_add
if ($replacing) {
require OpenBSD::Replace;
- OpenBSD::ProgressMeter::set_header("$pkgname (extracting)");
+ $state->progress->set_header("$pkgname (extracting)");
OpenBSD::Replace::save_old_libraries($set, $state);
@@ -383,14 +382,14 @@ sub really_add
$errors++;
}
};
- OpenBSD::ProgressMeter::next();
+ $state->progress->next;
if ($state->{interrupted} || $errors) {
Fatal partial_install("Installation of $pkgname failed",
$set, $state);
}
for my $op (@toreplace) {
- OpenBSD::ProgressMeter::set_header($op->pkgname." (deleting)");
+ $state->progress->set_header($op->pkgname." (deleting)");
$state->set_pkgname($op->pkgname);
require OpenBSD::Search;
if (OpenBSD::Search::PkgSpec->new("mozilla-thunderbird-<=1.0.2p0")->filter($op->pkgname)) {
@@ -416,7 +415,7 @@ sub really_add
}
# Here there should be code to handle old libs
- OpenBSD::ProgressMeter::set_header("$pkgname (installing)");
+ $state->progress->set_header("$pkgname (installing)");
$state->set_pkgname($pkgname);
}
@@ -431,7 +430,7 @@ sub really_add
$errors++;
}
};
- OpenBSD::ProgressMeter::next();
+ $state->progress->next;
unlink($plist->infodir.CONTENTS);
if ($state->{interrupted} || $errors) {
@@ -682,19 +681,11 @@ $state->{allow_replacing} = $opt_r || $opt_u;
if (@ARGV == 0 && !$opt_u) {
Usage "Missing pkgname";
}
-if (!$opt_x && !$state->{beverbose}) {
- OpenBSD::ProgressMeter::enable();
-}
-
-if ($< && !$forced{nonroot}) {
- if ($state->{not}) {
- Warn "$0 should be run as root\n";
- } else {
- Fatal "must be run as root";
- }
-}
lock_db($state->{not});
+$state->setup_progressmeter($opt_x);
+$state->check_root;
+
my @todo = ();
diff --git a/usr.sbin/pkg_add/pkg_delete b/usr.sbin/pkg_add/pkg_delete
index 3da5a1f66a8..8f8fc3b879d 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.105 2007/05/31 13:33:17 espie Exp $
+# $OpenBSD: pkg_delete,v 1.106 2007/06/01 14:58:29 espie Exp $
#
# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
#
@@ -22,7 +22,6 @@ use OpenBSD::Getopt;
use OpenBSD::PackingList;
use OpenBSD::RequiredBy;
use OpenBSD::Error;
-use OpenBSD::ProgressMeter;
use OpenBSD::Interactive;
use OpenBSD::Delete;
use OpenBSD::PackageInfo;
@@ -33,7 +32,7 @@ our %forced = ();
our $not;
package OpenBSD::pkg_delete::State;
-our @ISA=(qw(OpenBSD::Error));
+our @ISA=(qw(OpenBSD::pkg_foo::State));
package main;
@@ -106,17 +105,8 @@ if ($opt_B eq '') {
$ENV{'PKG_DELETE_EXTRA'} = $state->{extra} ? "Yes" : "No";
lock_db($opt_n);
-if (!$opt_x && !$state->{beverbose}) {
- OpenBSD::ProgressMeter::enable();
-}
-
-if ($< && !$forced{nonroot}) {
- if ($state->{not}) {
- Warn "$0 should be run as root\n";
- } else {
- Fatal "$0 must be run as root";
- }
-}
+$state->setup_progressmeter($opt_x);
+$state->check_root;
# First, resolve pkg names
@@ -193,7 +183,7 @@ eval {
next;
}
}
- if (!OpenBSD::ProgressMeter::set_header($pkgname)) {
+ if (!$state->progress->set_header($pkgname)) {
print $opt_n ? "Pretending to delete " : "Deleting ",
"$pkgname\n";
}