~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: abentley
  • Date: 2005-10-18 01:15:44 UTC
  • mto: (147.2.18)
  • mto: This revision was merged to the branch mainline in revision 240.
  • Revision ID: abentley@lappy-20051018011544-a0752bf879920ee1
Added did-nothing message to clean-tree, forced test silent

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from bzrlib.osutils import has_symlinks, isdir
19
19
import shutil
20
20
from bzrlib.branch import Branch
 
21
import sys
21
22
 
22
23
def is_detrius(subp):
23
24
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
36
37
            if unknown:
37
38
                yield tree.abspath(subp), subp
38
39
 
39
 
def clean_tree(directory, ignored=False, detrius=False, dry_run=False):
 
40
def clean_tree(directory, ignored=False, detrius=False, dry_run=False,
 
41
               to_file=sys.stdout):
40
42
    br = Branch.open_containing(directory)
41
43
    tree = br.working_tree()
42
44
    deletables = iter_deletables(tree, ignored=ignored, detrius=detrius)
43
 
    delete_items(deletables, dry_run=dry_run)
 
45
    delete_items(deletables, dry_run=dry_run, to_file=to_file)
44
46
 
45
 
def delete_items(deletables, dry_run=False):
 
47
def delete_items(deletables, dry_run=False, to_file=sys.stdout):
46
48
    """Delete files in the deletables iterable"""
47
 
    printed_once = False
 
49
    has_deleted = False
48
50
    for path, subp in deletables:
49
 
        if not printed_once:
50
 
            print "deleting paths:"
51
 
            printed_once = True
52
 
        print ' ', subp
 
51
        if not has_deleted:
 
52
            print >> to_file, "deleting paths:"
 
53
            has_deleted = True
 
54
        print >> to_file, ' ', subp
53
55
        if not dry_run:
54
56
            if isdir(path):
55
57
                shutil.rmtree(path)
56
58
            else:
57
59
                os.unlink(path)
 
60
    if not has_deleted:
 
61
        print >> to_file, "No files deleted."
58
62
def test_suite():
59
63
    from bzrlib.selftest import TestCaseInTempDir
60
64
    from unittest import makeSuite
61
65
    import os.path
 
66
    from StringIO import StringIO
62
67
    class TestCleanTree(TestCaseInTempDir):
63
68
        def test_symlinks(self):
64
69
            if has_symlinks() is False:
69
74
            os.mkdir('no-die-please')
70
75
            assert os.path.exists('branch/die-please')
71
76
            os.mkdir('no-die-please/child')
72
 
            clean_tree('branch')
 
77
 
 
78
            clean_tree('branch', to_file=StringIO())
73
79
            assert os.path.exists('no-die-please')
74
80
            assert os.path.exists('no-die-please/child')
75
81