1
# Copyright (C) 2005 by Aaron Bentley
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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):
26
return subp.endswith('.THIS') or subp.endswith('.BASE') or\
27
subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
29
def iter_deletables(tree, unknown=True, ignored=False, detritus=False):
30
"""Iterate through files that may be deleted"""
31
for subp in tree.extras():
32
if detritus and is_detritus(subp):
33
yield tree.abspath(subp), subp
35
if tree.is_ignored(subp):
37
yield tree.abspath(subp), subp
40
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)
48
def delete_items(deletables, dry_run=False, to_file=sys.stdout):
49
"""Delete files in the deletables iterable"""
51
for path, subp in deletables:
53
print >> to_file, "deleting paths:"
55
print >> to_file, ' ', subp
62
print >> to_file, "No files deleted."
64
from bzrlib.tests import TestCaseInTempDir
65
from unittest import makeSuite
67
from StringIO import StringIO
68
class TestCleanTree(TestCaseInTempDir):
69
def test_symlinks(self):
70
if has_symlinks() is False:
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')
79
clean_tree('branch', to_file=StringIO())
80
assert os.path.exists('no-die-please')
81
assert os.path.exists('no-die-please/child')
83
def test_iter_deletable(self):
84
"""Files are selected for deletion appropriately"""
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
104
return makeSuite(TestCleanTree)