diff options
| author | Renaud Paquay <rpaquay@google.com> | 2016-11-01 11:34:55 -0700 |
|---|---|---|
| committer | David Pursehouse <dpursehouse@collab.net> | 2017-05-29 19:33:07 +0900 |
| commit | ad1abcb556bfff2744928a8f29d216aee43fdbe3 (patch) | |
| tree | c0d515e2d7c2d097ecb8b31e3e365d86696205b1 | |
| parent | a65adf74f990eeac0d90011476376c7239cb7af5 (diff) | |
| download | git-repo-ad1abcb556bfff2744928a8f29d216aee43fdbe3.tar.gz | |
Port os.rename calls to work on Windows
os.rename fails on Windows if the destination exists, so replace
os.rename to platform_utils.rename which handles the platform
differences.
Change-Id: I15a86f10f65eedee5b003b80f88a0c28a3e1aa48
| -rw-r--r-- | platform_utils.py | 17 | ||||
| -rw-r--r-- | project.py | 4 |
2 files changed, 19 insertions, 2 deletions
diff --git a/platform_utils.py b/platform_utils.py index 4417c5a3..e0fa9dcc 100644 --- a/platform_utils.py +++ b/platform_utils.py | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | # See the License for the specific language governing permissions and | 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. | 14 | # limitations under the License. |
| 15 | 15 | ||
| 16 | import errno | ||
| 16 | import os | 17 | import os |
| 17 | import platform | 18 | import platform |
| 18 | import select | 19 | import select |
| @@ -225,3 +226,19 @@ def handle_rmtree_error(function, path, excinfo): | |||
| 225 | # Allow deleting read-only files | 226 | # Allow deleting read-only files |
| 226 | os.chmod(path, stat.S_IWRITE) | 227 | os.chmod(path, stat.S_IWRITE) |
| 227 | function(path) | 228 | function(path) |
| 229 | |||
| 230 | |||
| 231 | def rename(src, dst): | ||
| 232 | if isWindows(): | ||
| 233 | # On Windows, rename fails if destination exists, see | ||
| 234 | # https://docs.python.org/2/library/os.html#os.rename | ||
| 235 | try: | ||
| 236 | os.rename(src, dst) | ||
| 237 | except OSError as e: | ||
| 238 | if e.errno == errno.EEXIST: | ||
| 239 | os.remove(dst) | ||
| 240 | os.rename(src, dst) | ||
| 241 | else: | ||
| 242 | raise | ||
| 243 | else: | ||
| 244 | os.rename(src, dst) | ||
| @@ -63,7 +63,7 @@ def _lwrite(path, content): | |||
| 63 | fd.close() | 63 | fd.close() |
| 64 | 64 | ||
| 65 | try: | 65 | try: |
| 66 | os.rename(lock, path) | 66 | platform_utils.rename(lock, path) |
| 67 | except OSError: | 67 | except OSError: |
| 68 | os.remove(lock) | 68 | os.remove(lock) |
| 69 | raise | 69 | raise |
| @@ -2198,7 +2198,7 @@ class Project(object): | |||
| 2198 | 2198 | ||
| 2199 | if os.path.exists(tmpPath): | 2199 | if os.path.exists(tmpPath): |
| 2200 | if curlret == 0 and self._IsValidBundle(tmpPath, quiet): | 2200 | if curlret == 0 and self._IsValidBundle(tmpPath, quiet): |
| 2201 | os.rename(tmpPath, dstPath) | 2201 | platform_utils.rename(tmpPath, dstPath) |
| 2202 | return True | 2202 | return True |
| 2203 | else: | 2203 | else: |
| 2204 | os.remove(tmpPath) | 2204 | os.remove(tmpPath) |
