mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
* Replace installer script with a new one. Resolve #3420 * temp file name fix * get-platformio.py script update. * small fix
This commit is contained in:
@ -12,173 +12,94 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import os
|
import tempfile
|
||||||
import subprocess
|
import io
|
||||||
import site
|
|
||||||
import sys
|
import sys
|
||||||
from platform import system
|
import subprocess
|
||||||
from tempfile import NamedTemporaryFile
|
|
||||||
|
|
||||||
CURINTERPRETER_PATH = os.path.normpath(sys.executable)
|
NEW_SCRIPT_URL = "https://raw.githubusercontent.com/platformio/platformio-core-installer/develop/get-platformio.py"
|
||||||
IS_WINDOWS = system().lower() == "windows"
|
|
||||||
|
|
||||||
|
|
||||||
def fix_winpython_pathenv():
|
def download_with_requests(url, dst):
|
||||||
"""
|
import requests
|
||||||
Add Python & Python Scripts to the search path on Windows
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
import _winreg as winreg
|
|
||||||
except ImportError:
|
|
||||||
import winreg
|
|
||||||
|
|
||||||
# took these lines from the native "win_add2path.py"
|
resp = requests.get(url, stream=True)
|
||||||
pythonpath = os.path.dirname(os.path.normpath(sys.executable))
|
itercontent = resp.iter_content(chunk_size=io.DEFAULT_BUFFER_SIZE)
|
||||||
scripts = os.path.join(pythonpath, "Scripts")
|
with open(dst, "wb") as fp:
|
||||||
appdata = os.environ["APPDATA"]
|
for chunk in itercontent:
|
||||||
if hasattr(site, "USER_SITE"):
|
fp.write(chunk)
|
||||||
userpath = site.USER_SITE.replace(appdata, "%APPDATA%")
|
return dst
|
||||||
userscripts = os.path.join(userpath, "Scripts")
|
|
||||||
else:
|
|
||||||
userscripts = None
|
|
||||||
|
|
||||||
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, "Environment") as key:
|
|
||||||
try:
|
|
||||||
envpath = winreg.QueryValueEx(key, "PATH")[0]
|
|
||||||
except WindowsError:
|
|
||||||
envpath = u"%PATH%"
|
|
||||||
|
|
||||||
paths = [envpath]
|
|
||||||
for path in (pythonpath, scripts, userscripts):
|
|
||||||
if path and path not in envpath and os.path.isdir(path):
|
|
||||||
paths.append(path)
|
|
||||||
|
|
||||||
envpath = os.pathsep.join(paths)
|
|
||||||
winreg.SetValueEx(key, "PATH", 0, winreg.REG_EXPAND_SZ, envpath)
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def exec_command(*args, **kwargs):
|
def download_with_urllib3(url, dst):
|
||||||
result = {"out": None, "err": None, "returncode": None}
|
import urllib3
|
||||||
|
http = urllib3.PoolManager()
|
||||||
|
r = http.request('GET', url, preload_content=False)
|
||||||
|
|
||||||
kwargs['stdout'] = subprocess.PIPE
|
with open(dst, 'wb') as out:
|
||||||
kwargs['stderr'] = subprocess.PIPE
|
while True:
|
||||||
kwargs['shell'] = IS_WINDOWS
|
data = r.read(io.DEFAULT_BUFFER_SIZE)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
out.write(data)
|
||||||
|
|
||||||
p = subprocess.Popen(*args, **kwargs)
|
r.release_conn()
|
||||||
result['out'], result['err'] = p.communicate()
|
return dst
|
||||||
result['returncode'] = p.returncode
|
|
||||||
|
|
||||||
for k, v in result.items():
|
|
||||||
if v and isinstance(v, str):
|
|
||||||
result[k].strip()
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def print_exec_result(result):
|
def download_with_urllib(url, dst):
|
||||||
if result['returncode'] == 0:
|
if sys.version_info[0] == 3:
|
||||||
print(result['out'])
|
|
||||||
else:
|
|
||||||
raise Exception("\n".join([result['out'], result['err']]))
|
|
||||||
|
|
||||||
|
|
||||||
def exec_python_cmd(args):
|
|
||||||
return exec_command([CURINTERPRETER_PATH] + args)
|
|
||||||
|
|
||||||
|
|
||||||
def install_pip():
|
|
||||||
r = exec_python_cmd(["-m", "pip", "--version"])
|
|
||||||
if r['returncode'] == 0:
|
|
||||||
print(r['out'])
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
from urllib2 import urlopen
|
|
||||||
except ImportError:
|
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
|
else:
|
||||||
|
from urllib import urlopen
|
||||||
|
|
||||||
f = NamedTemporaryFile(delete=False)
|
response = urlopen(url)
|
||||||
response = urlopen("https://bootstrap.pypa.io/get-pip.py")
|
CHUNK = 16 * 1024
|
||||||
f.write(response.read())
|
with open(dst, 'wb') as f:
|
||||||
f.close()
|
while True:
|
||||||
|
chunk = response.read(CHUNK)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
f.write(chunk)
|
||||||
|
|
||||||
try:
|
return dst
|
||||||
r = exec_python_cmd([f.name])
|
|
||||||
finally:
|
|
||||||
os.unlink(f.name)
|
|
||||||
|
|
||||||
print_exec_result(r)
|
|
||||||
|
|
||||||
|
|
||||||
def install_platformio():
|
def download_with_curl(url, dst):
|
||||||
r = None
|
subprocess.check_output(["curl", "-o", dst, url])
|
||||||
cmd = ["-m", "pip", "install", "-U", "platformio"]
|
return dst
|
||||||
# cmd = [
|
|
||||||
# "-m", "pip", "install", "-U",
|
|
||||||
# "https://github.com/platformio/platformio-core/archive/develop.zip"
|
def download_with_wget(url, dst):
|
||||||
# ]
|
subprocess.check_output(["wget", "-O", dst, url])
|
||||||
try:
|
return dst
|
||||||
r = exec_python_cmd(cmd)
|
|
||||||
assert r['returncode'] == 0
|
|
||||||
except AssertionError:
|
def download_file(url, dst):
|
||||||
cmd.insert(2, "--no-cache-dir")
|
methods = [
|
||||||
r = exec_python_cmd(cmd)
|
download_with_requests,
|
||||||
if r:
|
download_with_urllib3,
|
||||||
print_exec_result(r)
|
download_with_urllib,
|
||||||
|
download_with_curl,
|
||||||
|
download_with_wget
|
||||||
|
]
|
||||||
|
for method in methods:
|
||||||
|
try:
|
||||||
|
method(url, dst)
|
||||||
|
return dst
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
raise Exception("Could not download file '%s' to '%s' "%(url, dst))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
steps = [("Fixing Windows %PATH% Environment", fix_winpython_pathenv),
|
print("This installer script is deprecated and will be removed in the next release. Please use %s" %
|
||||||
("Installing Python Package Manager", install_pip),
|
NEW_SCRIPT_URL)
|
||||||
("Installing PlatformIO and dependencies", install_platformio)]
|
with tempfile.NamedTemporaryFile() as tmp_file:
|
||||||
|
dst = download_file(NEW_SCRIPT_URL, str(tmp_file.name))
|
||||||
if not IS_WINDOWS:
|
command = [sys.executable, dst]
|
||||||
del steps[0]
|
command.extend(sys.argv[1:])
|
||||||
|
subprocess.check_call(command)
|
||||||
is_error = False
|
|
||||||
for s in steps:
|
|
||||||
if is_error:
|
|
||||||
break
|
|
||||||
print("\n==> %s ..." % s[0])
|
|
||||||
try:
|
|
||||||
s[1]()
|
|
||||||
print("[SUCCESS]")
|
|
||||||
except Exception as e:
|
|
||||||
is_error = True
|
|
||||||
print(str(e))
|
|
||||||
print("[FAILURE]")
|
|
||||||
|
|
||||||
permission_errors = ("permission denied", "not permitted")
|
|
||||||
if (any([m in str(e).lower() for m in permission_errors]) and
|
|
||||||
not IS_WINDOWS):
|
|
||||||
print("""
|
|
||||||
-----------------
|
|
||||||
Permission denied
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
You need the `sudo` permission to install Python packages. Try
|
|
||||||
|
|
||||||
$ sudo python -c "$(curl -fsSL
|
|
||||||
https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)"
|
|
||||||
""")
|
|
||||||
|
|
||||||
if is_error:
|
|
||||||
print("The installation process has been FAILED!\n"
|
|
||||||
"Please report about this problem here\n"
|
|
||||||
"< https://github.com/platformio/platformio-core/issues >")
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
print("\n ==> Installation process has been "
|
|
||||||
"successfully FINISHED! <==\n")
|
|
||||||
print("""
|
|
||||||
|
|
||||||
----------------------------------------
|
|
||||||
Please RESTART your Terminal Application
|
|
||||||
----------------------------------------
|
|
||||||
|
|
||||||
Then run `platformio --help` command.
|
|
||||||
|
|
||||||
""")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Reference in New Issue
Block a user