Skip to content

Commit

Permalink
stubber: Allow checkout of stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Josverl committed Jun 19, 2024
1 parent 78020b1 commit 4059af5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 deletions.
14 changes: 8 additions & 6 deletions src/mpflash/mpflash/vendor/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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]


#############################################################
6 changes: 6 additions & 0 deletions src/stubber/basicgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions src/stubber/commands/switch_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/stubber/utils/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)}")
Expand Down
36 changes: 22 additions & 14 deletions src/stubber/utils/versions.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"}

Expand All @@ -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")
Expand Down Expand Up @@ -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())
Expand All @@ -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]]
Expand All @@ -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]

2 changes: 1 addition & 1 deletion src/stubber/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4059af5

Please sign in to comment.