Skip to content

Commit 5f8ad7a

Browse files
committed
stubber: Simplify config to use repo-path = "./repos"
1 parent 4834f72 commit 5f8ad7a

File tree

7 files changed

+83
-65
lines changed

7 files changed

+83
-65
lines changed

src/stubber/commands/clone_cmd.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ def cli_clone(path: Union[str, Path], stubs: bool = False):
3131
dest_path = Path(path)
3232
if not dest_path.exists():
3333
os.mkdir(dest_path)
34-
# repos are relative to provided path
35-
if dest_path != CONFIG.repo_path:
34+
if dest_path == CONFIG.repo_path:
35+
# same as default
36+
mpy_path = CONFIG.mpy_path
37+
mpy_lib_path = CONFIG.mpy_lib_path
38+
mpy_stubs_path = CONFIG.mpy_stubs_path
39+
else:
40+
# repos are relative to provided path
3641
mpy_path = dest_path / "micropython"
3742
mpy_lib_path = dest_path / "micropython-lib"
3843
mpy_stubs_path = dest_path / "micropython-stubs"
39-
else:
40-
mpy_path = CONFIG.mpy_path
41-
mpy_lib_path = CONFIG.mpy_lib_path
42-
43-
mpy_stubs_path = CONFIG.stub_path.parent
4444

