Skip to content

Commit 1dc35a9

Browse files
Support default host in netrc
Support the fallback to `default` if the user did not set a specific host name in their netrc file. Signed-off-by: Marcel Bochtler <[email protected]>
1 parent b25992c commit 1dc35a9

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/python_inspector/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
def get_netrc_auth(url, netrc):
2626
"""
27-
Return login and password if the hostname is in netrc
27+
Return login and password if either the hostname is in netrc or a default is set in netrc
2828
else return login and password as None
2929
"""
3030
hostname = urlparse(url).hostname
@@ -33,6 +33,11 @@ def get_netrc_auth(url, netrc):
3333
url_auth = hosts.get(hostname)
3434
# netrc returns a tuple of (login, account, password)
3535
return (url_auth[0], url_auth[2])
36+
37+
if 'default' in hosts:
38+
default_auth = hosts.get('default')
39+
return (default_auth[0], default_auth[2])
40+
3641
return (None, None)
3742

3843

tests/data/test-default.netrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
machine example.com login test password test123
2+
default login defaultuser password defaultpass

tests/test_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def test_get_netrc_auth_with_with_subdomains():
6767
assert get_netrc_auth(url="https://another.example.com/simple", netrc=parsed_netrc) == (None, None)
6868

6969

70+
def test_get_netrc_auth_with_default():
71+
netrc_file = test_env.get_test_loc("test-default.netrc")
72+
parsed_netrc = netrc(netrc_file)
73+
74+
assert get_netrc_auth(url="https://example.com/simple", netrc=parsed_netrc) == ("test", "test123")
75+
assert get_netrc_auth(url="https://non-existing.org/simple", netrc=parsed_netrc) == ("defaultuser", "defaultpass")
76+
77+
7078
@pytest.mark.asyncio
7179
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
7280
@mock.patch("python_inspector.utils_pypi.CACHE.get")

0 commit comments

Comments
 (0)