Skip to content

Commit b1d0ab7

Browse files
committed
If local packages add new dependencies, add them to lock file
1 parent 3b4aa74 commit b1d0ab7

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

scripts/pyodide_packages.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102

103103

104104
# =============================================
105-
# Data structures used in our requirements.json
105+
# Data structures used in our shinylive_requirements.json
106106
# =============================================
107107
class RequirementsPackage(TypedDict):
108108
name: str
@@ -111,7 +111,7 @@ class RequirementsPackage(TypedDict):
111111

112112

113113
# ====================================================
114-
# Data structures used in our extra_packages_lock.json
114+
# Data structures used in our shinylive_lock.json
115115
# ====================================================
116116
class LockfileDependency(TypedDict):
117117
name: str
@@ -214,6 +214,15 @@ class PyodidePackagesFile(TypedDict):
214214
packages: dict[str, PyodidePackageInfo]
215215

216216

217+
# =================================================
218+
# Internal data structures
219+
# =================================================
220+
# This BasicPackageInfo type is used internally for package resolution.
221+
class BasicPackageInfo(TypedDict):
222+
name: str
223+
depends: list[str]
224+
225+
217226
# =============================================================================
218227
# Functions for generating the lockfile from the requirements file.
219228
# =============================================================================
@@ -241,6 +250,16 @@ def update_lockfile_local() -> None:
241250

242251
lockfile_info.update(required_package_info)
243252

253+
# If any dependencies in the lockfile_info are not in the union of {lockfile_info,
254+
# orig_pyodide_lock()["packages"]}, then we need to add them to the lockfile_info.
255+
# This can happen when a local package adds a new dependency.
256+
print("Searching for new dependencies")
257+
# basic_package_info is the union of lockfile_info and original pyodide packages.
258+
basic_package_info = _to_basic_package_info(lockfile_info)
259+
basic_package_info.update(_to_basic_package_info(orig_pyodide_lock()["packages"]))
260+
_recurse_dependencies_lockfile(lockfile_info, basic_package_info)
261+
262+
print(f"Writing {package_lock_file}")
244263
with open(package_lock_file, "w") as f:
245264
json.dump(
246265
_mark_no_indent(lockfile_info, _is_lockfile_dependency),
@@ -277,12 +296,15 @@ def generate_lockfile() -> None:
277296

278297
def _recurse_dependencies_lockfile(
279298
pkgs: dict[str, LockfilePackageInfo],
299+
pyodide_packages_info: dict[str, BasicPackageInfo] | None = None,
280300
) -> None:
281301
"""
282302
Recursively find all dependencies of the given packages. This will mutate the object
283303
passed in.
284304
"""
285-
pyodide_packages_info = orig_pyodide_lock()["packages"]
305+
if pyodide_packages_info is None:
306+
pyodide_packages_info = _to_basic_package_info(orig_pyodide_lock()["packages"])
307+
286308
i = 0
287309
while i < len(pkgs):
288310
pkg_info = pkgs[list(pkgs.keys())[i]]
@@ -506,6 +528,35 @@ def _dep_filter(dep_str: str) -> bool:
506528
return list(res)
507529

508530

531+
def _to_basic_package_info(
532+
packages: dict[str, PyodidePackageInfo] | dict[str, LockfilePackageInfo]
533+
) -> dict[str, BasicPackageInfo]:
534+
"""
535+
Convert Pyodide package information to a simplified format.
536+
537+
Args:
538+
packages (dict[str, PyodidePackageInfo]): Original package information from Pyodide lock file
539+
540+
Returns:
541+
dict[str, BasicPackageInfo]: Simplified package information with name and depends
542+
"""
543+
basic_info: dict[str, BasicPackageInfo] = {}
544+
for pkg_name, pkg_data in packages.items():
545+
depends = pkg_data["depends"]
546+
depends_str_list: list[str] = []
547+
for dep in depends:
548+
if isinstance(dep, str):
549+
depends_str_list.append(dep)
550+
else:
551+
depends_str_list.append(dep["name"])
552+
553+
basic_info[pkg_name.lower()] = {
554+
"name": pkg_name,
555+
"depends": depends_str_list,
556+
}
557+
return basic_info
558+
559+
509560
# =============================================================================
510561
# Functions for copying and downloading the wheel files.
511562
# =============================================================================

0 commit comments

Comments
 (0)