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
This commit is contained in:
Renaud Paquay 2016-11-11 14:25:29 -08:00
parent e8595e9df7
commit 010fed7711
6 changed files with 43 additions and 24 deletions

View File

@ -21,6 +21,7 @@ import subprocess
import tempfile import tempfile
from error import EditorError from error import EditorError
import platform_utils
class Editor(object): class Editor(object):
"""Manages the user's preferred text editor.""" """Manages the user's preferred text editor."""
@ -107,4 +108,4 @@ least one of these before using this command.""", file=sys.stderr)
finally: finally:
if fd: if fd:
os.close(fd) os.close(fd)
os.remove(path) platform_utils.remove(path)

View File

@ -42,6 +42,7 @@ else:
from signal import SIGTERM from signal import SIGTERM
from error import GitError, UploadError from error import GitError, UploadError
import platform_utils
from trace import Trace from trace import Trace
if is_python3(): if is_python3():
from http.client import HTTPException from http.client import HTTPException
@ -268,7 +269,7 @@ class GitConfig(object):
try: try:
if os.path.getmtime(self._json) \ if os.path.getmtime(self._json) \
<= os.path.getmtime(self.file): <= os.path.getmtime(self.file):
os.remove(self._json) platform_utils.remove(self._json)
return None return None
except OSError: except OSError:
return None return None
@ -280,7 +281,7 @@ class GitConfig(object):
finally: finally:
fd.close() fd.close()
except (IOError, ValueError): except (IOError, ValueError):
os.remove(self._json) platform_utils.remove(self._json)
return None return None
def _SaveJson(self, cache): def _SaveJson(self, cache):
@ -292,7 +293,7 @@ class GitConfig(object):
fd.close() fd.close()
except (IOError, TypeError): except (IOError, TypeError):
if os.path.exists(self._json): if os.path.exists(self._json):
os.remove(self._json) platform_utils.remove(self._json)
def _ReadGit(self): def _ReadGit(self):
""" """

View File

@ -166,7 +166,7 @@ class XmlManifest(object):
try: try:
if os.path.lexists(self.manifestFile): if os.path.lexists(self.manifestFile):
os.remove(self.manifestFile) platform_utils.remove(self.manifestFile)
platform_utils.symlink(os.path.join('manifests', name), self.manifestFile) platform_utils.symlink(os.path.join('manifests', name), self.manifestFile)
except OSError as e: except OSError as e:
raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e))) raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))

View File

@ -244,6 +244,23 @@ def rename(src, dst):
os.rename(src, dst) os.rename(src, dst)
def remove(path):
"""Remove (delete) the file path. This is a replacement for os.remove, but
allows deleting read-only files on Windows.
"""
if isWindows():
try:
os.remove(path)
except OSError as e:
if e.errno == errno.EACCES:
os.chmod(path, stat.S_IWRITE)
os.remove(path)
else:
raise
else:
os.remove(path)
def islink(path): def islink(path):
"""Test whether a path is a symbolic link. """Test whether a path is a symbolic link.

View File

@ -65,7 +65,7 @@ def _lwrite(path, content):
try: try:
platform_utils.rename(lock, path) platform_utils.rename(lock, path)
except OSError: except OSError:
os.remove(lock) platform_utils.remove(lock)
raise raise
@ -250,7 +250,7 @@ class _CopyFile(object):
try: try:
# remove existing file first, since it might be read-only # remove existing file first, since it might be read-only
if os.path.exists(dest): if os.path.exists(dest):
os.remove(dest) platform_utils.remove(dest)
else: else:
dest_dir = os.path.dirname(dest) dest_dir = os.path.dirname(dest)
if not os.path.isdir(dest_dir): if not os.path.isdir(dest_dir):
@ -279,7 +279,7 @@ class _LinkFile(object):
try: try:
# remove existing file first, since it might be read-only # remove existing file first, since it might be read-only
if os.path.lexists(absDest): if os.path.lexists(absDest):
os.remove(absDest) platform_utils.remove(absDest)
else: else:
dest_dir = os.path.dirname(absDest) dest_dir = os.path.dirname(absDest)
if not os.path.isdir(dest_dir): if not os.path.isdir(dest_dir):
@ -1242,7 +1242,7 @@ class Project(object):
if not self._ExtractArchive(tarpath, path=topdir): if not self._ExtractArchive(tarpath, path=topdir):
return False return False
try: try:
os.remove(tarpath) platform_utils.remove(tarpath)
except OSError as e: except OSError as e:
_warn("Cannot remove archive %s: %s", tarpath, str(e)) _warn("Cannot remove archive %s: %s", tarpath, str(e))
self._CopyAndLinkFiles() self._CopyAndLinkFiles()
@ -1302,7 +1302,7 @@ class Project(object):
else: else:
self._InitMirrorHead() self._InitMirrorHead()
try: try:
os.remove(os.path.join(self.gitdir, 'FETCH_HEAD')) platform_utils.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
except OSError: except OSError:
pass pass
return True return True
@ -1812,7 +1812,7 @@ class Project(object):
except GitError: except GitError:
return [], [] return [], []
finally: finally:
os.remove(temp_gitmodules_path) platform_utils.remove(temp_gitmodules_path)
names = set() names = set()
paths = {} paths = {}
@ -2104,7 +2104,7 @@ class Project(object):
if old_packed != '': if old_packed != '':
_lwrite(packed_refs, old_packed) _lwrite(packed_refs, old_packed)
else: else:
os.remove(packed_refs) platform_utils.remove(packed_refs)
self.bare_git.pack_refs('--all', '--prune') self.bare_git.pack_refs('--all', '--prune')
if is_sha1 and current_branch_only: if is_sha1 and current_branch_only:
@ -2166,14 +2166,14 @@ class Project(object):
ok = GitCommand(self, cmd, bare=True).Wait() == 0 ok = GitCommand(self, cmd, bare=True).Wait() == 0
if os.path.exists(bundle_dst): if os.path.exists(bundle_dst):
os.remove(bundle_dst) platform_utils.remove(bundle_dst)
if os.path.exists(bundle_tmp): if os.path.exists(bundle_tmp):
os.remove(bundle_tmp) platform_utils.remove(bundle_tmp)
return ok return ok
def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet): def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet):
if os.path.exists(dstPath): if os.path.exists(dstPath):
os.remove(dstPath) platform_utils.remove(dstPath)
cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location'] cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location']
if quiet: if quiet:
@ -2183,7 +2183,7 @@ class Project(object):
if size >= 1024: if size >= 1024:
cmd += ['--continue-at', '%d' % (size,)] cmd += ['--continue-at', '%d' % (size,)]
else: else:
os.remove(tmpPath) platform_utils.remove(tmpPath)
if 'http_proxy' in os.environ and 'darwin' == sys.platform: if 'http_proxy' in os.environ and 'darwin' == sys.platform:
cmd += ['--proxy', os.environ['http_proxy']] cmd += ['--proxy', os.environ['http_proxy']]
with GetUrlCookieFile(srcUrl, quiet) as (cookiefile, _proxy): with GetUrlCookieFile(srcUrl, quiet) as (cookiefile, _proxy):
@ -2217,7 +2217,7 @@ class Project(object):
platform_utils.rename(tmpPath, dstPath) platform_utils.rename(tmpPath, dstPath)
return True return True
else: else:
os.remove(tmpPath) platform_utils.remove(tmpPath)
return False return False
else: else:
return False return False
@ -2390,7 +2390,7 @@ class Project(object):
continue continue
if os.path.exists(dst): if os.path.exists(dst):
if filecmp.cmp(stock_hook, dst, shallow=False): if filecmp.cmp(stock_hook, dst, shallow=False):
os.remove(dst) platform_utils.remove(dst)
else: else:
_warn("%s: Not replacing locally modified %s hook", _warn("%s: Not replacing locally modified %s hook",
self.relpath, name) self.relpath, name)
@ -2508,7 +2508,7 @@ class Project(object):
# file doesn't either. # file doesn't either.
if name in symlink_files and not os.path.lexists(src): if name in symlink_files and not os.path.lexists(src):
try: try:
os.remove(dst) platform_utils.remove(dst)
except OSError: except OSError:
pass pass

View File

@ -489,7 +489,7 @@ later is required to fix a server side protocol bug.
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
for f in files: for f in files:
try: try:
os.remove(os.path.join(root, f)) platform_utils.remove(os.path.join(root, f))
except OSError: except OSError:
print('Failed to remove %s' % os.path.join(root, f), file=sys.stderr) print('Failed to remove %s' % os.path.join(root, f), file=sys.stderr)
failed = True failed = True
@ -500,7 +500,7 @@ later is required to fix a server side protocol bug.
for d in reversed(dirs_to_remove): for d in reversed(dirs_to_remove):
if platform_utils.islink(d): if platform_utils.islink(d):
try: try:
os.remove(d) platform_utils.remove(d)
except OSError: except OSError:
print('Failed to remove %s' % os.path.join(root, d), file=sys.stderr) print('Failed to remove %s' % os.path.join(root, d), file=sys.stderr)
failed = True failed = True
@ -712,7 +712,7 @@ later is required to fix a server side protocol bug.
else: # Not smart sync or smart tag mode else: # Not smart sync or smart tag mode
if os.path.isfile(smart_sync_manifest_path): if os.path.isfile(smart_sync_manifest_path):
try: try:
os.remove(smart_sync_manifest_path) platform_utils.remove(smart_sync_manifest_path)
except OSError as e: except OSError as e:
print('error: failed to remove existing smart sync override manifest: %s' % print('error: failed to remove existing smart sync override manifest: %s' %
e, file=sys.stderr) e, file=sys.stderr)
@ -956,7 +956,7 @@ class _FetchTimes(object):
f.close() f.close()
except (IOError, ValueError): except (IOError, ValueError):
try: try:
os.remove(self._path) platform_utils.remove(self._path)
except OSError: except OSError:
pass pass
self._times = {} self._times = {}
@ -980,7 +980,7 @@ class _FetchTimes(object):
f.close() f.close()
except (IOError, TypeError): except (IOError, TypeError):
try: try:
os.remove(self._path) platform_utils.remove(self._path)
except OSError: except OSError:
pass pass