-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·95 lines (81 loc) · 3.02 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·95 lines (81 loc) · 3.02 KB
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
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import emk
if sys.platform == "win32":
import _winreg as winreg
from ctypes import windll
class EnvPath(object):
def __init__(self):
regpath = 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment'
self.SendMessage = windll.user32.SendMessageW
self.HWND_BROADCAST = 0xFFFF
self.WM_SETTINGCHANGE = 0x001A
self.reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
self.key = winreg.OpenKey(self.reg, regpath, 0, winreg.KEY_ALL_ACCESS)
def _query(self):
value, type_id = winreg.QueryValueEx(self.key, 'PATH')
return value.split(';')
def add(self, path):
items = self._query()
if path not in items:
items.append(path)
value = ';'.join(items)
winreg.SetValueEx(self.key, 'PATH', 0, winreg.REG_EXPAND_SZ, value)
self.SendMessage(self.HWND_BROADCAST, self.WM_SETTINGCHANGE, 0, 'Environment')
return True
return False
def remove(self, path):
items = self._query()
if path in items:
items.remove(path)
value = ';'.join(items)
winreg.SetValueEx(self.key, 'PATH', 0, winreg.REG_EXPAND_SZ, value)
self.SendMessage(self.HWND_BROADCAST, self.WM_SETTINGCHANGE, 0, 'Environment')
return True
return False
def usage():
print("Usage (as root/sudo/admin): 'setup.py install' or 'setup.py uninstall'")
sys.exit(1)
def install():
emk_dir, tail = os.path.split(emk._module_path)
if sys.platform == "win32":
path = EnvPath()
if path.add(emk_dir):
print("emk directory added to PATH")
else:
print("emk directory already in PATH")
else:
bin_path = os.path.join(emk_dir, "emk")
if os.path.exists("/usr/bin/emk"):
print("/usr/bin/emk already exists; will not overwrite.")
else:
os.symlink(os.path.join(emk_dir, "emk"), "/usr/bin/emk")
print("Created symlink /usr/bin/emk -> %s" % (bin_path))
def uninstall():
emk_dir, tail = os.path.split(emk._module_path)
if sys.platform == "win32":
path = EnvPath()
if path.remove(emk_dir):
print("emk directory removed from PATH")
else:
print("emk directory not found in PATH")
else:
bin_path = os.path.join(emk_dir, "emk")
try:
if os.readlink("/usr/bin/emk") == bin_path:
os.remove("/usr/bin/emk")
print("Removed /usr/bin/emk")
return
except OSError:
pass
print("/usr/bin/emk is not a symlink, or does not point to this instance of emk (%s)" % (bin_path))
if len(sys.argv) != 2:
usage()
if sys.argv[1] == "install":
install()
elif sys.argv[1] == "uninstall":
uninstall()
else:
usage()