-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-smartanthill.py
123 lines (97 loc) · 3.49 KB
/
get-smartanthill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright (C) Ivan Kravets <[email protected]>
# 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")
if not os.path.isdir(scripts):
os.makedirs(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 SmartAnthill and dependencies", install_pypi_packages,
(["-e git://github.com/ivankravets/smartanthill.git"
"@develop#egg=smartanthill", "--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/smartanthill/issues >")
return
else:
print ("\n ==> Installation process has been "
"successfully FINISHED! <==\n")
try:
print (check_output("smartanthill --help", shell=IS_WINDOWS))
except:
try:
print (exec_python_cmd(["-m", "smartanthill", "--help"]))
finally:
print ("\n Please RESTART your Terminal Application and run "
"`smartanthill --help` command.")
if __name__ == "__main__":
sys.exit(main())