15365 pkg history could use a way to show the most recent N entries in139
authorRichard Lowe <richlowe@richlowe.net>
Mon, 03 May 2010 21:54:24 -0400
changeset 1889 e670eae1a5f4
parent 1888 2f3d39c3000e
child 1890 011afb71c52a
15365 pkg history could use a way to show the most recent N entries
src/client.py
src/man/pkg.1.txt
src/tests/cli/t_pkg_history.py
--- a/src/client.py	Thu May 06 08:39:26 2010 -0700
+++ b/src/client.py	Mon May 03 21:54:24 2010 -0400
@@ -19,8 +19,11 @@
 #
 # CDDL HEADER END
 #
-# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
-# Use is subject to license terms.
+
+#
+# Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
+#
+
 #
 # pkg - package system client utility
 #
@@ -189,7 +192,7 @@
             [publisher]
         pkg unset-publisher publisher ...
         pkg publisher [-HPn] [publisher ...]
-        pkg history [-Hl]
+        pkg history [-Hl] [-n number]
         pkg purge-history
         pkg rebuild-index
 
@@ -3523,13 +3526,26 @@
 
         omit_headers = False
         long_format = False
-
-        opts, pargs = getopt.getopt(args, "Hl")
+        display_limit = None    # Infinite
+
+        opts, pargs = getopt.getopt(args, "Hln:")
         for opt, arg in opts:
                 if opt == "-H":
                         omit_headers = True
                 elif opt == "-l":
                         long_format = True
+                elif opt == "-n":
+                        try:
+                                display_limit = int(arg)
+                        except ValueError:
+                                logger.error(
+                                    _("Argument to -n must be numeric"))
+                                return EXIT_BADOPT
+
+                        if display_limit <= 0:
+                                logger.error(
+                                    _("Argument to -n must be positive"))
+                                return EXIT_BADOPT
 
         if omit_headers and long_format:
                 usage(_("-H and -l may not be combined"), cmd="history")
@@ -3543,7 +3559,13 @@
                 # Nothing to display.
                 return EXIT_OK
 
-        for entry in sorted(os.listdir(img.history.path)):
+        if display_limit:
+                n = -display_limit
+                entries = sorted(os.listdir(img.history.path))[n:]
+        else:
+                entries = sorted(os.listdir(img.history.path))
+
+        for entry in entries:
                 # Load the history entry.
                 try:
                         he = history.History(root_dir=img.history.root_dir,
--- a/src/man/pkg.1.txt	Thu May 06 08:39:26 2010 -0700
+++ b/src/man/pkg.1.txt	Mon May 03 21:54:24 2010 -0400
@@ -58,7 +58,7 @@
      /usr/bin/pkg unset-publisher publisher ...
      /usr/bin/pkg publisher [-HPn] [publisher ...]
 
-     /usr/bin/pkg history [-Hl]
+     /usr/bin/pkg history [-Hl] [-n number]
      /usr/bin/pkg purge-history
 
      /usr/bin/pkg rebuild-index
@@ -656,14 +656,19 @@
 
           With -n, display only enabled publishers.
 
-     history [-Hl]
-          Displays the command history of the applicable image.  With -H, omit
-          the headers from the listing.  With -l, display log records in long
-          format, which in addition to the standard format, includes the outcome
-          of the command, when the command completed, version and name of the
-          client used, what user performed the operation, and any errors that
+     history [-Hl] [-n number]
+          Display the command history of the applicable image.  
+
+          With -H, omit the headers from the listing.
+
+          With -l, display log records in long format, which, in addition to
+          the standard format, includes the outcome of the command, the time
+          the command completed, the version and name of the client used, the
+          name of the user who performed the operation, and any errors that
           were encountered while executing the command.
 
+          With -n, display only the specified number of most recent entries.
+
      purge-history
           Deletes all existing history information.
 
--- a/src/tests/cli/t_pkg_history.py	Thu May 06 08:39:26 2010 -0700
+++ b/src/tests/cli/t_pkg_history.py	Mon May 03 21:54:24 2010 -0400
@@ -21,8 +21,7 @@
 #
 
 #
-# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
-# Use is subject to license terms.
+# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 #
 
 import testutils
@@ -78,6 +77,10 @@
                 self.pkg("history")
                 self.pkg("history -l")
                 self.pkg("history -H")
+                self.pkg("history -n 5")
+                self.pkg("history -n foo", exit=2)
+                self.pkg("history -n -5", exit=2)
+                self.pkg("history -n 0", exit=2)
                 self.pkg("history -lH", exit=2)
 
         def test_2_history_record(self):
@@ -89,9 +92,9 @@
                 commands = [
                     ("install foo", 0),
                     ("uninstall foo", 0),
-                    ("image-update", 4), 
+                    ("image-update", 4),
                     ("set-publisher -O " + durl2 + " test2", 0),
-                    ("set-publisher -P test1", 0), 
+                    ("set-publisher -P test1", 0),
                     ("set-publisher -m " + durl2 + " test1", 0),
                     ("set-publisher -M " + durl2 + " test1", 0),
                     ("unset-publisher test2", 0),
@@ -278,6 +281,31 @@
                                 raise RuntimeError("Command: %s wasn't recorded,"
                                     " o:%s" % (cmd, o))
 
+        def test_9_history_limit(self):
+                """Verify limiting the number of records to output
+                """
+
+                #
+                # Make sure we have a nice number of entries with which to
+                # experiment.
+                #
+                for i in xrange(5):
+                        self.pkg("install pkg%d" % i, exit=1)
+                self.pkg("history -Hn 3")
+                self.assertEqual(len(self.output.splitlines()), 3)
+
+                self.pkg("history -ln 3")
+                lines = self.output.splitlines()
+                nentries = len([l for l in lines if l.find("Operation:") >= 0])
+                self.assertEqual(nentries, 3)
+
+                count = len(os.listdir(
+                    os.path.join(self.get_img_path(), "var", "pkg", "history")))
+
+                # Asking for too many objects should return the full set
+                self.pkg("history -Hn %d" % (count + 5))
+                self.assertEqual(len(self.output.splitlines()), count)
+
 
 if __name__ == "__main__":
         unittest.main()