From 965a1cdffa2c4e85aaf1df3e14015aa5b5fdf24b Mon Sep 17 00:00:00 2001 From: Nick Timkovich Date: Thu, 13 Sep 2018 13:46:57 -0500 Subject: [PATCH] Quiet warnings about netrc If there isn't a .netrc file specified by an environment variable, it can be confusing to see warnings about it. If NETRC isn't set, don't warn. Only warn if: a) can't resolve HOME, b) can load, but can't parse file, c) can't find file, d) file appears to exist at the default location but is unreadable for some reason. --- aiohttp/helpers.py | 54 +++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index b062917b846..4cdf3e40c10 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -9,6 +9,7 @@ import inspect import netrc import os +import platform import re import sys import time @@ -146,30 +147,39 @@ def strip_auth_from_url(url: URL) -> Tuple[URL, Optional[BasicAuth]]: def netrc_from_env(): - netrc_obj = None - netrc_path = os.environ.get('NETRC') - try: - if netrc_path is not None: - netrc_path = Path(netrc_path) - else: + """Attempt to load the netrc file from the path specified by the env-var + NETRC or in the default location in the user's home directory. + + Returns ``None`` if it couldn't be found or fails to parse. + """ + netrc_env = os.environ.get('NETRC') + + if netrc_env is not None: + netrc_path = Path(netrc_env) + else: + try: home_dir = Path.home() - if os.name == 'nt': # pragma: no cover - netrc_path = home_dir.joinpath('_netrc') - else: - netrc_path = home_dir.joinpath('.netrc') + except RuntimeError as e: # pragma: no cover + # if pathlib can't resolve home, it may raise a RuntimeError + client_logger.warning('Could not resolve home directory when ' + 'trying to look for .netrc file: %s', e) + return None - if netrc_path and netrc_path.is_file(): - try: - netrc_obj = netrc.netrc(str(netrc_path)) - except (netrc.NetrcParseError, OSError) as e: - client_logger.warning(".netrc file parses fail: %s", e) - - if netrc_obj is None: - client_logger.warning("could't find .netrc file") - except RuntimeError as e: # pragma: no cover - """ handle error raised by pathlib """ - client_logger.warning("could't find .netrc file: %s", e) - return netrc_obj + netrc_path = home_dir / ( + '_netrc' if platform.system() == 'Windows' else '.netrc') + + try: + return netrc.netrc(str(netrc_path)) + except netrc.NetrcParseError as e: + client_logger.warning('Could not parse .netrc file: %s', e) + except OSError as e: + # we couldn't read the file (doesn't exist, permissions, etc.) + if netrc_env or netrc_path.is_file(): + # only warn if the enviroment wanted us to load it, + # or it appears like the default file does actually exist + client_logger.warning('Could not read .netrc file: %s', e) + + return None @attr.s(frozen=True, slots=True)