Skip to content

Commit 4f5c636

Browse files
committed
Support comments in netrc file #107
Reference: #107 Signed-off-by: Tushar Goel <[email protected]>
1 parent 9e765ec commit 4f5c636

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/python_inspector/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def resolve_dependencies(
128128
if netrc_file:
129129
if verbose:
130130
printer(f"Using netrc file {netrc_file}")
131+
netrc_file = utils.ignore_comments_from_netrc(netrc_file)
131132
netrc = Netrc(file=netrc_file)
132133
else:
133134
netrc = None

src/python_inspector/utils.py

+38
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
@@ -69,3 +70,40 @@ def get_response(url: str) -> Dict:
6970
resp = requests.get(url)
7071
if resp.status_code == 200:
7172
return resp.json()
73+
74+
75+
def ignore_comments_from_netrc(netrc_location):
76+
"""
77+
Read the netrc file and if any line starts with ``#`` then return a new temp file
78+
with the lines that are not commented out.
79+
"""
80+
if not check_if_file_has_comments(netrc_location):
81+
return netrc_location
82+
return create_temp_file_without_comments(netrc_location)
83+
84+
85+
def create_temp_file_without_comments(netrc_location):
86+
"""
87+
Return a temp file location with the lines that are not commented out.
88+
"""
89+
temp = tempfile.NamedTemporaryFile(delete=False)
90+
location = temp.name
91+
with open(netrc_location) as f:
92+
lines = f.readlines()
93+
for line in lines:
94+
if not line.startswith("#"):
95+
temp.write(line.encode())
96+
temp.close()
97+
return location
98+
99+
100+
def check_if_file_has_comments(file_location):
101+
"""
102+
Return True if the ``file_location`` has commented lines.
103+
"""
104+
with open(file_location) as f:
105+
lines = f.readlines()
106+
for line in lines:
107+
if line.startswith("#"):
108+
return True
109+
return False

tests/data/test-commented.netrc

+2
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_utils.py

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from _packagedcode.pypi import SetupCfgHandler
2121
from python_inspector.resolution import fetch_and_extract_sdist
2222
from python_inspector.utils import get_netrc_auth
23+
from python_inspector.utils import ignore_comments_from_netrc
2324
from python_inspector.utils_pypi import PypiSimpleRepository
2425
from python_inspector.utils_pypi import valid_python_version
2526

@@ -35,6 +36,13 @@ def test_get_netrc_auth():
3536
assert get_netrc_auth(url="https://pyp1.org/simple", netrc=netrc) == ("test", "test123")
3637

3738

39+
def test_get_commented_netrc_auth():
40+
netrc_file = test_env.get_test_loc("test-commented.netrc")
41+
netrc_file = ignore_comments_from_netrc(netrc_file)
42+
netrc = Netrc(netrc_file)
43+
assert get_netrc_auth(url="https://pyp2.org/simple", netrc=netrc) == ("test", "test123")
44+
45+
3846
def test_get_netrc_auth_with_no_matching_url():
3947
netrc_file = test_env.get_test_loc("test.netrc")
4048
netrc = Netrc(netrc_file)

0 commit comments

Comments
 (0)