~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Charlie Shepherd
  • Date: 2007-04-04 18:09:00 UTC
  • mto: This revision was merged to the branch mainline in revision 538.
  • Revision ID: masterdriverz@gentoo.org-20070404180900-ln1hh48xz541nstz
Make clean-tree output to stdout instead of stderr

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
16
import errno
 
17
import os
 
18
import shutil
 
19
import sys
 
20
 
 
21
from bzrlib.osutils import has_symlinks, isdir
 
22
from bzrlib.workingtree import WorkingTree
 
23
 
 
24
 
 
25
def is_detritus(subp):
 
26
    """Return True if the supplied path is detritus, False otherwise"""
 
27
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
 
28
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
 
29
 
 
30
 
 
31
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
 
32
    """Iterate through files that may be deleted"""
 
33
    for subp in tree.extras():
 
34
        if detritus and is_detritus(subp):
 
35
            yield tree.abspath(subp), subp
 
36
            continue
 
37
        if tree.is_ignored(subp):
 
38
            if ignored:
 
39
                yield tree.abspath(subp), subp
 
40
        else:
 
41
            if unknown:
 
42
                yield tree.abspath(subp), subp
 
43
 
 
44
 
 
45
def clean_tree(directory, out=sys.stdout, unknown=False, ignored=False,
 
46
               detritus=False, dry_run=False):
 
47
    """Remove files in the specified classes from the tree"""
 
48
    tree = WorkingTree.open_containing(directory)[0]
 
49
    tree.lock_read()
 
50
    try:
 
51
        deletables = iter_deletables(tree, unknown=unknown, ignored=ignored,
 
52
                                     detritus=detritus)
 
53
        delete_items(deletables, dry_run=dry_run)
 
54
    finally:
 
55
        tree.unlock()
 
56
 
 
57
 
 
58
def delete_items(deletables, out=sys.stdout, dry_run=False):
 
59
    """Delete files in the deletables iterable"""
 
60
    has_deleted = False
 
61
    for path, subp in deletables:
 
62
        if not has_deleted:
 
63
            print >>out, "deleting paths:"
 
64
            has_deleted = True
 
65
        print >>out, '  ' + subp
 
66
        if not dry_run:
 
67
            if isdir(path):
 
68
                shutil.rmtree(path)
 
69
            else:
 
70
                os.unlink(path)
 
71
    if not has_deleted:
 
72
        print >>out, "No files deleted."