~bzr-pqm/bzr/bzr.dev

5365.5.25 by John Arbash Meinel
Merge bzr.dev 5387 in prep for NEWS
1
# Copyright (C) 2009, 2010 Canonical Ltd
4020.1.4 by Aaron Bentley
Assign copyright, update tests.
2
#
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
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.
4020.1.4 by Aaron Bentley
Assign copyright, update tests.
7
#
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
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.
4020.1.4 by Aaron Bentley
Assign copyright, update tests.
12
#
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4020.1.4 by Aaron Bentley
Assign copyright, update tests.
16
17
5376.1.3 by Parth Malwankar
clean-tree now specifically handes EACCES
18
import errno
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
19
import os
20
import shutil
21
5376.1.6 by Parth Malwankar
closed review comments
22
from bzrlib import (
6207.3.3 by jelmer at samba
Fix tests and the like.
23
    controldir,
5376.1.6 by Parth Malwankar
closed review comments
24
    errors,
25
    ui,
26
    )
5121.2.4 by Jelmer Vernooij
Remove more unused imports.
27
from bzrlib.osutils import isdir
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
28
from bzrlib.trace import note
29
from bzrlib.workingtree import WorkingTree
6138.3.2 by Jonathan Riddell
gettext-ify bzrlib/commit.py
30
from bzrlib.i18n import gettext
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
31
32
def is_detritus(subp):
33
    """Return True if the supplied path is detritus, False otherwise"""
34
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
35
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
36
37
38
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
39
    """Iterate through files that may be deleted"""
40
    for subp in tree.extras():
41
        if detritus and is_detritus(subp):
42
            yield tree.abspath(subp), subp
43
            continue
44
        if tree.is_ignored(subp):
45
            if ignored:
46
                yield tree.abspath(subp), subp
47
        else:
48
            if unknown:
49
                yield tree.abspath(subp), subp
50
51
4020.1.5 by Aaron Bentley
Fix some formatting issues.
52
def clean_tree(directory, unknown=False, ignored=False, detritus=False,
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
53
               dry_run=False, no_prompt=False):
54
    """Remove files in the specified classes from the tree"""
55
    tree = WorkingTree.open_containing(directory)[0]
56
    tree.lock_read()
57
    try:
58
        deletables = list(iter_deletables(tree, unknown=unknown,
59
            ignored=ignored, detritus=detritus))
5195.4.4 by Alexander Belchenko
make _filter_out_nested_bzrdirs private method and clarify the non-recursive behavior of tree.extras() and implication of hidden bzrdirs behind non-versioned directory.
60
        deletables = _filter_out_nested_bzrdirs(deletables)
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
61
        if len(deletables) == 0:
6138.3.2 by Jonathan Riddell
gettext-ify bzrlib/commit.py
62
            note(gettext('Nothing to delete.'))
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
63
            return 0
64
        if not no_prompt:
65
            for path, subp in deletables:
6228.1.1 by Benoît Pierre
Fix clean_tree messages and prompt handling.
66
                ui.ui_factory.note(subp)
6228.1.4 by Benoît Pierre
Translate prompt messages in clean_tree.
67
            prompt = gettext('Are you sure you wish to delete these')
6228.1.1 by Benoît Pierre
Fix clean_tree messages and prompt handling.
68
            if not ui.ui_factory.get_boolean(prompt):
6228.1.4 by Benoît Pierre
Translate prompt messages in clean_tree.
69
                ui.ui_factory.note(gettext('Canceled'))
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
70
                return 0
71
        delete_items(deletables, dry_run=dry_run)
72
    finally:
73
        tree.unlock()
74
75
5195.4.4 by Alexander Belchenko
make _filter_out_nested_bzrdirs private method and clarify the non-recursive behavior of tree.extras() and implication of hidden bzrdirs behind non-versioned directory.
76
def _filter_out_nested_bzrdirs(deletables):
5195.4.2 by Alexander Belchenko
clean-tree: don't delete nested bzrdirs.
77
    result = []
78
    for path, subp in deletables:
5195.4.4 by Alexander Belchenko
make _filter_out_nested_bzrdirs private method and clarify the non-recursive behavior of tree.extras() and implication of hidden bzrdirs behind non-versioned directory.
79
        # bzr won't recurse into unknowns/ignored directories by default
5195.4.6 by Alexander Belchenko
more comments
80
        # so we don't pay a penalty for checking subdirs of path for nested
81
        # bzrdir.
82
        # That said we won't detect the branch in the subdir of non-branch
83
        # directory and therefore delete it. (worth to FIXME?)
5195.4.2 by Alexander Belchenko
clean-tree: don't delete nested bzrdirs.
84
        if isdir(path):
85
            try:
6207.3.3 by jelmer at samba
Fix tests and the like.
86
                controldir.ControlDir.open(path)
5195.4.2 by Alexander Belchenko
clean-tree: don't delete nested bzrdirs.
87
            except errors.NotBranchError:
88
                result.append((path,subp))
5195.4.6 by Alexander Belchenko
more comments
89
            else:
90
                # TODO may be we need to notify user about skipped directories?
91
                pass
5195.4.2 by Alexander Belchenko
clean-tree: don't delete nested bzrdirs.
92
        else:
93
            result.append((path,subp))
94
    return result
95
96
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
97
def delete_items(deletables, dry_run=False):
98
    """Delete files in the deletables iterable"""
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
99
    def onerror(function, path, excinfo):
100
        """Show warning for errors seen by rmtree.
101
        """
5376.1.9 by Parth Malwankar
improved comments
102
        # Handle only permission error while removing files.
103
        # Other errors are re-raised.
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
104
        if function is not os.remove or excinfo[1].errno != errno.EACCES:
105
            raise
6138.3.2 by Jonathan Riddell
gettext-ify bzrlib/commit.py
106
        ui.ui_factory.show_warning(gettext('unable to remove %s') % path)
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
107
    has_deleted = False
108
    for path, subp in deletables:
109
        if not has_deleted:
6138.3.2 by Jonathan Riddell
gettext-ify bzrlib/commit.py
110
            note(gettext("deleting paths:"))
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
111
            has_deleted = True
112
        if not dry_run:
5376.1.3 by Parth Malwankar
clean-tree now specifically handes EACCES
113
            if isdir(path):
114
                shutil.rmtree(path, onerror=onerror)
115
            else:
116
                try:
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
117
                    os.unlink(path)
5376.1.6 by Parth Malwankar
closed review comments
118
                    note('  ' + subp)
5376.1.3 by Parth Malwankar
clean-tree now specifically handes EACCES
119
                except OSError, e:
120
                    # We handle only permission error here
121
                    if e.errno != errno.EACCES:
122
                        raise e
6138.3.2 by Jonathan Riddell
gettext-ify bzrlib/commit.py
123
                    ui.ui_factory.show_warning(gettext(
6147.1.1 by Jonathan Riddell
use .format() instead of % for string formatting where there are multiple formats in one string to allow for translations
124
                        'unable to remove "{0}": {1}.').format(
125
                                                    path, e.strerror))
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
126
        else:
127
            note('  ' + subp)
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
128
    if not has_deleted:
6138.3.2 by Jonathan Riddell
gettext-ify bzrlib/commit.py
129
        note(gettext("No files deleted."))