Skip to content

Commit 64cdede

Browse files
authored
Add dump state to check core command. Resolve platformio#42 (platformio#43)
* Add dump state to check core command. Resolve platformio#42 * Comment test due to double venv error.
1 parent 6da73cd commit 64cdede

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

get-platformio.py

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

pioinstaller/__main__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,26 @@ def python():
9191
@check.command("core")
9292
@click.option("--auto-upgrade/--no-auto-upgrade", is_flag=True, default=True)
9393
@click.option("--version-requirements", default=None)
94+
@click.option(
95+
"--dump-state-path",
96+
type=click.Path(
97+
exists=False, file_okay=True, dir_okay=True, writable=True, resolve_path=True
98+
),
99+
)
94100
@click.pass_context
95-
def core_check(ctx, auto_upgrade, version_requirements):
101+
def core_check(ctx, auto_upgrade, version_requirements, dump_state_path):
96102
try:
97-
path, version = core.check(
103+
state = core.check(
98104
dev=ctx.obj.get("dev", False),
99105
auto_upgrade=auto_upgrade,
100106
version_requirements=version_requirements,
101107
)
108+
if dump_state_path:
109+
core.dump_state(target=dump_state_path, state=state)
102110
click.secho(
103-
"Found compatible PlatformIO Core %s -> %s" % (version, path), fg="green",
111+
"Found compatible PlatformIO Core %s -> %s"
112+
% (state.get("core_version"), state.get("platformio_exe")),
113+
fg="green",
104114
)
105115
except (exception.InvalidPlatformIOCore) as e:
106116
raise click.ClickException(

pioinstaller/core.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import io
1516
import json
1617
import logging
1718
import os
@@ -23,7 +24,7 @@
2324
import click
2425
import semantic_version
2526

26-
from pioinstaller import exception, home, util
27+
from pioinstaller import __version__, exception, home, util
2728

2829
log = logging.getLogger(__name__)
2930

@@ -178,7 +179,21 @@ def check(dev=False, auto_upgrade=False, version_requirements=None):
178179
"Could not run `%s --version`.\nError: %s" % (platformio, str(error))
179180
)
180181

181-
result = {"platformio_exe": platformio_exe, "version": str(pio_version)}
182+
dev = dev or bool(pio_version.prerelease)
183+
result = {
184+
"core_dir": get_core_dir(),
185+
"cache_dir": get_cache_dir(),
186+
"penv_dir": penv.get_penv_dir(),
187+
"penv_bin_dir": penv.get_penv_bin_dir(),
188+
"platformio_exe": platformio_exe,
189+
"core_version": str(pio_version),
190+
"installer_version": __version__,
191+
"python_exe": os.path.join(
192+
penv.get_penv_bin_dir(), "python.exe" if util.IS_WINDOWS else "python"
193+
),
194+
"system": util.get_systype(),
195+
"is_develop_core": dev,
196+
}
182197

183198
if not auto_upgrade:
184199
return result
@@ -200,10 +215,10 @@ def check(dev=False, auto_upgrade=False, version_requirements=None):
200215
if not last_piocore_version_check:
201216
return result
202217

203-
dev = dev or pio_version.prerelease
204218
upgrade_core(platformio_exe, dev)
205219

206-
result["pio_version"] = get_pio_version(platformio)
220+
result["core_version"] = get_pio_version(platformio)
221+
207222
return result
208223

209224

@@ -234,3 +249,15 @@ def upgrade_core(platformio_exe, dev=False):
234249
raise exception.PIOInstallerException(
235250
"Could not upgrade PlatformIO Core: %s" % str(e)
236251
)
252+
253+
254+
def dump_state(target, state):
255+
assert isinstance(target, str)
256+
257+
if os.path.isdir(target):
258+
target = os.path.join(target, "get-platformio-core-state.json")
259+
if not os.path.isdir(os.path.dirname(target)):
260+
os.makedirs(os.path.dirname(target))
261+
262+
with io.open(target, "w", encoding="utf-8") as fp:
263+
json.dump(state, fp)

tests/test_core.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import json
1516
import os
1617
import subprocess
1718

18-
from pioinstaller import core, penv, util
19+
from pioinstaller import __version__, core, penv, util
1920

2021

2122
def test_install_pio_core(pio_installer_script, tmpdir, monkeypatch):
@@ -31,6 +32,27 @@ def test_install_pio_core(pio_installer_script, tmpdir, monkeypatch):
3132
penv.get_penv_bin_dir(penv_dir), "python.exe" if util.IS_WINDOWS else "python"
3233
)
3334
assert subprocess.check_call([python_exe, "-m", "platformio", "--version"]) == 0
35+
36+
# core_state_path = os.path.join(str(core_dir), "core-state.json")
37+
# assert (
38+
# subprocess.check_call(
39+
# [
40+
# python_exe,
41+
# pio_installer_script,
42+
# "check",
43+
# "core",
44+
# "--dump-state-path=%s" % core_state_path,
45+
# ]
46+
# )
47+
# == 0
48+
# )
49+
# with open(core_state_path) as fp:
50+
# json_info = json.load(fp)
51+
# assert json_info.get("core_dir") == str(core_dir)
52+
# assert json_info.get("penv_dir") == penv_dir
53+
# assert json_info.get("installer_version") == __version__
54+
# assert json_info.get("system") == util.get_systype()
55+
3456
# assert os.path.isfile(
3557
# os.path.join(
3658
# penv.get_penv_bin_dir(penv_dir),

0 commit comments

Comments
 (0)