Skip to content

Instantly share code, notes, and snippets.

@beckermr
Last active April 10, 2020 12:32
Show Gist options
  • Save beckermr/324328c2e3db944c3e02919b5611a4f9 to your computer and use it in GitHub Desktop.
Save beckermr/324328c2e3db944c3e02919b5611a4f9 to your computer and use it in GitHub Desktop.
homebrew-rename
#!/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