From 8bf44b72afe39f178d5cbf1ee74ac07cbd26e623 Mon Sep 17 00:00:00 2001 From: Pepijn de Vos Date: Sat, 25 Feb 2023 18:14:59 +0100 Subject: [PATCH 1/7] look for already compiled .mpy modules --- pybricksdev/compile.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pybricksdev/compile.py b/pybricksdev/compile.py index 674cd44..03f1c25 100644 --- a/pybricksdev/compile.py +++ b/pybricksdev/compile.py @@ -4,6 +4,7 @@ import asyncio import logging import os +import sys from modulefinder import ModuleFinder from typing import List, Optional @@ -123,12 +124,26 @@ async def compile_multi_file(path: str, abi: int): parts: List[bytes] = [] for name, module in finder.modules.items(): + if not module.__file__: + continue # system module 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 path in sys.path: + try: + with open(os.path.join(path, f"{name}.mpy"), "rb") as f: + mpy = f.read() + parts.append(len(mpy).to_bytes(4, "little")) + parts.append(name.encode() + b"\x00") + parts.append(mpy) + except IOError: + continue + return b"".join(parts) From 87a9c3417fbe80d58715b25c2ee7a465bc3bf9ca Mon Sep 17 00:00:00 2001 From: Pepijn de Vos Date: Sat, 25 Feb 2023 21:01:45 +0100 Subject: [PATCH 2/7] Update pybricksdev/compile.py Co-authored-by: David Lechner --- pybricksdev/compile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pybricksdev/compile.py b/pybricksdev/compile.py index 03f1c25..463e3b1 100644 --- a/pybricksdev/compile.py +++ b/pybricksdev/compile.py @@ -141,7 +141,7 @@ async def compile_multi_file(path: str, abi: int): parts.append(len(mpy).to_bytes(4, "little")) parts.append(name.encode() + b"\x00") parts.append(mpy) - except IOError: + except OSError: continue return b"".join(parts) From 10cacebff4c7b19e0111a341a0de3be582bc50b1 Mon Sep 17 00:00:00 2001 From: Pepijn de Vos Date: Sat, 25 Feb 2023 21:07:54 +0100 Subject: [PATCH 3/7] don't look in sys.path --- pybricksdev/compile.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pybricksdev/compile.py b/pybricksdev/compile.py index 463e3b1..3f358cc 100644 --- a/pybricksdev/compile.py +++ b/pybricksdev/compile.py @@ -4,7 +4,6 @@ import asyncio import logging import os -import sys from modulefinder import ModuleFinder from typing import List, Optional @@ -114,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.* @@ -125,7 +125,7 @@ async def compile_multi_file(path: str, abi: int): for name, module in finder.modules.items(): if not module.__file__: - continue # system module + continue mpy = await compile_file(module.__file__, abi) parts.append(len(mpy).to_bytes(4, "little")) @@ -134,9 +134,9 @@ async def compile_multi_file(path: str, abi: int): # look for .mpy modules for name in finder.any_missing(): - for path in sys.path: + for spath in searchpath: try: - with open(os.path.join(path, f"{name}.mpy"), "rb") as f: + with open(os.path.join(spath, f"{name}.mpy"), "rb") as f: mpy = f.read() parts.append(len(mpy).to_bytes(4, "little")) parts.append(name.encode() + b"\x00") From 2b76f6d8dbc85fff611f368ae54b6e7da227f082 Mon Sep 17 00:00:00 2001 From: Pepijn de Vos Date: Sat, 25 Feb 2023 21:11:58 +0100 Subject: [PATCH 4/7] add abi version check --- pybricksdev/compile.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pybricksdev/compile.py b/pybricksdev/compile.py index 3f358cc..ba782b9 100644 --- a/pybricksdev/compile.py +++ b/pybricksdev/compile.py @@ -138,6 +138,10 @@ async def compile_multi_file(path: str, abi: int): 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) From a98b391bcc97e456fba3a4f88c55a633a9deaeda Mon Sep 17 00:00:00 2001 From: Pepijn de Vos Date: Sat, 25 Feb 2023 21:14:27 +0100 Subject: [PATCH 5/7] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9761f51..591a978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +- Add support for including precompiled .mpy libraries ## [1.0.0-alpha.36] - 2023-02-18 From 75dec4aa78c54e63eef973fb33faf219e15848e8 Mon Sep 17 00:00:00 2001 From: Pepijn de Vos Date: Sat, 25 Feb 2023 21:15:02 +0100 Subject: [PATCH 6/7] Update pybricksdev/compile.py Co-authored-by: David Lechner --- pybricksdev/compile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pybricksdev/compile.py b/pybricksdev/compile.py index ba782b9..4269482 100644 --- a/pybricksdev/compile.py +++ b/pybricksdev/compile.py @@ -140,7 +140,7 @@ async def compile_multi_file(path: str, abi: int): mpy = f.read() if mpy[1] != abi: raise ValueError( - f"{name} has abi version {mpy[1]} while {abi} is required" + f"{name}.mpy has abi version {mpy[1]} while {abi} is required" ) parts.append(len(mpy).to_bytes(4, "little")) parts.append(name.encode() + b"\x00") From 5e6c05c891b95d010d6a1755620ff82065b56b55 Mon Sep 17 00:00:00 2001 From: Pepijn de Vos Date: Sat, 25 Feb 2023 21:16:05 +0100 Subject: [PATCH 7/7] rename search_path --- pybricksdev/compile.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pybricksdev/compile.py b/pybricksdev/compile.py index 4269482..3ea0ccf 100644 --- a/pybricksdev/compile.py +++ b/pybricksdev/compile.py @@ -113,8 +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 - searchpath = [os.path.dirname(path)] - finder = ModuleFinder(searchpath) + search_path = [os.path.dirname(path)] + finder = ModuleFinder(search_path) finder.run_script(path) # we expect missing modules, namely builtin MicroPython packages like pybricks.* @@ -134,7 +134,7 @@ async def compile_multi_file(path: str, abi: int): # look for .mpy modules for name in finder.any_missing(): - for spath in searchpath: + for spath in search_path: try: with open(os.path.join(spath, f"{name}.mpy"), "rb") as f: mpy = f.read()