~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Aaron Bentley
  • Date: 2005-12-14 14:33:05 UTC
  • Revision ID: abentley@panoramicfeedback.com-20051214143305-42718d97f27c03bd
Avoided leaving junk all over the place when running standalone tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
import errno
17
17
import os
 
18
from bzrlib.osutils import has_symlinks, isdir
18
19
import shutil
 
20
from bzrlib.branch import Branch
19
21
import sys
20
22
 
21
 
from bzrlib.osutils import has_symlinks, isdir
22
 
from bzrlib.trace import note
23
 
from bzrlib.workingtree import WorkingTree
24
 
 
25
 
 
26
23
def is_detritus(subp):
27
 
    """Return True if the supplied path is detritus, False otherwise"""
28
24
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
29
25
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
30
26
 
31
 
 
32
 
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
 
27
def iter_deletables(tree, unknown=True, ignored=False, detritus=False):
33
28
    """Iterate through files that may be deleted"""
34
29
    for subp in tree.extras():
35
30
        if detritus and is_detritus(subp):
42
37
            if unknown:
43
38
                yield tree.abspath(subp), subp
44
39
 
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):
 
40
def clean_tree(directory, ignored=False, detritus=False, dry_run=False,
 
41
               to_file=sys.stdout):
 
42
    br = Branch.open_containing(directory)[0]
 
43
    tree = br.working_tree()
 
44
    deletables = iter_deletables(tree, ignored=ignored, detritus=detritus)
 
45
    delete_items(deletables, dry_run=dry_run, to_file=to_file)
 
46
 
 
47
def delete_items(deletables, dry_run=False, to_file=sys.stdout):
60
48
    """Delete files in the deletables iterable"""
61
49
    has_deleted = False
62
50
    for path, subp in deletables:
63
51
        if not has_deleted:
64
 
            note("deleting paths:")
 
52
            print >> to_file, "deleting paths:"
65
53
            has_deleted = True
66
 
        note('  ' + subp)
 
54
        print >> to_file, ' ', subp
67
55
        if not dry_run:
68
56
            if isdir(path):
69
57
                shutil.rmtree(path)
70
58
            else:
71
59
                os.unlink(path)
72
60
    if not has_deleted:
73
 
        note("No files deleted.")
 
61
        print >> to_file, "No files deleted."
 
62
def test_suite():
 
63
    from bzrlib.tests import TestCaseInTempDir
 
64
    from unittest import makeSuite
 
65
    import os.path
 
66
    from StringIO import StringIO
 
67
    class TestCleanTree(TestCaseInTempDir):
 
68
        def test_symlinks(self):
 
69
            if has_symlinks() is False:
 
70
                return
 
71
            os.mkdir('branch')
 
72
            b = Branch.initialize('branch')
 
73
            os.symlink(os.path.realpath('no-die-please'), 'branch/die-please')
 
74
            os.mkdir('no-die-please')
 
75
            assert os.path.exists('branch/die-please')
 
76
            os.mkdir('no-die-please/child')
 
77
 
 
78
            clean_tree('branch', to_file=StringIO())
 
79
            assert os.path.exists('no-die-please')
 
80
            assert os.path.exists('no-die-please/child')
 
81
 
 
82
        def test_iter_deletable(self):
 
83
            """Files are selected for deletion appropriately"""
 
84
            os.mkdir('branch')
 
85
            b = Branch.initialize('branch')
 
86
            tree = b.working_tree()
 
87
            file('branch/file.BASE', 'wb').write('contents')
 
88
            self.assertEqual(len(list(iter_deletables(tree))), 1)
 
89
            file('branch/file~', 'wb').write('contents')
 
90
            file('branch/file.pyc', 'wb').write('contents')
 
91
            dels = [r for a,r in iter_deletables(tree)]
 
92
            assert 'file~' not in dels
 
93
            assert 'file.pyc' not in dels
 
94
            dels = [r for a,r in iter_deletables(tree, detritus=True)]
 
95
            assert 'file~' in dels
 
96
            assert 'file.pyc' not in dels
 
97
            dels = [r for a,r in iter_deletables(tree, ignored=True)]
 
98
            assert 'file~' in dels
 
99
            assert 'file.BASE' in dels
 
100
            assert 'file.pyc' in dels
 
101
            dels = [r for a,r in iter_deletables(tree, unknown=False)]
 
102
            assert 'file.BASE' not in dels
 
103
 
 
104
    return makeSuite(TestCleanTree)