Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 1 | # Copyright (C) 2016 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 15 | import errno |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 16 | import os |
| 17 | import platform |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 18 | import shutil |
| 19 | import stat |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | def isWindows(): |
| 23 | """ Returns True when running with the native port of Python for Windows, |
| 24 | False when running on any other platform (including the Cygwin port of |
| 25 | Python). |
| 26 | """ |
| 27 | # Note: The cygwin port of Python returns "CYGWIN_NT_xxx" |
| 28 | return platform.system() == "Windows" |
| 29 | |
| 30 | |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 31 | def symlink(source, link_name): |
| 32 | """Creates a symbolic link pointing to source named link_name. |
| 33 | Note: On Windows, source must exist on disk, as the implementation needs |
| 34 | to know whether to create a "File" or a "Directory" symbolic link. |
| 35 | """ |
| 36 | if isWindows(): |
| 37 | import platform_utils_win32 |
| 38 | source = _validate_winpath(source) |
| 39 | link_name = _validate_winpath(link_name) |
| 40 | target = os.path.join(os.path.dirname(link_name), source) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 41 | if isdir(target): |
| 42 | platform_utils_win32.create_dirsymlink(_makelongpath(source), link_name) |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 43 | else: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 44 | platform_utils_win32.create_filesymlink(_makelongpath(source), link_name) |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 45 | else: |
| 46 | return os.symlink(source, link_name) |
| 47 | |
| 48 | |
| 49 | def _validate_winpath(path): |
| 50 | path = os.path.normpath(path) |
| 51 | if _winpath_is_valid(path): |
| 52 | return path |
| 53 | raise ValueError("Path \"%s\" must be a relative path or an absolute " |
| 54 | "path starting with a drive letter".format(path)) |
| 55 | |
| 56 | |
| 57 | def _winpath_is_valid(path): |
| 58 | """Windows only: returns True if path is relative (e.g. ".\\foo") or is |
| 59 | absolute including a drive letter (e.g. "c:\\foo"). Returns False if path |
| 60 | is ambiguous (e.g. "x:foo" or "\\foo"). |
| 61 | """ |
| 62 | assert isWindows() |
| 63 | path = os.path.normpath(path) |
| 64 | drive, tail = os.path.splitdrive(path) |
| 65 | if tail: |
| 66 | if not drive: |
| 67 | return tail[0] != os.sep # "\\foo" is invalid |
| 68 | else: |
| 69 | return tail[0] == os.sep # "x:foo" is invalid |
| 70 | else: |
| 71 | return not drive # "x:" is invalid |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 72 | |
| 73 | |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 74 | def _makelongpath(path): |
| 75 | """Return the input path normalized to support the Windows long path syntax |
| 76 | ("\\\\?\\" prefix) if needed, i.e. if the input path is longer than the |
| 77 | MAX_PATH limit. |
| 78 | """ |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 79 | if isWindows(): |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 80 | # Note: MAX_PATH is 260, but, for directories, the maximum value is actually 246. |
| 81 | if len(path) < 246: |
| 82 | return path |
| 83 | if path.startswith(u"\\\\?\\"): |
| 84 | return path |
| 85 | if not os.path.isabs(path): |
| 86 | return path |
| 87 | # Append prefix and ensure unicode so that the special longpath syntax |
| 88 | # is supported by underlying Win32 API calls |
| 89 | return u"\\\\?\\" + os.path.normpath(path) |
| 90 | else: |
| 91 | return path |
| 92 | |
| 93 | |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 94 | def rmtree(path, ignore_errors=False): |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 95 | """shutil.rmtree(path) wrapper with support for long paths on Windows. |
| 96 | |
| 97 | Availability: Unix, Windows.""" |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 98 | onerror = None |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 99 | if isWindows(): |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 100 | path = _makelongpath(path) |
| 101 | onerror = handle_rmtree_error |
| 102 | shutil.rmtree(path, ignore_errors=ignore_errors, onerror=onerror) |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def handle_rmtree_error(function, path, excinfo): |
| 106 | # Allow deleting read-only files |
| 107 | os.chmod(path, stat.S_IWRITE) |
| 108 | function(path) |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def rename(src, dst): |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 112 | """os.rename(src, dst) wrapper with support for long paths on Windows. |
| 113 | |
| 114 | Availability: Unix, Windows.""" |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 115 | if isWindows(): |
| 116 | # On Windows, rename fails if destination exists, see |
| 117 | # https://docs.python.org/2/library/os.html#os.rename |
| 118 | try: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 119 | os.rename(_makelongpath(src), _makelongpath(dst)) |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 120 | except OSError as e: |
| 121 | if e.errno == errno.EEXIST: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 122 | os.remove(_makelongpath(dst)) |
| 123 | os.rename(_makelongpath(src), _makelongpath(dst)) |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 124 | else: |
| 125 | raise |
| 126 | else: |
| 127 | os.rename(src, dst) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 128 | |
| 129 | |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 130 | def remove(path): |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 131 | """Remove (delete) the file path. This is a replacement for os.remove that |
| 132 | allows deleting read-only files on Windows, with support for long paths and |
| 133 | for deleting directory symbolic links. |
| 134 | |
| 135 | Availability: Unix, Windows.""" |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 136 | if isWindows(): |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 137 | longpath = _makelongpath(path) |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 138 | try: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 139 | os.remove(longpath) |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 140 | except OSError as e: |
| 141 | if e.errno == errno.EACCES: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 142 | os.chmod(longpath, stat.S_IWRITE) |
| 143 | # Directory symbolic links must be deleted with 'rmdir'. |
| 144 | if islink(longpath) and isdir(longpath): |
| 145 | os.rmdir(longpath) |
| 146 | else: |
| 147 | os.remove(longpath) |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 148 | else: |
| 149 | raise |
| 150 | else: |
| 151 | os.remove(path) |
| 152 | |
| 153 | |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 154 | def walk(top, topdown=True, onerror=None, followlinks=False): |
| 155 | """os.walk(path) wrapper with support for long paths on Windows. |
| 156 | |
| 157 | Availability: Windows, Unix. |
| 158 | """ |
| 159 | if isWindows(): |
| 160 | return _walk_windows_impl(top, topdown, onerror, followlinks) |
| 161 | else: |
| 162 | return os.walk(top, topdown, onerror, followlinks) |
| 163 | |
| 164 | |
| 165 | def _walk_windows_impl(top, topdown, onerror, followlinks): |
| 166 | try: |
| 167 | names = listdir(top) |
David Pursehouse | d26146d | 2018-11-01 11:54:10 +0900 | [diff] [blame] | 168 | except Exception as err: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 169 | if onerror is not None: |
| 170 | onerror(err) |
| 171 | return |
| 172 | |
| 173 | dirs, nondirs = [], [] |
| 174 | for name in names: |
| 175 | if isdir(os.path.join(top, name)): |
| 176 | dirs.append(name) |
| 177 | else: |
| 178 | nondirs.append(name) |
| 179 | |
| 180 | if topdown: |
| 181 | yield top, dirs, nondirs |
| 182 | for name in dirs: |
| 183 | new_path = os.path.join(top, name) |
| 184 | if followlinks or not islink(new_path): |
| 185 | for x in _walk_windows_impl(new_path, topdown, onerror, followlinks): |
| 186 | yield x |
| 187 | if not topdown: |
| 188 | yield top, dirs, nondirs |
| 189 | |
| 190 | |
| 191 | def listdir(path): |
| 192 | """os.listdir(path) wrapper with support for long paths on Windows. |
| 193 | |
| 194 | Availability: Windows, Unix. |
| 195 | """ |
| 196 | return os.listdir(_makelongpath(path)) |
| 197 | |
| 198 | |
| 199 | def rmdir(path): |
| 200 | """os.rmdir(path) wrapper with support for long paths on Windows. |
| 201 | |
| 202 | Availability: Windows, Unix. |
| 203 | """ |
| 204 | os.rmdir(_makelongpath(path)) |
| 205 | |
| 206 | |
| 207 | def isdir(path): |
| 208 | """os.path.isdir(path) wrapper with support for long paths on Windows. |
| 209 | |
| 210 | Availability: Windows, Unix. |
| 211 | """ |
| 212 | return os.path.isdir(_makelongpath(path)) |
| 213 | |
| 214 | |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 215 | def islink(path): |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 216 | """os.path.islink(path) wrapper with support for long paths on Windows. |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 217 | |
| 218 | Availability: Windows, Unix. |
| 219 | """ |
| 220 | if isWindows(): |
| 221 | import platform_utils_win32 |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 222 | return platform_utils_win32.islink(_makelongpath(path)) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 223 | else: |
| 224 | return os.path.islink(path) |
| 225 | |
| 226 | |
| 227 | def readlink(path): |
| 228 | """Return a string representing the path to which the symbolic link |
| 229 | points. The result may be either an absolute or relative pathname; |
| 230 | if it is relative, it may be converted to an absolute pathname using |
| 231 | os.path.join(os.path.dirname(path), result). |
| 232 | |
| 233 | Availability: Windows, Unix. |
| 234 | """ |
| 235 | if isWindows(): |
| 236 | import platform_utils_win32 |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 237 | return platform_utils_win32.readlink(_makelongpath(path)) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 238 | else: |
| 239 | return os.readlink(path) |
| 240 | |
| 241 | |
| 242 | def realpath(path): |
| 243 | """Return the canonical path of the specified filename, eliminating |
| 244 | any symbolic links encountered in the path. |
| 245 | |
| 246 | Availability: Windows, Unix. |
| 247 | """ |
| 248 | if isWindows(): |
| 249 | current_path = os.path.abspath(path) |
| 250 | path_tail = [] |
| 251 | for c in range(0, 100): # Avoid cycles |
| 252 | if islink(current_path): |
| 253 | target = readlink(current_path) |
| 254 | current_path = os.path.join(os.path.dirname(current_path), target) |
| 255 | else: |
| 256 | basename = os.path.basename(current_path) |
| 257 | if basename == '': |
| 258 | path_tail.append(current_path) |
| 259 | break |
| 260 | path_tail.append(basename) |
| 261 | current_path = os.path.dirname(current_path) |
| 262 | path_tail.reverse() |
| 263 | result = os.path.normpath(os.path.join(*path_tail)) |
| 264 | return result |
| 265 | else: |
| 266 | return os.path.realpath(path) |