~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Aaron Bentley
  • Date: 2005-10-18 20:17:59 UTC
  • Revision ID: abentley@panoramicfeedback.com-20051018201759-a5dfcbed504c3d22
Adopted Branch.open_containing's new format

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