File tree 4 files changed +49
-0
lines changed
4 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -128,6 +128,7 @@ def resolve_dependencies(
128
128
if netrc_file :
129
129
if verbose :
130
130
printer (f"Using netrc file { netrc_file } " )
131
+ netrc_file = utils .ignore_comments_from_netrc (netrc_file )
131
132
netrc = Netrc (file = netrc_file )
132
133
else :
133
134
netrc = None
Original file line number Diff line number Diff line change 11
11
12
12
import json
13
13
import os
14
+ import tempfile
14
15
from typing import Dict
15
16
from typing import List
16
17
from typing import NamedTuple
@@ -69,3 +70,40 @@ def get_response(url: str) -> Dict:
69
70
resp = requests .get (url )
70
71
if resp .status_code == 200 :
71
72
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
Original file line number Diff line number Diff line change
1
+ machine https://pyp2.org/simple login test password test123
2
+ # machine https://pyp1.org/simple login test password test123
Original file line number Diff line number Diff line change 20
20
from _packagedcode .pypi import SetupCfgHandler
21
21
from python_inspector .resolution import fetch_and_extract_sdist
22
22
from python_inspector .utils import get_netrc_auth
23
+ from python_inspector .utils import ignore_comments_from_netrc
23
24
from python_inspector .utils_pypi import PypiSimpleRepository
24
25
from python_inspector .utils_pypi import valid_python_version
25
26
@@ -35,6 +36,13 @@ def test_get_netrc_auth():
35
36
assert get_netrc_auth (url = "https://pyp1.org/simple" , netrc = netrc ) == ("test" , "test123" )
36
37
37
38
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
+
38
46
def test_get_netrc_auth_with_no_matching_url ():
39
47
netrc_file = test_env .get_test_loc ("test.netrc" )
40
48
netrc = Netrc (netrc_file )
You can’t perform that action at this time.
0 commit comments