A safe alternative to os.path.relpath(), avoiding an exception on Windows if the drive needs to change - in this case we use os.path.abspath(). Args: path: Path to be processed. start: Start directory or current directory if None. all
(path, start=None, allow_up=True)
| 3110 | |
| 3111 | |
| 3112 | def relpath(path, start=None, allow_up=True): |
| 3113 | ''' |
| 3114 | A safe alternative to os.path.relpath(), avoiding an exception on Windows |
| 3115 | if the drive needs to change - in this case we use os.path.abspath(). |
| 3116 | |
| 3117 | Args: |
| 3118 | path: |
| 3119 | Path to be processed. |
| 3120 | start: |
| 3121 | Start directory or current directory if None. |
| 3122 | allow_up: |
| 3123 | If false we return absolute path is <path> is not within <start>. |
| 3124 | ''' |
| 3125 | if windows(): |
| 3126 | try: |
| 3127 | ret = os.path.relpath(path, start) |
| 3128 | except ValueError: |
| 3129 | # os.path.relpath() fails if trying to change drives. |
| 3130 | ret = os.path.abspath(path) |
| 3131 | else: |
| 3132 | ret = os.path.relpath(path, start) |
| 3133 | if not allow_up and ret.startswith('../') or ret.startswith('..\\'): |
| 3134 | ret = os.path.abspath(path) |
| 3135 | return ret |
| 3136 | |
| 3137 | |
| 3138 | def _so_suffix(use_so_versioning=True): |