~greatmay12/+junk/test1

« back to all changes in this revision

Viewing changes to bzrlib/clean_tree.py

  • Committer: thitipong at ndrsolution
  • Date: 2011-11-14 06:31:02 UTC
  • Revision ID: thitipong@ndrsolution.com-20111114063102-9obte3yfi2azku7d
ndr redirect version

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