Skip to content

Commit

Permalink
Update code and tests to use 'preview'
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <[email protected]>
  • Loading branch information
Josverl committed Feb 5, 2024
1 parent 912943f commit 4e2a35f
Show file tree
Hide file tree
Showing 28 changed files with 373 additions and 432 deletions.
19 changes: 8 additions & 11 deletions src/stubber/basicgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from loguru import logger as log
from packaging.version import parse

# from stubber.utils.versions import SET_PREVIEW

# Token with no permissions to avoid throttling
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#getting-a-higher-rate-limit
PAT_NO_ACCESS = (
Expand Down Expand Up @@ -68,7 +70,7 @@ def clone(remote_repo: str, path: Path, shallow: bool = False, tag: Optional[str
cmd = ["git", "clone"]
if shallow:
cmd += ["--depth", "1"]
if tag in ("latest", "master"):
if tag in {"preview", "latest", "master"}:
tag = None
cmd += [remote_repo, "--branch", tag, str(path)] if tag else [remote_repo, str(path)]
if result := _run_local_git(cmd, expect_stderr=True, capture_output=False):
Expand Down Expand Up @@ -99,21 +101,16 @@ def get_local_tag(repo: Optional[Union[str, Path]] = None, abbreviate: bool = Tr
return None
tag: str = result.stdout
tag = tag.replace("\r", "").replace("\n", "")
if abbreviate and "-" in tag:
if result := _run_local_git(
["git", "status", "--branch"],
repo=repo.as_posix(),
expect_stderr=True,
):
lines = result.stdout.replace("\r", "").split("\n")
if lines[0].startswith("On branch") and lines[0].endswith("master"):
tag = "latest"
if not abbreviate or "-" not in tag:
return tag
if "-preview" in tag:
tag = tag.split("-preview")[0] + "-preview"
return tag


def get_local_tags(repo: Optional[Path] = None, minver: Optional[str] = None) -> List[str]:
"""
get list of tag of a local repo
get list of all tags of a local repo
"""
if not repo:
repo = Path(".")
Expand Down
10 changes: 6 additions & 4 deletions src/stubber/commands/build_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
multiple=True,
default=[CONFIG.stable_version],
show_default=True,
help="multiple: ",
help="MicroPython version to build, or 'preview' for the latest preview version",
)
@click.option(
"--port",
Expand Down Expand Up @@ -72,9 +72,11 @@ def cli_build(
ports = list(ports)
boards = list(boards)

if len(versions) > 1 :
raise NotImplementedError("Multiple versions are not supported yet\n See https://github.com/Josverl/micropython-stubber/issues/487")

if len(versions) > 1:
raise NotImplementedError(
"Multiple versions are not supported yet\n See https://github.com/Josverl/micropython-stubber/issues/487"
)

# db = get_database(publish_path=CONFIG.publish_path, production=production)
log.info(f"Build {family} {versions} {ports} {boards}")

Expand Down
17 changes: 6 additions & 11 deletions src/stubber/commands/get_docstubs_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
show_default=True,
)
# @click.option("--family", "-f", "basename", default="micropython", help="Micropython family.", show_default=True)
@click.option(
"--version", "--tag", default="", type=str, help="Version number to use. [default: Git tag]"
)
@click.option("--version", "--tag", default="", type=str, help="Version number to use. [default: Git tag]")
@click.option("--black/--no-black", "-b/-nb", default=True, help="Run black", show_default=True)
@click.pass_context
def cli_docstubs(
Expand Down Expand Up @@ -72,16 +70,13 @@ def cli_docstubs(
result = fetch_repos(version, CONFIG.mpy_path, CONFIG.mpy_lib_path)
if not result:
return -1
# get the current checked out version
version = utils.checkedout_version(CONFIG.mpy_path)

v_tag = git.get_local_tag(rst_path.as_posix())
if not v_tag:
# if we can't find a tag , bail
raise ValueError("No valid Tag found")
v_tag = utils.clean_version(v_tag, flat=True, drop_v=False)
release = git.get_local_tag(rst_path.as_posix(), abbreviate=False) or ""

dst_path = Path(target) / f"{basename}-{v_tag}-docstubs"
dst_path = Path(target) / f"{basename}-{version}-docstubs"

log.info(f"Get docstubs for MicroPython {utils.clean_version(v_tag, drop_v=False)}")
generate_from_rst(rst_path, dst_path, v_tag, release=release, suffix=".pyi",black=black)
log.info(f"Get docstubs for MicroPython {utils.clean_version(version, drop_v=False)}")
generate_from_rst(rst_path, dst_path, version, release=release, suffix=".pyi", black=black)
log.info("::group:: Done")
17 changes: 6 additions & 11 deletions src/stubber/commands/get_frozen_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
default="",
# default=[CONFIG.stable_version],
show_default=True,
help="The version of MicroPython to get the frozen modules for. Use 'preview' to get the latest version from the micropython repo",
)
@click.option(
"--stubgen/--no-stubgen",
Expand Down Expand Up @@ -78,27 +79,21 @@ def cli_get_frozen(
else:
version = utils.clean_version(git.get_local_tag(CONFIG.mpy_path.as_posix()) or "0.0")
if not version:
log.warning(
"Unable to find the micropython repo in folder : {}".format(CONFIG.mpy_path.as_posix())
)
log.warning("Unable to find the micropython repo in folder : {}".format(CONFIG.mpy_path.as_posix()))

log.info("MicroPython version : {}".format(version))
# folder/{family}-{version}-frozen
family = "micropython"
stub_path = Path(stub_folder) / f"{family}-{utils.clean_version(version, flat=True)}-frozen"

stub_path = freeze_any(version=version, mpy_path=CONFIG.mpy_path, mpy_lib_path=CONFIG.mpy_lib_path)
stub_paths.append(stub_path)
freeze_any(
stub_path, version=version, mpy_path=CONFIG.mpy_path, mpy_lib_path=CONFIG.mpy_lib_path
)
# Also enrich the frozen modules from the doc stubs if available

# first create .pyi files so they can be enriched
utils.do_post_processing(stub_paths, stubgen=stubgen, black=False, autoflake=False)
family = "micropython"
docstubs_path = (
Path(CONFIG.stub_path)
/ f"{family}-{utils.clean_version(version, drop_v=False, flat=True)}-docstubs"
)
_version = utils.clean_version(version, drop_v=False, flat=True)
docstubs_path = Path(CONFIG.stub_path) / f"{family}-{_version}-docstubs"
if docstubs_path.exists():
log.info(f"Enriching {str(stub_path)} with {docstubs_path}")
if merged := enrich_folder(
Expand Down
10 changes: 6 additions & 4 deletions src/stubber/commands/switch_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import stubber.basicgit as git
from stubber.utils.config import CONFIG
from stubber.utils.repos import fetch_repos, repo_paths
from stubber.utils.versions import SET_PREVIEW, V_PREVIEW

from .cli import stubber_cli

Expand All @@ -22,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") + ["latest"]
VERSION_LIST = git.get_tags("micropython/micropython", minver="v1.9.3") + [V_PREVIEW, "latest"]
except Exception:
# offline fallback
VERSION_LIST = ["v1.91.1", "v1.20.0", "latest"]
VERSION_LIST = ["v1.91.1", "v1.20.1", "v1.21.0", "v1.22.1", "preview"]


@stubber_cli.command(name="switch")
Expand All @@ -46,7 +47,8 @@ def cli_switch(path: Union[str, Path], tag: Optional[str] = None):
mpy_path, mpy_lib_path = repo_paths(Path(path))
except Exception:
return -1
if not tag:
tag = "latest"
if not tag or tag in SET_PREVIEW:
tag = V_PREVIEW

result = fetch_repos(tag, mpy_path, mpy_lib_path)
return -1 if result else 0
3 changes: 2 additions & 1 deletion src/stubber/freeze/freeze_manifest_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def freeze_one_manifest_2(manifest: Path, frozen_stub_path: Path, mpy_path: Path
# so we need to get the port and board from the path
log.info(f"input_manifest: {manifest}")
port, board = get_portboard(manifest)
log.info(f"port-board: '{port}-{board}'")

log.info("port-board: {}".format((port + "-" +board).rstrip("-")))

path_vars = make_path_vars(port=port, board=board, mpy_path=mpy_path, mpy_lib_path=mpy_lib_path)
upy_manifest = ManifestFile(MODE_FREEZE, path_vars)
Expand Down
41 changes: 28 additions & 13 deletions src/stubber/freeze/get_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@

from loguru import logger as log
from packaging.version import Version

import stubber.basicgit as git
from stubber import utils
from stubber.codemod.add_comment import AddComment
from stubber.freeze.freeze_folder import freeze_folders # Micropython < v1.12
from stubber.freeze.freeze_manifest_2 import freeze_one_manifest_2
from stubber.utils.config import CONFIG
from stubber.codemod.add_comment import AddComment

from stubber.utils.versions import SET_PREVIEW, V_PREVIEW

# globals
FAMILY = "micropython"


Expand All @@ -54,13 +55,18 @@ def add_comment_to_path(path: Path, comment: str) -> None:
Add a comment to the top of each file in the path
using a codemod
"""
#TODO: #305 add comment line to each file with the micropython version it was generated from
# TODO: #305 add comment line to each file with the micropython version it was generated from
# frozen_stub_path
# python -m libcst.tool codemod --include-stubs --no-format add_comment.AddComment .\repos\micropython-stubs\stubs\micropython-v1_19_1-frozen\ --comment "# Micropython 1.19.1 frozen stubs"
pass


def freeze_any(stub_folder: Path, version: str, mpy_path: Optional[Path] = None, mpy_lib_path: Optional[Path] = None) -> int:
def freeze_any(
stub_folder: Optional[Path] = None,
version: str = V_PREVIEW,
mpy_path: Optional[Path] = None,
mpy_lib_path: Optional[Path] = None,
) -> Path:
"""
Get and parse the to-be-frozen .py modules for micropython to extract the static type information
- requires that the MicroPython and Micropython-lib repos are checked out and available on a local path
Expand All @@ -73,24 +79,24 @@ def freeze_any(stub_folder: Path, version: str, mpy_path: Optional[Path] = None,
current_dir = os.getcwd()
mpy_path = Path(mpy_path).absolute() if mpy_path else CONFIG.mpy_path.absolute()
mpy_lib_path = Path(mpy_lib_path).absolute() if mpy_lib_path else CONFIG.mpy_path.absolute()
if not stub_folder:
frozen_stub_path = Path("{}/{}_{}_frozen".format(CONFIG.stub_path, FAMILY, utils.clean_version(version, flat=True))).absolute()
else:
frozen_stub_path: Path = Path(stub_folder).absolute()

# if old version of micropython, use the old freeze method
if version not in ["master", "latest"] and Version(version) <= Version("1.11"):
if version not in SET_PREVIEW and Version(version) <= Version("1.11"):
frozen_stub_path = get_fsp(version, stub_folder)
log.debug("MicroPython v1.11, older or other")
# others
modules = freeze_folders(frozen_stub_path.as_posix(), mpy_path.as_posix(), mpy_lib_path.as_posix(), version)
count = len(modules)
else:
# get the current checked out version
version = utils.checkedout_version(CONFIG.mpy_path)

frozen_stub_path = get_fsp(version, stub_folder)
# get the manifests of the different ports and boards
all_manifests = get_manifests(mpy_path)

# process all_manifests under the ports folder and update the frozen files in the stubs folder
# we are goning to jump around, avoid relative paths
frozen_stub_path = frozen_stub_path.absolute()
# we are going to jump around, avoid relative paths
mpy_path = mpy_path.absolute()
mpy_lib_path = mpy_lib_path.absolute()

Expand All @@ -111,4 +117,13 @@ def freeze_any(stub_folder: Path, version: str, mpy_path: Optional[Path] = None,

# restore cwd
os.chdir(current_dir)
return count
return frozen_stub_path


def get_fsp(version: str, stub_folder: Optional[Path] = None) -> Path:
if not stub_folder:
frozen_stub_path = CONFIG.stub_path / f"{FAMILY}-{utils.clean_version(version, flat=True)}-frozen"
frozen_stub_path = frozen_stub_path.absolute()
else:
frozen_stub_path: Path = Path(stub_folder).absolute()
return frozen_stub_path
6 changes: 4 additions & 2 deletions src/stubber/minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import python_minifier
from loguru import logger as log

from stubber.utils.versions import SET_PREVIEW, V_PREVIEW

# Type Aliases for minify
StubSource = Union[Path, str, StringIO, TextIOWrapper]
XCompileDest = Union[Path, BytesIO]
Expand Down Expand Up @@ -360,7 +362,7 @@ def cross_compile(
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")
result = pipx_mpy_cross("latest", source_file, _target)
result = pipx_mpy_cross(V_PREVIEW, source_file, _target)

if result.returncode == 0:
log.debug(f"mpy-cross compiled to : {_target.name}")
Expand All @@ -380,7 +382,7 @@ def pipx_mpy_cross(version: str, source_file, _target):
"""Run mpy-cross using pipx"""

log.info(f"Compiling with mpy-cross version: {version}")
if version == "latest":
if version in SET_PREVIEW:
version = ""
if version:
version = "==" + version
Expand Down
Loading

0 comments on commit 4e2a35f

Please sign in to comment.