~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
232.1.1 by abentley
Added did-nothing message to clean-tree, forced test silent
19
import sys
118 by aaron.bentley at utoronto
Added clean-tree command
20
313 by Aaron Bentley
Updated to match API changes
21
from bzrlib.osutils import has_symlinks, isdir
391 by Aaron Bentley
Updates from the bzr clean-tree branch
22
from bzrlib.trace import note
313 by Aaron Bentley
Updated to match API changes
23
from bzrlib.workingtree import WorkingTree
24
392 by Aaron Bentley
Updated formatting
25
265 by Aaron Bentley
Fixed spelling of detritus
26
def is_detritus(subp):
392 by Aaron Bentley
Updated formatting
27
    """Return True if the supplied path is detritus, False otherwise"""
222 by abentley
Added --dry-run, --detrius options to clean-tree
28
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
225 by abentley
Added .tmp to --detrius
29
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
222 by abentley
Added --dry-run, --detrius options to clean-tree
30
392 by Aaron Bentley
Updated formatting
31
415.1.1 by Adeodato Simó
Make clean-tree --detritus or --ignored not delete also unknown files,
32
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
222 by abentley
Added --dry-run, --detrius options to clean-tree
33
    """Iterate through files that may be deleted"""
34
    for subp in tree.extras():
265 by Aaron Bentley
Fixed spelling of detritus
35
        if detritus and is_detritus(subp):
120 by aaron.bentley at utoronto
Tweaks for clean-tree
36
            yield tree.abspath(subp), subp
227 by Aaron Bentley
Prevented double-deletion in clean-tree
37
            continue
222 by abentley
Added --dry-run, --detrius options to clean-tree
38
        if tree.is_ignored(subp):
39
            if ignored:
40
                yield tree.abspath(subp), subp
41
        else:
228 by Aaron Bentley
Fixed detrius handling, strengthened tests
42
            if unknown:
222 by abentley
Added --dry-run, --detrius options to clean-tree
43
                yield tree.abspath(subp), subp
44
392 by Aaron Bentley
Updated formatting
45
416 by Aaron Bentley
clean-tree --detritus no longer implies --unknown
46
def clean_tree(directory, unknown=False, ignored=False, detritus=False, 
47
               dry_run=False):
392 by Aaron Bentley
Updated formatting
48
    """Remove files in the specified classes from the tree"""
313 by Aaron Bentley
Updated to match API changes
49
    tree = WorkingTree.open_containing(directory)[0]
416 by Aaron Bentley
clean-tree --detritus no longer implies --unknown
50
    deletables = iter_deletables(tree, unknown=unknown, ignored=ignored, 
51
                                 detritus=detritus)
391 by Aaron Bentley
Updates from the bzr clean-tree branch
52
    delete_items(deletables, dry_run=dry_run)
222 by abentley
Added --dry-run, --detrius options to clean-tree
53
392 by Aaron Bentley
Updated formatting
54
391 by Aaron Bentley
Updates from the bzr clean-tree branch
55
def delete_items(deletables, dry_run=False):
222 by abentley
Added --dry-run, --detrius options to clean-tree
56
    """Delete files in the deletables iterable"""
232.1.1 by abentley
Added did-nothing message to clean-tree, forced test silent
57
    has_deleted = False
222 by abentley
Added --dry-run, --detrius options to clean-tree
58
    for path, subp in deletables:
232.1.1 by abentley
Added did-nothing message to clean-tree, forced test silent
59
        if not has_deleted:
391 by Aaron Bentley
Updates from the bzr clean-tree branch
60
            note("deleting paths:")
232.1.1 by abentley
Added did-nothing message to clean-tree, forced test silent
61
            has_deleted = True
391 by Aaron Bentley
Updates from the bzr clean-tree branch
62
        note('  ' + subp)
222 by abentley
Added --dry-run, --detrius options to clean-tree
63
        if not dry_run:
226 by abentley
Fixed symlink traversal in clean-tree
64
            if isdir(path):
222 by abentley
Added --dry-run, --detrius options to clean-tree
65
                shutil.rmtree(path)
66
            else:
67
                os.unlink(path)
232.1.1 by abentley
Added did-nothing message to clean-tree, forced test silent
68
    if not has_deleted:
391 by Aaron Bentley
Updates from the bzr clean-tree branch
69
        note("No files deleted.")