]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
Add support in abspath_expanduser_unicode for expanding relative to a base path....
authorDaira Hopwood <daira@jacaranda.org>
Mon, 25 Aug 2014 17:23:59 +0000 (18:23 +0100)
committerDaira Hopwood <daira@jacaranda.org>
Tue, 27 Jan 2015 17:00:56 +0000 (17:00 +0000)
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
src/allmydata/util/fileutil.py

index 7f4f297522da81c1885751ccd64f6fe93ea44230..8ed14264ec4141807ccca7abc21b5826bc7b8a0b 100644 (file)
@@ -276,6 +276,20 @@ def put_file(pathname, inf):
         outf.close()
 
 
+def precondition_abspath(path):
+    if not isinstance(path, unicode):
+        raise AssertionError("an abspath must be a Unicode string")
+
+    if sys.platform == "win32":
+        # This intentionally doesn't view absolute paths starting with a drive specification, or
+        # paths relative to the current drive, as acceptable.
+        if not path.startswith("\\\\"):
+            raise AssertionError("an abspath should be normalized using abspath_expanduser_unicode")
+    else:
+        # This intentionally doesn't view the path '~' or paths starting with '~/' as acceptable.
+        if not os.path.isabs(path):
+            raise AssertionError("an abspath should be normalized using abspath_expanduser_unicode")
+
 # Work around <http://bugs.python.org/issue3426>. This code is adapted from
 # <http://svn.python.org/view/python/trunk/Lib/ntpath.py?revision=78247&view=markup>
 # with some simplifications.
@@ -286,9 +300,18 @@ try:
 except ImportError:
     pass
 
-def abspath_expanduser_unicode(path):
-    """Return the absolute version of a path."""
-    assert isinstance(path, unicode), path
+def abspath_expanduser_unicode(path, base=None):
+    """
+    Return the absolute version of a path. If 'base' is given and 'path' is relative,
+    the path will be expanded relative to 'base'.
+    'path' must be a Unicode string. 'base', if given, must be a Unicode string
+    corresponding to an absolute path as returned by a previous call to
+    abspath_expanduser_unicode.
+    """
+    if not isinstance(path, unicode):
+        raise AssertionError("paths must be Unicode strings")
+    if base is not None:
+        precondition_abspath(base)
 
     path = os.path.expanduser(path)