Last active
December 8, 2020 23:15
-
-
Save Monsterovich/363a219551e07ecfc14169c765ae13b2 to your computer and use it in GitHub Desktop.
cmake_uninstall script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
import os | |
import click | |
if len(sys.argv) < 2: | |
exit("Usage: cmake_uninstall <install_manifest.txt>") | |
if os.geteuid() != 0: | |
exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.") | |
file = open(sys.argv[1], 'r') | |
content = file.readlines() | |
print("The following files will be REMOVED:\n") | |
for line in content: | |
print("\t" + line, end = "") | |
print("") | |
if click.confirm('Do you want to continue?', default = False): | |
for line in content: | |
try: | |
os.remove(line.rstrip()) | |
except FileNotFoundError: | |
print("File doesn't exist: " + line.rstrip()) | |
exit("Aborting.") | |
path = os.path.commonpath([line.rstrip() for line in content]) | |
print("Removing empty directories in " + path + "\n") | |
empty_dirs = [] | |
while True: | |
for root, dirs, files in os.walk(path): | |
if not len(dirs) and not len(files): | |
empty_dirs.append(root) | |
if len(empty_dirs) == 0: | |
break | |
for dir in empty_dirs: | |
print("\t" + dir) | |
os.rmdir(dir) | |
empty_dirs = [] | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment