Skip to content

Commit e4e7a30

Browse files
Some more fixes for install order precedence
1 parent 443c206 commit e4e7a30

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

packages/pyodide-kernel/py/piplite/piplite/cli.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ async def get_transformed_code(argv: list[str]) -> typing.Optional[str]:
130130

131131
async def get_action_kwargs(argv: list[str]) -> tuple[typing.Optional[str], dict]:
132132
"""Get the arguments to `piplite` subcommands from CLI-like tokens."""
133-
134133
parser = _get_parser()
135134

136135
try:
@@ -151,40 +150,40 @@ async def get_action_kwargs(argv: list[str]) -> tuple[typing.Optional[str], dict
151150
for req_file in args.requirements or []:
152151
context = RequirementsContext()
153152

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-
159153
if not Path(req_file).exists():
160154
warn(f"piplite could not find requirements file {req_file}")
161155
continue
162156

157+
# First let the file be processed normally to capture any index URL
163158
for line_no, line in enumerate(
164159
Path(req_file).read_text(encoding="utf-8").splitlines()
165160
):
166161
await _packages_from_requirements_line(
167162
Path(req_file), line_no + 1, line, context
168163
)
169164

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)
171174

172175
if all_requirements:
173-
by_index = {}
174-
file_index_url = None
176+
kwargs["requirements"] = []
177+
active_index_url = None
175178

176179
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)
180183

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
188187

189188
if args.pre:
190189
kwargs["pre"] = True

packages/pyodide-kernel/py/piplite/piplite/piplite.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,15 @@ async def _install(
129129
*,
130130
verbose: bool | int = False,
131131
):
132-
"""Invoke micropip.install with a patch to get data from local indexes"""
132+
"""Invoke micropip.install with a patch to get data from local indexes.
133133
134+
This function handles the installation of Python packages, respecting index URLs
135+
from various sources (CLI, requirements files, or defaults) while maintaining
136+
precedence order provided for indices.
137+
138+
Arguments maintain the same semantics as micropip.install, but with additional
139+
handling of index URLs and installation defaults.
140+
"""
134141
try:
135142
install_args = _PIPLITE_DEFAULT_INSTALL_ARGS.copy()
136143

@@ -140,13 +147,18 @@ async def _install(
140147
"deps": deps,
141148
"credentials": credentials,
142149
"pre": pre,
143-
"index_urls": index_urls,
144150
"verbose": verbose,
145151
}
152+
153+
if index_urls is not None:
154+
provided_args["index_urls"] = index_urls
155+
146156
install_args.update({k: v for k, v in provided_args.items() if v is not None})
147157

148158
if verbose:
149159
logger.info(f"Installing with arguments: {install_args}")
160+
if install_args.get("index_urls"):
161+
logger.info(f"Using index URL: {install_args['index_urls']}")
150162

151163
with patch("micropip.package_index.query_package", _query_package):
152164
return await micropip.install(**install_args)

0 commit comments

Comments
 (0)