Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve failure modes for os.rename() as used in distribution caching. #271

Merged
merged 1 commit into from
May 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pex/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ def safe_rmtree(directory):
shutil.rmtree(directory, True)


def rename_if_empty(src, dest, allowable_errors=(errno.EEXIST, errno.ENOTEMPTY)):
"""Rename `src` to `dest` using `os.rename()`.

If an `OSError` with errno in `allowable_errors` is encountered during the rename, the `dest`
dir is left unchanged and the `src` directory will simply be removed.
"""
try:
os.rename(src, dest)
except OSError as e:
if e.errno in allowable_errors:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the case of ENOTEMPTY, it's no-op only if dest == src, otherwise we remove src but dest != src

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct - it's not pretty, but this is the current behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eliminated the no-op terminology.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

safe_rmtree(src)
else:
raise


def chmod_plus_x(path):
"""Equivalent of unix `chmod a+x path`"""
path_mode = os.stat(path).st_mode
Expand Down
12 changes: 3 additions & 9 deletions pex/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import absolute_import

import contextlib
import errno
import os
import shutil
import tempfile
Expand All @@ -14,7 +13,7 @@

from pkg_resources import find_distributions, resource_isdir, resource_listdir, resource_string

from .common import safe_mkdir, safe_mkdtemp, safe_open, safe_rmtree
from .common import rename_if_empty, safe_mkdir, safe_mkdtemp, safe_open
from .finders import register_finders


Expand Down Expand Up @@ -176,13 +175,8 @@ def cache_distribution(cls, zf, source, target_dir):
with contextlib.closing(zf.open(name)) as zi:
with safe_open(os.path.join(target_dir_tmp, target_name), 'wb') as fp:
shutil.copyfileobj(zi, fp)
try:
os.rename(target_dir_tmp, target_dir)
except OSError as e:
if e.errno == errno.ENOTEMPTY:
safe_rmtree(target_dir_tmp)
else:
raise

rename_if_empty(target_dir_tmp, target_dir)

dist = DistributionHelper.distribution_from_path(target_dir)
assert dist is not None, 'Failed to cache distribution %s' % source
Expand Down
44 changes: 44 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import errno
import os
from contextlib import contextmanager

import pytest

from pex.common import rename_if_empty

try:
from unittest import mock
except ImportError:
import mock


@contextmanager
def maybe_raises(exception=None):
@contextmanager
def noop():
yield

with (noop() if exception is None else pytest.raises(exception)):
yield


def rename_if_empty_test(errno, expect_raises=None):
with mock.patch('os.rename', spec_set=True, autospec=True) as mock_rename:
mock_rename.side_effect = OSError(errno, os.strerror(errno))
with maybe_raises(expect_raises):
rename_if_empty('from.dir', 'to.dir')


def test_rename_if_empty_eexist():
rename_if_empty_test(errno.EEXIST)


def test_rename_if_empty_enotempty():
rename_if_empty_test(errno.ENOTEMPTY)


def test_rename_if_empty_eperm():
rename_if_empty_test(errno.EPERM, expect_raises=OSError)