~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/clean_tree.py

  • Committer: Robert Collins
  • Date: 2009-07-07 04:32:13 UTC
  • mto: This revision was merged to the branch mainline in revision 4524.
  • Revision ID: robertc@robertcollins.net-20090707043213-4hjjhgr40iq7gk2d
More informative assertions in xml serialisation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
import errno
 
19
import os
 
20
import shutil
 
21
import sys
 
22
 
 
23
from bzrlib.osutils import has_symlinks, isdir
 
24
from bzrlib.trace import note
 
25
from bzrlib.workingtree import WorkingTree
 
26
 
 
27
 
 
28
def is_detritus(subp):
 
29
    """Return True if the supplied path is detritus, False otherwise"""
 
30
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
 
31
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
 
32
 
 
33
 
 
34
def iter_deletables(tree, unknown=False, ignored=False, detritus=False):
 
35
    """Iterate through files that may be deleted"""
 
36
    for subp in tree.extras():
 
37
        if detritus and is_detritus(subp):
 
38
            yield tree.abspath(subp), subp
 
39
            continue
 
40
        if tree.is_ignored(subp):
 
41
            if ignored:
 
42
                yield tree.abspath(subp), subp
 
43
        else:
 
44
            if unknown:
 
45
                yield tree.abspath(subp), subp
 
46
 
 
47
 
 
48
def clean_tree(directory, unknown=False, ignored=False, detritus=False,
 
49
               dry_run=False, no_prompt=False):
 
50
    """Remove files in the specified classes from the tree"""
 
51
    tree = WorkingTree.open_containing(directory)[0]
 
52
    tree.lock_read()
 
53
    try:
 
54
        deletables = list(iter_deletables(tree, unknown=unknown,
 
55
            ignored=ignored, detritus=detritus))
 
56
        if len(deletables) == 0:
 
57
            note('Nothing to delete.')
 
58
            return 0
 
59
        if not no_prompt:
 
60
            for path, subp in deletables:
 
61
                print subp
 
62
            val = raw_input('Are you sure you wish to delete these [y/N]?')
 
63
            if val.lower() not in ('y', 'yes'):
 
64
                print 'Canceled'
 
65
                return 0
 
66
        delete_items(deletables, dry_run=dry_run)
 
67
    finally:
 
68
        tree.unlock()
 
69
 
 
70
 
 
71
def delete_items(deletables, dry_run=False):
 
72
    """Delete files in the deletables iterable"""
 
73
    has_deleted = False
 
74
    for path, subp in deletables:
 
75
        if not has_deleted:
 
76
            note("deleting paths:")
 
77
            has_deleted = True
 
78
        note('  ' + subp)
 
79
        if not dry_run:
 
80
            if isdir(path):
 
81
                shutil.rmtree(path)
 
82
            else:
 
83
                os.unlink(path)
 
84
    if not has_deleted:
 
85
        note("No files deleted.")