@@ -130,7 +130,6 @@ async def get_transformed_code(argv: list[str]) -> typing.Optional[str]:
130
130
131
131
async def get_action_kwargs (argv : list [str ]) -> tuple [typing .Optional [str ], dict ]:
132
132
"""Get the arguments to `piplite` subcommands from CLI-like tokens."""
133
-
134
133
parser = _get_parser ()
135
134
136
135
try :
@@ -151,40 +150,40 @@ async def get_action_kwargs(argv: list[str]) -> tuple[typing.Optional[str], dict
151
150
for req_file in args .requirements or []:
152
151
context = RequirementsContext ()
153
152
154
- # If CLI index URL is provided, it should override within-file-level
155
- # index URL for all requirements.
156
- if args .index_url :
157
- context .index_url = args .index_url
158
-
159
153
if not Path (req_file ).exists ():
160
154
warn (f"piplite could not find requirements file { req_file } " )
161
155
continue
162
156
157
+ # First let the file be processed normally to capture any index URL
163
158
for line_no , line in enumerate (
164
159
Path (req_file ).read_text (encoding = "utf-8" ).splitlines ()
165
160
):
166
161
await _packages_from_requirements_line (
167
162
Path (req_file ), line_no + 1 , line , context
168
163
)
169
164
170
- all_requirements .extend (context .requirements )
165
+ # If CLI provided an index URL, it should override the file's index URL
166
+ # We update all requirements to use the CLI index URL instead. Or, we use
167
+ # whatever index URL was found in the file (if any).
168
+ if args .index_url :
169
+ all_requirements .extend (
170
+ (req , args .index_url ) for req , _ in context .requirements
171
+ )
172
+ else :
173
+ all_requirements .extend (context .requirements )
171
174
172
175
if all_requirements :
173
- by_index = {}
174
- file_index_url = None
176
+ kwargs [ "requirements" ] = []
177
+ active_index_url = None
175
178
176
179
for req , idx in all_requirements :
177
- if idx :
178
- file_index_url = idx
179
- by_index . setdefault ( file_index_url , []) .append (req )
180
+ if idx is not None :
181
+ active_index_url = idx
182
+ kwargs [ "requirements" ] .append (req )
180
183
181
- # Build final kwargs. We set the index URL if one was found
182
- # (either passed to the CLI or passed within the requirements file)
183
- kwargs ["requirements" ] = []
184
- for idx , reqs in by_index .items ():
185
- if idx :
186
- kwargs ["index_urls" ] = idx
187
- kwargs ["requirements" ].extend (reqs )
184
+ # Set the final index URL, if we found one
185
+ if active_index_url is not None :
186
+ kwargs ["index_urls" ] = active_index_url
188
187
189
188
if args .pre :
190
189
kwargs ["pre" ] = True
0 commit comments