[cedar-backup-svn] SF.net SVN: cedar-backup: [913] cedar-backup2/trunk
Brought to you by:
pronovic
|
From: <pro...@us...> - 2008-04-28 02:38:12
|
Revision: 913
http://cedar-backup.svn.sourceforge.net/cedar-backup/?rev=913&view=rev
Author: pronovic
Date: 2008-04-27 19:38:06 -0700 (Sun, 27 Apr 2008)
Log Message:
-----------
Implement the dereferenceLink() function
Modified Paths:
--------------
cedar-backup2/trunk/CedarBackup2/util.py
cedar-backup2/trunk/test/utiltests.py
Modified: cedar-backup2/trunk/CedarBackup2/util.py
===================================================================
--- cedar-backup2/trunk/CedarBackup2/util.py 2008-04-26 19:25:49 UTC (rev 912)
+++ cedar-backup2/trunk/CedarBackup2/util.py 2008-04-28 02:38:06 UTC (rev 913)
@@ -1987,3 +1987,22 @@
os.environ[LANG_VAR] = DEFAULT_LANGUAGE
return os.environ.copy()
+
+#############################
+# dereferenceLink() function
+#############################
+
+def dereferenceLink(path, absolute=True):
+ """
+ Deference a soft link, optionally normalizing it to an absolute path.
+ @param path: Path of link to dereference
+ @param absolute: Whether to normalize the result to an absolute path
+ @return: Dereferenced path, or original path if original is not a link.
+ """
+ if os.path.islink(path):
+ result = os.readlink(path)
+ if absolute and not os.path.isabs(result):
+ result = os.path.abspath(os.path.join(os.path.dirname(path), result))
+ return result
+ return path
+
Modified: cedar-backup2/trunk/test/utiltests.py
===================================================================
--- cedar-backup2/trunk/test/utiltests.py 2008-04-26 19:25:49 UTC (rev 912)
+++ cedar-backup2/trunk/test/utiltests.py 2008-04-28 02:38:06 UTC (rev 913)
@@ -82,13 +82,14 @@
import logging
from os.path import isdir
-from CedarBackup2.testutil import findResources, removedir, platformHasEcho, platformWindows, captureOutput
+from CedarBackup2.testutil import findResources, removedir, extractTar, buildPath, captureOutput
+from CedarBackup2.testutil import platformHasEcho, platformWindows, platformSupportsLinks
from CedarBackup2.util import UnorderedList, AbsolutePathList, ObjectTypeList
from CedarBackup2.util import RestrictedContentList, RegexMatchList, RegexList
from CedarBackup2.util import DirectedGraph, PathResolverSingleton, Diagnostics
from CedarBackup2.util import sortDict, resolveCommand, executeCommand, getFunctionReference, encodePath
from CedarBackup2.util import convertSize, UNIT_BYTES, UNIT_SECTORS, UNIT_KBYTES, UNIT_MBYTES, UNIT_GBYTES
-from CedarBackup2.util import displayBytes, deriveDayOfWeek, isStartOfWeek
+from CedarBackup2.util import displayBytes, deriveDayOfWeek, isStartOfWeek, dereferenceLink
from CedarBackup2.util import buildNormalizedPath, splitCommandLine, nullDevice
@@ -97,7 +98,7 @@
#######################################################################
DATA_DIRS = [ "./data", "./test/data" ]
-RESOURCES = [ "lotsoflines.py", ]
+RESOURCES = [ "lotsoflines.py", "tree10.tar.gz", ]
#######################################################################
@@ -2137,7 +2138,16 @@
except: pass
return name
+ def extractTar(self, tarname):
+ """Extracts a tarfile with a particular name."""
+ extractTar(self.tmpdir, self.resources['%s.tar.gz' % tarname])
+ def buildPath(self, components):
+ """Builds a complete search path from a list of components."""
+ components.insert(0, self.tmpdir)
+ return buildPath(components)
+
+
##################
# Test sortDict()
##################
@@ -3912,6 +3922,105 @@
self.failUnlessEqual(["cback", "'this", "is", "a", "really", "long", "single-quoted", "argument'", ], result)
+ #########################
+ # Test dereferenceLink()
+ #########################
+
+ def testDereferenceLink_001(self):
+ """
+ Test for a path that is a link, absolute=false.
+ """
+ self.extractTar("tree10")
+ path = self.buildPath(["tree10", "link002"])
+ if platformSupportsLinks():
+ expected = "file002"
+ else:
+ expected = path
+ actual = dereferenceLink(path, absolute=False)
+ self.failUnlessEqual(expected, actual)
+
+ def testDereferenceLink_002(self):
+ """
+ Test for a path that is a link, absolute=true.
+ """
+ self.extractTar("tree10")
+ path = self.buildPath(["tree10", "link002"])
+ if platformSupportsLinks():
+ expected = self.buildPath(["tree10", "file002"])
+ else:
+ expected = path
+ actual = dereferenceLink(path)
+ self.failUnlessEqual(expected, actual)
+ actual = dereferenceLink(path, absolute=True)
+ self.failUnlessEqual(expected, actual)
+
+ def testDereferenceLink_003(self):
+ """
+ Test for a path that is a file (not a link), absolute=false.
+ """
+ self.extractTar("tree10")
+ path = self.buildPath(["tree10", "file001"])
+ expected = path
+ actual = dereferenceLink(path, absolute=False)
+ self.failUnlessEqual(expected, actual)
+
+ def testDereferenceLink_004(self):
+ """
+ Test for a path that is a file (not a link), absolute=true.
+ """
+ self.extractTar("tree10")
+ path = self.buildPath(["tree10", "file001"])
+ expected = path
+ actual = dereferenceLink(path)
+ self.failUnlessEqual(expected, actual)
+ actual = dereferenceLink(path, absolute=True)
+ self.failUnlessEqual(expected, actual)
+
+ def testDereferenceLink_005(self):
+ """
+ Test for a path that is a directory (not a link), absolute=false.
+ """
+ self.extractTar("tree10")
+ path = self.buildPath(["tree10", "dir001"])
+ expected = path
+ actual = dereferenceLink(path, absolute=False)
+ self.failUnlessEqual(expected, actual)
+
+ def testDereferenceLink_006(self):
+ """
+ Test for a path that is a directory (not a link), absolute=true.
+ """
+ self.extractTar("tree10")
+ path = self.buildPath(["tree10", "dir001"])
+ expected = path
+ actual = dereferenceLink(path)
+ self.failUnlessEqual(expected, actual)
+ actual = dereferenceLink(path, absolute=True)
+ self.failUnlessEqual(expected, actual)
+
+ def testDereferenceLink_007(self):
+ """
+ Test for a path that does not exist, absolute=false.
+ """
+ self.extractTar("tree10")
+ path = self.buildPath(["tree10", "blech"])
+ expected = path
+ actual = dereferenceLink(path, absolute=False)
+ self.failUnlessEqual(expected, actual)
+
+ def testDereferenceLink_008(self):
+ """
+ Test for a path that does not exist, absolute=true.
+ """
+ self.extractTar("tree10")
+ path = self.buildPath(["tree10", "blech"])
+ expected = path
+ actual = dereferenceLink(path)
+ self.failUnlessEqual(expected, actual)
+ actual = dereferenceLink(path, absolute=True)
+ self.failUnlessEqual(expected, actual)
+
+
#######################################################################
# Suite definition
#######################################################################
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|