-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremover.pyw
More file actions
71 lines (61 loc) · 2.54 KB
/
Copy pathremover.pyw
File metadata and controls
71 lines (61 loc) · 2.54 KB
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
import shutil
from pathlib import Path
import getpass
import os
import psutil
SOURCE_DIR = Path(__file__).parent
username = getpass.getuser()
PROGRAMS_DIR = Path(rf"C:\Users\{username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs")
BLACKLIST_FILES = {"TERMS.txt", "LICENCE.txt", "NEWS.txt"}
BLACKLIST_EXTS = set() # fixato
BLACKLIST_DIRS = {"OpenFile", "__pycache__"}
def kill_python_processes():
try:
current_pid = os.getpid()
for proc in psutil.process_iter(attrs=['pid', 'name', 'exe']):
try:
if proc.info['name'] in ('python.exe', 'pythonw.exe') and proc.info['pid'] != current_pid:
if proc.info['exe'] and str(PROGRAMS_DIR) in proc.info['exe']:
proc.kill()
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
except Exception:
pass
def safe_remove_tree(src: Path, dst: Path):
try:
kill_python_processes()
src = src.resolve()
dst = dst.resolve()
for root, dirs, files in os.walk(src):
root_path = Path(root)
rel = Path(os.path.relpath(root_path, src))
target_root = dst / rel
target_root.mkdir(parents=True, exist_ok=True)
dirs[:] = [d for d in dirs if d not in BLACKLIST_DIRS]
for fname in files:
if fname in BLACKLIST_FILES:
continue
if Path(fname).suffix.lower() in BLACKLIST_EXTS:
continue
target_file = target_root / fname
if target_file.exists():
try:
if target_file.is_file():
target_file.unlink()
elif target_file.is_dir():
shutil.rmtree(target_file)
except Exception as e:
print(f"Error removing {target_file}: {e}")
# pulizia cartelle vuote
for root, dirs, _ in os.walk(dst, topdown=False):
for d in dirs:
dir_path = Path(root) / d
if not any(dir_path.iterdir()):
try:
dir_path.rmdir()
except Exception as e:
print(f"Error removing directory {dir_path}: {e}")
except Exception as e:
print(f"Error during removal: {e}")
if __name__ == "__main__":
safe_remove_tree(SOURCE_DIR, PROGRAMS_DIR)