15
15
from pipenv .utils .pip import get_trusted_hosts
16
16
17
17
18
- def redact_netloc (netloc : str ) -> Tuple [ str ] :
18
+ def redact_netloc (netloc : str ) -> str :
19
19
"""
20
20
Replace the sensitive data in a netloc with "****", if it exists, unless it's an environment variable.
21
21
@@ -26,7 +26,7 @@ def redact_netloc(netloc: str) -> Tuple[str]:
26
26
"""
27
27
netloc , (user , password ) = split_auth_from_netloc (netloc )
28
28
if user is None :
29
- return ( netloc ,)
29
+ return netloc
30
30
if password is None :
31
31
# Check if user is an environment variable
32
32
if not re .match (r"\$\{\w+\}" , user ):
@@ -42,12 +42,16 @@ def redact_netloc(netloc: str) -> Tuple[str]:
42
42
# If it is, leave it as is
43
43
password = ":" + password
44
44
user = quote (user )
45
- return ( f"{ user } { password } @{ netloc } " ,)
45
+ return f"{ user } { password } @{ netloc } "
46
46
47
47
48
48
def redact_auth_from_url (url : str ) -> str :
49
49
"""Replace the password in a given url with ****."""
50
- return _transform_url (url , redact_netloc )[0 ]
50
+
51
+ def _redact_netloc_wrapper (netloc : str ) -> Tuple [str ]:
52
+ return (redact_netloc (netloc ),)
53
+
54
+ return _transform_url (url , _redact_netloc_wrapper )[0 ]
51
55
52
56
53
57
def normalize_name (pkg ) -> str :
@@ -68,9 +72,10 @@ def import_requirements(project, r=None, dev=False, categories=None):
68
72
contents = f .read ()
69
73
if categories is None :
70
74
categories = []
75
+
76
+ # Collect indexes and trusted hosts first
71
77
indexes = []
72
78
trusted_hosts = []
73
- # Find and add extra indexes.
74
79
for line in contents .split ("\n " ):
75
80
index , extra_index , trusted_host , _ = parse_indexes (line .strip (), strict = True )
76
81
if index :
@@ -79,8 +84,11 @@ def import_requirements(project, r=None, dev=False, categories=None):
79
84
indexes .append (extra_index )
80
85
if trusted_host :
81
86
trusted_hosts .append (get_host_and_port (trusted_host ))
82
- # Convert Path object to string to avoid 'PosixPath' has no attribute 'decode' error
87
+
88
+ # Collect all packages for batch processing
89
+ packages_to_add = []
83
90
req_path = str (r ) if isinstance (r , Path ) else r
91
+
84
92
for f in parse_requirements (req_path , session = PipSession ()):
85
93
package = install_req_from_parsed_requirement (f )
86
94
if package .name not in BAD_PACKAGES :
@@ -91,29 +99,26 @@ def import_requirements(project, r=None, dev=False, categories=None):
91
99
package_string = unquote (
92
100
redact_auth_from_url (package .original_link .url )
93
101
)
94
-
95
- if categories :
96
- for category in categories :
97
- project .add_package_to_pipfile (
98
- package , package_string , dev = dev , category = category
99
- )
100
- else :
101
- project .add_package_to_pipfile (package , package_string , dev = dev )
102
102
else :
103
103
package_string = str (package .req )
104
104
if package .markers :
105
105
package_string += f" ; { package .markers } "
106
- if categories :
107
- for category in categories :
108
- project .add_package_to_pipfile (
109
- package , package_string , dev = dev , category = category
110
- )
111
- else :
112
- project .add_package_to_pipfile (package , package_string , dev = dev )
106
+
107
+ packages_to_add .append ((package , package_string ))
108
+
109
+ # Batch add all packages to Pipfile
110
+ if packages_to_add :
111
+ project .add_packages_to_pipfile_batch (
112
+ packages_to_add , dev = dev , categories = categories
113
+ )
114
+
115
+ # Add indexes after packages
113
116
indexes = sorted (set (indexes ))
114
117
trusted_hosts = sorted (set (trusted_hosts ))
115
118
for index in indexes :
116
119
add_index_to_pipfile (project , index , trusted_hosts )
120
+
121
+ # Recase pipfile once at the end
117
122
project .recase_pipfile ()
118
123
119
124
0 commit comments