bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.40.10
by Parth Malwankar
assigned copyright to canonical |
1 |
# Copyright (C) 2010 Canonical Ltd
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
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 |
||
17 |
from bzrlib.lazy_import import lazy_import |
|
18 |
lazy_import(globals(), """ |
|
19 |
import os
|
|
20 |
import re
|
|
|
0.40.47
by Parth Malwankar
fixes bug #531336. binary files are now skipped. |
21 |
import cStringIO
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
22 |
from fnmatch import fnmatch
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
23 |
|
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
24 |
from bzrlib import log as logcmd
|
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
25 |
from bzrlib import bzrdir
|
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
26 |
from bzrlib.workingtree import WorkingTree
|
27 |
from bzrlib.revisionspec import RevisionSpec, RevisionSpec_revid
|
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
28 |
from bzrlib import (
|
29 |
errors,
|
|
30 |
lazy_regex,
|
|
|
0.40.47
by Parth Malwankar
fixes bug #531336. binary files are now skipped. |
31 |
osutils,
|
32 |
textfile,
|
|
33 |
trace,
|
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
34 |
)
|
35 |
""") |
|
36 |
||
37 |
def compile_pattern(pattern, flags=0): |
|
38 |
patternc = None |
|
39 |
try: |
|
40 |
# use python's re.compile as we need to catch re.error in case of bad pattern
|
|
41 |
lazy_regex.reset_compile() |
|
42 |
patternc = re.compile(pattern, flags) |
|
43 |
except re.error, e: |
|
44 |
raise errors.BzrError("Invalid pattern: '%s'" % pattern) |
|
45 |
return patternc |
|
46 |
||
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
47 |
|
48 |
def versioned_grep(revision, compiled_pattern, path_list, recursive, |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
49 |
line_number, from_root, eol_marker, print_revno, levels, |
50 |
include, exclude, outf): |
|
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
51 |
|
52 |
wt, relpath = WorkingTree.open_containing('.') |
|
53 |
||
54 |
start_rev = revision[0] |
|
55 |
end_rev = revision[0] |
|
56 |
if len(revision) == 2: |
|
57 |
end_rev = revision[1] |
|
58 |
||
59 |
start_revid = start_rev.as_revision_id(wt.branch) |
|
60 |
end_revid = end_rev.as_revision_id(wt.branch) |
|
61 |
||
62 |
given_revs = logcmd._graph_view_revisions(wt.branch, start_revid, end_revid) |
|
|
0.41.13
by Parth Malwankar
working tree grep is now working |
63 |
given_revs = list(given_revs) |
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
64 |
|
65 |
for revid, revno, merge_depth in given_revs: |
|
66 |
if levels == 1 and merge_depth != 0: |
|
67 |
# with level=1 show only top level
|
|
68 |
continue
|
|
69 |
||
70 |
wt.lock_read() |
|
71 |
rev = RevisionSpec_revid.from_string("revid:"+revid) |
|
72 |
try: |
|
73 |
for path in path_list: |
|
74 |
tree = rev.as_tree(wt.branch) |
|
75 |
path_for_id = osutils.pathjoin(relpath, path) |
|
76 |
id = tree.path2id(path_for_id) |
|
77 |
if not id: |
|
|
0.41.22
by Parth Malwankar
added basic --exclude/include tests |
78 |
trace.warning("Skipped unknown file '%s'." % path) |
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
79 |
continue
|
80 |
||
81 |
if osutils.isdir(path): |
|
82 |
path_prefix = path |
|
83 |
dir_grep(tree, path, relpath, recursive, line_number, |
|
84 |
compiled_pattern, from_root, eol_marker, revno, print_revno, |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
85 |
include, exclude, outf, path_prefix) |
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
86 |
else: |
87 |
tree.lock_read() |
|
88 |
try: |
|
89 |
versioned_file_grep(tree, id, '.', path, |
|
90 |
compiled_pattern, eol_marker, line_number, revno, |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
91 |
print_revno, include, exclude, outf) |
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
92 |
finally: |
93 |
tree.unlock() |
|
94 |
finally: |
|
95 |
wt.unlock() |
|
96 |
||
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
97 |
def workingtree_grep(compiled_pattern, path_list, recursive, |
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
98 |
line_number, from_root, eol_marker, include, exclude, outf): |
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
99 |
revno = print_revno = None # for working tree set revno to None |
100 |
for path in path_list: |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
101 |
tree, branch, relpath = \ |
102 |
bzrdir.BzrDir.open_containing_tree_or_branch('.') |
|
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
103 |
if osutils.isdir(path): |
104 |
path_prefix = path |
|
105 |
dir_grep(tree, path, relpath, recursive, line_number, |
|
106 |
compiled_pattern, from_root, eol_marker, revno, print_revno, |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
107 |
include, exclude, outf, path_prefix) |
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
108 |
else: |
109 |
_file_grep(open(path).read(), '.', path, compiled_pattern, eol_marker, |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
110 |
line_number, revno, print_revno, include, exclude, outf) |
|
0.41.11
by Parth Malwankar
moved top level grep code to versioned_grep. |
111 |
|
|
0.40.43
by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep |
112 |
def dir_grep(tree, path, relpath, recursive, line_number, compiled_pattern, |
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
113 |
from_root, eol_marker, revno, print_revno, include, exclude, outf, path_prefix): |
|
0.40.43
by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep |
114 |
# setup relpath to open files relative to cwd
|
115 |
rpath = relpath |
|
116 |
if relpath: |
|
117 |
rpath = osutils.pathjoin('..',relpath) |
|
118 |
||
|
0.41.13
by Parth Malwankar
working tree grep is now working |
119 |
from_dir = osutils.pathjoin(relpath, path) |
120 |
if from_root: |
|
121 |
# start searching recursively from root
|
|
122 |
from_dir=None |
|
123 |
recursive=True |
|
124 |
||
|
0.40.43
by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep |
125 |
tree.lock_read() |
126 |
try: |
|
127 |
for fp, fc, fkind, fid, entry in tree.list_files(include_root=False, |
|
128 |
from_dir=from_dir, recursive=recursive): |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
129 |
|
|
0.40.43
by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep |
130 |
if fc == 'V' and fkind == 'file': |
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
131 |
if revno != None: |
132 |
versioned_file_grep(tree, fid, rpath, fp, |
|
133 |
compiled_pattern, eol_marker, line_number, |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
134 |
revno, print_revno, include, exclude, outf, path_prefix) |
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
135 |
else: |
136 |
# we are grepping working tree.
|
|
|
0.41.13
by Parth Malwankar
working tree grep is now working |
137 |
if from_dir == None: |
138 |
from_dir = '.' |
|
139 |
||
140 |
path_for_file = osutils.pathjoin(tree.basedir, from_dir, fp) |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
141 |
_file_grep(open(path_for_file).read(), rpath, fp, |
142 |
compiled_pattern, eol_marker, line_number, revno, |
|
143 |
print_revno, include, exclude, outf, path_prefix) |
|
|
0.40.43
by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep |
144 |
finally: |
145 |
tree.unlock() |
|
146 |
||
147 |
||
|
0.41.8
by Parth Malwankar
code cleanup. |
148 |
def _make_display_path(relpath, path): |
149 |
"""Return path string relative to user cwd. |
|
|
0.40.42
by Parth Malwankar
fix to make grep paths relative to cwd |
150 |
|
|
0.41.8
by Parth Malwankar
code cleanup. |
151 |
Take tree's 'relpath' and user supplied 'path', and return path
|
152 |
that can be displayed to the user.
|
|
153 |
"""
|
|
|
0.40.15
by Parth Malwankar
some fixes and test updates |
154 |
if relpath: |
|
0.40.52
by Parth Malwankar
code cleanup and documentation |
155 |
# update path so to display it w.r.t cwd
|
156 |
# handle windows slash separator
|
|
|
0.40.20
by Parth Malwankar
used path functions from bzrlib.osutils |
157 |
path = osutils.normpath(osutils.pathjoin(relpath, path)) |
|
0.40.22
by Parth Malwankar
fixed display path formatting on windows |
158 |
path = path.replace('\\', '/') |
159 |
path = path.replace(relpath + '/', '', 1) |
|
|
0.41.8
by Parth Malwankar
code cleanup. |
160 |
return path |
161 |
||
162 |
||
|
0.41.9
by Parth Malwankar
refactored code towards support for working tree grep. |
163 |
def versioned_file_grep(tree, id, relpath, path, patternc, eol_marker, |
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
164 |
line_number, revno, print_revno, include, exclude, outf, |
165 |
path_prefix = None): |
|
|
0.41.10
by Parth Malwankar
code cleanup. added comments. path adjustment is now done |
166 |
"""Create a file object for the specified id and pass it on to _file_grep. |
167 |
"""
|
|
168 |
||
169 |
path = _make_display_path(relpath, path) |
|
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
170 |
file_text = tree.get_file_text(id) |
171 |
_file_grep(file_text, relpath, path, patternc, eol_marker, |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
172 |
line_number, revno, print_revno, include, exclude, outf, path_prefix) |
173 |
||
174 |
def _path_in_glob_list(path, glob_list): |
|
175 |
present = False |
|
176 |
for glob in glob_list: |
|
177 |
if fnmatch(path, glob): |
|
178 |
present = True |
|
179 |
break
|
|
180 |
return present |
|
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
181 |
|
182 |
def _file_grep(file_text, relpath, path, patternc, eol_marker, |
|
|
0.41.21
by Parth Malwankar
include/exclude working now. tests not added. |
183 |
line_number, revno, print_revno, include, exclude, outf, |
184 |
path_prefix=None): |
|
185 |
||
186 |
if include and not _path_in_glob_list(path, include): |
|
187 |
return
|
|
188 |
||
189 |
if exclude and _path_in_glob_list(path, exclude): |
|
190 |
return
|
|
|
0.41.8
by Parth Malwankar
code cleanup. |
191 |
|
|
0.41.9
by Parth Malwankar
refactored code towards support for working tree grep. |
192 |
# test and skip binary files
|
|
0.41.12
by Parth Malwankar
initial support for working tree grep (no test cases yet!) |
193 |
str_file = cStringIO.StringIO(file_text) |
|
0.41.9
by Parth Malwankar
refactored code towards support for working tree grep. |
194 |
try: |
195 |
file_iter = textfile.text_file(str_file) |
|
196 |
except errors.BinaryFile, e: |
|
197 |
trace.warning("Binary file '%s' skipped." % path) |
|
198 |
return
|
|
199 |
||
|
0.40.52
by Parth Malwankar
code cleanup and documentation |
200 |
if path_prefix and path_prefix != '.': |
201 |
# user has passed a dir arg, show that as result prefix
|
|
202 |
path = osutils.pathjoin(path_prefix, path) |
|
203 |
||
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
204 |
revfmt = '' |
205 |
if print_revno: |
|
206 |
revfmt = "~%s" |
|
207 |
||
208 |
fmt_with_n = path + revfmt + ":%d:%s" + eol_marker |
|
209 |
fmt_without_n = path + revfmt + ":%s" + eol_marker |
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
210 |
|
|
0.41.10
by Parth Malwankar
code cleanup. added comments. path adjustment is now done |
211 |
# grep through iterable file object and print out the lines
|
212 |
# matching the compiled pattern in the specified format.
|
|
|
0.40.24
by Parth Malwankar
added support for --line-number. |
213 |
index = 1 |
|
0.41.8
by Parth Malwankar
code cleanup. |
214 |
for line in file_iter: |
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
215 |
res = patternc.search(line) |
216 |
if res: |
|
|
0.40.24
by Parth Malwankar
added support for --line-number. |
217 |
if line_number: |
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
218 |
if print_revno: |
219 |
out = (revno, index, line.strip()) |
|
220 |
else: |
|
221 |
out = (index, line.strip()) |
|
222 |
outf.write(fmt_with_n % out) |
|
|
0.40.24
by Parth Malwankar
added support for --line-number. |
223 |
else: |
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
224 |
if print_revno: |
225 |
out = (revno, line.strip()) |
|
226 |
else: |
|
227 |
out = (line.strip(),) |
|
228 |
outf.write(fmt_without_n % out) |
|
229 |
||
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
230 |
index += 1 |
231 |
||
232 |