~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: abentley
  • Date: 2005-10-17 01:50:19 UTC
  • Revision ID: abentley@lappy-20051017015019-769ba41b6b011718
Fixed symlink traversal in clean-tree

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
20
from bzrlib.branch import Branch
20
21
 
34
35
            if unknowns:
35
36
                yield tree.abspath(subp), subp
36
37
 
 
38
def clean_tree(directory, ignored=False, detrius=False, dry_run=False):
 
39
    br = Branch.open_containing(directory)
 
40
    tree = br.working_tree()
 
41
    deletables = iter_deletables(tree, ignored=ignored, detrius=detrius)
 
42
    delete_items(deletables, dry_run=dry_run)
37
43
 
38
 
def clean_tree(deletables, dry_run=False):
 
44
def delete_items(deletables, dry_run=False):
39
45
    """Delete files in the deletables iterable"""
40
46
    printed_once = False
41
47
    for path, subp in deletables:
44
50
            printed_once = True
45
51
        print ' ', subp
46
52
        if not dry_run:
47
 
            if os.path.isdir(path):
 
53
            if isdir(path):
48
54
                shutil.rmtree(path)
49
55
            else:
50
56
                os.unlink(path)
 
57
def test_suite():
 
58
    from bzrlib.selftest import TestCaseInTempDir
 
59
    from unittest import makeSuite
 
60
    import os.path
 
61
    class TestCleanTree(TestCaseInTempDir):
 
62
        def test_symlinks(self):
 
63
            if has_symlinks() is False:
 
64
                return
 
65
            os.mkdir('branch')
 
66
            b = Branch.initialize('branch')
 
67
            os.symlink(os.path.realpath('no-die-please'), 'branch/die-please')
 
68
            os.mkdir('no-die-please')
 
69
            assert os.path.exists('branch/die-please')
 
70
            os.mkdir('no-die-please/child')
 
71
            clean_tree('branch')
 
72
            assert os.path.exists('no-die-please')
 
73
            assert os.path.exists('no-die-please/child')
 
74
    return makeSuite(TestCleanTree)