-
Notifications
You must be signed in to change notification settings - Fork 1
/
poof.py
executable file
·179 lines (147 loc) · 4.68 KB
/
poof.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Poof: List and uninstall/remove macOS packages
# Copyright (c) 2011-2017 Rudá Moura <[email protected]>
#
"""Poof is a command line utility to list and uninstall/remove macOS packages.
*NO WARRANTY* DON'T BLAME ME if you destroy your installation!
NEVER REMOVE com.apple.* packages unless you know what you are doing.
How it works:
It first removes all files and directories declared by the package and
then forget the metadata (the receipt data).
Usage:
List packages (but ignore all from Apple).
$ ./poof.py | grep -v com.apple.pkg
com.accessagility.wifiscanner
com.adobe.pkg.FlashPlayer
com.amazon.Kindle
com.christiankienle.CoreDataEditor
com.ea.realracing2.mac.bv
com.google.pkg.GoogleVoiceAndVideo
com.google.pkg.Keystone
com.Growl.GrowlHelperApp
com.lightheadsw.caffeine
com.Logitech.Control Center.pkg
...
Remove FlashPlayer (com.adobe.pkg.FlashPlayer).
$ sudo ./poof.py com.adobe.pkg.FlashPlayer
...
Forgot package 'com.adobe.pkg.FlashPlayer' on '/'.
"""
from subprocess import Popen, PIPE
import sys
import os
class Shell(object):
def __getattribute__(self, attr):
return Command(attr)
class Command(object):
def __init__(self, command):
self.command = command
def __call__(self, params=None):
args = [self.command]
if params:
args += params.split()
return self.run(args)
def run(self, args):
p = Popen(args, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
out, err = out.strip(), err.strip()
if sys.version_info >= (3, 0):
out, err = str(out, 'utf-8'), str(err, 'utf-8')
if p.returncode == 0:
return True, out.split('\n')
else:
return False, err.split('\n')
def package_list():
sh = Shell()
sts, out = sh.pkgutil('--pkgs')
return out
def package_info(package_id):
sh = Shell()
ok, info = sh.pkgutil('--pkg-info ' + package_id)
if ok == False:
raise IOError('Unknown package or name mispelled')
info = [x.split(': ') for x in info]
return dict(info)
def package_files(package_id):
sh = Shell()
ok, files = sh.pkgutil('--only-files --files ' + package_id)
ok, dirs = sh.pkgutil('--only-dirs --files ' + package_id)
# Guess AppStore receipt
for dir in dirs:
if dir.endswith('.app'):
dirs.append(dir + '/Contents/_MASReceipt')
files.append(dir + '/Contents/_MASReceipt/receipt')
break
return files, dirs
def package_forget(package_id):
sh = Shell()
ok, msg = sh.pkgutil('--verbose --forget ' + package_id)
return msg
def package_remove(package_id, force=True, verbose=False):
try:
info = package_info(package_id)
except IOError as e:
print("%s: '%s'" % (e, package_id))
return False
prefix = info['volume']
if info['location']:
prefix += info['location'] + os.sep
files, dirs = package_files(package_id)
files = [prefix + x for x in files]
clean = True
for path in files:
try:
os.remove(path)
except OSError as e:
clean = False
print(e)
dirs = [prefix + x for x in dirs]
kcmp = lambda p1, p2: p1.count('/') - p2.count('/')
if sys.version_info >= (3, 0):
dirs.sort(key=cmp_to_key(kcmp), reverse=True)
else:
dirs.sort(kcmp, reverse=True)
for dir in dirs:
try:
os.rmdir(dir)
if verbose:
print('Removing', dir)
except OSError as e:
clean = False
print(e)
if force or clean:
msg = package_forget(package_id)
print(msg[0])
return clean
# From https://docs.python.org/3/howto/sorting.html
def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class K:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K
def main(argv=None):
if argv == None:
argv = sys.argv
if len(argv) == 1:
for pkg in package_list():
print(pkg)
for arg in argv[1:]:
package_remove(arg)
return 0
if __name__ == '__main__':
sys.exit(main())