4020.1.4
by Aaron Bentley
Assign copyright, update tests. |
1 |
# Copyright (C) 2005 Canonical Ltd
|
2 |
#
|
|
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
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.
|
|
4020.1.4
by Aaron Bentley
Assign copyright, update tests. |
7 |
#
|
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
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.
|
|
4020.1.4
by Aaron Bentley
Assign copyright, update tests. |
12 |
#
|
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
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
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
4020.1.4
by Aaron Bentley
Assign copyright, update tests. |
16 |
|
17 |
||
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
18 |
import os |
19 |
import shutil |
|
20 |
||
5195.4.2
by Alexander Belchenko
clean-tree: don't delete nested bzrdirs. |
21 |
from bzrlib import bzrdir, errors |
5121.2.4
by Jelmer Vernooij
Remove more unused imports. |
22 |
from bzrlib.osutils import isdir |
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
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 |
||
4020.1.5
by Aaron Bentley
Fix some formatting issues. |
47 |
def clean_tree(directory, unknown=False, ignored=False, detritus=False, |
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
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)) |
|
5195.4.4
by Alexander Belchenko
make _filter_out_nested_bzrdirs private method and clarify the non-recursive behavior of tree.extras() and implication of hidden bzrdirs behind non-versioned directory. |
55 |
deletables = _filter_out_nested_bzrdirs(deletables) |
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
56 |
if len(deletables) == 0: |
57 |
note('Nothing to delete.') |
|
58 |
return 0 |
|
59 |
if not no_prompt: |
|
60 |
for path, subp in deletables: |
|
5195.4.6
by Alexander Belchenko
more comments |
61 |
# FIXME using print is very bad idea
|
62 |
# clean_tree should accept to_file argument to write the output
|
|
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
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 |
||
5195.4.4
by Alexander Belchenko
make _filter_out_nested_bzrdirs private method and clarify the non-recursive behavior of tree.extras() and implication of hidden bzrdirs behind non-versioned directory. |
73 |
def _filter_out_nested_bzrdirs(deletables): |
5195.4.2
by Alexander Belchenko
clean-tree: don't delete nested bzrdirs. |
74 |
result = [] |
75 |
for path, subp in deletables: |
|
5195.4.4
by Alexander Belchenko
make _filter_out_nested_bzrdirs private method and clarify the non-recursive behavior of tree.extras() and implication of hidden bzrdirs behind non-versioned directory. |
76 |
# bzr won't recurse into unknowns/ignored directories by default
|
5195.4.6
by Alexander Belchenko
more comments |
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?)
|
|
5195.4.2
by Alexander Belchenko
clean-tree: don't delete nested bzrdirs. |
81 |
if isdir(path): |
82 |
try: |
|
83 |
bzrdir.BzrDir.open(path) |
|
84 |
except errors.NotBranchError: |
|
85 |
result.append((path,subp)) |
|
5195.4.6
by Alexander Belchenko
more comments |
86 |
else: |
87 |
# TODO may be we need to notify user about skipped directories?
|
|
88 |
pass
|
|
5195.4.2
by Alexander Belchenko
clean-tree: don't delete nested bzrdirs. |
89 |
else: |
90 |
result.append((path,subp)) |
|
91 |
return result |
|
92 |
||
93 |
||
4020.1.1
by Jelmer Vernooij
Import clean-tree from bzrtools. |
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.") |