Skip to content
Merged
Changes from 4 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
21 changes: 20 additions & 1 deletion pybricksdev/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ async def compile_multi_file(path: str, abi: int):
"""

# compile files using Python to find imports contained within the same directory as path
finder = ModuleFinder([os.path.dirname(path)])
searchpath = [os.path.dirname(path)]
finder = ModuleFinder(searchpath)
finder.run_script(path)

# we expect missing modules, namely builtin MicroPython packages like pybricks.*
Expand All @@ -123,12 +124,30 @@ async def compile_multi_file(path: str, abi: int):
parts: List[bytes] = []

for name, module in finder.modules.items():
if not module.__file__:
continue
mpy = await compile_file(module.__file__, abi)

parts.append(len(mpy).to_bytes(4, "little"))
parts.append(name.encode() + b"\x00")
parts.append(mpy)

# look for .mpy modules
for name in finder.any_missing():
for spath in searchpath:
try:
with open(os.path.join(spath, f"{name}.mpy"), "rb") as f:
mpy = f.read()
if mpy[1] != abi:
raise ValueError(
f"{name} has abi version {mpy[1]} while {abi} is required"
)
parts.append(len(mpy).to_bytes(4, "little"))
parts.append(name.encode() + b"\x00")
parts.append(mpy)
except OSError:
continue

return b"".join(parts)


Expand Down