~bzr/bazaar-mac-uninstaller/trunk

1 by Szilveszter Farkas (Phanatic)
Initial commit.
1
#!/usr/bin/env python
2
# Copyright (c) Szilveszter Farkas, 2008
3
# Bazaar bundle uninstaller (licensed under the GPLv2)
4
5
import os
6
import sys
7
8
if os.getuid() != 0:
9
    print "Please run this script with sudo."
10
    sys.exit(1)
11
12
from subprocess import Popen, PIPE
13
14
# Set default path
15
default_path = '/Library/Python/2.5/site-packages'
16
exclusion_path = {
17
    'bzrscripts': '/usr/local/bin'
18
}
19
20
# Fetch the list of possibly installed modules
21
bom_raw = Popen("ls /Library/Receipts/boms/org.pythonmac.bazaar*", shell=True, stdout=PIPE).communicate()[0]
22
bom_list = bom_raw.split('\n')
23
bom_list.remove('')
24
print "DEBUG: modules_list =", bom_list
25
26
# Go through the modules and delete the files
27
for bom in bom_list:
28
    root = ''
29
    for k in exclusion_path.iterkeys():
30
        if bom.find(k) > -1:
31
            root = exclusion_path[k]
32
    if len(root) == 0:
33
        root = default_path
34
    
35
    dirs = []
36
    
37
    files_raw = Popen(["lsbom", "-s", bom], stdout=PIPE).communicate()[0]
38
    files_list = files_raw.split('\n')
39
    files_list.remove('.')
40
    files_list.remove('')
41
    
42
    for f in files_list:
43
        f = f[2:]
44
        fullpath = os.path.join(root, f)
45
        if os.path.isdir(fullpath):
46
            dirs.append(f)
47
            continue
48
        try:
49
            os.remove(fullpath)
50
        except OSError, e:
51
            if e.errno == 2:
52
                pass
53
            else:
54
                raise
55
    
56
    for d in dirs:
57
        fullpath = os.path.join(root, d)
58
        if os.path.exists(fullpath):
59
            try:
60
                os.removedirs(fullpath)
61
            except OSError, e:
62
                if e.errno == 66:
63
                    print "WARNING: Directory not empty:", fullpath
64
                else:
65
                    raise
66
    
67
    os.remove(bom)