4545
repos: List[Tuple[Path, str, str]] = [
4646
(mpy_path, "https://github.com/micropython/micropython.git", "master"),

src/stubber/commands/config_cmd.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Show the current configuration"""
2+
23
# pragma: no cover
34

45
from loguru import logger as log
6+
57
from stubber.utils.config import CONFIG
68

79
from .cli import stubber_cli
@@ -20,6 +22,7 @@ def cli_config():
2022
log.info(f"CONFIG.repo_path {CONFIG.repo_path}")
2123
log.info(f"CONFIG.mpy_path {CONFIG.mpy_path}")
2224
log.info(f"CONFIG.mpy_lib_path {CONFIG.mpy_lib_path}")
25+
log.info(f"CONFIG.mpy_stubs_path {CONFIG.mpy_stubs_path}")
2326

2427
log.info(f"CONFIG.stub_path {CONFIG.stub_path}")
2528
log.info(f"CONFIG.publish_path {CONFIG.publish_path}")

src/stubber/utils/config.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
@section("micropython-stubber")
1616
class StubberConfig(Config):
1717
"stubber configuration class"
18-
stub_path = key(key_name="stub-path", cast=Path, required=False, default=Path("./stubs"))
19-
"a Path to the stubs directory"
18+
# legacy
19+
# stub_path = key(key_name="stub-path", cast=Path, required=False, default=Path("./stubs"))
20+
# "a Path to the stubs directory"
21+
2022
# relative to stubs folder
2123
fallback_path = key(key_name="fallback-path", cast=Path, required=False, default=Path("typings/fallback"))
2224
"a Path to the fallback stubs directory"
@@ -32,24 +34,8 @@ class StubberConfig(Config):
3234
mpy_lib_path = key(key_name="mpy-lib-path", cast=Path, required=False, default=Path("micropython-lib"))
3335
"a Path to the micropython-lib folder in the repos directory"
3436

35-
# mpy_stubs_repo_path = key(key_name="mpy-stubs-repo-path", cast=Path, required=False, default=Path("./micropython-stubs"))
36-
# "a Path to the micropython-stubs folder in the repos directory"
37-
38-
publish_path = key(
39-
key_name="publish-path",
40-
cast=Path,
41-
required=False,
42-
default=Path("./repos/micropython-stubs/publish"),
43-
)
44-
"A Path to the folder where all stub publication artifacts are stored"
45-
46-
template_path = key(
47-
key_name="template-path",
48-
cast=Path,
49-
required=False,
50-
default=Path("./repos/micropython-stubs/publish/template"),
51-
)
52-
"a Path to the publication folder that has the template files"
37+
mpy_stubs_path = key(key_name="mpy-stubs-path", cast=Path, required=False, default=Path("micropython-stubs"))
38+
"a Path to the micropython-stubs folder in the repos directory (or current directory)"
5339

5440
stable_version = key(key_name="stable-version", cast=str, required=False, default="1.20.0")
5541

@@ -66,25 +52,33 @@ class StubberConfig(Config):
6652
BLOCKED_PORTS = ["minimal", "bare-arm"]
6753
"ports that should be ignored as a source of stubs"
6854

69-
# def __init__(self):
70-
# super().__init__()
71-
# self.update_versions()
55+
@property
56+
def stub_path(self) -> Path:
57+
"return the stubs path in the microypthon-stubs repo"
58+
return self.mpy_stubs_path / "stubs"
59+
60+
@property
61+
def publish_path(self) -> Path:
62+
"return the stubs path in the microypthon-stubs repo"
63+
return self.mpy_stubs_path / "publish"
7264

73-
# def update_versions(self):
74-
# try:
75-
# self.ALL_VERSIONS = git.get_tags(self.mpy_path, minver="v1.17")
76-
# except Exception:
77-
# self.ALL_VERSIONS = ["1.17", "1.18", "1.19", "1.19.1"]
78-
# self.STABLE_VERSION = self.ALL_VERSIONS[-1]
65+
@property
66+
def template_path(self) -> Path:
67+
"return the stubs path in the microypthon-stubs repo"
68+
return self.mpy_stubs_path / "publish" / "template"
7969

8070
def post_read_hook(self) -> dict:
8171
config_updates = {}
8272
# relative to stubs
83-
config_updates.update(fallback_path=self.stub_path / self.fallback_path)
73+
# config_updates.update(fallback_path=self.stub_path / self.fallback_path)
8474

8575
# relative to repo path
8676
config_updates.update(mpy_path=self.repo_path / self.mpy_path)
8777
config_updates.update(mpy_lib_path=self.repo_path / self.mpy_lib_path)
78+
if self.mpy_stubs_path.is_absolute() or self.mpy_stubs_path == Path("."):
79+
config_updates.update(mpy_stubs_path=self.mpy_stubs_path)
80+
else:
81+
config_updates.update(mpy_stubs_path=self.repo_path / self.mpy_stubs_path)
8882
# read the versions from the git tags
8983
all_versions = []
9084
try:

src/stubber/utils/repos.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ def fetch_repos(tag: str, mpy_path: Path, mpy_lib_path: Path):
107107
git.fetch(mpy_path)
108108
git.fetch(mpy_lib_path)
109109
try:
110-
git.fetch(CONFIG.stub_path.parent)
110+
git.fetch(CONFIG.mpy_stubs_path)
111111
except Exception:
112-
log.trace("no stubs repo found : {CONFIG.stub_path.parent}")
112+
log.trace("no stubs repo found : {CONFIG.mpy_stubs_path}")
113113

114114
if not tag:
115115
tag = V_PREVIEW

tests/common/config_test.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import os
12
from pathlib import Path
23

34
import pytest
5+
46
from stubber.utils.config import StubberConfig, TomlConfigSource, readconfig
57

68
pytestmark = [pytest.mark.stubber]
79

10+
811
def test_toplevel_config():
912
# exists on top-level
1013
from stubber.utils.config import CONFIG
@@ -47,8 +50,44 @@ def test_config():
4750
assert isinstance(config.mpy_path, Path)
4851
assert isinstance(config.mpy_lib_path, Path)
4952

50-
assert config.stub_path == Path("./my-stubs")
5153
assert config.repo_path == Path("./my-repos")
5254
assert config.mpy_path == Path("./my-repos/micropython")
55+
assert config.mpy_lib_path == Path("./my-repos/micropython-lib")
56+
assert config.mpy_stubs_path == Path("./my-repos/micropython-stubs")
5357
# TODO Make sure this is relative to the stubs repo
58+
assert config.stub_path == Path("./my-repos/micropython-stubs/stubs")
59+
assert config.publish_path == Path("./my-repos/micropython-stubs/publish")
60+
61+
62+
def test_config_stubs_repo():
63+
# test config used in the stubs repo
64+
config = readconfig(filename="tests/data/stubs_repo.toml", prefix="tool.", must_exist=True)
65+
assert config.repo_path == Path("./repos")
66+
assert config.mpy_path == Path("./repos/micropython")
67+
assert config.mpy_lib_path == Path("./repos/micropython-lib")
68+
69+
assert config.mpy_stubs_path == Path(".")
70+
assert config.stub_path == Path("./stubs")
71+
assert config.publish_path == Path("./publish")
72+
73+
74+
@pytest.fixture
75+
def change_test_dir(request):
76+
"""Change the working directory to the test directory, and back after the test."""
77+
os.chdir(request.fspath.dirname)
78+
try:
79+
yield
80+
finally:
81+
os.chdir(request.config.invocation_params.dir)
82+
83+
84+
def test_config_no_config(change_test_dir, tmp_path: Path):
85+
# there will be no config file in the test directory
86+
os.chdir(tmp_path)
87+
config = readconfig()
88+
assert config.repo_path == Path("./repos")
89+
assert config.mpy_path == Path("./repos/micropython")
90+
assert config.mpy_lib_path == Path("./repos/micropython-lib")
91+
assert config.mpy_stubs_path == Path("./repos/micropython-stubs")
92+
5493
assert config.publish_path == Path("./repos/micropython-stubs/publish")

tests/data/stubs_repo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# stripped pyproject toml file used for testing
2+
# same config used in the stubber repo
3+
[tool.micropython-stubber]
4+
"mpy-stubs-path" = "."
5+
6+
7+

tests/data/test.toml

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
11
# stripped pyproject toml file used for testing
22

33
[tool.micropython-stubber]
4-
stub-path = "./my-stubs"
54
repo-path = "./my-repos"
65

7-
[tool.poetry.scripts]
8-
stubber = "stubber.stubber:stubber_cli"
9-
pyboard = "stubber.tools.pyboard:main"
106

11-
[build-system]
12-
requires = ["poetry-core>=1.0.0"]
13-
build-backend = "poetry.core.masonry.api"
14-
15-
[tool.black]
16-
# use long lines to avoid _log lines from wrapping , as this causes issues with the minification.
17-
line-length = 140
18-
target_version = ['py38']
19-
include = '\.pyi?$'
20-
exclude = '''
21-
(
22-
/(
23-
| \..*
24-
| minified
25-
| micropython
26-
| micropython-lib
27-
| tests[\\/]mocks
28-
| scratch
29-
)/
30-
)
31-
'''

0 commit comments

Comments
 (0)