Revision 57d56130

b/lib/utils.py
1715 1715
                 source=constants.LOCALHOST_IP_ADDRESS)
1716 1716

  
1717 1717

  
1718
def ListVisibleFiles(path):
1718
def ListVisibleFiles(path, sort=True):
1719 1719
  """Returns a list of visible files in a directory.
1720 1720

  
1721 1721
  @type path: str
1722 1722
  @param path: the directory to enumerate
1723
  @type sort: boolean
1724
  @param sort: whether to provide a sorted output
1723 1725
  @rtype: list
1724 1726
  @return: the list of all files not starting with a dot
1725 1727
  @raise ProgrammerError: if L{path} is not an absolue and normalized path
......
1729 1731
    raise errors.ProgrammerError("Path passed to ListVisibleFiles is not"
1730 1732
                                 " absolute/normalized: '%s'" % path)
1731 1733
  files = [i for i in os.listdir(path) if not i.startswith(".")]
1732
  files.sort()
1734
  if sort:
1735
    files.sort()
1733 1736
  return files
1734 1737

  
1735 1738

  
b/test/ganeti.utils_unittest.py
1334 1334
  def tearDown(self):
1335 1335
    shutil.rmtree(self.path)
1336 1336

  
1337
  def _test(self, files, expected):
1338
    # Sort a copy
1339
    expected = expected[:]
1340
    expected.sort()
1341

  
1337
  def _CreateFiles(self, files):
1342 1338
    for name in files:
1343
      f = open(os.path.join(self.path, name), 'w')
1344
      try:
1345
        f.write("Test\n")
1346
      finally:
1347
        f.close()
1339
      utils.WriteFile(os.path.join(self.path, name), data="test")
1348 1340

  
1341
  def _test(self, files, expected):
1342
    self._CreateFiles(files)
1349 1343
    found = ListVisibleFiles(self.path)
1350
    found.sort()
1351

  
1352
    self.assertEqual(found, expected)
1344
    # by default ListVisibleFiles sorts its output
1345
    self.assertEqual(found, sorted(expected))
1353 1346

  
1354 1347
  def testAllVisible(self):
1355 1348
    files = ["a", "b", "c"]
......
1366 1359
    expected = ["a", "b"]
1367 1360
    self._test(files, expected)
1368 1361

  
1362
  def testForceSort(self):
1363
    files = ["c", "b", "a"]
1364
    self._CreateFiles(files)
1365
    found = ListVisibleFiles(self.path, sort=True)
1366
    self.assertEqual(found, sorted(files))
1367

  
1368
  def testForceNonSort(self):
1369
    files = ["c", "b", "a"]
1370
    self._CreateFiles(files)
1371
    found = ListVisibleFiles(self.path, sort=False)
1372
    # We can't actually check that they weren't sorted, because they might come
1373
    # out sorted by chance
1374
    self.assertEqual(set(found), set(files))
1375

  
1369 1376
  def testNonAbsolutePath(self):
1370 1377
    self.failUnlessRaises(errors.ProgrammerError, ListVisibleFiles, "abc")
1371 1378

  

Also available in: Unified diff