Changeset 381 for python


Ignore:
Timestamp:
Mar 13, 2014, 4:58:53 PM (11 years ago)
Author:
dmik
Message:

Add OS/2 version of os.path.relpath.

Based on the NT version (with a bug fix related to
trailing slashes).

Location:
python/trunk/Lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk/Lib/os2emxpath.py

    r2 r381  
    158158
    159159supports_unicode_filenames = False
     160
     161
     162def relpath(path, start=curdir):
     163    """Return a relative version of a path"""
     164
     165    if not path:
     166        raise ValueError("no path specified")
     167    start_list = abspath(start).split(sep)
     168    path_list = abspath(path).split(sep)
     169    # Remove empty components after trailing slashes
     170    if (start_list[-1] == ''):
     171        start_list.pop()
     172    if (path_list[-1] == ''):
     173        path_list.pop()
     174    if start_list[0].lower() != path_list[0].lower():
     175        unc_path, rest = splitunc(path)
     176        unc_start, rest = splitunc(start)
     177        if bool(unc_path) ^ bool(unc_start):
     178            raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
     179                                                                % (path, start))
     180        else:
     181            raise ValueError("path is on drive %s, start on drive %s"
     182                                                % (path_list[0], start_list[0]))
     183    # Work out how much of the filepath is shared by start and path.
     184    for i in range(min(len(start_list), len(path_list))):
     185        if start_list[i].lower() != path_list[i].lower():
     186            break
     187    else:
     188        i += 1
     189
     190    rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
     191    if not rel_list:
     192        return curdir
     193    return join(*rel_list)
  • python/trunk/Lib/os2knixpath.py

    r348 r381  
    246246
    247247supports_unicode_filenames = False
     248
     249
     250def relpath(path, start=curdir):
     251    """Return a relative version of a path"""
     252
     253    if not path:
     254        raise ValueError("no path specified")
     255    start_list = abspath(start).split(sep)
     256    path_list = abspath(path).split(sep)
     257    # Remove empty components after trailing slashes
     258    if (start_list[-1] == ''):
     259        start_list.pop()
     260    if (path_list[-1] == ''):
     261        path_list.pop()
     262    if start_list[0].lower() != path_list[0].lower():
     263        unc_path, rest = splitunc(path)
     264        unc_start, rest = splitunc(start)
     265        if bool(unc_path) ^ bool(unc_start):
     266            raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
     267                                                                % (path, start))
     268        else:
     269            raise ValueError("path is on drive %s, start on drive %s"
     270                                                % (path_list[0], start_list[0]))
     271    # Work out how much of the filepath is shared by start and path.
     272    for i in range(min(len(start_list), len(path_list))):
     273        if start_list[i].lower() != path_list[i].lower():
     274            break
     275    else:
     276        i += 1
     277
     278    rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
     279    if not rel_list:
     280        return curdir
     281    return join(*rel_list)
Note: See TracChangeset for help on using the changeset viewer.