120
by aaron.bentley at utoronto
Tweaks for clean-tree |
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
|
|
118
by aaron.bentley at utoronto
Added clean-tree command |
16 |
import errno |
17 |
import os |
|
226
by abentley
Fixed symlink traversal in clean-tree |
18 |
from bzrlib.osutils import has_symlinks, isdir |
118
by aaron.bentley at utoronto
Added clean-tree command |
19 |
import shutil |
20 |
from bzrlib.branch import Branch |
|
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
21 |
import sys |
118
by aaron.bentley at utoronto
Added clean-tree command |
22 |
|
265
by Aaron Bentley
Fixed spelling of detritus |
23 |
def is_detritus(subp): |
222
by abentley
Added --dry-run, --detrius options to clean-tree |
24 |
return subp.endswith('.THIS') or subp.endswith('.BASE') or\ |
225
by abentley
Added .tmp to --detrius |
25 |
subp.endswith('.OTHER') or subp.endswith('~') or subp.endswith('.tmp') |
222
by abentley
Added --dry-run, --detrius options to clean-tree |
26 |
|
265
by Aaron Bentley
Fixed spelling of detritus |
27 |
def iter_deletables(tree, unknown=True, ignored=False, detritus=False): |
222
by abentley
Added --dry-run, --detrius options to clean-tree |
28 |
"""Iterate through files that may be deleted"""
|
29 |
for subp in tree.extras(): |
|
265
by Aaron Bentley
Fixed spelling of detritus |
30 |
if detritus and is_detritus(subp): |
120
by aaron.bentley at utoronto
Tweaks for clean-tree |
31 |
yield tree.abspath(subp), subp |
227
by Aaron Bentley
Prevented double-deletion in clean-tree |
32 |
continue
|
222
by abentley
Added --dry-run, --detrius options to clean-tree |
33 |
if tree.is_ignored(subp): |
34 |
if ignored: |
|
35 |
yield tree.abspath(subp), subp |
|
36 |
else: |
|
228
by Aaron Bentley
Fixed detrius handling, strengthened tests |
37 |
if unknown: |
222
by abentley
Added --dry-run, --detrius options to clean-tree |
38 |
yield tree.abspath(subp), subp |
39 |
||
265
by Aaron Bentley
Fixed spelling of detritus |
40 |
def clean_tree(directory, ignored=False, detritus=False, dry_run=False, |
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
41 |
to_file=sys.stdout): |
234
by Aaron Bentley
Adopted Branch.open_containing's new format |
42 |
br = Branch.open_containing(directory)[0] |
226
by abentley
Fixed symlink traversal in clean-tree |
43 |
tree = br.working_tree() |
265
by Aaron Bentley
Fixed spelling of detritus |
44 |
deletables = iter_deletables(tree, ignored=ignored, detritus=detritus) |
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
45 |
delete_items(deletables, dry_run=dry_run, to_file=to_file) |
222
by abentley
Added --dry-run, --detrius options to clean-tree |
46 |
|
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
47 |
def delete_items(deletables, dry_run=False, to_file=sys.stdout): |
222
by abentley
Added --dry-run, --detrius options to clean-tree |
48 |
"""Delete files in the deletables iterable"""
|
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
49 |
has_deleted = False |
222
by abentley
Added --dry-run, --detrius options to clean-tree |
50 |
for path, subp in deletables: |
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
51 |
if not has_deleted: |
52 |
print >> to_file, "deleting paths:" |
|
53 |
has_deleted = True |
|
54 |
print >> to_file, ' ', subp |
|
222
by abentley
Added --dry-run, --detrius options to clean-tree |
55 |
if not dry_run: |
226
by abentley
Fixed symlink traversal in clean-tree |
56 |
if isdir(path): |
222
by abentley
Added --dry-run, --detrius options to clean-tree |
57 |
shutil.rmtree(path) |
58 |
else: |
|
59 |
os.unlink(path) |
|
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
60 |
if not has_deleted: |
61 |
print >> to_file, "No files deleted." |
|
226
by abentley
Fixed symlink traversal in clean-tree |
62 |
def test_suite(): |
63 |
from bzrlib.selftest import TestCaseInTempDir |
|
64 |
from unittest import makeSuite |
|
65 |
import os.path |
|
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
66 |
from StringIO import StringIO |
226
by abentley
Fixed symlink traversal in clean-tree |
67 |
class TestCleanTree(TestCaseInTempDir): |
68 |
def test_symlinks(self): |
|
69 |
if has_symlinks() is False: |
|
70 |
return
|
|
71 |
os.mkdir('branch') |
|
72 |
b = Branch.initialize('branch') |
|
73 |
os.symlink(os.path.realpath('no-die-please'), 'branch/die-please') |
|
74 |
os.mkdir('no-die-please') |
|
75 |
assert os.path.exists('branch/die-please') |
|
76 |
os.mkdir('no-die-please/child') |
|
232.1.1
by abentley
Added did-nothing message to clean-tree, forced test silent |
77 |
|
78 |
clean_tree('branch', to_file=StringIO()) |
|
226
by abentley
Fixed symlink traversal in clean-tree |
79 |
assert os.path.exists('no-die-please') |
80 |
assert os.path.exists('no-die-please/child') |
|
227
by Aaron Bentley
Prevented double-deletion in clean-tree |
81 |
|
82 |
def test_iter_deletable(self): |
|
83 |
"""Files are selected for deletion appropriately"""
|
|
84 |
os.mkdir('branch') |
|
85 |
b = Branch.initialize('branch') |
|
86 |
tree = b.working_tree() |
|
87 |
file('branch/file.BASE', 'wb').write('contents') |
|
88 |
self.assertEqual(len(list(iter_deletables(tree))), 1) |
|
228
by Aaron Bentley
Fixed detrius handling, strengthened tests |
89 |
file('branch/file~', 'wb').write('contents') |
90 |
file('branch/file.pyc', 'wb').write('contents') |
|
91 |
dels = [r for a,r in iter_deletables(tree)] |
|
92 |
assert 'file~' not in dels |
|
93 |
assert 'file.pyc' not in dels |
|
265
by Aaron Bentley
Fixed spelling of detritus |
94 |
dels = [r for a,r in iter_deletables(tree, detritus=True)] |
228
by Aaron Bentley
Fixed detrius handling, strengthened tests |
95 |
assert 'file~' in dels |
96 |
assert 'file.pyc' not in dels |
|
97 |
dels = [r for a,r in iter_deletables(tree, ignored=True)] |
|
98 |
assert 'file~' in dels |
|
99 |
assert 'file.BASE' in dels |
|
100 |
assert 'file.pyc' in dels |
|
101 |
dels = [r for a,r in iter_deletables(tree, unknown=False)] |
|
102 |
assert 'file.BASE' not in dels |
|
103 |
||
226
by abentley
Fixed symlink traversal in clean-tree |
104 |
return makeSuite(TestCleanTree) |