Created
August 4, 2014 05:09
-
-
Save meoow/7d195bbcc283a6287d84 to your computer and use it in GitHub Desktop.
Remove pkg packages on Mac OS X
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 python2.7 | |
| import subprocess as subp | |
| import shutil | |
| import sys, os | |
| import pdb | |
| class Delpkg(object): | |
| pkgInfoCommand = 'pkgutil --pkg-info "{pkgname}"' | |
| pkgFilesCommand = 'pkgutil --files "{pkgname}" --only-files' | |
| pkgDirsCommand = 'pkgutil --files "{pkgname}" --only-dirs' | |
| def __init__(self,pkglist=[]): | |
| self.pkgsInfo = {} | |
| for i in pkglist: | |
| pkgInfo = self.getPkgInfo(i) | |
| if pkgInfo is not None: | |
| self.pkgsInfo[i] = pkgInfo | |
| def __call__(self): | |
| if os.getuid() != 0: | |
| for i in self.listFiles(): | |
| print i | |
| for i in self.listDirs(): | |
| print i | |
| else: | |
| for i in self.listFiles(): | |
| try: | |
| os.remove(i) | |
| except OSError: | |
| sys.stderr.write('Failed to remove "{0}"\n'.format(i)) | |
| for i in self.listDirs(): | |
| try: | |
| os.rmdir(i) | |
| except OSError: | |
| sys.stderr.write('Failed to remove "{0}"\n'.format(i)) | |
| for i in self.pkgsInfo: | |
| subp.call('pkgutil --forget "{0}"'.format(i),shell=True) | |
| def getPkgInfo(self,pkgname): | |
| runPkgInfoComm = subp.Popen(self.pkgInfoCommand.format(pkgname=pkgname),\ | |
| shell=True,stdout=subp.PIPE) | |
| out = runPkgInfoComm.communicate()[0] | |
| if runPkgInfoComm.returncode == 0: | |
| pkgInfo = dict(i.split(': ') for i in out.rstrip().split('\n')) | |
| pkgInfo['prefixpath'] = os.path.join(pkgInfo['volume'],pkgInfo['location']) | |
| return pkgInfo | |
| else: | |
| return None | |
| def generalListComm(self,command): | |
| for i in self.pkgsInfo: | |
| runComm = subp.Popen(command.format(pkgname=i),\ | |
| shell=True, stdout = subp.PIPE) | |
| out = runComm.communicate()[0].rstrip().split('\n') | |
| for j in out: | |
| yield os.path.join(self.pkgsInfo[i]['prefixpath'],j) | |
| def listDirs(self): | |
| dirs = [ i for i in self.generalListComm(self.pkgDirsCommand) ] | |
| dirs.sort(reverse=True) | |
| for i in dirs: | |
| yield i | |
| def listFiles(self): | |
| for i in self.generalListComm(self.pkgFilesCommand): | |
| yield i | |
| if __name__=='__main__': | |
| delpkg = Delpkg(sys.argv[1:]) | |
| #pdb.set_trace() | |
| delpkg() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment