|
| 1 | +# Copyright (C) Ivan Kravets <[email protected]> |
| 2 | +# See LICENSE for details. |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from subprocess import check_output |
| 7 | +from tempfile import NamedTemporaryFile |
| 8 | + |
| 9 | + |
| 10 | +CURINTERPRETER_PATH = os.path.normpath(sys.executable) |
| 11 | +IS_WINDOWS = sys.platform.startswith("win") |
| 12 | + |
| 13 | + |
| 14 | +def fix_winpython_pathenv(): |
| 15 | + """ |
| 16 | + Add Python & Python Scripts to the search path on Windows |
| 17 | + """ |
| 18 | + import ctypes |
| 19 | + from ctypes.wintypes import HWND, UINT, WPARAM, LPARAM, LPVOID |
| 20 | + try: |
| 21 | + import _winreg as winreg |
| 22 | + except ImportError: |
| 23 | + import winreg |
| 24 | + |
| 25 | + # took these lines from the native "win_add2path.py" |
| 26 | + pythonpath = os.path.dirname(CURINTERPRETER_PATH) |
| 27 | + scripts = os.path.join(pythonpath, "Scripts") |
| 28 | + |
| 29 | + with winreg.CreateKey(winreg.HKEY_CURRENT_USER, u"Environment") as key: |
| 30 | + try: |
| 31 | + envpath = winreg.QueryValueEx(key, u"PATH")[0] |
| 32 | + except WindowsError: |
| 33 | + envpath = u"%PATH%" |
| 34 | + |
| 35 | + paths = [envpath] |
| 36 | + for path in (pythonpath, scripts): |
| 37 | + if path and path not in envpath and os.path.isdir(path): |
| 38 | + paths.append(path) |
| 39 | + |
| 40 | + envpath = os.pathsep.join(paths) |
| 41 | + winreg.SetValueEx(key, u"PATH", 0, winreg.REG_EXPAND_SZ, envpath) |
| 42 | + winreg.ExpandEnvironmentStrings(envpath) |
| 43 | + |
| 44 | + # notify the system about the changes |
| 45 | + SendMessage = ctypes.windll.user32.SendMessageW |
| 46 | + SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID |
| 47 | + SendMessage.restype = LPARAM |
| 48 | + SendMessage(0xFFFF, 0x1A, 0, u"Environment") |
| 49 | + return True |
| 50 | + |
| 51 | + |
| 52 | +def exec_python_cmd(args): |
| 53 | + return check_output([CURINTERPRETER_PATH] + args, shell=IS_WINDOWS).strip() |
| 54 | + |
| 55 | + |
| 56 | +def install_pip(): |
| 57 | + try: |
| 58 | + from urllib2 import urlopen |
| 59 | + except ImportError: |
| 60 | + from urllib.request import urlopen |
| 61 | + |
| 62 | + f = NamedTemporaryFile(delete=False) |
| 63 | + response = urlopen("https://bootstrap.pypa.io/get-pip.py") |
| 64 | + f.write(response.read()) |
| 65 | + f.close() |
| 66 | + |
| 67 | + try: |
| 68 | + print (exec_python_cmd([f.name])) |
| 69 | + finally: |
| 70 | + os.unlink(f.name) |
| 71 | + |
| 72 | + |
| 73 | +def install_pypi_packages(packages): |
| 74 | + for p in packages: |
| 75 | + print (exec_python_cmd(["-m", "pip", "install", "-U"] + p.split())) |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + steps = [ |
| 80 | + ("Fixing Windows %PATH% Environment", fix_winpython_pathenv, []), |
| 81 | + ("Installing Python Package Manager", install_pip, []), |
| 82 | + ("Installing PlatformIO and dependencies", install_pypi_packages, |
| 83 | + (["platformio", "--egg scons"],)), |
| 84 | + ] |
| 85 | + |
| 86 | + if not IS_WINDOWS: |
| 87 | + del steps[0] |
| 88 | + |
| 89 | + is_error = False |
| 90 | + for s in steps: |
| 91 | + print ("\n==> %s ..." % s[0]) |
| 92 | + try: |
| 93 | + s[1](*s[2]) |
| 94 | + print ("[SUCCESS]") |
| 95 | + except Exception, e: |
| 96 | + is_error = True |
| 97 | + print (e) |
| 98 | + print ("[FAILURE]") |
| 99 | + |
| 100 | + if is_error: |
| 101 | + print ("The installation process has been FAILED!\n" |
| 102 | + "Please report about this problem here\n" |
| 103 | + "< https://github.com/ivankravets/platformio/issues >") |
| 104 | + return |
| 105 | + else: |
| 106 | + print ("\n ==> Installation process has been " |
| 107 | + "successfully FINISHED! <==\n") |
| 108 | + |
| 109 | + try: |
| 110 | + print (check_output("platformio", shell=IS_WINDOWS)) |
| 111 | + except: |
| 112 | + try: |
| 113 | + print (exec_python_cmd(["-m", "platformio"])) |
| 114 | + finally: |
| 115 | + print ("\n Please RESTART your Terminal Application and run " |
| 116 | + "`platformio --help` command.") |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + sys.exit(main()) |
0 commit comments