~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Robert Collins
  • Date: 2006-04-24 01:35:08 UTC
  • mto: (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 366.
  • Revision ID: robertc@robertcollins.net-20060424013508-2d37e47814d9493e
Support importing into empty branches.

Show diffs side-by-side

added added

removed removed

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