This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
uninstall.py
202 lines (153 loc) · 5.82 KB
/
uninstall.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
196
197
198
199
200
201
202
#
# Copyright (C) 2020 IBM. All Rights Reserved.
#
# See LICENSE.txt file in the root directory
# of this source tree for licensing information.
#
# pylint: disable=no-name-in-module,import-error
import getpass
import io
import json
import os
import sys
from distutils.dir_util import remove_tree
from clai.datasource.config_storage import ConfigStorage
from clai.datasource.stats_tracker import StatsTracker
from clai.server.agent_datasource import AgentDatasource
from clai.tools.anonymizer import Anonymizer
from clai.tools.console_helper import print_complete, print_error
from clai.tools.file_util import get_rc_file, get_setup_file, get_rc_files, is_rw_with_EBCDIC
def remove(path):
print("cleaning %s" % path)
try:
remove_tree(path)
except OSError:
print("folder not found")
def remove_system_folder(path: str = None):
if path is not None:
# path to the clai dir in a local user installation
default_system_destdir = "/".join(path.split("/")[0:-1])
else:
default_system_destdir = os.path.join(
os.path.expanduser("/opt/local/share"),
"clai",
)
remove(default_system_destdir)
def clai_installed(rc_file_path):
path = os.path.expanduser(rc_file_path)
if os.path.isfile(path):
line_to_search = "export CLAI_PATH="
print("searching %s" % line_to_search)
lines = io.open(path, "r",
encoding="utf-8",
errors="ignore").readlines()
lines_found = list(filter(lambda line: line_to_search in line, lines))
if lines_found:
my_path = lines_found[0].replace(line_to_search, "").replace("\n", "").strip()
return my_path
return None
def is_root_user():
return os.geteuid() == 0
# pylint: disable=bare-except
def read_users(bin_path):
try:
with open(bin_path + "/usersInstalled.json") as file:
users = json.load(file)
return users
except:
return []
def unregister_the_user(bin_path):
users = read_users(bin_path)
user_path = os.path.expanduser(get_rc_file())
if user_path in users:
users.remove(user_path)
with open(bin_path + "/usersInstalled.json", "w") as json_file:
json.dump(users, json_file)
return users
def stat_uninstall(bin_path):
agent_datasource = AgentDatasource(config_storage=ConfigStorage(alternate_path=f"{bin_path}/configPlugins.json"))
report_enable = agent_datasource.get_report_enable()
stats_tracker = StatsTracker(sync=True, anonymizer=Anonymizer(alternate_path=f"{bin_path}/anonymize.json"))
stats_tracker.report_enable = report_enable
login = getpass.getuser()
stats_tracker.log_uninstall(login)
print("record uninstall")
def remove_setup_file(rc_file_path):
path = os.path.expanduser(rc_file_path)
os.remove(path)
def remove_between(rc_file_path, start, end):
path = os.path.expanduser(rc_file_path)
if os.path.isfile(path):
codeset = "utf-8"
lines = []
if is_rw_with_EBCDIC(path):
# The open() with encoding cp1047 (IBM-1047), working as cp037
# (IBM-037), doesn't identify the newline correctly. Use \x85 to
# identify the newline.
codeset = "cp1047"
newline = '\x85'
for err_line in open(path, "r", encoding=codeset, errors="ignore").readlines():
right_line_list = err_line.split(newline)
length = len(right_line_list)
i = 0
for right_line in right_line_list:
i = i + 1
# not empty element or not the last emtpy element
if right_line or length != i:
lines.append(right_line + newline)
else:
lines = open(path, "r",
encoding=codeset,
errors="ignore").readlines()
remove_line = False
lines_after_remove = []
for line in lines:
if line.strip() == start.strip():
remove_line = True
if not remove_line:
lines_after_remove.append(line)
if line.strip() == end.strip():
remove_line = False
io.open(path, "w", encoding=codeset).writelines(lines_after_remove)
def remove_lines_setup(rc_file_path):
remove_between(rc_file_path, "# CLAI setup\n", "# End CLAI setup\n")
def remove_setup_register():
rc_files = get_rc_files()
for file in rc_files:
remove_lines_setup(file)
def is_user_install(bin_path):
plugins_config = ConfigStorage(
alternate_path=f"{bin_path}/configPlugins.json"
).read_config(None).user_install
return plugins_config
def execute(args):
bin_path = os.getenv('CLAI_PATH', None)
if "-h" in args or "--help" in args:
print(
"usage: uninstall.py [-h] [--help] [--user]\n \
\nUninstall CLAI.\n \
\noptional arguments: \
\n-h, --help show this help message and exit \
\n--user Uninstalls a local installation of clai for the current user"
)
sys.exit(0)
path = clai_installed(get_setup_file())
print(f"path= {path}")
print(f"bin path= {bin_path}")
if not path and bin_path is None:
print_error("CLAI is not installed.")
sys.exit(1)
if not bin_path:
bin_path = path
stat_uninstall(path)
users = unregister_the_user(path)
if "--user" in args or is_user_install(bin_path):
remove_system_folder(bin_path)
elif not users:
remove_system_folder()
remove_setup_file(get_setup_file())
remove_setup_register()
print_complete("CLAI has been uninstalled correctly, you will need to restart your shell.")
return 0
if __name__ == "__main__":
sys.exit(execute(sys.argv))