Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Mar 2008 09:59:14 +0100 (CET)
From:      Ulrich Spoerlein <uspoerlein@gmail.com>
To:        FreeBSD-gnats-submit@FreeBSD.org
Cc:        stefan@FreeBSD.org
Subject:   ports/121913: [PATCH] ports-mgmt/pkg_cutleaves: Implement "visual" mode
Message-ID:  <200803200859.m2K8xEeV062261@roadrunner.spoerlein.net>
Resent-Message-ID: <200803201500.m2KF033J099054@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         121913
>Category:       ports
>Synopsis:       [PATCH] ports-mgmt/pkg_cutleaves: Implement "visual" mode
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-ports-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Thu Mar 20 15:00:03 UTC 2008
>Closed-Date:
>Last-Modified:
>Originator:     Ulrich Spoerlein
>Release:        FreeBSD 7.0-STABLE i386
>Organization:
>Environment:
System: FreeBSD roadrunner.spoerlein.net 7.0-STABLE FreeBSD 7.0-STABLE #2: Sat Mar 15 23:22:37 CET 2008
>Description:
- Update to 20080320
- Implement a "visual" mode, where the user is given the complete list of
  leaf packages inside his editor. He can then remove packages by removing
  the lines in the file, save and exit. The now missing packages will then
  be removed.

If find this easier to use than the interactive mode, where I frequently will
press enter too fast, and then have to either start all over again or do a
second run.

Feel free to change the "visual" name and the -V flag to anything you like.

Port maintainer (stefan@FreeBSD.org) is cc'd.

Generated with FreeBSD Port Tools 0.77
>How-To-Repeat:
>Fix:

--- pkg_cutleaves-20080320.patch begins here ---
? work
Index: Makefile
===================================================================
RCS file: /home/ncvs/ports/ports-mgmt/pkg_cutleaves/Makefile,v
retrieving revision 1.14
diff -u -p -u -r1.14 Makefile
--- Makefile	21 Oct 2007 08:29:06 -0000	1.14
+++ Makefile	20 Mar 2008 08:55:15 -0000
@@ -6,7 +6,7 @@
 #
 
 PORTNAME=	pkg_cutleaves
-PORTVERSION=	20071021
+PORTVERSION=	20080320
 CATEGORIES=	ports-mgmt
 MASTER_SITES=	# none
 DISTFILES=	# none
Index: files/pkg_cutleaves
===================================================================
RCS file: /home/ncvs/ports/ports-mgmt/pkg_cutleaves/files/pkg_cutleaves,v
retrieving revision 1.1
diff -u -p -u -r1.1 pkg_cutleaves
--- files/pkg_cutleaves	21 Oct 2007 08:29:06 -0000	1.1
+++ files/pkg_cutleaves	20 Mar 2008 08:55:15 -0000
@@ -1,4 +1,5 @@
 #!/usr/bin/perl
+# $Id$
 #
 # Copyright (c) 2003 Stefan Walter <sw@gegenunendlich.de>
 #
@@ -27,7 +28,7 @@
 
 # Interactive script for deinstalling "leaf" packages
 #
-# Syntax: pkg_cutleaves [-cFgLlRx]
+# Syntax: pkg_cutleaves [-cFgLlRVx]
 # Options:
 #   -c: Show comments, too; only works with '-l' (ignored otherwise)
 #   -F: Fix package db after each deinstallation run (via 'pkgdb -F')
@@ -35,8 +36,10 @@
 #   -L: Interpret exclude file as list of packages that *should* be installed
 #   -l: List leaf packages only, don't ask if they should be deinstalled
 #   -R: Autoprune new leaves 
+#   -V: Visual mode, invoke $EDITOR
 #   -x: Honor exclude list in $excludefile
 
+use File::Temp qw/ tempfile /;
 use Getopt::Std;
 use strict;
 
@@ -48,7 +51,7 @@ my $exclpattern; 
 my %leavestokeep;
 my %opt;
 
-getopts('cFgLlRx', \%opt);
+getopts('cFgLlRVx', \%opt);
 set_excl_pattern();
 
 # LIST MODE
