Skip to content

Commit fd1fc99

Browse files
committed
Implemented auto-installer script
1 parent c4be964 commit fd1fc99

File tree

3 files changed

+151
-8
lines changed

3 files changed

+151
-8
lines changed

README.rst

+30-7
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ Quickstart
8282

8383
.. code-block:: bash
8484
85-
# Install platformio
86-
$ pip install platformio && pip install --egg scons
87-
8885
# Print all availalbe development platforms for installing
8986
$ platformio search all
9087
@@ -107,20 +104,46 @@ Installation
107104

108105
All commands below should be executed in
109106
`Command-line <http://en.wikipedia.org/wiki/Command-line_interface>`_
110-
application in your OS:
107+
application in your *OS*:
111108

112109
* *Unix/Linux/OS X* this is *Terminal* application.
113110
* *Windows* this is
114111
`Command Prompt <http://en.wikipedia.org/wiki/Command_Prompt>`_ (``cmd.exe``)
115112
application.
116113

117-
1. Check a ``python`` version:
114+
Also, the `Python Interpreter <https://www.python.org/downloads/>`_ (2.6 or 2.7)
115+
is required.
116+
117+
118+
Super-Quick
119+
~~~~~~~~~~~
120+
121+
To install or upgrade *PlatformIO*, download
122+
`get-platformio.py <https://raw.githubusercontent.com/ivankravets/platformio/develop/scripts/get-platformio.py>`_ script.
123+
124+
Then run the following (which may require administrator access):
125+
126+
.. code-block:: bash
127+
128+
$ python get-platformio.py
129+
130+
On *Windows OS* it may look like:
131+
132+
.. code-block:: bash
133+
134+
C:\Python27\python.exe get-platformio.py
135+
136+
137+
Full Guide
138+
~~~~~~~~~~
139+
140+
1. Check a ``python`` version (only 2.6-2.7 is supported):
118141

119142
.. code-block:: bash
120143
121144
$ python --version
122145
123-
Windows OS Users only:
146+
*Windows OS* Users only:
124147

125148
* `Download Python 2.7 <https://www.python.org/downloads/>`_ and install it.
126149
* Add to PATH system variable ``;C:\Python27;C:\Python27\Scripts;`` and
@@ -129,7 +152,7 @@ Windows OS Users only:
129152
<http://www.computerhope.com/issues/ch000549.htm>`_.
130153

131154

132-
2. Check a ``pip`` tool for installing and managing Python packages:
155+
2. Check a ``pip`` tool for installing and managing *Python* packages:
133156

134157
.. code-block:: bash
135158

platformio/__main__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_command(self, ctx, name):
3535

3636

3737
@command(cls=PlatformioCLI)
38-
@version_option(__version__)
38+
@version_option(__version__, "platformio")
3939
def cli():
4040
pass
4141

scripts/get-platformio.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)