Last active
April 10, 2020 12:32
-
-
Save beckermr/324328c2e3db944c3e02919b5611a4f9 to your computer and use it in GitHub Desktop.
homebrew-rename
This file contains hidden or 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/env python | |
from __future__ import print_function | |
import os | |
import subprocess | |
import shutil | |
import uuid | |
import sys | |
MANGLE_STR = 'h' + uuid.uuid4().hex[0:6] | |
def _mangele_path(pth): | |
parts = os.path.split(pth) | |
new_parts = [parts[0], parts[1]] | |
new_parts[1] = MANGLE_STR + "_" + parts[1] + "_" + MANGLE_STR | |
return os.path.join(*new_parts) | |
proc_out = subprocess.check_output( | |
["./uninstall_homebrew", "--dry-run"], | |
stderr=subprocess.STDOUT | |
) | |
try: | |
proc_out = proc_out.decode("utf-8") | |
except Exception: | |
pass | |
for line in proc_out.splitlines(): | |
if "->" in line: | |
parts = line.split("->") | |
else: | |
parts = [line] | |
for p in parts: | |
p = p.strip() | |
if os.path.exists(p) and os.path.isfile(p): | |
mangled_p = _mangele_path(p) | |
try: | |
shutil.move(p, mangled_p) | |
print("MOVED %s -> %s" % (p, mangled_p)) | |
except shutil.Error: | |
try: | |
os.remove(p) | |
print("REMOVED %s " % p) | |
except Exception as e: | |
print("ERROR moving or removing %s: %s" % (p, repr(e))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment