Skip to content

Commit f81111c

Browse files
committed
Add support for comments in netrc file
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 39588f1 commit f81111c

5 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/python_inspector/api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#
1111

1212
import os
13+
from netrc import netrc
1314
from typing import Dict
1415
from typing import List
1516
from typing import NamedTuple
@@ -19,7 +20,6 @@
1920
from packvers.requirements import Requirement
2021
from resolvelib import BaseReporter
2122
from resolvelib import Resolver
22-
from tinynetrc import Netrc
2323

2424
from _packagedcode.models import DependentPackage
2525
from _packagedcode.models import PackageData
@@ -128,9 +128,9 @@ def resolve_dependencies(
128128
if netrc_file:
129129
if verbose:
130130
printer(f"Using netrc file {netrc_file}")
131-
netrc = Netrc(file=netrc_file)
131+
parsed_netrc = netrc(netrc_file)
132132
else:
133-
netrc = None
133+
parsed_netrc = None
134134

135135
# TODO: deduplicate me
136136
direct_dependencies = []
@@ -233,8 +233,8 @@ def resolve_dependencies(
233233
repos.append(existing)
234234
else:
235235
credentials = None
236-
if netrc:
237-
login, password = utils.get_netrc_auth(index_url, netrc)
236+
if parsed_netrc:
237+
login, password = utils.get_netrc_auth(index_url, parsed_netrc)
238238
credentials = (
239239
dict(login=login, password=password) if login and password else None
240240
)

src/python_inspector/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import json
1313
import os
14+
import tempfile
1415
from typing import Dict
1516
from typing import List
1617
from typing import NamedTuple
@@ -23,8 +24,11 @@ def get_netrc_auth(url, netrc):
2324
Return login and password if url is in netrc
2425
else return login and password as None
2526
"""
26-
if netrc.get(url):
27-
return (netrc[url].get("login"), netrc[url].get("password"))
27+
hosts = netrc.hosts
28+
if url in hosts:
29+
url_auth = hosts.get(url)
30+
# netrc returns a tuple of (login, account, password)
31+
return (url_auth[0], url_auth[2])
2832
return (None, None)
2933

3034

tests/data/test-commented.netrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
machine https://pyp2.org/simple login test password test123
2+
# machine https://pyp1.org/simple login test password test123

tests/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_cli_with_multiple_index_url_and_tilde_req_with_max_rounds():
219219
@pytest.mark.online
220220
def test_cli_with_multiple_index_url_and_tilde_req_and_netrc_file_without_matching_url():
221221
expected_file = test_env.get_test_loc("tilde_req-expected.json", must_exist=False)
222-
netrc_file = test_env.get_test_loc("test.netrc", must_exist=False)
222+
netrc_file = test_env.get_test_loc("test-commented.netrc", must_exist=False)
223223
specifier = "zipp~=3.8.0"
224224
extra_options = [
225225
"--index-url",

tests/test_utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import collections
1212
import json
1313
import os
14+
from netrc import netrc
1415
from unittest import mock
1516

1617
from commoncode.testcase import FileDrivenTesting
1718
from test_cli import check_json_results
18-
from tinynetrc import Netrc
1919

2020
from _packagedcode.pypi import SetupCfgHandler
2121
from python_inspector.resolution import fetch_and_extract_sdist
@@ -31,14 +31,20 @@
3131

3232
def test_get_netrc_auth():
3333
netrc_file = test_env.get_test_loc("test.netrc")
34-
netrc = Netrc(netrc_file)
35-
assert get_netrc_auth(url="https://pyp1.org/simple", netrc=netrc) == ("test", "test123")
34+
parsed_netrc = netrc(netrc_file)
35+
assert get_netrc_auth(url="https://pyp1.org/simple", netrc=parsed_netrc) == ("test", "test123")
36+
37+
38+
def test_get_commented_netrc_auth():
39+
netrc_file = test_env.get_test_loc("test-commented.netrc")
40+
parsed_netrc = netrc(netrc_file)
41+
assert get_netrc_auth(url="https://pyp2.org/simple", netrc=parsed_netrc) == ("test", "test123")
3642

3743

3844
def test_get_netrc_auth_with_no_matching_url():
3945
netrc_file = test_env.get_test_loc("test.netrc")
40-
netrc = Netrc(netrc_file)
41-
assert get_netrc_auth(url="https://pypi2.org/simple", netrc=netrc) == (None, None)
46+
parsed_netrc = netrc(netrc_file)
47+
assert get_netrc_auth(url="https://pypi2.org/simple", netrc=parsed_netrc) == (None, None)
4248

4349

4450
@mock.patch("python_inspector.utils_pypi.CACHE.get")

0 commit comments

Comments
 (0)