-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistmanager-code-remover
128 lines (115 loc) · 4.91 KB
/
histmanager-code-remover
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
#!/usr/bin/python3
import time
import os
from shutil import copyfile
dbh = os.path.expanduser('~/.bash_history') # default bash history
fp = os.path.expanduser('~/.khist/') # filepath
def clear_rc():
"""removes the call to histmanager from the .bashrc file"""
f = open(os.path.expanduser("~/.bashrc"), 'r')
lines = f.readlines()
f.close()
try:
lines.remove('sh ~/.khist/histmanager.sh # Added by HistManager\n')
except Exception:
pass
f = open(os.path.expanduser("~/.bashrc"), 'w')
for l in lines:
f.write(l)
f.close()
def restore_hist():
"""[misnomer] creates two (complete) history backup files, after updating Master"""
if (os.path.isfile(dbh)):
f = open(dbh, 'r')
lines = f.readlines()
f.close()
f = open(fp + 'history/Master.khtxt', 'a')
for l in lines:
f.write(l)
f.close()
tm = time.localtime(time.time())
year = tm[0]
month = tm[1]
day = tm[2]
copyfile(fp + 'history/Master.khtxt', dbh + '_backup_master')
copyfile(fp + 'history/Master.khtxt', os.path.expanduser('~/') + 'BashHistoryBackup_' + str(tm[0]) + '-' + str(tm[1]) + '-' + str(tm[2]) + '_' + str(tm[3]) + ':' + str(tm[4]) + ':' + str(tm[5]) + '.txt')
def relocate_backups():
"""creates two (possibly incomplete) history backup files - in case the others couldn't be created"""
if (os.path.isfile(fp + ".bash_history_khbackup")):
os.rename(fp + ".bash_history_khbackup", dbh + '_backup_khbackup')
if (os.path.isfile(fp + "history/BashHistBackup.khtxt")):
os.rename(fp + "history/BashHistBackup.khtxt", dbh + '_backup_bashhistbackup')
def dumperror(exc):
"""
prints the stack trace to the errorlog file
(BaseException) exc: The Exception/BaseException that occured
"""
try:
tm = time.localtime(time.time())
timestamp = str(tm[0]) + '-' + str(tm[1]) + '-' + str(tm[2]) + ' ' + str(tm[3]) + ':' + str(tm[4]) + ':' + str(tm[5])
f = open(fp + 'errorlog.khtxt', 'a')
f.write("\n\n\n")
f.write("============================== " + timestamp + " ==============================\n\n")
f.write(str(type(e)))
f.write("\n\n")
f.write(traceback.format_exc())
f.write("\n============================== " + timestamp + " ==============================\n\n\n\n")
f.close()
except:
print('')
print("-----------------------------------------------------------------------")
print("Unknown UninstallationError - Please Report it to histmanager@gmail.com")
print("-----------------------------------------------------------------------")
print('')
traceback.print_exc()
def uninstall():
"""error proofed program flow control function"""
# seperate try/catch blocks per command, to ensure all are tried upon exception occurence
try:
clear_rc()
except Exception as e:
dumperror(e)
except BaseException as e:
dumperror(e)
except:
print("-------------------------------------------------------------------")
print("Unknown RestoreDefError - Please Report it to histmanager@gmail.com") #restoreDefaultTerminalHistory error
print("-------------------------------------------------------------------")
try:
restore_hist()
except Exception as e:
dumperror(e)
except BaseException as e:
dumperror(e)
except:
print("----------------------------------------------------------------")
print("Unknown CBackupError - Please Report it to histmanager@gmail.com") #completeBackup error
print("----------------------------------------------------------------")
try:
relocate_backups()
except Exception as e:
dumperror(e)
except BaseException as e:
dumperror(e)
except:
print("----------------------------------------------------------------")
print("Unknown iBackupError - Please Report it to histmanager@gmail.com") #incompeteBackup error
print("----------------------------------------------------------------")
def main():
uninstall()
if __name__ == '__main__':
main()
# HistManager - Effectively Manage Terminal History
# Copyright (C) 2016 Kaivalya Rawal (histmanager@gmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.