~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Aaron Bentley
  • Date: 2008-04-11 00:03:51 UTC
  • Revision ID: aaron@aaronbentley.com-20080411000351-dmbvgmanygnphzgp
Add escaping to HTML output

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
import errno
17
17
import os
18
18
import shutil
19
 
from bzrlib.branch import Branch
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
 
19
import sys
 
20
 
 
21
from bzrlib.osutils import has_symlinks, isdir
 
22
from bzrlib.trace import note
 
23
from bzrlib.workingtree import WorkingTree
 
24
 
 
25
 
 
26
def is_detritus(subp):
 
27
    """Return True if the supplied path is detritus, False otherwise"""
 
28
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
 
29
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
 
30
 
 
31
 
 
32
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
 
33
    """Iterate through files that may be deleted"""
 
34
    for subp in tree.extras():
 
35
        if detritus and is_detritus(subp):
 
36
            yield tree.abspath(subp), subp
 
37
            continue
 
38
        if tree.is_ignored(subp):
 
39
            if ignored:
 
40
                yield tree.abspath(subp), subp
 
41
        else:
 
42
            if unknown:
 
43
                yield tree.abspath(subp), subp
 
44
 
 
45
 
 
46
def clean_tree(directory, unknown=False, ignored=False, detritus=False, 
 
47
               dry_run=False):
 
48
    """Remove files in the specified classes from the tree"""
 
49
    tree = WorkingTree.open_containing(directory)[0]
 
50
    tree.lock_read()
 
51
    try:
 
52
        deletables = iter_deletables(tree, unknown=unknown, ignored=ignored,
 
53
                                     detritus=detritus)
 
54
        delete_items(deletables, dry_run=dry_run)
 
55
    finally:
 
56
        tree.unlock()
 
57
 
 
58
 
 
59
def delete_items(deletables, dry_run=False):
 
60
    """Delete files in the deletables iterable"""
 
61
    has_deleted = False
 
62
    for path, subp in deletables:
 
63
        if not has_deleted:
 
64
            note("deleting paths:")
 
65
            has_deleted = True
 
66
        note('  ' + subp)
 
67
        if not dry_run:
 
68
            if isdir(path):
 
69
                shutil.rmtree(path)
27
70
            else:
28
 
                if not unknowns:
29
 
                    continue
30
 
            yield tree.abspath(subp), subp
31
 
    br = Branch('.')
32
 
    tree = br.working_tree()
33
 
    printed_once = False
34
 
    for path, subp in deletables(tree):
35
 
        if not printed_once:
36
 
            print "deletings paths:"
37
 
            printed_once = True
38
 
        print ' ', subp
39
 
        try:
40
 
            os.unlink(path)
41
 
        except OSError, e:
42
 
            if e.errno != errno.EISDIR:
43
 
                raise
44
 
            shutil.rmtree(path)
 
71
                os.unlink(path)
 
72
    if not has_deleted:
 
73
        note("No files deleted.")