diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2017-09-09 11:58:33 -0400 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2017-09-09 11:58:33 -0400 |
commit | eea94d01a59f7f43dd41c271ff6849995a4d1e7e (patch) | |
tree | a21a1ac13ab53141d9819b14cf2c91b75bba1231 /pkg_resources | |
parent | e9e4cf8c1b55c55f16337f31bbbadd44da615557 (diff) | |
download | external_python_setuptools-eea94d01a59f7f43dd41c271ff6849995a4d1e7e.tar.gz external_python_setuptools-eea94d01a59f7f43dd41c271ff6849995a4d1e7e.tar.bz2 external_python_setuptools-eea94d01a59f7f43dd41c271ff6849995a4d1e7e.zip |
Extract function for listdir
Diffstat (limited to 'pkg_resources')
-rw-r--r-- | pkg_resources/__init__.py | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 3f7e5b66..d9154e6d 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -2030,21 +2030,8 @@ def find_on_path(importer, path_item, only=False): ) return - try: - entries = os.listdir(path_item) - except (PermissionError, NotADirectoryError): - return - except OSError as e: - # Ignore the directory if does not exist, not a directory or we - # don't have permissions - ignorable = ( - e.errno in (errno.ENOTDIR, errno.EACCES, errno.ENOENT) - # Python 2 on Windows needs to be handled this way :( - or getattr(e, "winerror", None) == 267 - ) - if ignorable: - return - raise + entries = safe_listdir(path_item) + # scan for .egg and .egg-info in directory path_item_entries = _by_version_descending(entries) for entry in path_item_entries: @@ -2064,6 +2051,27 @@ def find_on_path(importer, path_item, only=False): yield dist +def safe_listdir(path): + """ + Attempt to list contents of path, but suppress some exceptions. + """ + try: + return os.listdir(path) + except (PermissionError, NotADirectoryError): + pass + except OSError as e: + # Ignore the directory if does not exist, not a directory or + # permission denied + ignorable = ( + e.errno in (errno.ENOTDIR, errno.EACCES, errno.ENOENT) + # Python 2 on Windows needs to be handled this way :( + or getattr(e, "winerror", None) == 267 + ) + if not ignorable: + raise + return () + + def distributions_from_metadata(path): root = os.path.dirname(path) if os.path.isdir(path): |