#NEWLINE# def relpathto( thisdir , origin , dest ) : #NEWLINE# #INDENT# "\n    Given two paths relative to a directory, work out a path from origin\n    to destination.\n\n    Assumes UNIX/URL type relative paths.\n    If origin doesn't *end* with '/' we assume it's a file rather than a\n    directory.\n\n    If the same paths are passed in :\n        if the path ends with ('/') then we return ''\n        else we return the last part of the path (presumably a filename)\n\n    If thisdir doesn't start with '/' then we add one\n        (this makes the top level of thisdir our root directory)\n    " #NEWLINE# orig_thisdir = thisdir #NEWLINE# if ( not thisdir . startswith ( '/' ) ) : #NEWLINE# #INDENT# thisdir = ( '/' + thisdir ) #NEWLINE# #UNINDENT# orig_abs = posixpath . normpath ( posixpath . join ( thisdir , origin ) ) #NEWLINE# dest_abs = posixpath . normpath ( posixpath . join ( thisdir , dest ) ) #NEWLINE# if ( origin . endswith ( '/' ) and ( not orig_abs . endswith ( '/' ) ) ) : #NEWLINE# #INDENT# orig_abs = ( orig_abs + '/' ) #NEWLINE# #UNINDENT# if ( dest . endswith ( '/' ) and ( not dest_abs . endswith ( '/' ) ) ) : #NEWLINE# #INDENT# dest_abs = ( dest_abs + '/' ) #NEWLINE# #UNINDENT# orig_list = orig_abs . split ( '/' ) [ : ( - 1 ) ] #NEWLINE# dest_list = dest_abs . split ( '/' ) #NEWLINE# if ( orig_list [ 0 ] != dest_list [ 0 ] ) : #NEWLINE# #INDENT# return dest #NEWLINE# #UNINDENT# i = 0 #NEWLINE# for ( start_seg , dest_seg ) in zip ( orig_list , dest_list ) : #NEWLINE# #INDENT# if ( start_seg != dest_seg ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# i += 1 #NEWLINE# #UNINDENT# segments = ( [ '..' ] * ( len ( orig_list ) - i ) ) #NEWLINE# segments += dest_list [ i : ] #NEWLINE# if ( len ( segments ) == 0 ) : #NEWLINE# #INDENT# if dest . endswith ( '/' ) : #NEWLINE# #INDENT# return '' #NEWLINE# #UNINDENT# return dest_list [ ( - 1 ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return '/' . join ( segments )