Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/guides/bring-up-a-board.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ rebuild the firmware, or put `/lib` ahead of `.frozen` on `sys.path`, which
`omip` notices and then installs as usual. `force=True` installs everything
regardless.

When the firmware has only part of a package frozen in, `omip` installs the
modules it lacks and nothing else: `Installing only medialink of omnipython`. A
firmware that left modules out on purpose to save flash says so in a frozen
`_omip_left_out` module, and `omip` doesn't install those either.

**Take both halves from the same place.** The pair above mixes the *released*
`omnipython` (the index) with the board config from `main` (`github:`). That
is fine when the two agree, and wrong the moment `main`'s board configs pass
Expand Down
70 changes: 58 additions & 12 deletions lib/omip.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,57 @@ def _how_to_replace():
return "To run a newer copy, rebuild the firmware with it"


def _skip_frozen(plan, spec, paths):
"""Whether every module a package installs is frozen in already, where it would win."""
def _left_out():
"""The modules a firmware left out of a package it froze, on purpose, to save flash.

A build that freezes only part of a package names the rest in a frozen
`_omip_left_out` module, and those count as there: installing them would
fill the filesystem the build made room in.
"""
path = sys.path[:]
sys.path[:] = [".frozen"]
try:
import _omip_left_out

return tuple(_omip_left_out.MODULES)
except ImportError:
return ()
finally:
sys.path[:] = path
sys.modules.pop("_omip_left_out", None)


def _in_modules(path, names):
"""Whether the file at path belongs to one of the modules named."""
stem, dot, ext = path.rpartition(".")
dotted = (stem if dot and ext in ("py", "mpy") else path).replace("/", ".")
return any(dotted == name or dotted.startswith(name + ".") for name in names)


def _frozen_part(plan, spec, paths):
"""What of a package not to install, as (modules, whole).

The modules are the ones the firmware has frozen in, where they would win,
and the ones it left out on purpose. whole is True when that is all of them,
and the package is skipped. A package the firmware froze an older copy of,
without a module added since, gets that module alone.
"""
names = _module_names(paths)
if not names or not _frozen_wins(plan):
return False
return [], False
found = [_in_firmware(name) for name in names]
# A module the firmware has built in counts when the rest is frozen: CircuitPython's own
# wifi, say, beside a frozen library. Built in alone doesn't, as a package may extend one.
if None in found or "frozen" not in found:
return False
print("Skipping %s: it's frozen into this firmware, and that copy runs ahead of one in %s." % (spec, plan.libs))
print(_how_to_replace())
return True
if "frozen" not in found:
return [], False
left_out = _left_out()
missing = [n for n, f in zip(names, found) if f is None and n.split(".")[0] not in left_out]
if not missing:
print("Skipping %s: it's frozen into this firmware, and that copy runs ahead of one in %s." % (spec, plan.libs))
print(_how_to_replace())
return names, True
print("Installing only %s of %s: the firmware has the rest frozen in." % (", ".join(missing), spec))
return [n for n, f in zip(names, found) if f == "frozen" or (f is None and n not in missing)], False


def _skip_stand_in(plan, spec, module):
Expand Down Expand Up @@ -448,7 +486,7 @@ def _walk(plan, spec, version, indexes, owner=None):
url = _rewrite_url(spec, version)
if spec.endswith(".py") or spec.endswith(".mpy"):
name = spec.rsplit("/", 1)[-1]
if not _skip_frozen(plan, spec, [name]):
if not _frozen_part(plan, spec, [name])[1]:
plan.files.append((name, url, None, owner))
return indexes[0]
if not spec.endswith(".json"):
Expand Down Expand Up @@ -478,10 +516,15 @@ def _walk(plan, spec, version, indexes, owner=None):
entry[3] = _beside(plan.target, folder)
if entry is plan.roots[0]:
plan.libs = _libs_for_app(plan.target)
elif _skip_stand_in(plan, spec, body.get("stands_in_for")) or _skip_frozen(
plan, spec, [path for path, _ in hashes] + [path for path, _ in urls]
):
elif _skip_stand_in(plan, spec, body.get("stands_in_for")):
hashes = urls = ()
else:
drop, whole = _frozen_part(plan, spec, [path for path, _ in hashes] + [path for path, _ in urls])
if whole:
hashes = urls = ()
elif drop:
hashes = [h for h in hashes if not _in_modules(h[0], drop)]
urls = [u for u in urls if not _in_modules(u[0], drop)]
for path, short in hashes:
plan.files.append((path, entry[1], short, owner))
for path, url in urls:
Expand Down Expand Up @@ -809,6 +852,9 @@ def install(package, index=None, target=None, version=None, mpy=True, prompt=Tru
and installed. When the target folder comes before `.frozen` on `sys.path`,
the copy would run, so nothing is skipped.

Of a package the firmware has only part of frozen in, just the modules it
lacks are installed, leaving out any a frozen `_omip_left_out` names.

Args:
package (str): A name on an index, or a `github:`, `gitlab:`, `codeberg:`
or `http(s):` URL, as for `mip.install`. A name may end in
Expand Down
18 changes: 18 additions & 0 deletions tests/test_omip.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def build_indexes(scratch):
# micropython is built into every unix MicroPython: beside a frozen module it counts, alone it doesn't.
write_package(ours, "frozen-and-built-in", {"asyncio/__init__.py": "COPY = True\n", "micropython.py": "COPY = True\n"})
write_package(ours, "built-in-only", {"micropython.py": "COPY = True\n"})
# asyncio frozen and left_out not: only left_out lands, unless the firmware left it out on purpose.
write_package(ours, "partly-frozen", {"asyncio/__init__.py": "COPY = True\n", "left_out.py": "COPY = True\n"})
# The stand-in for a module: skipped where the firmware has it, installed where it doesn't.
write_package(ours, "stand-in-built-in", {"micropython.py": "COPY = True\n"}, stands_in_for="micropython")
write_package(ours, "stand-in-frozen", {"asyncio/__init__.py": "COPY = True\n"}, stands_in_for="asyncio")
Expand Down Expand Up @@ -793,6 +795,22 @@ def test_a_module_built_in_beside_frozen_ones_counts_as_frozen_in(self):
self.assertIn("Nothing to install: the firmware has frozen-and-built-in frozen in", out)
self.assertEqual(self.landed(), [])

def test_a_package_partly_frozen_installs_only_what_the_firmware_lacks(self):
out = self.frozen("partly-frozen")
self.assertIn("Installing only left_out of partly-frozen", out)
self.assertIn("RESULT True", out)
self.assertIn("ASYNCIO frozen", out)
self.assertEqual(self.landed(), ["left_out.py"])

def test_a_module_the_firmware_left_out_on_purpose_isnt_installed(self):
out = self.frozen("partly-frozen", before="omip._left_out = lambda: ('left_out',)")
self.assertIn("Skipping partly-frozen: it's frozen into this firmware", out)
self.assertEqual(self.landed(), [])

def test_no_left_out_module_means_nothing_left_out(self):
out = self.run_mp("import omip\nprint('LEFT', omip._left_out())\n")
self.assertIn("LEFT ()", out)

def test_a_package_of_built_in_modules_alone_still_installs(self):
# A package may extend a built-in module, as micropython-lib's time does.
out = self.frozen("built-in-only")
Expand Down