@@ -124,31 +127,89 @@ else {
     # Initialize counter for progress status
     $i = 1;
 
-    LEAVESLOOP: foreach my $leaf (sort keys %leaves) {
-      print "Package $i of $nleaves:\n";
-      print "$leaf - $leaves{$leaf}\n";
-      print "$leaf - [keep]/(d)elete/(f)lush marked pkgs/(a)bort? ";
-      # Get first character of input, without leading whitespace
-      my ($answer) = (lc(<STDIN>) =~ m/(\S)/);
-      if ($answer eq 'd') {
-        print "** Marking $leaf for removal.\n\n";
-        $leavestocut{$leaf} = 1;
+    if ($opt{V}) {
+      # Write list, including descriptions out to temp file
+      my ($fh, $filename) = tempfile() or die "Can't create file: $!";
+      print $fh "# Delete the lines whose packages you want removed.\n";
+      print $fh "# Simply save and quit, when you are done.\n";
+      print $fh "# There are $nleaves (new) leaf packages:\n";
+      foreach my $leaf (sort keys %leaves) {
+	# XXX Calculate maximum length first instead of 40?
+	printf $fh "%-40s - %s\n", $leaf, $leaves{$leaf};
       }
-      elsif ($answer eq 'f') {
-        print "\n";
-        last LEAVESLOOP;
+      close($fh);
+
+      # Invoke $EDITOR, vi if no EDITOR set
+      my $editor = ($ENV{'EDITOR'} eq "") ? "vi" : $ENV{'EDITOR'};
+      system "$editor $filename";
+      if ($? == -1) {
+	print "failed to execute: $!\n";
+	unlink($filename);
+	exit 1;
       }
-      elsif ($answer eq 'a') {
-        print "\n";
-        $opt{aborted} = 1;
-        last ROUND;
+
+      # Read back and parse list, the diff to the original list will be removed
+      open($fh, $filename) or die "Can't reopen file: $!";
+      while(<$fh>) {
+	next if $_ =~ m/\s*#/;
+	next if $_ =~ m/^\s*$/;
+	my ($leaf, $desc) = split(/\s/, $_, 2);
+
+	if (defined $leaves{$leaf}) {
+	  delete $leaves{$leaf};
+	  $leavestokeep{$leaf} = 1;
+	} else {
+	  print "Package \"$leaf\" not known, skipping\n";
+	}
       }
-      else {
-        print "** Keeping $leaf.\n\n";
-        $leavestokeep{$leaf} = 1;
+      close($fh);
+      unlink($filename);
+
+      exit 0 if (keys %leaves == 0);
+      # print the lists of packages which will be removed, due to
+      # typos in the editor, the user might have 'removed' a package
+      # he is still interested in. This batch mode, really should get a last
+      # confirmation from the user.
+      print "The following packages will be removed:\n";
+      # XXX Use map here?
+      # XXX check empty hash FIXME
+      foreach my $leaf (sort keys %leaves) {
+	$leavestocut{$leaf} = 1;
+	print "   $leaf\n";
       }
-      $i++;
-    } # LEAVESLOOP
+      print "Are you sure? [yes]: ";
+      my ($answer) = (lc(<STDIN>) =~ /(\S)/o);
+      print "\n";
+      exit 0 if $answer =~ /n/;
+
+      # Fallthrough to actual package removal
+    } else {
+      LEAVESLOOP: foreach my $leaf (sort keys %leaves) {
+	print "Package $i of $nleaves:\n";
+	print "$leaf - $leaves{$leaf}\n";
+	print "$leaf - [keep]/(d)elete/(f)lush marked pkgs/(a)bort? ";
+	# Get first character of input, without leading whitespace
+	my ($answer) = (lc(<STDIN>) =~ m/(\S)/);
+	if ($answer eq 'd') {
+	  print "** Marking $leaf for removal.\n\n";
+	  $leavestocut{$leaf} = 1;
+	}
+	elsif ($answer eq 'f') {
+	  print "\n";
+	  last LEAVESLOOP;
+	}
+	elsif ($answer eq 'a') {
+	  print "\n";
+	  $opt{aborted} = 1;
+	  last ROUND;
+	}
+	else {
+	  print "** Keeping $leaf.\n\n";
+	  $leavestokeep{$leaf} = 1;
+	}
+	$i++;
+      } # LEAVESLOOP
+    }
 
     AUTOPRUNE: # The -R switch jump
     # Initialize 'progress meter'
Index: files/pkg_cutleaves.1
===================================================================
RCS file: /home/ncvs/ports/ports-mgmt/pkg_cutleaves/files/pkg_cutleaves.1,v
retrieving revision 1.1
diff -u -p -u -r1.1 pkg_cutleaves.1
--- files/pkg_cutleaves.1	21 Oct 2007 08:29:06 -0000	1.1
+++ files/pkg_cutleaves.1	20 Mar 2008 08:55:15 -0000
@@ -1,6 +1,6 @@
 .\" PKG_CUTLEAVES 1 "Jul 2003" FreeBSD
 .\"
-.Dd July 27, 2003
+.Dd March 20, 2008
 .Dt PKG_CUTLEAVES 1
 .Os FreeBSD
 .Sh NAME
@@ -8,7 +8,7 @@
 .Nd deinstall 'leaf' packages
 .Sh SYNOPSIS
 .Nm
-.Op Fl cFglRx
+.Op Fl cFglRVx
 .Sh DESCRIPTION
 .Nm pkg_cutleaves
 finds installed 
@@ -67,6 +67,11 @@ leaf packages.
 List leaf packages only, one per line, and don't ask for anything to be
 deinstalled.
 .Pp
+.It Fl V
+Visual mode. Will compile a list of leaf packages and invoke the users
+.Ev EDITOR .
+Lines or package names that are deleted in the editor will then be removed.
+.Pp
 .It Fl R
 Autoprune mode. Automatically deinstall non-required packages on which 
 the removed leaf packages depended. Will not remove packages on the 
@@ -132,6 +137,15 @@ will exclude all packages with names sta
 .Fl g
 option. 
 .El
+.Sh ENVIRONMENT
+The following environment variables will be used in visual mode:
+.Bl -tag -width EDITOR
+.It Ev EDITOR
+The editor specified by the string
+.Ev EDITOR
+will be invoked instead of the default editor
+.Xr vi 1 .
+.El
 .Sh SEE ALSO
 .Xr pkg_deinstall 1 ,
 .Xr pkgdb 1 ,
--- pkg_cutleaves-20080320.patch ends here ---

>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200803200859.m2K8xEeV062261>