From fd1fc9941aa9128c04393067b831151bb023b2a0 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Sat, 21 Jun 2014 18:21:26 +0300 Subject: [PATCH] Implemented auto-installer script --- README.rst | 37 +++++++++--- platformio/__main__.py | 2 +- scripts/get-platformio.py | 120 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 scripts/get-platformio.py diff --git a/README.rst b/README.rst index 8b1255e0..f89eed60 100644 --- a/README.rst +++ b/README.rst @@ -82,9 +82,6 @@ Quickstart .. code-block:: bash - # Install platformio - $ pip install platformio && pip install --egg scons - # Print all availalbe development platforms for installing $ platformio search all @@ -107,20 +104,46 @@ Installation All commands below should be executed in `Command-line `_ -application in your OS: +application in your *OS*: * *Unix/Linux/OS X* this is *Terminal* application. * *Windows* this is `Command Prompt `_ (``cmd.exe``) application. -1. Check a ``python`` version: +Also, the `Python Interpreter `_ (2.6 or 2.7) +is required. + + +Super-Quick +~~~~~~~~~~~ + +To install or upgrade *PlatformIO*, download +`get-platformio.py `_ script. + +Then run the following (which may require administrator access): + +.. code-block:: bash + + $ python get-platformio.py + +On *Windows OS* it may look like: + +.. code-block:: bash + + C:\Python27\python.exe get-platformio.py + + +Full Guide +~~~~~~~~~~ + +1. Check a ``python`` version (only 2.6-2.7 is supported): .. code-block:: bash $ python --version -Windows OS Users only: +*Windows OS* Users only: * `Download Python 2.7 `_ and install it. * Add to PATH system variable ``;C:\Python27;C:\Python27\Scripts;`` and @@ -129,7 +152,7 @@ Windows OS Users only: `_. -2. Check a ``pip`` tool for installing and managing Python packages: +2. Check a ``pip`` tool for installing and managing *Python* packages: .. code-block:: bash diff --git a/platformio/__main__.py b/platformio/__main__.py index a7a74f2a..3b9be174 100644 --- a/platformio/__main__.py +++ b/platformio/__main__.py @@ -35,7 +35,7 @@ class PlatformioCLI(MultiCommand): @command(cls=PlatformioCLI) -@version_option(__version__) +@version_option(__version__, "platformio") def cli(): pass diff --git a/scripts/get-platformio.py b/scripts/get-platformio.py new file mode 100644 index 00000000..79bf85d7 --- /dev/null +++ b/scripts/get-platformio.py @@ -0,0 +1,120 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. + +import os +import sys +from subprocess import check_output +from tempfile import NamedTemporaryFile + + +CURINTERPRETER_PATH = os.path.normpath(sys.executable) +IS_WINDOWS = sys.platform.startswith("win") + + +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") + + 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 + + +def exec_python_cmd(args): + return check_output([CURINTERPRETER_PATH] + args, shell=IS_WINDOWS).strip() + + +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): + for p in packages: + print (exec_python_cmd(["-m", "pip", "install", "-U"] + p.split())) + + +def main(): + steps = [ + ("Fixing Windows %PATH% Environment", fix_winpython_pathenv, []), + ("Installing Python Package Manager", install_pip, []), + ("Installing PlatformIO and dependencies", install_pypi_packages, + (["platformio", "--egg scons"],)), + ] + + if not IS_WINDOWS: + del steps[0] + + is_error = False + for s in steps: + print ("\n==> %s ..." % s[0]) + try: + s[1](*s[2]) + print ("[SUCCESS]") + except Exception, e: + is_error = True + print (e) + print ("[FAILURE]") + + if is_error: + print ("The installation process has been FAILED!\n" + "Please report about this problem here\n" + "< https://github.com/ivankravets/platformio/issues >") + return + else: + print ("\n ==> Installation process has been " + "successfully FINISHED! <==\n") + + try: + print (check_output("platformio", shell=IS_WINDOWS)) + except: + try: + print (exec_python_cmd(["-m", "platformio"])) + finally: + print ("\n Please RESTART your Terminal Application and run " + "`platformio --help` command.") + + +if __name__ == "__main__": + sys.exit(main())