~abentley/bzrtools/bzrtools.dev

120 by aaron.bentley at utoronto
Tweaks for clean-tree
1
# Copyright (C) 2005 by Aaron Bentley
2
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.
7
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.
12
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
118 by aaron.bentley at utoronto
Added clean-tree command
16
import errno
17
import os
18
import shutil
19
from bzrlib.branch import Branch
20
222 by abentley
Added --dry-run, --detrius options to clean-tree
21
def is_detrius(subp):
22
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
225 by abentley
Added .tmp to --detrius
23
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
222 by abentley
Added --dry-run, --detrius options to clean-tree
24
25
def iter_deletables(tree, unknowns=True, ignored=False, detrius=False):
26
    """Iterate through files that may be deleted"""
27
    for subp in tree.extras():
28
        if is_detrius(subp):
120 by aaron.bentley at utoronto
Tweaks for clean-tree
29
            yield tree.abspath(subp), subp
222 by abentley
Added --dry-run, --detrius options to clean-tree
30
        if tree.is_ignored(subp):
31
            if ignored:
32
                yield tree.abspath(subp), subp
33
        else:
34
            if unknowns:
35
                yield tree.abspath(subp), subp
36
37
38
def clean_tree(deletables, dry_run=False):
39
    """Delete files in the deletables iterable"""
118 by aaron.bentley at utoronto
Added clean-tree command
40
    printed_once = False
222 by abentley
Added --dry-run, --detrius options to clean-tree
41
    for path, subp in deletables:
118 by aaron.bentley at utoronto
Added clean-tree command
42
        if not printed_once:
149 by abentley
Typo fix from David Clymer
43
            print "deleting paths:"
118 by aaron.bentley at utoronto
Added clean-tree command
44
            printed_once = True
120 by aaron.bentley at utoronto
Tweaks for clean-tree
45
        print ' ', subp
222 by abentley
Added --dry-run, --detrius options to clean-tree
46
        if not dry_run:
47
            if os.path.isdir(path):
48
                shutil.rmtree(path)
49
            else:
50
                os.unlink(path)