~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: abentley
  • Date: 2005-10-15 01:00:06 UTC
  • Revision ID: abentley@lappy-20051015010006-ed880d280fb52391
Added --dry-run, --detrius options to clean-tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import shutil
19
19
from bzrlib.branch import Branch
20
20
 
21
 
def clean_tree(unknowns=True, ignored=False):
22
 
    def deletables(tree):
23
 
        for subp in tree.extras():
24
 
            if tree.is_ignored(subp):
25
 
                if not ignored:
26
 
                    continue
27
 
            else:
28
 
                if not unknowns:
29
 
                    continue
 
21
def is_detrius(subp):
 
22
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
 
23
        subp.endswith('.OTHER') or subp.endswith('~')
 
24
 
 
25
def iter_deletables(tree, unknowns=True, ignored=False, detrius=False):
 
26
    """Iterate through files that may be deleted"""
 
27
    for subp in tree.extras():
 
28
        if is_detrius(subp):
30
29
            yield tree.abspath(subp), subp
31
 
    br = Branch.open_containing('.')
32
 
    tree = br.working_tree()
 
30
        if tree.is_ignored(subp):
 
31
            if ignored:
 
32
                yield tree.abspath(subp), subp
 
33
        else:
 
34
            if unknowns:
 
35
                yield tree.abspath(subp), subp
 
36
 
 
37
 
 
38
def clean_tree(deletables, dry_run=False):
 
39
    """Delete files in the deletables iterable"""
33
40
    printed_once = False
34
 
    for path, subp in deletables(tree):
 
41
    for path, subp in deletables:
35
42
        if not printed_once:
36
43
            print "deleting paths:"
37
44
            printed_once = True
38
45
        print ' ', subp
39
 
        if os.path.isdir(path):
40
 
            shutil.rmtree(path)
41
 
        else:
42
 
            os.unlink(path)
 
46
        if not dry_run:
 
47
            if os.path.isdir(path):
 
48
                shutil.rmtree(path)
 
49
            else:
 
50
                os.unlink(path)