Skip to content

Commit 4f5cb49

Browse files
committed
initial working version
0 parents  commit 4f5cb49

File tree

7 files changed

+277
-0
lines changed

7 files changed

+277
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text eol=lf

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__/
2+
*.pyc

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Python packaging for multi-target mpy-cross
2+
3+
This repository contains Python packaging to distribute the `mpy-cross` tool
4+
from [MicroPython](https://github.com/micropython/micropython) via PyPI.
5+
6+
This package includes multiple versions of `mpy-cross` to support different
7+
MicroPython runtime versions.
8+
9+
## Installation
10+
11+
To install the latest version of `mpy-cross-multi`:
12+
13+
pip install mpy-cross-multi
14+
15+
## Usage
16+
17+
This package can be used programmatically or as a command line script.
18+
19+
### Script
20+
21+
This can be used just like the original `mpy-cross` tool: by substituting
22+
the name `mpy-cross` with `mpy-cross-multi` in the command line.
23+
24+
mpy-cross-multi --version
25+
26+
It has an optional extra command line option to target older MicroPython
27+
runtimes. The oldest support MicroPython runtime is v1.12.
28+
29+
mpy-cross-multi --micropython 1.22 ...
30+
31+
### API
32+
33+
TODO

mpy_cross_multi/__init__.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from semver.version import Version
2+
3+
4+
def _mp_version_to_mpy_abi_version(mp_ver: Version) -> str:
5+
"""
6+
Convert MicroPython version to mpy-cross ABI version.
7+
8+
See https://docs.micropython.org/en/latest/reference/mpyfiles.html#versioning-and-compatibility-of-mpy-files
9+
10+
Parameters:
11+
mp_ver: MicroPython version
12+
13+
Returns:
14+
str: mpy-cross ABI version
15+
16+
Raises:
17+
TypeError: If the input is not a valid version string
18+
NotImplementedError: If the MicroPython version is not supported
19+
"""
20+
if mp_ver.match(">=1.23.0"):
21+
raise NotImplementedError("MicroPython version must be <1.23.0")
22+
23+
if mp_ver.match(">=1.22.0"):
24+
return "6.2"
25+
26+
if mp_ver.match(">=1.20.0"):
27+
return "6.1"
28+
29+
if mp_ver.match(">=1.19.0"):
30+
return "6"
31+
32+
if mp_ver.match(">=1.12.0"):
33+
return "5"
34+
35+
raise NotImplementedError("MicroPython version must be >=1.12.0")

mpy_cross_multi/__main__.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import sys
2+
import subprocess
3+
import importlib
4+
import itertools
5+
6+
from semver.version import Version
7+
8+
from . import _mp_version_to_mpy_abi_version
9+
10+
11+
def _run():
12+
"""
13+
Run mpy-cross directly with extra arg for getting the right version.
14+
"""
15+
16+
# remove the first argument (the script name)
17+
# if there is --micropython=X.Y, split it into two arguments
18+
args = list(
19+
itertools.chain(
20+
*(
21+
arg.split("=") if arg.startswith("--micropython") else (arg,)
22+
for arg in sys.argv[1:]
23+
)
24+
)
25+
)
26+
27+
try:
28+
idx = args.index("--micropython")
29+
except ValueError:
30+
# default if argument is not given
31+
mp_ver = "1.22"
32+
else:
33+
# argument is given
34+
mp_ver = args.pop(idx + 1)
35+
# also remove the --micropython flag
36+
del args[idx]
37+
38+
# validate the version argument
39+
try:
40+
mp_semver = Version.parse(mp_ver, optional_minor_and_patch=True)
41+
except ValueError:
42+
print(
43+
f"Error: invalid version format for --micropython: '{mp_ver}'",
44+
file=sys.stderr,
45+
)
46+
sys.exit(1)
47+
48+
try:
49+
abi = _mp_version_to_mpy_abi_version(mp_semver)
50+
except NotImplementedError:
51+
print(
52+
f"Error: targeting MicroPython v{mp_semver} is not supported",
53+
file=sys.stderr,
54+
)
55+
sys.exit(1)
56+
57+
# get the right mpy-cross version for the target ABI
58+
mpy_cross = importlib.import_module(f"mpy_cross_v{abi.replace('.', '_')}")
59+
60+
# run mpy-cross with the remaining arguments
61+
proc = subprocess.run([mpy_cross.MPY_CROSS_PATH] + args)
62+
sys.exit(proc.returncode)
63+
64+
65+
if __name__ == "__main__":
66+
_run()

poetry.lock

Lines changed: 115 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[tool.poetry]
2+
name = "mpy-cross-multi"
3+
version = "1.0.0"
4+
description = "MicroPython cross-compiler targeting multiple runtime versions."
5+
authors = ["David Lechner <[email protected]>"]
6+
license = "MIT"
7+
readme = "README.md"
8+
9+
[tool.poetry.scripts]
10+
"mpy-cross-multi" = "mpy_cross_multi.__main__:_run"
11+
12+
[tool.poetry.dependencies]
13+
python = "^3.10"
14+
mpy-cross-v5 = "^1.0.2"
15+
mpy-cross-v6 = "^1.0.2"
16+
"mpy-cross-v6.1" = "^1.0.1"
17+
"mpy-cross-v6.2" = "^1.0.0"
18+
semver = "^3.0.2"
19+
20+
[tool.poetry.group.dev.dependencies]
21+
ruff = "^0.4.3"
22+
23+
[build-system]
24+
requires = ["poetry-core"]
25+
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)