1
# Copyright (C) 2005 Canonical Ltd
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
from bzrlib import bzrdir, errors
22
from bzrlib.osutils import isdir
23
from bzrlib.trace import note
24
from bzrlib.workingtree import WorkingTree
27
def is_detritus(subp):
28
"""Return True if the supplied path is detritus, False otherwise"""
29
return subp.endswith('.THIS') or subp.endswith('.BASE') or\
30
subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
33
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
34
"""Iterate through files that may be deleted"""
35
for subp in tree.extras():
36
if detritus and is_detritus(subp):
37
yield tree.abspath(subp), subp
39
if tree.is_ignored(subp):
41
yield tree.abspath(subp), subp
44
yield tree.abspath(subp), subp
47
def clean_tree(directory, unknown=False, ignored=False, detritus=False,
48
dry_run=False, no_prompt=False):
49
"""Remove files in the specified classes from the tree"""
50
tree = WorkingTree.open_containing(directory)[0]
53
deletables = list(iter_deletables(tree, unknown=unknown,
54
ignored=ignored, detritus=detritus))
55
deletables = _filter_out_nested_bzrdirs(deletables)
56
if len(deletables) == 0:
57
note('Nothing to delete.')
60
for path, subp in deletables:
61
# FIXME using print is very bad idea
62
# clean_tree should accept to_file argument to write the output
64
val = raw_input('Are you sure you wish to delete these [y/N]?')
65
if val.lower() not in ('y', 'yes'):
68
delete_items(deletables, dry_run=dry_run)
73
def _filter_out_nested_bzrdirs(deletables):
75
for path, subp in deletables:
76
# bzr won't recurse into unknowns/ignored directories by default
77
# so we don't pay a penalty for checking subdirs of path for nested
79
# That said we won't detect the branch in the subdir of non-branch
80
# directory and therefore delete it. (worth to FIXME?)
83
bzrdir.BzrDir.open(path)
84
except errors.NotBranchError:
85
result.append((path,subp))
87
# TODO may be we need to notify user about skipped directories?
90
result.append((path,subp))
94
def delete_items(deletables, dry_run=False):
95
"""Delete files in the deletables iterable"""
97
for path, subp in deletables:
99
note("deleting paths:")
108
note("No files deleted.")