Skip to content

Commit 95f86e5

Browse files
authored
refactor and remove hard coded "package" path
1 parent 2f98b5d commit 95f86e5

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

platform.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@
1414

1515
import os
1616
import subprocess
17-
import urllib
1817
import sys
19-
import json
20-
import re
21-
import requests
2218
import shutil
23-
from os.path import isfile, isdir, join
19+
from os.path import isfile, join
2420

2521
from platformio.public import PlatformBase, to_unix_path
2622
from platformio.proc import get_pythonexe_path
23+
from platformio.package.manager.tool import ToolPackageManager
2724

2825
python_exe = get_pythonexe_path()
26+
pm = ToolPackageManager()
2927
IS_WINDOWS = sys.platform.startswith("win")
3028
IDF_TOOLS_PATH_DEFAULT = os.path.join(os.path.expanduser("~"), ".espressif")
31-
IDF_TOOLS = os.path.join(os.path.expanduser("~"), ".platformio", "packages", "tl-install", "tools", "idf_tools.py")
29+
try:
30+
tl_path = pm.get_package("tl-install").path
31+
IDF_TOOLS = os.path.join(tl_path, "tools", "idf_tools.py")
32+
except:
33+
IDF_TOOLS = ""
3234
IDF_TOOLS_FLAG = ["install"]
3335
IDF_TOOLS_CMD = [python_exe, IDF_TOOLS] + IDF_TOOLS_FLAG
3436

@@ -41,16 +43,20 @@ def configure_default_packages(self, variables, targets):
4143
board_config = self.board_config(variables.get("board"))
4244
mcu = variables.get("board_build.mcu", board_config.get("build.mcu", "esp32"))
4345
frameworks = variables.get("pioframework", [])
46+
try:
47+
tl_flag = bool(pm.get_package("tl-install").path)
48+
except:
49+
tl_flag = False
4450

4551
# IDF Install is needed only one time
46-
if not os.path.exists(join(IDF_TOOLS_PATH_DEFAULT, "tools")) and os.path.exists(IDF_TOOLS):
52+
if not os.path.exists(join(IDF_TOOLS_PATH_DEFAULT, "tools")) and tl_flag:
4753
rc = subprocess.call(IDF_TOOLS_CMD)
4854
if rc != 0:
4955
sys.stderr.write("Error: Couldn't execute 'idf_tools.py install' \n")
5056
else:
5157
shutil.copytree(join(IDF_TOOLS_PATH_DEFAULT, "tools", "tool-packages"), join(IDF_TOOLS_PATH_DEFAULT, "tools"), symlinks=False, ignore=None, ignore_dangling_symlinks=False, dirs_exist_ok=True)
5258

53-
if os.path.exists(IDF_TOOLS):
59+
if tl_flag:
5460
# Install all tools and toolchains
5561
self.packages["tl-install"]["optional"] = True
5662
for p in self.packages:
@@ -62,6 +68,17 @@ def configure_default_packages(self, variables, targets):
6268
for p in self.packages:
6369
if p in ("tool-cmake", "tool-ninja", "tc-ulp"):
6470
self.packages[p]["optional"] = False if "espidf" in frameworks else True
71+
# Enabling of following tools is not needed, installing is enough
72+
for p in self.packages:
73+
if p in ("contrib-pioremote", "contrib-piohome", "tool-scons"):
74+
try:
75+
pkg_dir = pm.get_package(p).path
76+
# When package is not found an execption happens -> install is forced
77+
# else the are removed from current env
78+
self.packages[p]["optional"] = True
79+
except:
80+
pass
81+
6582

6683
# Enable debug tool gdb only when build debug is enabled
6784
if variables.get("build_type") or "debug" in "".join(targets):
@@ -71,7 +88,7 @@ def configure_default_packages(self, variables, targets):
7188
self.packages["tl-rv-gdb"]["optional"] = True
7289
self.packages["tl-xt-gdb"]["optional"] = True
7390

74-
# Enable check tools only when "check_tool" is set in platformio.ini
91+
# Enable check tools only when "check_tool" is enabled
7592
for p in self.packages:
7693
if p in ("tool-cppcheck", "tool-clangtidy", "tool-pvs-studio"):
7794
self.packages[p]["optional"] = False if str(variables.get("check_tool")).strip("['']") in p else True
@@ -123,7 +140,7 @@ def configure_default_packages(self, variables, targets):
123140

124141
# Enable needed toolchains
125142
for available_mcu in ("esp32", "esp32s2", "esp32s3"):
126-
if available_mcu == mcu and os.path.exists(IDF_TOOLS):
143+
if available_mcu == mcu and tl_flag:
127144
tc_path = "file://" + join(IDF_TOOLS_PATH_DEFAULT, "tools", "tc-xt-%s" % mcu)
128145
self.packages["tc-xt-%s" % mcu]["optional"] = False
129146
self.packages["tc-xt-%s" % mcu]["version"] = tc_path
@@ -134,7 +151,7 @@ def configure_default_packages(self, variables, targets):
134151
if mcu in ("esp32c2", "esp32c3", "esp32c6", "esp32h2"):
135152
del self.packages["tc-ulp"]
136153
# RISC-V based toolchain for ESP32C3, ESP32C6 ESP32S2, ESP32S3 ULP
137-
if os.path.exists(IDF_TOOLS):
154+
if tl_flag:
138155
self.packages["tc-rv32"]["optional"] = False
139156

140157
return super().configure_default_packages(variables, targets)

0 commit comments

Comments
 (0)