~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Aaron Bentley
  • Date: 2007-01-17 14:30:19 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070117143019-rj3n4op1aqav7bs5
Update versions to 0.15

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 is_detrius(subp):
 
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"""
22
28
    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):
 
29
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
 
30
 
 
31
 
 
32
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
26
33
    """Iterate through files that may be deleted"""
27
34
    for subp in tree.extras():
28
 
        if is_detrius(subp):
 
35
        if detritus and is_detritus(subp):
29
36
            yield tree.abspath(subp), subp
 
37
            continue
30
38
        if tree.is_ignored(subp):
31
39
            if ignored:
32
40
                yield tree.abspath(subp), subp
33
41
        else:
34
 
            if unknowns:
 
42
            if unknown:
35
43
                yield tree.abspath(subp), subp
36
44
 
37
45
 
38
 
def clean_tree(deletables, dry_run=False):
 
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
    deletables = iter_deletables(tree, unknown=unknown, ignored=ignored, 
 
51
                                 detritus=detritus)
 
52
    delete_items(deletables, dry_run=dry_run)
 
53
 
 
54
 
 
55
def delete_items(deletables, dry_run=False):
39
56
    """Delete files in the deletables iterable"""
40
 
    printed_once = False
 
57
    has_deleted = False
41
58
    for path, subp in deletables:
42
 
        if not printed_once:
43
 
            print "deleting paths:"
44
 
            printed_once = True
45
 
        print ' ', subp
 
59
        if not has_deleted:
 
60
            note("deleting paths:")
 
61
            has_deleted = True
 
62
        note('  ' + subp)
46
63
        if not dry_run:
47
 
            if os.path.isdir(path):
 
64
            if isdir(path):
48
65
                shutil.rmtree(path)
49
66
            else:
50
67
                os.unlink(path)
 
68
    if not has_deleted:
 
69
        note("No files deleted.")