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