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 |
# Copyright (C) 2010 Parth Malwankar <parth.malwankar@gmail.com>
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License as published by
|
|
6 |
# the Free Software Foundation; either version 2 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
17 |
"""bzr grep"""
|
|
18 |
||
19 |
||
20 |
from bzrlib.lazy_import import lazy_import |
|
21 |
lazy_import(globals(), """ |
|
22 |
import os
|
|
23 |
import re
|
|
24 |
||
25 |
from bzrlib import (
|
|
|
0.40.20
by Parth Malwankar
used path functions from bzrlib.osutils |
26 |
osutils,
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
27 |
errors,
|
28 |
lazy_regex,
|
|
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.20
by Parth Malwankar
used path functions from bzrlib.osutils |
70 |
path = osutils.normpath(osutils.pathjoin(relpath, path)) |
|
0.40.22
by Parth Malwankar
fixed display path formatting on windows |
71 |
path = path.replace('\\', '/') |
72 |
path = path.replace(relpath + '/', '', 1) |
|
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
73 |
|
74 |
revfmt = '' |
|
75 |
if print_revno: |
|
76 |
revfmt = "~%s" |
|
77 |
||
|
0.40.44
by Parth Malwankar
improved display of path when dir is given as argument |
78 |
if path_prefix and path_prefix != '.': |
79 |
# user has passed a dir arg, show that as result prefix
|
|
80 |
path = osutils.pathjoin(path_prefix, path) |
|
81 |
||
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
82 |
fmt_with_n = path + revfmt + ":%d:%s" + eol_marker |
83 |
fmt_without_n = path + revfmt + ":%s" + eol_marker |
|
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
84 |
|
|
0.40.24
by Parth Malwankar
added support for --line-number. |
85 |
index = 1 |
|
0.40.16
by Parth Malwankar
tree.get_file_lines is now used to get lines of text |
86 |
for line in tree.get_file_lines(id): |
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
87 |
res = patternc.search(line) |
88 |
if res: |
|
|
0.40.24
by Parth Malwankar
added support for --line-number. |
89 |
if line_number: |
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
90 |
if print_revno: |
91 |
out = (revno, index, line.strip()) |
|
92 |
else: |
|
93 |
out = (index, line.strip()) |
|
94 |
outf.write(fmt_with_n % out) |
|
|
0.40.24
by Parth Malwankar
added support for --line-number. |
95 |
else: |
|
0.40.30
by Parth Malwankar
revno is now printed when rspec is given |
96 |
if print_revno: |
97 |
out = (revno, line.strip()) |
|
98 |
else: |
|
99 |
out = (line.strip(),) |
|
100 |
outf.write(fmt_without_n % out) |
|
101 |
||
|
0.40.9
by Parth Malwankar
factored out grep related code to grep.py |
102 |
index += 1 |
103 |
||
104 |