Files
platformio-core/scripts/get-platformio.py

192 lines
5.2 KiB
Python
Raw Normal View History

2016-08-03 22:18:51 +03:00
# Copyright 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2014-06-21 18:21:26 +03:00
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)
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")
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):
2015-12-22 19:53:54 +02:00
result = {
"out": None,
"err": None,
"returncode": None
}
2015-02-23 11:50:14 +02:00
kwargs['stdout'] = subprocess.PIPE
kwargs['stderr'] = subprocess.PIPE
kwargs['shell'] = IS_WINDOWS
p = subprocess.Popen(*args, **kwargs)
2015-12-22 19:53:54 +02:00
result['out'], result['err'] = p.communicate()
result['returncode'] = p.returncode
for k, v in result.iteritems():
if v and isinstance(v, basestring):
result[k].strip()
return result
def print_exec_result(result):
if result['returncode'] == 0:
2016-01-01 20:51:48 +02:00
print(result['out'])
2015-12-22 19:53:54 +02:00
else:
raise Exception("\n".join([result['out'], result['err']]))
2015-02-23 11:50:14 +02:00
2014-06-21 18:21:26 +03:00
def exec_python_cmd(args):
return exec_command([CURINTERPRETER_PATH] + args)
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:
2015-12-22 19:53:54 +02:00
r = exec_python_cmd([f.name])
2014-06-21 18:21:26 +03:00
finally:
os.unlink(f.name)
2015-12-22 19:53:54 +02:00
print_exec_result(r)
2014-06-21 18:21:26 +03:00
def install_platformio():
2015-12-22 19:53:54 +02:00
r = None
cmd = ["-m", "pip.__main__" if sys.version_info < (2, 7, 0) else "pip"]
try:
2015-12-22 19:53:54 +02:00
r = exec_python_cmd(cmd + ["install", "-U", "platformio"])
assert r['returncode'] == 0
except AssertionError:
r = exec_python_cmd(
cmd + ["--no-cache-dir", "install", "-U", "platformio"])
if r:
print_exec_result(r)
2014-06-21 18:21:26 +03:00
def main():
steps = [
("Fixing Windows %PATH% Environment", fix_winpython_pathenv),
("Installing Python Package Manager", install_pip),
("Installing PlatformIO and dependencies", install_platformio)
2014-06-21 18:21:26 +03:00
]
if not IS_WINDOWS:
del steps[0]
is_error = False
for s in steps:
if is_error:
break
2016-01-01 20:51:48 +02:00
print("\n==> %s ..." % s[0])
2014-06-21 18:21:26 +03:00
try:
s[1]()
2016-01-01 20:51:48 +02:00
print("[SUCCESS]")
2014-06-21 18:21:26 +03:00
except Exception, e:
is_error = True
2016-01-01 20:51:48 +02:00
print(str(e))
print("[FAILURE]")
2015-12-22 19:53:54 +02:00
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/master/scripts/get-platformio.py)"
""")
2014-06-21 18:21:26 +03:00
if is_error:
2016-01-01 20:51:48 +02:00
print("The installation process has been FAILED!\n"
"Please report about this problem here\n"
"< https://github.com/platformio/platformio/issues >")
2014-06-21 18:21:26 +03:00
return
else:
2016-01-01 20:51:48 +02:00
print("\n ==> Installation process has been "
"successfully FINISHED! <==\n")
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())