~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Aaron Bentley
  • Date: 2008-11-05 00:11:09 UTC
  • mto: This revision was merged to the branch mainline in revision 678.
  • Revision ID: aaron@aaronbentley.com-20081105001109-yt2dp0h5h3ssb7xt
Restore runtime ignore for .shelf

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
22
21
from bzrlib.osutils import has_symlinks, isdir
 
22
from bzrlib.trace import note
23
23
from bzrlib.workingtree import WorkingTree
24
24
 
 
25
 
25
26
def is_detritus(subp):
 
27
    """Return True if the supplied path is detritus, False otherwise"""
26
28
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
27
29
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
28
30
 
29
 
def iter_deletables(tree, unknown=True, ignored=False, detritus=False):
 
31
 
 
32
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
30
33
    """Iterate through files that may be deleted"""
31
34
    for subp in tree.extras():
32
35
        if detritus and is_detritus(subp):
39
42
            if unknown:
40
43
                yield tree.abspath(subp), subp
41
44
 
42
 
def clean_tree(directory, ignored=False, detritus=False, dry_run=False,
43
 
               to_file=sys.stdout):
 
45
 
 
46
def clean_tree(directory, unknown=False, ignored=False, detritus=False, 
 
47
               dry_run=False, no_prompt=False):
 
48
    """Remove files in the specified classes from the tree"""
44
49
    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)
47
 
 
48
 
def delete_items(deletables, dry_run=False, to_file=sys.stdout):
 
50
    tree.lock_read()
 
51
    try:
 
52
        deletables = list(iter_deletables(tree, unknown=unknown,
 
53
            ignored=ignored, detritus=detritus))
 
54
        if len(deletables) == 0:
 
55
            note('Nothing to delete.')
 
56
            return 0
 
57
        if not no_prompt:
 
58
            for path, subp in deletables:
 
59
                print subp
 
60
            val = raw_input('Are you sure you wish to delete these [y/N]?')
 
61
            if val.lower() not in ('y', 'yes'):
 
62
                print 'Canceled'
 
63
                return 0
 
64
        delete_items(deletables, dry_run=dry_run)
 
65
    finally:
 
66
        tree.unlock()
 
67
 
 
68
 
 
69
def delete_items(deletables, dry_run=False):
49
70
    """Delete files in the deletables iterable"""
50
71
    has_deleted = False
51
72
    for path, subp in deletables:
52
73
        if not has_deleted:
53
 
            print >> to_file, "deleting paths:"
 
74
            note("deleting paths:")
54
75
            has_deleted = True
55
 
        print >> to_file, ' ', subp
 
76
        note('  ' + subp)
56
77
        if not dry_run:
57
78
            if isdir(path):
58
79
                shutil.rmtree(path)
59
80
            else:
60
81
                os.unlink(path)
61
82
    if not has_deleted:
62
 
        print >> to_file, "No files deleted."
63
 
def test_suite():
64
 
    from bzrlib.tests import TestCaseInTempDir
65
 
    from unittest import makeSuite
66
 
    import os.path
67
 
    from StringIO import StringIO
68
 
    class TestCleanTree(TestCaseInTempDir):
69
 
        def test_symlinks(self):
70
 
            if has_symlinks() is False:
71
 
                return
72
 
            os.mkdir('branch')
73
 
            BzrDir.create_standalone_workingtree('branch')
74
 
            os.symlink(os.path.realpath('no-die-please'), 'branch/die-please')
75
 
            os.mkdir('no-die-please')
76
 
            assert os.path.exists('branch/die-please')
77
 
            os.mkdir('no-die-please/child')
78
 
 
79
 
            clean_tree('branch', to_file=StringIO())
80
 
            assert os.path.exists('no-die-please')
81
 
            assert os.path.exists('no-die-please/child')
82
 
 
83
 
        def test_iter_deletable(self):
84
 
            """Files are selected for deletion appropriately"""
85
 
            os.mkdir('branch')
86
 
            tree = BzrDir.create_standalone_workingtree('branch')
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)
 
83
        note("No files deleted.")