ovs-bugtool: Add ability to prioritize files by date.
authorRaju Subramanian <rsubramanian@nicira.com>
Fri, 23 Mar 2012 22:49:32 +0000 (15:49 -0700)
committerBen Pfaff <blp@nicira.com>
Mon, 26 Mar 2012 22:41:14 +0000 (15:41 -0700)
When size limit is reached in the middle of processing a dir,
the report ends up containing oldest files. This change adds
an optional param in the plugin to prioritize newer files.

Feature #9937
Requested-by: Ronald Lee <rlee@nicira.com>
Signed-off-by: Raju Subramanian <rsubramanian@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
AUTHORS
utilities/bugtool/ovs-bugtool.in

diff --git a/AUTHORS b/AUTHORS
index fa47e505082978ab1656d1c754f23a8e21160d54..92aa47b9a533f27127cdec1dcd89d43eea3517ca 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -39,6 +39,7 @@ Neil McKee              neil.mckee@inmon.com
 Paul Fazzone            pfazzone@nicira.com
 Philippe Jung           phil.jung@free.fr
 Pravin B Shelar         pshelar@nicira.com
+Raju Subramanian        rsubramanian@nicira.com
 Ravi Kerur              Ravi.Kerur@telekom.com
 Reid Price              reid@nicira.com
 Rob Hoes                rob.hoes@citrix.com
index f78289dee1046dfaa7c0afe06bcabc92d12ab030..1e0fc7c302bfee856bdeec07d8f728de328b2ec0 100755 (executable)
@@ -319,30 +319,42 @@ def cmd_output(cap, args, label = None, filter = None):
                 label = args
         data[label] = {'cap': cap, 'cmd_args': args, 'filter': filter}
 
-def file_output(cap, path_list):
+def file_output(cap, path_list, newest_first=False):
+    """
+    If newest_first is True, the list of files in path_list is sorted
+    by file modification time in descending order, else its sorted
+    in ascending order.
+    """
     if cap in entries:
-        for p in path_list:
-            if os.path.exists(p):
-                if unlimited_data or caps[cap][MAX_SIZE] == -1 or \
-                        cap_sizes[cap] < caps[cap][MAX_SIZE]:
-                    data[p] = {'cap': cap, 'filename': p}
-                    try:
-                        s = os.stat(p)
-                        cap_sizes[cap] += s.st_size
-                    except:
-                        pass
-                else:
-                    output("Omitting %s, size constraint of %s exceeded" % (p, cap))
+        path_entries = []
+        for path in path_list:
+            try:
+                s = os.stat(path)
+            except OSError, e:
+                continue
+            path_entries.append((path, s))
+
+        mtime = lambda(path, stat): stat.st_mtime
+        path_entries.sort(key=mtime, reverse=newest_first)
+        for p in path_entries:
+            if unlimited_data or caps[cap][MAX_SIZE] == -1 or \
+                    cap_sizes[cap] < caps[cap][MAX_SIZE]:
+                data[p] = {'cap': cap, 'filename': p[0]}
+                cap_sizes[cap] += p[1].st_size
+            else:
+                output("Omitting %s, size constraint of %s exceeded" % (p[0], cap))
 
-def tree_output(cap, path, pattern = None, negate = False):
+def tree_output(cap, path, pattern=None, negate=False, newest_first=False):
+    """
+    Walks the directory tree rooted at path. Files in current dir are processed
+    before files in sub-dirs.
+    """
     if cap in entries:
         if os.path.exists(path):
-            for f in os.listdir(path):
-                fn = os.path.join(path, f)
-                if os.path.isfile(fn) and matches(fn, pattern, negate):
-                    file_output(cap, [fn])
-                elif os.path.isdir(fn):
-                    tree_output(cap, fn, pattern, negate)
+            for root, dirs, files in os.walk(path):
+                fns = [fn for fn in [os.path.join(root, f) for f in files]
+                       if os.path.isfile(fn) and matches(fn, pattern, negate)]
+                file_output(cap, fns, newest_first=newest_first)
 
 def func_output(cap, label, func):
     if cap in entries:
@@ -876,12 +888,16 @@ def load_plugins(just_capabilities = False):
 
             for el in xmldoc.documentElement.getElementsByTagName("*"):
                 if el.tagName == "files":
-                    file_output(dir, getText(el.childNodes).split())
+                    newest_first = getBoolAttr(el, 'newest_first')
+                    file_output(dir, getText(el.childNodes).split(),
+                                newest_first=newest_first)
                 elif el.tagName == "directory":
                     pattern = el.getAttribute("pattern")
                     if pattern == '': pattern = None
                     negate = getBoolAttr(el, 'negate')
-                    tree_output(dir, getText(el.childNodes), pattern and re.compile(pattern) or None, negate)
+                    newest_first = getBoolAttr(el, 'newest_first')
+                    tree_output(dir, getText(el.childNodes), pattern and re.compile(pattern) or None,
+                                negate=negate, newest_first=newest_first)
                 elif el.tagName == "command":
                     label = el.getAttribute("label")
                     if label == '': label = None