2014-06-21 18:21:26 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
|
|
|
import os
|
2015-02-23 11:50:14 +02:00
|
|
|
import subprocess
|
2014-06-21 18:21:26 +03:00
|
|
|
import sys
|
2015-02-23 11:50:14 +02:00
|
|
|
from platform import system
|
2014-06-21 18:21:26 +03:00
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
|
|
|
|
|
|
|
|
CURINTERPRETER_PATH = os.path.normpath(sys.executable)
|
2015-07-21 17:51:33 +03:00
|
|
|
IS_WINDOWS = system().lower() == "windows"
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def fix_winpython_pathenv():
|
|
|
|
"""
|
|
|
|
Add Python & Python Scripts to the search path on Windows
|
|
|
|
"""
|
|
|
|
import ctypes
|
|
|
|
from ctypes.wintypes import HWND, UINT, WPARAM, LPARAM, LPVOID
|
|
|
|
try:
|
|
|
|
import _winreg as winreg
|
|
|
|
except ImportError:
|
|
|
|
import winreg
|
|
|
|
|
|
|
|
# took these lines from the native "win_add2path.py"
|
|
|
|
pythonpath = os.path.dirname(CURINTERPRETER_PATH)
|
|
|
|
scripts = os.path.join(pythonpath, "Scripts")
|
2014-06-21 23:04:33 +03:00
|
|
|
if not os.path.isdir(scripts):
|
|
|
|
os.makedirs(scripts)
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, u"Environment") as key:
|
|
|
|
try:
|
|
|
|
envpath = winreg.QueryValueEx(key, u"PATH")[0]
|
|
|
|
except WindowsError:
|
|
|
|
envpath = u"%PATH%"
|
|
|
|
|
|
|
|
paths = [envpath]
|
|
|
|
for path in (pythonpath, scripts):
|
|
|
|
if path and path not in envpath and os.path.isdir(path):
|
|
|
|
paths.append(path)
|
|
|
|
|
|
|
|
envpath = os.pathsep.join(paths)
|
|
|
|
winreg.SetValueEx(key, u"PATH", 0, winreg.REG_EXPAND_SZ, envpath)
|
|
|
|
winreg.ExpandEnvironmentStrings(envpath)
|
|
|
|
|
|
|
|
# notify the system about the changes
|
|
|
|
SendMessage = ctypes.windll.user32.SendMessageW
|
|
|
|
SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID
|
|
|
|
SendMessage.restype = LPARAM
|
|
|
|
SendMessage(0xFFFF, 0x1A, 0, u"Environment")
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2015-02-23 11:50:14 +02:00
|
|
|
def exec_command(*args, **kwargs):
|
|
|
|
kwargs['stdout'] = subprocess.PIPE
|
|
|
|
kwargs['stderr'] = subprocess.PIPE
|
|
|
|
kwargs['shell'] = IS_WINDOWS
|
|
|
|
|
|
|
|
p = subprocess.Popen(*args, **kwargs)
|
|
|
|
out, err = p.communicate()
|
|
|
|
|
|
|
|
if p.returncode != 0:
|
2015-07-21 17:51:33 +03:00
|
|
|
raise Exception("\n".join([out, err]))
|
2015-02-23 11:50:14 +02:00
|
|
|
return out
|
|
|
|
|
|
|
|
|
2014-06-21 18:21:26 +03:00
|
|
|
def exec_python_cmd(args):
|
2015-07-21 17:51:33 +03:00
|
|
|
return exec_command([CURINTERPRETER_PATH] + args).strip()
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def install_pip():
|
|
|
|
try:
|
|
|
|
from urllib2 import urlopen
|
|
|
|
except ImportError:
|
|
|
|
from urllib.request import urlopen
|
|
|
|
|
|
|
|
f = NamedTemporaryFile(delete=False)
|
|
|
|
response = urlopen("https://bootstrap.pypa.io/get-pip.py")
|
|
|
|
f.write(response.read())
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
try:
|
|
|
|
print (exec_python_cmd([f.name]))
|
|
|
|
finally:
|
|
|
|
os.unlink(f.name)
|
|
|
|
|
|
|
|
|
|
|
|
def install_pypi_packages(packages):
|
2015-08-22 17:09:28 +03:00
|
|
|
print (exec_python_cmd([
|
|
|
|
"-m", "pip.__main__" if sys.version_info < (2, 7, 0) else "pip",
|
|
|
|
"install", "-U"] + packages))
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
steps = [
|
|
|
|
("Fixing Windows %PATH% Environment", fix_winpython_pathenv, []),
|
|
|
|
("Installing Python Package Manager", install_pip, []),
|
2015-08-22 17:09:28 +03:00
|
|
|
("Installing PlatformIO and dependencies", install_pypi_packages,
|
2015-09-09 01:28:43 +03:00
|
|
|
[["setuptools", "virtualenv", "platformio"]])
|
2014-06-21 18:21:26 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
if not IS_WINDOWS:
|
|
|
|
del steps[0]
|
|
|
|
|
|
|
|
is_error = False
|
|
|
|
for s in steps:
|
2015-07-21 17:51:33 +03:00
|
|
|
if is_error:
|
|
|
|
break
|
2014-06-21 18:21:26 +03:00
|
|
|
print ("\n==> %s ..." % s[0])
|
|
|
|
try:
|
|
|
|
s[1](*s[2])
|
|
|
|
print ("[SUCCESS]")
|
|
|
|
except Exception, e:
|
|
|
|
is_error = True
|
2015-07-21 17:51:33 +03:00
|
|
|
print (str(e))
|
2014-06-21 18:21:26 +03:00
|
|
|
print ("[FAILURE]")
|
2015-07-21 17:51:33 +03:00
|
|
|
if "Permission denied" in str(e) 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/master/scripts/get-platformio.py)"
|
|
|
|
""")
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
if is_error:
|
|
|
|
print ("The installation process has been FAILED!\n"
|
|
|
|
"Please report about this problem here\n"
|
2015-03-25 11:34:48 +02:00
|
|
|
"< https://github.com/platformio/platformio/issues >")
|
2014-06-21 18:21:26 +03:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
print ("\n ==> Installation process has been "
|
|
|
|
"successfully FINISHED! <==\n")
|
|
|
|
|
|
|
|
try:
|
2015-07-21 17:51:33 +03:00
|
|
|
print (exec_command("platformio"))
|
2014-06-21 18:21:26 +03:00
|
|
|
except:
|
|
|
|
try:
|
2015-02-23 11:50:14 +02:00
|
|
|
print (exec_python_cmd([
|
|
|
|
"-m",
|
|
|
|
"platformio.__main__" if sys.version_info < (2, 7, 0) else
|
|
|
|
"platformio"]))
|
2015-07-21 17:51:33 +03:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
print ("""
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
Please RESTART your Terminal Application
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
Then run `platformio --help` command.
|
|
|
|
|
|
|
|
""")
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|