-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.py
195 lines (156 loc) · 5.92 KB
/
install.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python3 -u
import os
import sys
import json
import copy
import stat
import urllib.request
manifest_template = {
"name": "contextsearch_webext",
"description": "Launches external application",
"type": "stdio"
}
manifest_file = "contextsearch_webext.json"
binary_file = "ContextSearch.py"
bat_file = "ContextSearch.bat"
browsers_file = "browsers.json"
windows_install_path = "~\\AppData\\Roaming\\ContextSearch-webext\\"
nix_install_path = "~/ContextSearch-webext/"
install_global = False
repository_path = "https://raw.githubusercontent.com/ssborbis/ContextSearch-Native-App/master/"
def loadLocalThenRemote(local_path, remote_path):
try:
if os.path.exists(local_path):
return open(local_path).read()
else:
print(local_path, "No local file. Fetching remote ...")
response = urllib.request.urlopen(remote_path)
return response.read().decode("utf-8")
except Exception as e:
print("Cannot load", local_path, " - aborting install")
sys.exit(1)
def loadBrowsers():
return json.loads(loadLocalThenRemote(browsers_file, repository_path + browsers_file))
def loadBinary():
return loadLocalThenRemote(binary_file, repository_path + binary_file)
def installRegistryKey(key, manifest_path):
cmd = 'REG ADD %s /ve /t REG_SZ /d "%s" /f' % ( key, manifest_path )
print(cmd)
os.system(cmd)
def uninstallRegistryKey(key):
cmd = 'REG DELETE %s /va /f' % ( key )
print(cmd)
os.system(cmd)
def installBinary(path):
try:
os.mkdir(path)
except OSError as error:
pass
if not os.path.isdir(path):
print("Unable to create directory", path)
return
try:
with open(os.path.join(path, binary_file), "w" ) as f:
f.write(loadBinary())
except OSError as error:
print(error)
sys.exit(1)
try:
st = os.stat(os.path.join(path, binary_file))
os.chmod(os.path.join(path, binary_file), st.st_mode | stat.S_IEXEC)
except OSError as error:
print(error)
def installManifest(platform):
for b in loadBrowsers():
print()
path = os.path.expanduser(b["platforms"][platform]["user_config_path"])
path_browser_specific = os.path.join(path, b["name"])
parent_path = os.path.abspath(os.path.join(path, os.pardir))
if os.path.isdir(parent_path):
print('Installing for', b["name"])
print()
try:
os.mkdir(path)
except OSError as error:
pass
if platform == "windows":
if not os.path.isdir(path):
continue
try:
os.mkdir(path_browser_specific)
except OSError as error:
pass
if not os.path.isdir(path_browser_specific):
continue
manifest = copy.deepcopy(manifest_template)
manifest.update(b["manifest"])
if platform == "windows":
manifest_path = os.path.join(path_browser_specific, manifest_file)
else:
manifest_path = os.path.join(path, manifest_file)
if platform == "windows":
manifest["path"] = os.path.join(os.path.expanduser(windows_install_path), bat_file)
else:
manifest["path"] = os.path.join(os.path.expanduser(nix_install_path), binary_file)
try:
print(manifest_path)
print()
with open( manifest_path, 'w', encoding='utf-8') as f:
json.dump(manifest, f, ensure_ascii=False, indent=4)
f.close()
except OSError as error:
print(error)
if platform == "windows":
for key in b["platforms"][platform]["registry_keys_user"]:
installRegistryKey(key, manifest_path)
def getPlatform():
if sys.platform == "linux" or sys.platform == "linux2":
return "linux"
elif sys.platform == "darwin":
return "macOS"
elif sys.platform == "win32":
return "windows"
if len(sys.argv) == 2 and sys.argv[1] == "--uninstall":
print("Removing ContextSearch web-ext Native App")
if getPlatform() == "windows":
# remove registry keys
for b in loadBrowsers():
for key in b["platforms"]["windows"]["registry_keys_user"]:
print("removing registry key", key)
uninstallRegistryKey(key)
# remove app folder
app_folder = os.path.expanduser(windows_install_path)
try:
import shutil
shutil.rmtree(app_folder)
except OSError as e:
print("Error: %s : %s" % (app_folder, e.strerror))
else:
# remove manifests
for b in loadBrowsers():
delete_path = os.path.expanduser(b["platforms"][getPlatform()]["user_config_path"] + manifest_file)
if os.path.exists(delete_path):
os.remove(delete_path)
# remove app folder
app_folder = os.path.expanduser(nix_install_path)
try:
import shutil
shutil.rmtree(app_folder)
except OSError as e:
print("Error: %s : %s" % (app_folder, e.strerror))
sys.exit(0)
if getPlatform() == "linux":
installManifest("linux")
installBinary(os.path.expanduser(nix_install_path))
elif getPlatform() == "macOS":
installManifest("macOS")
installBinary(os.path.expanduser(nix_install_path))
elif getPlatform() == "windows":
installManifest("windows")
installBinary(os.path.expanduser(windows_install_path))
path = os.path.expanduser(windows_install_path)
bat_path = os.path.join(path, bat_file)
with open( bat_path, 'w' ) as f:
f.write("@echo off\r\n")
f.write("\"" + sys.executable + "\" -u " + binary_file + "\r\n")
sys.exit(0)