1313# limitations under the License.
1414
1515import glob
16+ import json
1617import logging
1718import os
1819import platform
2122import tempfile
2223
2324import click
25+ import requests
26+ import semantic_version
2427
2528from pioinstaller import exception , util
2629
2730log = 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-
4633def 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
6869def 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
93122def check ():
0 commit comments