Skip to content

Commit 3e699f3

Browse files
committed
Download portable Python from PlatformIO Trusted Registry
1 parent e42d857 commit 3e699f3

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

pioinstaller/penv.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ def create_core_penv(penv_dir=None, ignore_pythons=None):
5454
if result_dir:
5555
break
5656

57-
if (
58-
not result_dir
59-
and util.get_systype() in python.PORTABLE_PYTHONS
60-
and not python.is_portable()
61-
):
57+
if not result_dir and not python.is_portable():
6258
python_exe = python.fetch_portable_python(os.path.dirname(penv_dir))
6359
if python_exe:
6460
result_dir = create_virtualenv(python_exe, penv_dir)
@@ -95,6 +91,7 @@ def create_virtualenv(python_exe, penv_dir):
9591
"Could not create virtualenv with downloaded script. Error: %s",
9692
str(e),
9793
)
94+
return None
9895

9996

10097
def create_with_local_venv(python_exe, penv_dir):

pioinstaller/python.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import glob
16+
import json
1617
import logging
1718
import os
1819
import platform
@@ -21,28 +22,14 @@
2122
import tempfile
2223

2324
import click
25+
import requests
26+
import semantic_version
2427

2528
from pioinstaller import exception, util
2629

2730
log = logging.getLogger(__name__)
2831

2932

30-
PORTABLE_PYTHONS = {
31-
"windows_x86": (
32-
"https://dl.bintray.com/platformio/dl-misc/"
33-
"python-portable-windows_x86-3.7.7.tar.gz"
34-
),
35-
"windows_amd64": (
36-
"https://dl.bintray.com/platformio/dl-misc/"
37-
"python-portable-windows_amd64-3.7.7.tar.gz"
38-
),
39-
"darwin_x86_64": (
40-
"https://dl.bintray.com/platformio/dl-misc/"
41-
"python-portable-darwin_x86_64-3.8.4.tar.gz"
42-
),
43-
}
44-
45-
4633
def is_conda():
4734
return any(
4835
[
@@ -62,13 +49,27 @@ def is_portable():
6249

6350
return True
6451
except: # pylint:disable=bare-except
52+
pass
53+
print(os.path.normpath(sys.executable))
54+
python_dir = os.path.dirname(sys.executable)
55+
if not util.IS_WINDOWS:
56+
# skip "bin" folder
57+
python_dir = os.path.dirname(python_dir)
58+
manifest_path = os.path.join(python_dir, "package.json")
59+
if not os.path.isfile(manifest_path):
6560
return False
61+
try:
62+
with open(manifest_path) as fp:
63+
return json.load(fp).get("name") == "python-portable"
64+
except ValueError:
65+
pass
66+
return False
6667

6768

6869
def fetch_portable_python(dst):
69-
url = PORTABLE_PYTHONS.get(util.get_systype())
70+
url = get_portable_python_url()
7071
if not url:
71-
log.debug("There is no portable Python for %s", util.get_systype())
72+
log.debug("Could not find portable Python for %s", util.get_systype())
7273
return None
7374
try:
7475
log.debug("Downloading portable python...")
@@ -88,6 +89,34 @@ def fetch_portable_python(dst):
8889
return os.path.join(python_dir, "bin", "python3")
8990
except: # pylint:disable=bare-except
9091
log.debug("Could not download portable python")
92+
return None
93+
94+
95+
def get_portable_python_url():
96+
systype = util.get_systype()
97+
result = requests.get(
98+
"https://api.registry.platformio.org/v3/packages/"
99+
"platformio/tool/python-portable"
100+
).json()
101+
versions = [
102+
version
103+
for version in result["versions"]
104+
if is_version_system_compatible(version, systype)
105+
]
106+
best_version = None
107+
for version in versions:
108+
if not best_version or semantic_version.Version(
109+
version.name
110+
) > semantic_version.Version(best_version.name):
111+
best_version = version
112+
for item in (best_version or {}).get("files", []):
113+
if systype in item["system"]:
114+
return item["download_url"]
115+
return None
116+
117+
118+
def is_version_system_compatible(version, systype):
119+
return any(systype in item["system"] for item in version["files"])
91120

92121

93122
def check():

pioinstaller/util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def safe_create_dir(path, raise_exception=False):
8484
except Exception as e: # pylint: disable=broad-except
8585
if raise_exception:
8686
raise e
87+
return None
8788

8889

8990
def download_file(url, dst, cache=True):
@@ -127,6 +128,7 @@ def safe_remove_dir(path, raise_exception=False):
127128
except Exception as e: # pylint: disable=broad-except
128129
if raise_exception:
129130
raise e
131+
return None
130132

131133

132134
def pepver_to_semver(pepver):

0 commit comments

Comments
 (0)