Home | History | Annotate | Download | only in unittest2
      1 import os
      2 import sys
      3 
      4 try:
      5     from functools import wraps
      6 except ImportError:
      7     # only needed for Python 2.4
      8     def wraps(_):
      9         def _wraps(func):
     10             return func
     11         return _wraps
     12 
     13 __unittest = True
     14 
     15 def _relpath_nt(path, start=os.path.curdir):
     16     """Return a relative version of a path"""
     17 
     18     if not path:
     19         raise ValueError("no path specified")
     20     start_list = os.path.abspath(start).split(os.path.sep)
     21     path_list = os.path.abspath(path).split(os.path.sep)
     22     if start_list[0].lower() != path_list[0].lower():
     23         unc_path, rest = os.path.splitunc(path)
     24         unc_start, rest = os.path.splitunc(start)
     25         if bool(unc_path) ^ bool(unc_start):
     26             raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
     27                                                                 % (path, start))
     28         else:
     29             raise ValueError("path is on drive %s, start on drive %s"
     30                                                 % (path_list[0], start_list[0]))
     31     # Work out how much of the filepath is shared by start and path.
     32     for i in range(min(len(start_list), len(path_list))):
     33         if start_list[i].lower() != path_list[i].lower():
     34             break
     35     else:
     36         i += 1
     37 
     38     rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
     39     if not rel_list:
     40         return os.path.curdir
     41     return os.path.join(*rel_list)
     42 
     43 # default to posixpath definition
     44 def _relpath_posix(path, start=os.path.curdir):
     45     """Return a relative version of a path"""
     46 
     47     if not path:
     48         raise ValueError("no path specified")
     49     
     50     start_list = os.path.abspath(start).split(os.path.sep)
     51     path_list = os.path.abspath(path).split(os.path.sep)
     52 
     53     # Work out how much of the filepath is shared by start and path.
     54     i = len(os.path.commonprefix([start_list, path_list]))
     55 
     56     rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
     57     if not rel_list:
     58         return os.path.curdir
     59     return os.path.join(*rel_list)
     60 
     61 if os.path is sys.modules.get('ntpath'):
     62     relpath = _relpath_nt
     63 else:
     64     relpath = _relpath_posix
     65