Commit eedbda4b authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Add function to list files in a directory, excluding those beginning with a

dot.

Reviewed-by: iustinp
parent 0ee60a28
......@@ -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
......
......@@ -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(".")]
......@@ -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()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment