Skip to content

Commit

Permalink
Quiet warnings about netrc
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Nick Timkovich committed Sep 13, 2018
1 parent 2327a11 commit c9e3203
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,38 @@ 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 find .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
if os.name == 'nt': # pragma: no cover
netrc_path = home_dir.joinpath('_netrc')
else:
netrc_path = home_dir.joinpath('.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:
# if the environment is naive to netrc, don't mention it
if netrc_env or netrc_path.is_file():
client_logger.warning('Could not read .netrc file: %s', e)

return None


@attr.s(frozen=True, slots=True)
Expand Down

0 comments on commit c9e3203

Please sign in to comment.