diff --git a/src/mpflash/mpflash/vendor/versions.py b/src/mpflash/mpflash/vendor/versions.py index 97dd527e..00e96691 100644 --- a/src/mpflash/mpflash/vendor/versions.py +++ b/src/mpflash/mpflash/vendor/versions.py @@ -10,6 +10,8 @@ from packaging.version import parse from mpflash.common import GH_CLIENT +OLDEST_VERSION = "1.16" +"This is the oldest MicroPython version to build the stubs on" V_PREVIEW = "preview" "Latest preview version" @@ -74,7 +76,9 @@ def micropython_versions(minver: str = "v1.20", reverse: bool = False): try: gh_client = GH_CLIENT repo = gh_client.get_repo("micropython/micropython") - versions = [tag.name for tag in repo.get_tags()] + versions = [tag.name for tag in repo.get_tags() if parse(tag.name) >= parse(minver)] + # Only keep the last preview + versions = [v for v in versions if not v.endswith(V_PREVIEW) or v == versions[-1]] except Exception: versions = [ "v9.99.9-preview", @@ -99,19 +103,17 @@ def micropython_versions(minver: str = "v1.20", reverse: bool = False): versions = [v for v in versions if parse(v) >= parse(minver)] # remove all but the most recent (preview) version versions = versions[:1] + [v for v in versions if "preview" not in v] - return sorted(versions) + return sorted(versions, reverse=reverse) def get_stable_mp_version() -> str: # read the versions from the git tags - all_versions = micropython_versions(minver="v1.17") + all_versions = micropython_versions(minver=OLDEST_VERSION) return [v for v in all_versions if not v.endswith(V_PREVIEW)][-1] def get_preview_mp_version() -> str: # read the versions from the git tags - all_versions = micropython_versions(minver="v1.17") + all_versions = micropython_versions(minver=OLDEST_VERSION) return [v for v in all_versions if v.endswith(V_PREVIEW)][-1] - -############################################################# diff --git a/src/stubber/basicgit.py b/src/stubber/basicgit.py index 93fa81b6..9e9cf3e3 100644 --- a/src/stubber/basicgit.py +++ b/src/stubber/basicgit.py @@ -14,6 +14,9 @@ from loguru import logger as log from packaging.version import parse +# from stubber.utils.versions import get_stable_mp_version +from mpflash.vendor.versions import get_stable_mp_version + # from stubber.utils.versions import SET_PREVIEW # Token with no permissions to avoid throttling @@ -71,6 +74,9 @@ def clone(remote_repo: str, path: Path, shallow: bool = False, tag: Optional[str cmd = ["git", "clone"] if shallow: cmd += ["--depth", "1"] + if tag in {"stable"}: + tag = get_stable_mp_version() + if tag in {"preview", "latest", "master"}: tag = None cmd += [remote_repo, "--branch", tag, str(path)] if tag else [remote_repo, str(path)] diff --git a/src/stubber/commands/switch_cmd.py b/src/stubber/commands/switch_cmd.py index 0f8e3199..4507ecc2 100644 --- a/src/stubber/commands/switch_cmd.py +++ b/src/stubber/commands/switch_cmd.py @@ -23,10 +23,10 @@ # get version list from Git tags in the repo that is provided on the command line try: - VERSION_LIST = git.get_tags("micropython/micropython", minver="v1.9.3") + [V_PREVIEW, "latest"] + VERSION_LIST = git.get_tags("micropython/micropython", minver="v1.9.3") + [V_PREVIEW, "latest", "stable"] except Exception: # offline fallback - VERSION_LIST = ["v1.91.1", "v1.20.1", "v1.21.0", "v1.22.1", "preview"] + VERSION_LIST = ["v1.91.1", "v1.20.1", "v1.21.0", "v1.22.1", "preview", "stable"] @stubber_cli.command(name="switch") diff --git a/src/stubber/minify.py b/src/stubber/minify.py index 82bb66e5..522fe700 100644 --- a/src/stubber/minify.py +++ b/src/stubber/minify.py @@ -369,7 +369,7 @@ def cross_compile( _target = get_temp_file(suffix=".mpy") result = pipx_mpy_cross(version, source_file, _target) if result.stderr and "No matching distribution found for mpy-cross==" in result.stderr: - log.warning(f"mpy-cross=={version} not found, using latest") + log.warning(f"mpy-cross=={version} not found, using default version.") result = pipx_mpy_cross(V_PREVIEW, source_file, _target) if result.returncode == 0: diff --git a/src/stubber/utils/repos.py b/src/stubber/utils/repos.py index 5132c3fb..b00a50a0 100644 --- a/src/stubber/utils/repos.py +++ b/src/stubber/utils/repos.py @@ -12,6 +12,7 @@ from packaging.version import Version import stubber.basicgit as git +from mpflash.vendor.versions import get_stable_mp_version from stubber.utils.config import CONFIG from stubber.utils.versions import SET_PREVIEW, V_PREVIEW @@ -118,7 +119,9 @@ def fetch_repos(tag: str, mpy_path: Path, mpy_lib_path: Path): if tag == V_PREVIEW: git.switch_branch(repo=mpy_path, branch="master") else: - git.checkout_tag(repo=mpy_path, tag=tag) + if tag == "stable": + tag = get_stable_mp_version() + git.switch_tag(tag, repo=mpy_path) result = match_lib_with_mpy(version_tag=tag, mpy_path=mpy_path, lib_path=mpy_lib_path) log.info(f"{str(mpy_path):<40} {git.get_local_tag(mpy_path)}") diff --git a/src/stubber/utils/versions.py b/src/stubber/utils/versions.py index 130cc2e6..b2aab493 100644 --- a/src/stubber/utils/versions.py +++ b/src/stubber/utils/versions.py @@ -1,5 +1,6 @@ """Handle versions of micropython based on the git tags in the repo """ +from functools import lru_cache from pathlib import Path from github import Github @@ -13,7 +14,7 @@ "This is the oldest MicroPython version to build the stubs on" V_PREVIEW = "preview" -"Latest Preview version" +"Latest preview version" SET_PREVIEW = {"preview", "latest", "master"} @@ -32,12 +33,12 @@ def clean_version( if version in {"", "-"}: return version if version.lower() == "stable": - _v = get_stable_version() + _v = get_stable_mp_version() if not _v: log.warning("Could not determine the latest stable version") return "stable" version = _v - log.info(f"Using latest stable version: {version}") + log.trace(f"Using latest stable version: {version}") is_preview = "-preview" in version nibbles = version.split("-") ver_ = nibbles[0].lower().lstrip("v") @@ -69,6 +70,7 @@ def clean_version( return version +@lru_cache(maxsize=10) def checkedout_version(path: Path, flat: bool = False) -> str: """Get the checked-out version of the repo""" version = git.get_local_tag(path.as_posix()) @@ -78,11 +80,11 @@ def checkedout_version(path: Path, flat: bool = False) -> str: return version -def micropython_versions(minver: str = "v1.9.2"): +def micropython_versions(minver: str = "v1.20", reverse: bool = False): """Get the list of micropython versions from github tags""" try: - g = Github() - repo = g.get_repo("micropython/micropython") + gh_client = Github() + repo = gh_client.get_repo("micropython/micropython") versions = [tag.name for tag in repo.get_tags() if parse(tag.name) >= parse(minver)] # Only keep the last preview versions = [v for v in versions if not v.endswith(V_PREVIEW) or v == versions[-1]] @@ -106,15 +108,21 @@ def micropython_versions(minver: str = "v1.9.2"): "v1.12", "v1.11", "v1.10", - "v1.9.4", - "v1.9.3", ] - versions = [v for v in versions if parse(v) >= parse(minver)] - return sorted(versions) + versions = [v for v in versions if parse(v) >= parse(minver)] + # remove all but the most recent (preview) version + versions = versions[:1] + [v for v in versions if "preview" not in v] + return sorted(versions, reverse=reverse) -def get_stable_version() -> str: +def get_stable_mp_version() -> str: # read the versions from the git tags - all_versions = micropython_versions(minver="v1.17") - stable_version = [v for v in all_versions if not v.endswith(V_PREVIEW)][-1] - return stable_version + all_versions = micropython_versions(minver=OLDEST_VERSION) + return [v for v in all_versions if not v.endswith(V_PREVIEW)][-1] + + +def get_preview_mp_version() -> str: + # read the versions from the git tags + all_versions = micropython_versions(minver=OLDEST_VERSION) + return [v for v in all_versions if v.endswith(V_PREVIEW)][-1] + diff --git a/src/stubber/variants.py b/src/stubber/variants.py index bb50cf8b..36dbdc93 100644 --- a/src/stubber/variants.py +++ b/src/stubber/variants.py @@ -39,7 +39,7 @@ def create_variants( target_path : Path, optional Path to write the variants to, by default None version : str, optional - Version of mpy-cross to use, by default uses the latest version + Version of mpy-cross to use, by default uses the latest published version """ if target_path is None: