File: uninstall_wxPython.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (261 lines) | stat: -rwxr-xr-x 8,395 bytes parent folder | download | duplicates (3)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env python
"""
This script will search for installed versions of wxPython on OSX and
allow the user to choose one to uninstall.  It then will use the
metadata stored about the installed package to remove all the files
associated with that install.

Only the files installed by the main Installer Package will be
removed.  This includes the Python modules and the wxWidgets shared
libraries. If you also installed the demo or docs by dragging them out
of the disk image, then you will need to drag them to the Trash
yourself.
"""

import sys, os, glob
from fnmatch import fnmatchcase
import cPickle, urllib

RCPTDIR = "/Library/Receipts"
RSRCDIR = "Contents/Resources"

# Only completly clean out dirs that have one of these as a prefix.
# We do this because the file list returned from lsbom will include /,
# /usr, /usr/local, etc.
PREFIXES = [ '/Library/Python/2.3/',
             '/Library/Python/2.4/',
             '/Library/Python/2.5/',
             '/Library/Python/2.6/',
             '/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/',
             '/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/',
             '/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/',
             '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/',
             '/usr/local/lib/',
             ]

# The files that match one of the items in this list will only be
# removed if the last installation of wxPython on the system is being
# uninstalled.
COMMON_FILES = [ '/usr/local/bin/*',
                 'wx.pth',
                 'wxversion.py',
                 ]
    


class AccessError(Exception):
    pass

class ReceiptError(Exception):
    pass


class InstalledReceipt(object):
    def __init__(self, rcptPath):
        self.rcptPath = rcptPath
        self.rsrcPath = os.path.join(rcptPath, RSRCDIR)
        bf = glob.glob(os.path.join(self.rsrcPath, "*.bom"))
        if bf:
            self.bomFile = bf[0]
        else:
            print "WARNING: Unable to find %s/*.bom" % self.rsrcPath
            raise ReceiptError
        self.findMetaData()


    def findMetaData(self):
        # TODO: Make this be able to also look at Info.plist files
        infoFiles = glob.glob(os.path.join(self.rsrcPath, "*.info"))
        if infoFiles:
            # there should be only one
            infoFile = infoFiles[0]
            self.mdata = {}
            for line in open(infoFile, "r").readlines():
                line = line.strip()
                if line and line[0] != '#':
                    ls = line.split()
                    self.mdata[ls[0]] = line[len(ls[0])+1:]
        else:
            print "WARNING: Unable to find %s/*.info" % self.rsrcPath
            raise ReceiptError
            

    def getFileList(self):
        p = os.popen("lsbom -s %s" % self.bomFile, "r")
        data = p.read()
        data.strip()
        data = filter(lambda s: s!='' and s!='.', data.split('\n'))
        loc = self.mdata['DefaultLocation']
        return [loc+item for item in data]


    def walkFiles(self, handleFile, handleDir):
        dirs = []
        names = self.getFileList()

        # the plain files
        for name in names:
            name = os.path.abspath(name)
            if os.path.isdir(name):
                dirs.append(name)
            else:
                handleFile(name)

        # the directories
        dirs.reverse()
        for dir in dirs:
            for prefix in PREFIXES:
                if dir.startswith(prefix):
                    handleDir(dir)
                    break

        # Finally, remove the Receipts package, bottom-up
        for dirpath, dirname, filenames in os.walk(self.rcptPath, False):
            for name in filenames:
                name = os.path.join(dirpath, name)
                handleFile(name)
            handleDir(dirpath)

    # wxaddons should be always kept as the user may have installed
    # third-party modules seperate from wxpython.
    def testWxaddons(self, name):
        for prefix in PREFIXES:
            if name.startswith(prefix + "wxaddons"):
                return True
        return False

    def testCommon(self, name):
        for cmn in COMMON_FILES:
            if fnmatchcase(name, cmn) or fnmatchcase(os.path.basename(name), cmn):
                return True
        return False


    def showFiles(self):
        def show(name):
            if os.path.exists(name):
                if not self.lastInstall and self.testCommon(name):
                    return
                if self.testWxaddons(name):
                    return
                print "Will remove:", name
        self.walkFiles(show, show)


    def testUninstallAccess(self):
        def testFile(name):
            if os.path.exists(name):
                if not self.lastInstall and self.testCommon(name):
                    return
                if not os.access(name, os.W_OK):
                    raise AccessError(name)
        self.walkFiles(testFile, testFile)


    def doUninstall(self):
        def removeFile(name):
            if os.path.exists(name):
                if not self.lastInstall and self.testCommon(name):
                    return
                if self.testWxaddons(name):
                    return
                print "Removing:", name
                os.unlink(name)
        def removeDir(name):
            print "Removing:", name
            if os.path.exists(name) and not self.testWxaddons(name):
                hasFiles = os.listdir(name)
                if hasFiles:  # perhaps some stale symlinks, or .pyc files
                    for file in hasFiles:
                        fname = os.path.join(name, file)
                        if not os.path.isdir(fname):
                            os.unlink(fname)
                hasFiles = os.listdir(name)
                if not hasFiles:  # perhaps some stale symlinks, or .pyc files
                    os.rmdir(name)

        try:
            self.testUninstallAccess()
        except AccessError, e:
            print "UNABLE TO UNINSTALL!\nNo permission to remove: ", e.args[0]
            sys.exit()
            
        self.walkFiles(removeFile, removeDir)




def findInstalled():
    installed = []
    for name in glob.glob(os.path.join(RCPTDIR, "wxPython*")):
        try:
            ir = InstalledReceipt(name)
            installed.append(ir)
        except ReceiptError:
            pass  # just skip it...

    return installed


# Just in case a Python < 2.3 is used to run this
try:
    enumerate
except NameError:
    def enumerate(sequence):
        return zip(range(len(sequence)), sequence)


def main():
    # Need to handle case of starting this file using python
    # rather than starting this as an executable with ./
    if not sys.argv[0].startswith(r"./"):
        sys.argv[0] = "python %s" % sys.argv[0]
    if len(sys.argv) > 1 and sys.argv[1] == "-doit":
        inst = cPickle.loads(urllib.unquote(sys.argv[2]))
        inst.doUninstall()
        sys.exit()
        
    print __doc__
    installed = findInstalled()

    if not installed:
        print "*** No wxPython installations found!  ***"
        raw_input("Press RETURN...")
        sys.exit()
        
    for i, inst in enumerate(installed):
        print " %2d.  %-40s    %s" % (i+1, inst.mdata["Title"], inst.mdata["Version"])
        
    print 
    ans = raw_input("Enter the number of the install to examine or 'Q' to quit: ")
    if ans in ['Q', 'q']:
        sys.exit()
    inst = installed[int(ans) - 1]
    inst.lastInstall = len(installed) == 1

    while True:
        print
        print """
        Title:       %(Title)s
        Version:     %(Version)s
        Description: %(Description)s
        """ % inst.mdata

        ans = raw_input("(U)ninstall, (S)how what will be removed, or (Q)uit? [u,s,q] ")
        if ans in ['Q', 'q']:
            sys.exit()

        elif ans in ['S', 's']:
            inst.showFiles()

        elif ans in ['U', 'u']:
            print
            print "Launching uninstaller with sudo, please enter your password if prompted:"
            os.system("sudo %s -doit %s" %
                      (sys.argv[0],
                       urllib.quote(cPickle.dumps(inst))))
            sys.exit()
        
    
if __name__ == '__main__':
    main()