forked from Stepsize/jupyter_search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.py
150 lines (136 loc) · 5.95 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
import os
import sys
import json
import shutil
import datetime
from notebook.services.config import ConfigManager
from jupyter_core.paths import jupyter_config_dir
NB_EXT_DIR = os.path.join(os.path.expanduser('~'), '.ipython/nbextensions')
CUR_NB_EXT = os.path.join(NB_EXT_DIR, 'stepsize_nb-ext')
NB_CONFIG = os.path.join(ConfigManager()._config_dir_default(),
'notebook.json')
SERVER_EXT_DIR = os.path.join(os.path.expanduser('~'), '.ipython/extensions')
CUR_SERVER_EXT = os.path.join(SERVER_EXT_DIR, 'stepsize_server-ext')
SERVER_CONFIG = os.path.join(jupyter_config_dir(), 'jupyter_config.py')
SERVER_EXT_CONFIG = [
"c.NotebookApp.allow_credentials = True",
"c.NotebookApp.allow_origin_pat = "
"'(http\://localhost:[0-9]{4}.*|http\://127\.0\.0\.1:[0-9]{4}.*)'",
"sys.path.append(os.path.join(os.path.expanduser('~'), "
"'.ipython/extensions/stepsize_server-ext'))",
"if isinstance(c.NotebookApp.server_extensions, list):",
" c.NotebookApp.server_extensions.append('server_ext')",
"else:",
" c.NotebookApp.server_extensions = []",
" c.NotebookApp.server_extensions.append('server_ext')"
]
def main():
remove_extension(NB_EXT_DIR, CUR_NB_EXT)
remove_extension(SERVER_EXT_DIR, CUR_SERVER_EXT)
remove_notebook_config(NB_CONFIG)
remove_server_config(SERVER_CONFIG, SERVER_EXT_CONFIG)
remove_egg()
print("Stepsize uninstallation complete")
def remove_extension(DIR, CUR_EXT):
# Removes the installed stepsize extension
if os.path.exists(CUR_EXT):
try:
shutil.rmtree(CUR_EXT)
print("OUTCOME: Removed the Stepsize extension from your " \
"%s directory" % (DIR))
except:
print("WARNING: Unable to uninstall the extension in your " \
"%s directory" % (DIR))
print("ERROR: %s, at line %d" % (sys.exc_info()[0],
sys.exc_traceback.tb_lineno))
else:
print("WARNING: Unable to uninstall the Stepsize extension since it " \
"does not exist in %s" % (DIR))
def remove_notebook_config(CONFIG):
# Removes the config in the notebook json
if os.path.isfile(CONFIG):
# Creates a backup file
timestamp = datetime.datetime.now().strftime("%d-%m-%Y_T%H-%M-%S")
title = ('backup_notebook_' + timestamp + '.json')
BACKUP = os.path.join(ConfigManager()._config_dir_default(), title)
print("ACTION: Creating a backup notebook.json, %s" % (BACKUP))
shutil.copyfile(CONFIG, BACKUP)
# Element to be removed
data_element = {'stepsize_nb-ext/main_v0-2': True}
# Counter
count = 0
try:
if open(CONFIG).read() != "":
with open(CONFIG) as f:
entries = json.load(f)
for ext in entries['load_extensions'].keys():
if 'stepsize_nb-ext' in ext:
entries['load_extensions'].pop(ext)
count += 1
with open(CONFIG, 'w') as outfile:
json.dump(entries, outfile)
if count > 0:
print("OUTCOME: Removed the Stepsize extension "
"configuration from the notebook.json")
except:
print("WARNING: An error occured when trying to remove %s from "
"the notebook.json" % (data_element))
print("ERROR: %s, at line %d" % (sys.exc_info()[0],
sys.exc_traceback.tb_lineno))
else:
print("WARNING: Unable to remove the Stepsize extension "
"configuration since the notebook.json does not exist in %s"
% (ConfigManager()._config_dir_default()))
def remove_server_config(CONFIG, EXT_CONFIG):
# Removes the config in the jupyter_config.py
if os.path.isfile(CONFIG):
# Creates a backup file
timestamp = datetime.datetime.now().strftime("%d-%m-%Y_T%H-%M-%S")
title = ('backup_config_' + timestamp + '.py')
BACKUP = os.path.join(jupyter_config_dir(), title)
print("ACTION: Creating a backup jupter_config.py, %s" % (BACKUP))
shutil.copyfile(CONFIG, BACKUP)
# Counter
count = 0
try:
f = open(CONFIG, 'r')
lines = f.readlines()
f.close()
f = open(CONFIG, 'w')
for line in lines:
if any(i in line for i in EXT_CONFIG):
pass
count += 1
else:
f.write(line)
f.truncate()
f.close()
if count > 0:
print("OUTCOME: Removed the Stepsize extension " \
"configuration from the jupyter_config.py")
except:
print("WARNING: An error occured when trying to remove the " \
"Stepsize server configuration from the jupyter_config.py")
print("ERROR: %s, at line %d" % (sys.exc_info()[0],
sys.exc_traceback.tb_lineno))
else:
print("WARNING: Unable to remove the Stepsize extension " \
"configuration since the jupyter_config.py does not exist in " \
"%s" % (jupyter_config_dir()))
def remove_egg():
# Removes the Stepsize.egg-info
EGG_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'Stepsize.egg-info')
if os.path.exists(EGG_DIR):
try:
shutil.rmtree(EGG_DIR)
print("OUTCOME: Removed the Stepsize.egg-info")
except:
print("WARNING: Unable to remove the Stepsize.egg-info")
print("ERROR: %s, at line %d" % (sys.exc_info()[0],
sys.exc_traceback.tb_lineno))
else:
print("WARNING: Unable to remove the Stepsize.egg-info since it " \
"does not exist in the directory")
if __name__ == '__main__':
main()