/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4020.1.4 by Aaron Bentley
Assign copyright, update tests.
1
# Copyright (C) 2005 Canonical Ltd
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
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
18
import errno
19
import os
20
import shutil
21
import sys
22
4797.50.1 by Alexander Belchenko
backport to 2.1: ``bzr clean-tree`` should not delete nested bzrdirs. (bug #572098)
23
from bzrlib import bzrdir, errors
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
24
from bzrlib.osutils import has_symlinks, isdir
25
from bzrlib.trace import note
26
from bzrlib.workingtree import WorkingTree
27
28
29
def is_detritus(subp):
30
    """Return True if the supplied path is detritus, False otherwise"""
31
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
32
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
33
34
35
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
36
    """Iterate through files that may be deleted"""
37
    for subp in tree.extras():
38
        if detritus and is_detritus(subp):
39
            yield tree.abspath(subp), subp
40
            continue
41
        if tree.is_ignored(subp):
42
            if ignored:
43
                yield tree.abspath(subp), subp
44
        else:
45
            if unknown:
46
                yield tree.abspath(subp), subp
47
48
4020.1.5 by Aaron Bentley
Fix some formatting issues.
49
def clean_tree(directory, unknown=False, ignored=False, detritus=False,
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
50
               dry_run=False, no_prompt=False):
51
    """Remove files in the specified classes from the tree"""
52
    tree = WorkingTree.open_containing(directory)[0]
53
    tree.lock_read()
54
    try:
55
        deletables = list(iter_deletables(tree, unknown=unknown,
56
            ignored=ignored, detritus=detritus))
4797.50.1 by Alexander Belchenko
backport to 2.1: ``bzr clean-tree`` should not delete nested bzrdirs. (bug #572098)
57
        deletables = _filter_out_nested_bzrdirs(deletables)
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
58
        if len(deletables) == 0:
59
            note('Nothing to delete.')
60
            return 0
61
        if not no_prompt:
62
            for path, subp in deletables:
4797.50.1 by Alexander Belchenko
backport to 2.1: ``bzr clean-tree`` should not delete nested bzrdirs. (bug #572098)
63
                # FIXME using print is very bad idea
64
                # clean_tree should accept to_file argument to write the output
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
65
                print subp
66
            val = raw_input('Are you sure you wish to delete these [y/N]?')
67
            if val.lower() not in ('y', 'yes'):
68
                print 'Canceled'
69
                return 0
70
        delete_items(deletables, dry_run=dry_run)
71
    finally:
72
        tree.unlock()
73
74
4797.50.1 by Alexander Belchenko
backport to 2.1: ``bzr clean-tree`` should not delete nested bzrdirs. (bug #572098)
75
def _filter_out_nested_bzrdirs(deletables):
76
    result = []
77
    for path, subp in deletables:
78
        # bzr won't recurse into unknowns/ignored directories by default
79
        # so we don't pay a penalty for checking subdirs of path for nested
80
        # bzrdir.
81
        # That said we won't detect the branch in the subdir of non-branch
82
        # directory and therefore delete it. (worth to FIXME?)
83
        if isdir(path):
84
            try:
85
                bzrdir.BzrDir.open(path)
86
            except errors.NotBranchError:
87
                result.append((path,subp))
88
            else:
89
                # TODO may be we need to notify user about skipped directories?
90
                pass
91
        else:
92
            result.append((path,subp))
93
    return result
94
95
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
96
def delete_items(deletables, dry_run=False):
97
    """Delete files in the deletables iterable"""
98
    has_deleted = False
99
    for path, subp in deletables:
100
        if not has_deleted:
101
            note("deleting paths:")
102
            has_deleted = True
103
        note('  ' + subp)
104
        if not dry_run:
105
            if isdir(path):
106
                shutil.rmtree(path)
107
            else:
108
                os.unlink(path)
109
    if not has_deleted:
110
        note("No files deleted.")