2017-06-05 16:02:39 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
2015-11-18 17:16:17 +02:00
|
|
|
#
|
|
|
|
# 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
|
2017-12-15 13:47:44 +02:00
|
|
|
import site
|
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
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
import _winreg as winreg
|
|
|
|
except ImportError:
|
|
|
|
import winreg
|
|
|
|
|
|
|
|
# took these lines from the native "win_add2path.py"
|
2017-12-15 13:47:44 +02:00
|
|
|
pythonpath = os.path.dirname(os.path.normpath(sys.executable))
|
2014-06-21 18:21:26 +03:00
|
|
|
scripts = os.path.join(pythonpath, "Scripts")
|
2017-12-15 13:47:44 +02:00
|
|
|
appdata = os.environ["APPDATA"]
|
|
|
|
if hasattr(site, "USER_SITE"):
|
|
|
|
userpath = site.USER_SITE.replace(appdata, "%APPDATA%")
|
|
|
|
userscripts = os.path.join(userpath, "Scripts")
|
|
|
|
else:
|
|
|
|
userscripts = None
|
2014-06-21 18:21:26 +03:00
|
|
|
|
2017-12-15 13:47:44 +02:00
|
|
|
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, "Environment") as key:
|
2014-06-21 18:21:26 +03:00
|
|
|
try:
|
2017-12-15 13:47:44 +02:00
|
|
|
envpath = winreg.QueryValueEx(key, "PATH")[0]
|
2014-06-21 18:21:26 +03:00
|
|
|
except WindowsError:
|
|
|
|
envpath = u"%PATH%"
|
|
|
|
|
|
|
|
paths = [envpath]
|
2017-12-15 13:47:44 +02:00
|
|
|
for path in (pythonpath, scripts, userscripts):
|
2014-06-21 18:21:26 +03:00
|
|
|
if path and path not in envpath and os.path.isdir(path):
|
|
|
|
paths.append(path)
|
|
|
|
|
|
|
|
envpath = os.pathsep.join(paths)
|
2017-12-15 13:47:44 +02:00
|
|
|
winreg.SetValueEx(key, "PATH", 0, winreg.REG_EXPAND_SZ, envpath)
|
2014-06-21 18:21:26 +03:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2015-02-23 11:50:14 +02:00
|
|
|
def exec_command(*args, **kwargs):
|
2016-10-29 22:22:01 +03:00
|
|
|
result = {"out": None, "err": None, "returncode": None}
|
2015-12-22 19:53:54 +02:00
|
|
|
|
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):
|
2015-11-06 16:38:41 +02:00
|
|
|
return exec_command([CURINTERPRETER_PATH] + args)
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def install_pip():
|
2017-08-09 23:43:31 +03:00
|
|
|
r = exec_python_cmd(["-m", "pip", "--version"])
|
|
|
|
if r['returncode'] == 0:
|
2019-06-26 06:50:06 +10:00
|
|
|
print(r['out'])
|
2017-08-09 23:43:31 +03:00
|
|
|
return
|
2014-06-21 18:21:26 +03:00
|
|
|
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
|
|
|
|
2015-11-06 16:38:41 +02:00
|
|
|
def install_platformio():
|
2015-12-22 19:53:54 +02:00
|
|
|
r = None
|
2017-08-09 23:43:31 +03:00
|
|
|
cmd = ["-m", "pip", "install", "-U", "platformio"]
|
2017-06-26 18:04:40 +03:00
|
|
|
# cmd = [
|
2017-08-09 23:43:31 +03:00
|
|
|
# "-m", "pip", "install", "-U",
|
2017-06-26 18:04:40 +03:00
|
|
|
# "https://github.com/platformio/platformio-core/archive/develop.zip"
|
|
|
|
# ]
|
2015-11-06 16:38:41 +02:00
|
|
|
try:
|
2017-06-14 19:55:44 +03:00
|
|
|
r = exec_python_cmd(cmd)
|
2015-12-22 19:53:54 +02:00
|
|
|
assert r['returncode'] == 0
|
|
|
|
except AssertionError:
|
2017-09-02 14:43:08 +03:00
|
|
|
cmd.insert(2, "--no-cache-dir")
|
2017-06-14 19:55:44 +03:00
|
|
|
r = exec_python_cmd(cmd)
|
2015-12-22 19:53:54 +02:00
|
|
|
if r:
|
|
|
|
print_exec_result(r)
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-06-14 19:55:44 +03:00
|
|
|
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:
|
2015-07-21 17:51:33 +03:00
|
|
|
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:
|
2015-11-06 16:38:41 +02:00
|
|
|
s[1]()
|
2016-01-01 20:51:48 +02:00
|
|
|
print("[SUCCESS]")
|
2019-06-26 06:50:06 +10:00
|
|
|
except Exception as e:
|
2014-06-21 18:21:26 +03:00
|
|
|
is_error = True
|
2016-01-01 20:51:48 +02:00
|
|
|
print(str(e))
|
|
|
|
print("[FAILURE]")
|
2015-12-22 19:53:54 +02:00
|
|
|
|
2016-10-29 22:22:01 +03:00
|
|
|
permission_errors = ("permission denied", "not permitted")
|
|
|
|
if (any([m in str(e).lower() for m in permission_errors]) and
|
|
|
|
not IS_WINDOWS):
|
|
|
|
print("""
|
2015-07-21 17:51:33 +03:00
|
|
|
-----------------
|
|
|
|
Permission denied
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
You need the `sudo` permission to install Python packages. Try
|
|
|
|
|
|
|
|
$ sudo python -c "$(curl -fsSL
|
2017-06-14 19:55:44 +03:00
|
|
|
https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)"
|
2015-07-21 17:51:33 +03:00
|
|
|
""")
|
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"
|
2016-12-04 18:59:07 +02:00
|
|
|
"< https://github.com/platformio/platformio-core/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")
|
2016-10-29 22:22:01 +03:00
|
|
|
print("""
|
2015-07-21 17:51:33 +03:00
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
Please RESTART your Terminal Application
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
Then run `platformio --help` command.
|
|
|
|
|
|
|
|
""")
|
2014-06-21 18:21:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|