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.40.9
by Parth Malwankar
factored out grep related code to grep.py |
22 |
|
23 |
from bzrlib import (
|
|
24 |
errors,
|
|
25 |
lazy_regex,
|
|
|
0.40.47
by Parth Malwankar
fixes bug #531336. binary files are now skipped. |
26 |
osutils,
|
27 |
textfile,
|
|
28 |
trace,
|
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
29 |
)
|
30 |
""") |
|
31 |
||
32 |
def compile_pattern(pattern, flags=0): |
|
33 |
patternc = None |
|
34 |
try: |
|
35 |
# use python's re.compile as we need to catch re.error in case of bad pattern
|
|
36 |
lazy_regex.reset_compile() |
|
37 |
patternc = re.compile(pattern, flags) |
|
38 |
except re.error, e: |
|
39 |
raise errors.BzrError("Invalid pattern: '%s'" % pattern) |
|
40 |
return patternc |
|
41 |
||
|
0.40.43
by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep |
42 |
def dir_grep(tree, path, relpath, recursive, line_number, compiled_pattern, |
|
0.40.44
by Parth Malwankar
improved display of path when dir is given as argument |
43 |
from_root, eol_marker, revno, print_revno, outf, path_prefix): |
|
0.40.43
by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep |
44 |
# setup relpath to open files relative to cwd
|
45 |
rpath = relpath |
|
46 |
if relpath: |
|
47 |
rpath = osutils.pathjoin('..',relpath) |
|
48 |
||
49 |
tree.lock_read() |
|
50 |
try: |
|
51 |
from_dir = osutils.pathjoin(relpath, path) |
|
52 |
if from_root: |
|
53 |
# start searching recursively from root
|
|
54 |
from_dir=None |
|
55 |
recursive=True |
|
56 |
||
57 |
for fp, fc, fkind, fid, entry in tree.list_files(include_root=False, |
|
58 |
from_dir=from_dir, recursive=recursive): |
|
59 |
if fc == 'V' and fkind == 'file': |
|
60 |
file_grep(tree, fid, rpath, fp, compiled_pattern, |
|
|
0.40.44
by Parth Malwankar
improved display of path when dir is given as argument |
61 |
eol_marker, line_number, revno, print_revno, outf, path_prefix) |
|
0.40.43
by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep |
62 |
finally: |
63 |
tree.unlock() |
|
64 |
||
65 |
||
66 |
def file_grep(tree, id, relpath, path, patternc, eol_marker, |
|
|
0.40.44
by Parth Malwankar
improved display of path when dir is given as argument |
67 |
line_number, revno, print_revno, outf, path_prefix = None): |
|
0.40.42
by Parth Malwankar
fix to make grep paths relative to cwd |
68 |
|
|
0.40.15
by Parth Malwankar
some fixes and test updates |
69 |
if relpath: |
|
0.40.52
by Parth Malwankar
code cleanup and documentation |
70 |
# update path so to display it w.r.t cwd
|
71 |
# handle windows slash separator
|
|
|
0.40.20
by Parth Malwankar
used path functions from bzrlib.osutils |
72 |
path = osutils.normpath(osutils.pathjoin(relpath, path)) |
|
0.40.22
by Parth Malwankar
fixed display path formatting on windows |
73 |
path = path.replace('\\', '/') |
74 |
path = path.replace(relpath + '/', '', 1) |
|
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
75 |
|
|
0.40.52
by Parth Malwankar
code cleanup and documentation |
76 |
if path_prefix and path_prefix != '.': |
77 |
# user has passed a dir arg, show that as result prefix
|
|
78 |
path = osutils.pathjoin(path_prefix, path) |
|
79 |
||
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
80 |
revfmt = '' |
81 |
if print_revno: |
|
82 |
revfmt = "~%s" |
|
83 |
||
84 |
fmt_with_n = path + revfmt + ":%d:%s" + eol_marker |
|
85 |
fmt_without_n = path + revfmt + ":%s" + eol_marker |
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
86 |
|
|
0.40.52
by Parth Malwankar
code cleanup and documentation |
87 |
# test and skip binary files
|
|
0.40.47
by Parth Malwankar
fixes bug #531336. binary files are now skipped. |
88 |
str_file = cStringIO.StringIO(tree.get_file_text(id)) |
89 |
try: |
|
90 |
iter_file = textfile.text_file(str_file) |
|
91 |
except errors.BinaryFile, e: |
|
92 |
trace.warning("Binary file '%s' skipped." % path) |
|
93 |
return
|
|
94 |
||
|
0.40.24
by Parth Malwankar
added support for --line-number. |
95 |
index = 1 |
|
0.40.47
by Parth Malwankar
fixes bug #531336. binary files are now skipped. |
96 |
for line in iter_file: |
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
97 |
res = patternc.search(line) |
98 |
if res: |
|
|
0.40.24
by Parth Malwankar
added support for --line-number. |
99 |
if line_number: |
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
100 |
if print_revno: |
101 |
out = (revno, index, line.strip()) |
|
102 |
else: |
|
103 |
out = (index, line.strip()) |
|
104 |
outf.write(fmt_with_n % out) |
|
|
0.40.24
by Parth Malwankar
added support for --line-number. |
105 |
else: |
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
106 |
if print_revno: |
107 |
out = (revno, line.strip()) |
|
108 |
else: |
|
109 |
out = (line.strip(),) |
|
110 |
outf.write(fmt_without_n % out) |
|
111 |
||
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
112 |
index += 1 |
113 |
||
114 |