#NEWLINE# def relpath( origin , dest ) : #NEWLINE# #INDENT# "Given two absolute paths, work out a path from origin 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\n    a 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 origin or dest don't start with '/' then we add it.\n\n    We are *assuming* relative paths on the same device\n        (i.e. same top level directory)\n    " #NEWLINE# if ( not origin . startswith ( '/' ) ) : #NEWLINE# #INDENT# origin = ( '/' + origin ) #NEWLINE# #UNINDENT# if ( not dest . startswith ( '/' ) ) : #NEWLINE# #INDENT# dest = ( '/' + dest ) #NEWLINE# #UNINDENT# orig_list = origin . split ( '/' ) [ : ( - 1 ) ] #NEWLINE# dest_list = dest . split ( '/' ) #NEWLINE# i = 0 #NEWLINE# for ( start_seg , dest_seg ) in zip ( orig_list , dest ) : #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 )