-
Notifications
You must be signed in to change notification settings - Fork 0
/
the-purge.py
134 lines (110 loc) · 4.07 KB
/
the-purge.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
#!/bin/python3
'''
plugins go into /lib/python3.12/site-packages/dnf-plugins
or check your current version : python --version
https://github.com/acidburnmonkey/dnf-purge-command
'''
import time
import threading
import os
import shutil
import sys
import subprocess
import argparse
# the spinner thread
flag = threading.Event()
def spinner():
symbols = ['⣾', '⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽']
i = 0
while not flag.is_set():
i = (i + 1) % len(symbols)
print('\r\033[K%s Searching...' % symbols[i], flush=True, end='\r')
time.sleep(0.1)
# Clear the spinner line after the thread finishes
print(' ' * 20, end='\r')
t1 = threading.Thread(target=spinner)
parser = argparse.ArgumentParser(prog='Purge command',
description='Uninstalls programs via dnf , then searches and removes files and directories created by set programs in your Home directory',
epilog='End')
def set_argparser(parser):
parser.add_argument('packages', nargs='+',help=('packages to check'))
parser.add_argument('--nuke', dest='nuke', action='store_true' ,help=('Nuke option do not use , this will try to manually remove binaries and servises, Only takes 1 argument'))
# parser.add_argument('-v', '--verbose')
set_argparser(parser)
args = parser.parse_args()
def main():
"""Execute the util action here."""
user = os.getenv("SUDO_USER")
if user is None:
print("This program needs 'sudo'")
exit()
# Packsges arsgs here
pacages = args.packages
home = os.path.join('/home', os.getlogin())
show_user =[]
string_pack = []
exclude = set([])
string_pack.extend(pacages)
for index in pacages:
string_pack.append(index.capitalize())
string_pack.append(index.upper())
string_pack.append('.' + index)
string_pack.append('.' + index.upper())
string_pack.append('.' + index.capitalize())
#--nuke switch here
if args.nuke == True:
binary_locations=['/usr/local/bin','/usr/bin','/bin']
for binaries in binary_locations:
check = os.listdir(binaries)
if pacages[0] in check:
show_user.append(os.path.join(binaries,pacages[0]))
if os.path.exists(f'/etc/systemd/system/{pacages[0]}.service'):
show_user.append(f'/etc/systemd/system/{pacages[0]}.service')
#call DNF for uninstal
string_of_programs = ' '.join(pacages)
subprocess.run(f'dnf remove {string_of_programs}', shell=True)
t1.start()
# walk for directories
for root , directories , files in os.walk(home):
for directory in directories:
#packages loop
for pack in string_pack:
if pack == directory :
show_user.append(os.path.join(root,directory))
exclude.add(directory)
# walk for loose files
for root, dirs, files in os.walk(home, topdown=True):
[dirs.remove(d) for d in list(dirs) if d in exclude]
for file in files:
for pack in string_pack:
if pack == file:
show_user.append(os.path.join(root,file))
#stop spinner
flag.set()
t1.join()
print('\n' ,60 * '=')
# time to see what deletes
if len(show_user) < 1:
print("No remaining files found for purging")
sys.exit()
print('The following directories and files will be deleted')
print('🮶 ',*show_user ,sep='\n' )
print('🮵 ')
while True:
ask = str(input("Delete these files y/n :"))
if ask.lower() == 'y':
for root in show_user:
try:
shutil.rmtree(root)
except NotADirectoryError:
os.remove(root)
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
print("Files purged")
break
elif ask.lower() == 'n':
sys.exit()
else:
ask = str(input("Delete these files y/n :"))
if __name__ == '__main__':
main()