~abentley/bzrtools/bzrtools.dev

118 by aaron.bentley at utoronto
Added clean-tree command
1
import errno
2
import os
3
import shutil
4
from bzrlib.branch import Branch
5
6
def clean_tree(unknowns=True, ignored=False):
7
    def deletables(tree):
8
        for subp in tree.extras():
9
            if tree.is_ignored(subp):
10
                if ignored:
11
                    yield tree.abspath(subp)
12
            else:
13
                if unknowns:
14
                    yield tree.abspath(subp)
15
    br = Branch('.')
16
    tree = br.working_tree()
17
    printed_once = False
18
    for path in deletables(tree):
19
        if not printed_once:
20
            print "deletings paths:"
21
            printed_once = True
22
        print ' ', path
23
        try:
24
            os.unlink(path)
25
        except OSError, e:
26
            if e.errno != errno.EISDIR:
27
                raise
28
            shutil.rmtree(path)