Skip to content

Instantly share code, notes, and snippets.

@habnabit
Created February 26, 2013 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save habnabit/5036537 to your computer and use it in GitHub Desktop.
Save habnabit/5036537 to your computer and use it in GitHub Desktop.
purge packages since pkgutil can't
#!/usr/bin/python
import subprocess
import plistlib
import os.path
import errno
def check_output(cmd, *a, **kw):
proc = subprocess.Popen(cmd, *a, **kw)
stdout, stderr = proc.communicate()
if proc.returncode:
raise subprocess.CalledProcessError(proc.returncode, cmd, stderr)
return stdout, stderr
def soft_delete(path):
try:
try:
os.remove(path)
except OSError, e:
if e.errno != errno.EPERM:
raise
os.rmdir(path)
except OSError, e:
if e.errno != errno.ENOENT:
raise
def main(pkg):
print 'purging %r' % (pkg,)
info_string, _ = check_output(['pkgutil', '--pkg-info-plist', pkg], stdout=subprocess.PIPE)
info = plistlib.readPlistFromString(info_string)
base_path = os.path.join(info['volume'], info['install-location'])
file_paths, _ = check_output(['pkgutil', '--files', pkg], stdout=subprocess.PIPE)
for file_path in reversed(file_paths.splitlines()):
full_path = os.path.join(base_path, file_path)
print 'removing %r' % (full_path,)
soft_delete(full_path)
subprocess.check_call(['pkgutil', '--forget', pkg])
import sys
for pkg in sys.argv[1:]:
main(pkg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment