~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to clean_tree.py

  • Committer: Aaron Bentley
  • Date: 2011-06-27 23:07:10 UTC
  • Revision ID: aaron@aaronbentley.com-20110627230710-orth0tzf1kwknfen
Better handling of compound tar names.

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
 
from bzrlib.osutils import has_symlinks, isdir
19
 
import shutil
20
 
from bzrlib.branch import Branch
21
 
 
22
 
def is_detrius(subp):
23
 
    return subp.endswith('.THIS') or subp.endswith('.BASE') or\
24
 
        subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp')
25
 
 
26
 
def iter_deletables(tree, unknown=True, ignored=False, detrius=False):
27
 
    """Iterate through files that may be deleted"""
28
 
    for subp in tree.extras():
29
 
        if detrius and is_detrius(subp):
30
 
            yield tree.abspath(subp), subp
31
 
            continue
32
 
        if tree.is_ignored(subp):
33
 
            if ignored:
34
 
                yield tree.abspath(subp), subp
35
 
        else:
36
 
            if unknown:
37
 
                yield tree.abspath(subp), subp
38
 
 
39
 
def clean_tree(directory, ignored=False, detrius=False, dry_run=False):
40
 
    br = Branch.open_containing(directory)[0]
41
 
    tree = br.working_tree()
42
 
    deletables = iter_deletables(tree, ignored=ignored, detrius=detrius)
43
 
    delete_items(deletables, dry_run=dry_run)
44
 
 
45
 
def delete_items(deletables, dry_run=False):
46
 
    """Delete files in the deletables iterable"""
47
 
    printed_once = False
48
 
    for path, subp in deletables:
49
 
        if not printed_once:
50
 
            print "deleting paths:"
51
 
            printed_once = True
52
 
        print ' ', subp
53
 
        if not dry_run:
54
 
            if isdir(path):
55
 
                shutil.rmtree(path)
56
 
            else:
57
 
                os.unlink(path)
58
 
def test_suite():
59
 
    from bzrlib.selftest import TestCaseInTempDir
60
 
    from unittest import makeSuite
61
 
    import os.path
62
 
    class TestCleanTree(TestCaseInTempDir):
63
 
        def test_symlinks(self):
64
 
            if has_symlinks() is False:
65
 
                return
66
 
            os.mkdir('branch')
67
 
            b = Branch.initialize('branch')
68
 
            os.symlink(os.path.realpath('no-die-please'), 'branch/die-please')
69
 
            os.mkdir('no-die-please')
70
 
            assert os.path.exists('branch/die-please')
71
 
            os.mkdir('no-die-please/child')
72
 
            clean_tree('branch')
73
 
            assert os.path.exists('no-die-please')
74
 
            assert os.path.exists('no-die-please/child')
75
 
 
76
 
        def test_iter_deletable(self):
77
 
            """Files are selected for deletion appropriately"""
78
 
            os.mkdir('branch')
79
 
            b = Branch.initialize('branch')
80
 
            tree = b.working_tree()
81
 
            file('branch/file.BASE', 'wb').write('contents')
82
 
            self.assertEqual(len(list(iter_deletables(tree))), 1)
83
 
            file('branch/file~', 'wb').write('contents')
84
 
            file('branch/file.pyc', 'wb').write('contents')
85
 
            dels = [r for a,r in iter_deletables(tree)]
86
 
            assert 'file~' not in dels
87
 
            assert 'file.pyc' not in dels
88
 
            dels = [r for a,r in iter_deletables(tree, detrius=True)]
89
 
            assert 'file~' in dels
90
 
            assert 'file.pyc' not in dels
91
 
            dels = [r for a,r in iter_deletables(tree, ignored=True)]
92
 
            assert 'file~' in dels
93
 
            assert 'file.BASE' in dels
94
 
            assert 'file.pyc' in dels
95
 
            dels = [r for a,r in iter_deletables(tree, unknown=False)]
96
 
            assert 'file.BASE' not in dels
97
 
 
98
 
    return makeSuite(TestCleanTree)