@@ -44,6 +44,10 @@ def __post_init__(self):
44
44
if self .requirements is None :
45
45
self .requirements = []
46
46
47
+ def add_requirement (self , req : str ):
48
+ """Add a requirement with the currently active index URL."""
49
+ self .requirements .append ((req , self .index_url ))
50
+
47
51
48
52
REQ_FILE_PREFIX = r"^(-r|--requirements)\s*=?\s*(.*)\s*"
49
53
INDEX_URL_PREFIX = r"^(--index-url|-i)\s*=?\s*(.*)\s*"
@@ -135,11 +139,14 @@ async def get_action_kwargs(argv: list[str]) -> tuple[typing.Optional[str], dict
135
139
return None , {}
136
140
137
141
kwargs = {}
138
-
139
142
action = args .action
140
143
141
144
if action == "install" :
142
- kwargs ["requirements" ] = args .packages
145
+ if args .index_url :
146
+ # If there's a CLI index URL, use it for command-line packages
147
+ kwargs ["requirements" ] = [(pkg , args .index_url ) for pkg in args .packages ]
148
+ else :
149
+ kwargs ["requirements" ] = [(pkg , None ) for pkg in args .packages ]
143
150
144
151
if args .pre :
145
152
kwargs ["pre" ] = True
@@ -150,20 +157,23 @@ async def get_action_kwargs(argv: list[str]) -> tuple[typing.Optional[str], dict
150
157
if args .verbose :
151
158
kwargs ["keep_going" ] = True
152
159
153
- if args .index_url :
154
- kwargs ["index_urls" ] = args .index_url
155
-
156
- # Handle requirements files in case they contain index URLs
157
- current_index_url = args .index_url
158
160
for req_file in args .requirements or []:
159
- reqs , index_url = await _packages_from_requirements_file (Path (req_file ))
160
- kwargs ["requirements" ].extend (reqs )
161
- # Update index URL if one was found in requirements file
162
- if index_url is not None :
163
- current_index_url = index_url
164
-
165
- if current_index_url :
166
- kwargs ["index_urls" ] = current_index_url
161
+ reqs_with_indices = await _packages_from_requirements_file (Path (req_file ))
162
+ kwargs ["requirements" ].extend (reqs_with_indices )
163
+
164
+ # Convert requirements to proper format for piplite.install
165
+ if kwargs .get ("requirements" ):
166
+ by_index = {}
167
+ for req , idx in kwargs ["requirements" ]:
168
+ by_index .setdefault (idx , []).append (req )
169
+
170
+ # Install each group with its index URL
171
+ all_requirements = []
172
+ for idx , reqs in by_index .items ():
173
+ if idx :
174
+ kwargs ["index_urls" ] = idx
175
+ all_requirements .extend (reqs )
176
+ kwargs ["requirements" ] = all_requirements
167
177
168
178
return action , kwargs
169
179
@@ -208,10 +218,8 @@ async def _packages_from_requirements_line(
208
218
sub_req = Path (sub_path )
209
219
else :
210
220
sub_req = req_path .parent / sub_path
211
- sub_reqs , sub_index = await _packages_from_requirements_file (sub_req )
212
- # Only update index URL if one was found
213
- if sub_index is not None :
214
- context .index_url = sub_index
221
+ sub_reqs , _ = await _packages_from_requirements_file (sub_req )
222
+ # Use current context's index_url for nested requirements
215
223
context .requirements .extend (sub_reqs )
216
224
return
217
225
@@ -225,4 +233,4 @@ async def _packages_from_requirements_line(
225
233
warn (f"{ req_path } :{ line_no } : unrecognized requirement: { req } " )
226
234
return
227
235
228
- context .requirements . append (req )
236
+ context .add_requirement (req )
0 commit comments