~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Aaron Bentley
  • Date: 2013-08-20 03:02:43 UTC
  • Revision ID: aaron@aaronbentley.com-20130820030243-r8v1xfbcnd8f10p4
Fix zap command for 2.6/7

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
 
from bzrlib.branch import Branch
20
 
 
21
 
def is_detrius(subp):
22
 
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
23
 
        subp.endswith('.OTHER') or subp.endswith('~')
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):
29
 
            yield tree.abspath(subp), subp
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"""
40
 
    printed_once = False
41
 
    for path, subp in deletables:
42
 
        if not printed_once:
43
 
            print "deleting paths:"
44
 
            printed_once = True
45
 
        print ' ', subp
46
 
        if not dry_run:
47
 
            if os.path.isdir(path):
48
 
                shutil.rmtree(path)
49
 
            else:
50
 
                os.unlink(path)