diff options
| author | Renaud Paquay <rpaquay@google.com> | 2016-11-11 14:25:29 -0800 |
|---|---|---|
| committer | Renaud Paquay <rpaquay@google.com> | 2017-08-31 13:49:36 -0700 |
| commit | 010fed771183c23c0e7d04a4e7292782f68de9db (patch) | |
| tree | 438b928fa1adaa7105d88462299513ca59c47c9f | |
| parent | e8595e9df7980b0b7d9111de43d294c4439d474c (diff) | |
| download | git-repo-010fed771183c23c0e7d04a4e7292782f68de9db.tar.gz | |
Replace all os.remove calls
os.remove raises an exception when deleting read-only files on
Windows. Replace all calls with calls to platform_utils.remove,
which deals with deals with that issue.
Change-Id: I4dc9e0c9a36b4238880520c69f5075eca40f3e66
| -rw-r--r-- | editor.py | 3 | ||||
| -rw-r--r-- | git_config.py | 7 | ||||
| -rw-r--r-- | manifest_xml.py | 2 | ||||
| -rw-r--r-- | platform_utils.py | 17 | ||||
| -rw-r--r-- | project.py | 28 | ||||
| -rw-r--r-- | subcmds/sync.py | 10 |
6 files changed, 43 insertions, 24 deletions
| @@ -21,6 +21,7 @@ import subprocess | |||
| 21 | import tempfile | 21 | import tempfile |
| 22 | 22 | ||
| 23 | from error import EditorError | 23 | from error import EditorError |
| 24 | import platform_utils | ||
| 24 | 25 | ||
| 25 | class Editor(object): | 26 | class Editor(object): |
| 26 | """Manages the user's preferred text editor.""" | 27 | """Manages the user's preferred text editor.""" |
| @@ -107,4 +108,4 @@ least one of these before using this command.""", file=sys.stderr) | |||
| 107 | finally: | 108 | finally: |
| 108 | if fd: | 109 | if fd: |
| 109 | os.close(fd) | 110 | os.close(fd) |
| 110 | os.remove(path) | 111 | platform_utils.remove(path) |
diff --git a/git_config.py b/git_config.py index 9d5874a2..3ba9dbd1 100644 --- a/git_config.py +++ b/git_config.py | |||
| @@ -42,6 +42,7 @@ else: | |||
| 42 | 42 | ||
| 43 | from signal import SIGTERM | 43 | from signal import SIGTERM |
| 44 | from error import GitError, UploadError | 44 | from error import GitError, UploadError |
| 45 | import platform_utils | ||
| 45 | from trace import Trace | 46 | from trace import Trace |
| 46 | if is_python3(): | 47 | if is_python3(): |
| 47 | from http.client import HTTPException | 48 | from http.client import HTTPException |
| @@ -268,7 +269,7 @@ class GitConfig(object): | |||
| 268 | try: | 269 | try: |
| 269 | if os.path.getmtime(self._json) \ | 270 | if os.path.getmtime(self._json) \ |
| 270 | <= os.path.getmtime(self.file): | 271 | <= os.path.getmtime(self.file): |
| 271 | os.remove(self._json) | 272 | platform_utils.remove(self._json) |
| 272 | return None | 273 | return None |
| 273 | except OSError: | 274 | except OSError: |
| 274 | return None | 275 | return None |
| @@ -280,7 +281,7 @@ class GitConfig(object): | |||
| 280 | finally: | 281 | finally: |
| 281 | fd.close() | 282 | fd.close() |
| 282 | except (IOError, ValueError): | 283 | except (IOError, ValueError): |
| 283 | os.remove(self._json) | 284 | platform_utils.remove(self._json) |
| 284 | return None | 285 | return None |
| 285 | 286 | ||
| 286 | def _SaveJson(self, cache): | 287 | def _SaveJson(self, cache): |
| @@ -292,7 +293,7 @@ class GitConfig(object): | |||
| 292 | fd.close() | 293 | fd.close() |
| 293 | except (IOError, TypeError): | 294 | except (IOError, TypeError): |
| 294 | if os.path.exists(self._json): | 295 | if os.path.exists(self._json): |
| 295 | os.remove(self._json) | 296 | platform_utils.remove(self._json) |
| 296 | 297 | ||
| 297 | def _ReadGit(self): | 298 | def _ReadGit(self): |
| 298 | """ | 299 | """ |
diff --git a/manifest_xml.py b/manifest_xml.py index 05651c6c..9b5d7847 100644 --- a/manifest_xml.py +++ b/manifest_xml.py | |||
| @@ -166,7 +166,7 @@ class XmlManifest(object): | |||
| 166 | 166 | ||
| 167 | try: | 167 | try: |
| 168 | if os.path.lexists(self.manifestFile): | 168 | if os.path.lexists(self.manifestFile): |
| 169 | os.remove(self.manifestFile) | 169 | platform_utils.remove(self.manifestFile) |
| 170 | platform_utils.symlink(os.path.join('manifests', name), self.manifestFile) | 170 | platform_utils.symlink(os.path.join('manifests', name), self.manifestFile) |
| 171 | except OSError as e: | 171 | except OSError as e: |
| 172 | raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e))) | 172 | raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e))) |
diff --git a/platform_utils.py b/platform_utils.py index 2ad56490..33cb2ec3 100644 --- a/platform_utils.py +++ b/platform_utils.py | |||
| @@ -244,6 +244,23 @@ def rename(src, dst): | |||
| 244 | os.rename(src, dst) | 244 | os.rename(src, dst) |
| 245 | 245 | ||
| 246 | 246 | ||
| 247 | def remove(path): | ||
| 248 | """Remove (delete) the file path. This is a replacement for os.remove, but | ||
| 249 | allows deleting read-only files on Windows. | ||
| 250 | """ | ||
| 251 | if isWindows(): | ||
| 252 | try: | ||
| 253 | os.remove(path) | ||
| 254 | except OSError as e: | ||
| 255 | if e.errno == errno.EACCES: | ||
| 256 | os.chmod(path, stat.S_IWRITE) | ||
| 257 | os.remove(path) | ||
| 258 | else: | ||
| 259 | raise | ||
| 260 | else: | ||
| 261 | os.remove(path) | ||
| 262 | |||
| 263 | |||
| 247 | def islink(path): | 264 | def islink(path): |
| 248 | """Test whether a path is a symbolic link. | 265 | """Test whether a path is a symbolic link. |
| 249 | 266 | ||
| @@ -65,7 +65,7 @@ def _lwrite(path, content): | |||
| 65 | try: | 65 | try: |
| 66 | platform_utils.rename(lock, path) | 66 | platform_utils.rename(lock, path) |
| 67 | except OSError: | 67 | except OSError: |
| 68 | os.remove(lock) | 68 | platform_utils.remove(lock) |
| 69 | raise | 69 | raise |
| 70 | 70 | ||
| 71 | 71 | ||
| @@ -250,7 +250,7 @@ class _CopyFile(object): | |||
| 250 | try: | 250 | try: |
| 251 | # remove existing file first, since it might be read-only | 251 | # remove existing file first, since it might be read-only |
| 252 | if os.path.exists(dest): | 252 | if os.path.exists(dest): |
| 253 | os.remove(dest) | 253 | platform_utils.remove(dest) |
| 254 | else: | 254 | else: |
| 255 | dest_dir = os.path.dirname(dest) | 255 | dest_dir = os.path.dirname(dest) |
| 256 | if not os.path.isdir(dest_dir): | 256 | if not os.path.isdir(dest_dir): |
| @@ -279,7 +279,7 @@ class _LinkFile(object): | |||
| 279 | try: | 279 | try: |
| 280 | # remove existing file first, since it might be read-only | 280 | # remove existing file first, since it might be read-only |
| 281 | if os.path.lexists(absDest): | 281 | if os.path.lexists(absDest): |
| 282 | os.remove(absDest) | 282 | platform_utils.remove(absDest) |
| 283 | else: | 283 | else: |
| 284 | dest_dir = os.path.dirname(absDest) | 284 | dest_dir = os.path.dirname(absDest) |
| 285 | if not os.path.isdir(dest_dir): | 285 | if not os.path.isdir(dest_dir): |
| @@ -1242,7 +1242,7 @@ class Project(object): | |||
| 1242 | if not self._ExtractArchive(tarpath, path=topdir): | 1242 | if not self._ExtractArchive(tarpath, path=topdir): |
| 1243 | return False | 1243 | return False |
| 1244 | try: | 1244 | try: |
| 1245 | os.remove(tarpath) | 1245 | platform_utils.remove(tarpath) |
| 1246 | except OSError as e: | 1246 | except OSError as e: |
| 1247 | _warn("Cannot remove archive %s: %s", tarpath, str(e)) | 1247 | _warn("Cannot remove archive %s: %s", tarpath, str(e)) |
| 1248 | self._CopyAndLinkFiles() | 1248 | self._CopyAndLinkFiles() |
| @@ -1302,7 +1302,7 @@ class Project(object): | |||
| 1302 | else: | 1302 | else: |
| 1303 | self._InitMirrorHead() | 1303 | self._InitMirrorHead() |
| 1304 | try: | 1304 | try: |
| 1305 | os.remove(os.path.join(self.gitdir, 'FETCH_HEAD')) | 1305 | platform_utils.remove(os.path.join(self.gitdir, 'FETCH_HEAD')) |
| 1306 | except OSError: | 1306 | except OSError: |
| 1307 | pass | 1307 | pass |
| 1308 | return True | 1308 | return True |
| @@ -1812,7 +1812,7 @@ class Project(object): | |||
| 1812 | except GitError: | 1812 | except GitError: |
| 1813 | return [], [] | 1813 | return [], [] |
| 1814 | finally: | 1814 | finally: |
| 1815 | os.remove(temp_gitmodules_path) | 1815 | platform_utils.remove(temp_gitmodules_path) |
| 1816 | 1816 | ||
| 1817 | names = set() | 1817 | names = set() |
| 1818 | paths = {} | 1818 | paths = {} |
| @@ -2104,7 +2104,7 @@ class Project(object): | |||
| 2104 | if old_packed != '': | 2104 | if old_packed != '': |
| 2105 | _lwrite(packed_refs, old_packed) | 2105 | _lwrite(packed_refs, old_packed) |
| 2106 | else: | 2106 | else: |
| 2107 | os.remove(packed_refs) | 2107 | platform_utils.remove(packed_refs) |
| 2108 | self.bare_git.pack_refs('--all', '--prune') | 2108 | self.bare_git.pack_refs('--all', '--prune') |
| 2109 | 2109 | ||
| 2110 | if is_sha1 and current_branch_only: | 2110 | if is_sha1 and current_branch_only: |
| @@ -2166,14 +2166,14 @@ class Project(object): | |||
| 2166 | 2166 | ||
| 2167 | ok = GitCommand(self, cmd, bare=True).Wait() == 0 | 2167 | ok = GitCommand(self, cmd, bare=True).Wait() == 0 |
| 2168 | if os.path.exists(bundle_dst): | 2168 | if os.path.exists(bundle_dst): |
| 2169 | os.remove(bundle_dst) | 2169 | platform_utils.remove(bundle_dst) |
| 2170 | if os.path.exists(bundle_tmp): | 2170 | if os.path.exists(bundle_tmp): |
| 2171 | os.remove(bundle_tmp) | 2171 | platform_utils.remove(bundle_tmp) |
| 2172 | return ok | 2172 | return ok |
| 2173 | 2173 | ||
| 2174 | def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet): | 2174 | def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet): |
| 2175 | if os.path.exists(dstPath): | 2175 | if os.path.exists(dstPath): |
| 2176 | os.remove(dstPath) | 2176 | platform_utils.remove(dstPath) |
| 2177 | 2177 | ||
| 2178 | cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location'] | 2178 | cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location'] |
| 2179 | if quiet: | 2179 | if quiet: |
| @@ -2183,7 +2183,7 @@ class Project(object): | |||
| 2183 | if size >= 1024: | 2183 | if size >= 1024: |
| 2184 | cmd += ['--continue-at', '%d' % (size,)] | 2184 | cmd += ['--continue-at', '%d' % (size,)] |
| 2185 | else: | 2185 | else: |
| 2186 | os.remove(tmpPath) | 2186 | platform_utils.remove(tmpPath) |
| 2187 | if 'http_proxy' in os.environ and 'darwin' == sys.platform: | 2187 | if 'http_proxy' in os.environ and 'darwin' == sys.platform: |
| 2188 | cmd += ['--proxy', os.environ['http_proxy']] | 2188 | cmd += ['--proxy', os.environ['http_proxy']] |
| 2189 | with GetUrlCookieFile(srcUrl, quiet) as (cookiefile, _proxy): | 2189 | with GetUrlCookieFile(srcUrl, quiet) as (cookiefile, _proxy): |
| @@ -2217,7 +2217,7 @@ class Project(object): | |||
| 2217 | platform_utils.rename(tmpPath, dstPath) | 2217 | platform_utils.rename(tmpPath, dstPath) |
| 2218 | return True | 2218 | return True |
| 2219 | else: | 2219 | else: |
| 2220 | os.remove(tmpPath) | 2220 | platform_utils.remove(tmpPath) |
| 2221 | return False | 2221 | return False |
| 2222 | else: | 2222 | else: |
| 2223 | return False | 2223 | return False |
| @@ -2390,7 +2390,7 @@ class Project(object): | |||
| 2390 | continue | 2390 | continue |
| 2391 | if os.path.exists(dst): | 2391 | if os.path.exists(dst): |
| 2392 | if filecmp.cmp(stock_hook, dst, shallow=False): | 2392 | if filecmp.cmp(stock_hook, dst, shallow=False): |
| 2393 | os.remove(dst) | 2393 | platform_utils.remove(dst) |
| 2394 | else: | 2394 | else: |
| 2395 | _warn("%s: Not replacing locally modified %s hook", | 2395 | _warn("%s: Not replacing locally modified %s hook", |
| 2396 | self.relpath, name) | 2396 | self.relpath, name) |
| @@ -2508,7 +2508,7 @@ class Project(object): | |||
| 2508 | # file doesn't either. | 2508 | # file doesn't either. |
| 2509 | if name in symlink_files and not os.path.lexists(src): | 2509 | if name in symlink_files and not os.path.lexists(src): |
| 2510 | try: | 2510 | try: |
| 2511 | os.remove(dst) | 2511 | platform_utils.remove(dst) |
| 2512 | except OSError: | 2512 | except OSError: |
| 2513 | pass | 2513 | pass |
| 2514 | 2514 | ||
diff --git a/subcmds/sync.py b/subcmds/sync.py index 93fea23b..cda47fdd 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py | |||
| @@ -489,7 +489,7 @@ later is required to fix a server side protocol bug. | |||
| 489 | for root, dirs, files in os.walk(path): | 489 | for root, dirs, files in os.walk(path): |
| 490 | for f in files: | 490 | for f in files: |
| 491 | try: | 491 | try: |
| 492 | os.remove(os.path.join(root, f)) | 492 | platform_utils.remove(os.path.join(root, f)) |
| 493 | except OSError: | 493 | except OSError: |
| 494 | print('Failed to remove %s' % os.path.join(root, f), file=sys.stderr) | 494 | print('Failed to remove %s' % os.path.join(root, f), file=sys.stderr) |
| 495 | failed = True | 495 | failed = True |
| @@ -500,7 +500,7 @@ later is required to fix a server side protocol bug. | |||
| 500 | for d in reversed(dirs_to_remove): | 500 | for d in reversed(dirs_to_remove): |
| 501 | if platform_utils.islink(d): | 501 | if platform_utils.islink(d): |
| 502 | try: | 502 | try: |
| 503 | os.remove(d) | 503 | platform_utils.remove(d) |
| 504 | except OSError: | 504 | except OSError: |
| 505 | print('Failed to remove %s' % os.path.join(root, d), file=sys.stderr) | 505 | print('Failed to remove %s' % os.path.join(root, d), file=sys.stderr) |
| 506 | failed = True | 506 | failed = True |
| @@ -712,7 +712,7 @@ later is required to fix a server side protocol bug. | |||
| 712 | else: # Not smart sync or smart tag mode | 712 | else: # Not smart sync or smart tag mode |
| 713 | if os.path.isfile(smart_sync_manifest_path): | 713 | if os.path.isfile(smart_sync_manifest_path): |
| 714 | try: | 714 | try: |
| 715 | os.remove(smart_sync_manifest_path) | 715 | platform_utils.remove(smart_sync_manifest_path) |
| 716 | except OSError as e: | 716 | except OSError as e: |
| 717 | print('error: failed to remove existing smart sync override manifest: %s' % | 717 | print('error: failed to remove existing smart sync override manifest: %s' % |
| 718 | e, file=sys.stderr) | 718 | e, file=sys.stderr) |
| @@ -956,7 +956,7 @@ class _FetchTimes(object): | |||
| 956 | f.close() | 956 | f.close() |
| 957 | except (IOError, ValueError): | 957 | except (IOError, ValueError): |
| 958 | try: | 958 | try: |
| 959 | os.remove(self._path) | 959 | platform_utils.remove(self._path) |
| 960 | except OSError: | 960 | except OSError: |
| 961 | pass | 961 | pass |
| 962 | self._times = {} | 962 | self._times = {} |
| @@ -980,7 +980,7 @@ class _FetchTimes(object): | |||
| 980 | f.close() | 980 | f.close() |
| 981 | except (IOError, TypeError): | 981 | except (IOError, TypeError): |
| 982 | try: | 982 | try: |
| 983 | os.remove(self._path) | 983 | platform_utils.remove(self._path) |
| 984 | except OSError: | 984 | except OSError: |
| 985 | pass | 985 | pass |
| 986 | 986 | ||
