From eedbda4b07654d412eb10bc87197913219368b91 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Mon, 8 Oct 2007 08:59:26 +0000
Subject: [PATCH] Add function to list files in a directory, excluding those
 beginning with a dot.

Reviewed-by: iustinp
---
 lib/backend.py                |  6 ++---
 lib/utils.py                  |  7 ++++++
 test/ganeti.utils_unittest.py | 47 +++++++++++++++++++++++++++++++++--
 3 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index 500feba25..396b59d7d 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -966,7 +966,7 @@ def DiagnoseOS(top_dirs=None):
   for dir in top_dirs:
     if os.path.isdir(dir):
       try:
-        f_names = os.listdir(dir)
+        f_names = utils.ListVisibleFiles(dir)
       except EnvironmentError, err:
         logger.Error("Can't list the OS directory %s: %s" % (dir,str(err)))
         break
@@ -1282,7 +1282,7 @@ def ListExports():
 
   """
   if os.path.isdir(constants.EXPORT_DIR):
-    return os.listdir(constants.EXPORT_DIR)
+    return utils.ListVisibleFiles(constants.EXPORT_DIR)
   else:
     return []
 
@@ -1392,7 +1392,7 @@ class HooksRunner(object):
     subdir = "%s-%s.d" % (hpath, suffix)
     dir_name = "%s/%s" % (self._BASE_DIR, subdir)
     try:
-      dir_contents = os.listdir(dir_name)
+      dir_contents = utils.ListVisibleFiles(dir_name)
     except OSError, err:
       # must log
       return rr
diff --git a/lib/utils.py b/lib/utils.py
index bc9a1046f..be93d198f 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -848,3 +848,10 @@ def TcpPing(source, target, port, timeout=10, live_port_needed=True):
     success = (not live_port_needed) and (errcode == errno.ECONNREFUSED)
 
   return success
+
+
+def ListVisibleFiles(path):
+  """Returns a list of all visible files in a directory.
+
+  """
+  return [i for i in os.listdir(path) if not i.startswith(".")]
diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py
index 1ef9f9cf6..b9a93b4ea 100755
--- a/test/ganeti.utils_unittest.py
+++ b/test/ganeti.utils_unittest.py
@@ -28,13 +28,14 @@ import tempfile
 import os.path
 import md5
 import socket
-
+import shutil
 
 import ganeti
 from ganeti.utils import IsProcessAlive, Lock, Unlock, RunCmd, \
      RemoveFile, CheckDict, MatchNameComponent, FormatUnit, \
      ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \
-     ShellQuote, ShellQuoteArgs, _ParseIpOutput, TcpPing
+     ShellQuote, ShellQuoteArgs, _ParseIpOutput, TcpPing, \
+     ListVisibleFiles
 from ganeti.errors import LockError, UnitParseError
 
 
@@ -522,5 +523,47 @@ class TestTcpPingDeaf(unittest.TestCase):
                  "failed to ping alive host on deaf port")
 
 
+class TestListVisibleFiles(unittest.TestCase):
+  """Test case for ListVisibleFiles"""
+
+  def setUp(self):
+    self.path = tempfile.mkdtemp()
+
+  def tearDown(self):
+    shutil.rmtree(self.path)
+
+  def _test(self, files, expected):
+    # Sort a copy
+    expected = expected[:]
+    expected.sort()
+
+    for name in files:
+      f = open(os.path.join(self.path, name), 'w')
+      try:
+        f.write("Test\n")
+      finally:
+        f.close()
+
+    found = ListVisibleFiles(self.path)
+    found.sort()
+
+    self.assertEqual(found, expected)
+
+  def testAllVisible(self):
+    files = ["a", "b", "c"]
+    expected = files
+    self._test(files, expected)
+
+  def testNoneVisible(self):
+    files = [".a", ".b", ".c"]
+    expected = []
+    self._test(files, expected)
+
+  def testSomeVisible(self):
+    files = ["a", "b", ".c"]
+    expected = ["a", "b"]
+    self._test(files, expected)
+
+
 if __name__ == '__main__':
   unittest.main()
-- 
GitLab