~bzr/bazaar-mac-uninstaller/trunk

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
#!/usr/bin/env python
# Copyright (c) Szilveszter Farkas, 2008
# Bazaar bundle uninstaller (licensed under the GPLv2)

import os
import sys

if os.getuid() != 0:
    print "Please run this script with sudo."
    sys.exit(1)

from subprocess import Popen, PIPE

# Set default path
default_path = '/Library/Python/2.5/site-packages'
exclusion_path = {
    'bzrscripts': '/usr/local/bin'
}

# Fetch the list of possibly installed modules
bom_raw = Popen("ls /Library/Receipts/boms/org.pythonmac.bazaar*", shell=True, stdout=PIPE).communicate()[0]
bom_list = bom_raw.split('\n')
bom_list.remove('')
print "DEBUG: modules_list =", bom_list

# Go through the modules and delete the files
for bom in bom_list:
    root = ''
    for k in exclusion_path.iterkeys():
        if bom.find(k) > -1:
            root = exclusion_path[k]
    if len(root) == 0:
        root = default_path
    
    dirs = []
    
    files_raw = Popen(["lsbom", "-s", bom], stdout=PIPE).communicate()[0]
    files_list = files_raw.split('\n')
    files_list.remove('.')
    files_list.remove('')
    
    for f in files_list:
        f = f[2:]
        fullpath = os.path.join(root, f)
        if os.path.isdir(fullpath):
            dirs.append(f)
            continue
        try:
            os.remove(fullpath)
        except OSError, e:
            if e.errno == 2:
                pass
            else:
                raise
    
    for d in dirs:
        fullpath = os.path.join(root, d)
        if os.path.exists(fullpath):
            try:
                os.removedirs(fullpath)
            except OSError, e:
                if e.errno == 66:
                    print "WARNING: Directory not empty:", fullpath
                else:
                    raise
    
    os.remove(bom)