15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.osutils import has_symlinks, isdir
21
from bzrlib.bzrdir import BzrDir
22
from bzrlib.osutils import has_symlinks, isdir
23
from bzrlib.workingtree import WorkingTree
25
def is_detritus(subp):
20
from bzrlib.branch import Branch
26
23
return subp.endswith('.THIS') or subp.endswith('.BASE') or\
27
24
subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
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
35
32
if tree.is_ignored(subp):
40
37
yield tree.abspath(subp), subp
42
def clean_tree(directory, ignored=False, detritus=False, dry_run=False,
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)
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"""
51
48
for path, subp in deletables:
53
print >> to_file, "deleting paths:"
55
print >> to_file, ' ', subp
50
print "deleting paths:"
58
55
shutil.rmtree(path)
62
print >> to_file, "No files deleted."
64
from bzrlib.tests import TestCaseInTempDir
59
from bzrlib.selftest import TestCaseInTempDir
65
60
from unittest import makeSuite
67
from StringIO import StringIO
68
62
class TestCleanTree(TestCaseInTempDir):
69
63
def test_symlinks(self):
70
64
if has_symlinks() is False:
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')
79
clean_tree('branch', to_file=StringIO())
80
73
assert os.path.exists('no-die-please')
81
74
assert os.path.exists('no-die-please/child')
83
76
def test_iter_deletable(self):
84
77
"""Files are selected for deletion appropriately"""
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)]