1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# Copyright (C) 2010 Parth Malwankar <parth.malwankar@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""bzr grep"""
import os
import sys
from bzrlib import errors, lazy_regex
from bzrlib.commands import Command, register_command, display_command
from bzrlib.option import (
Option,
)
from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
import re
import bzrlib
from bzrlib import (
osutils,
bzrdir,
trace,
)
""")
version_info = (0, 1)
class cmd_grep(Command):
"""Print lines matching PATTERN for specified files.
"""
takes_args = ['pattern', 'path*']
takes_options = [
'verbose',
Option('ignore-case', short_name='i',
help='ignore case distinctions while matching.'),
Option('recursive', short_name='R',
help='Recurse into subdirectories.'),
Option('from-root',
help='Search for pattern starting from the root of the branch.'),
Option('null', short_name='z',
help='Write an ascii NUL (\\0) separator '
'between output lines rather than a newline.'),
]
@display_command
def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False,
null=False, path_list=None, pattern=None):
if path_list == None:
path_list = ['.']
else:
if from_root:
raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
re_flags = 0
if ignore_case:
re_flags = re.IGNORECASE
eol_marker = '\n'
if null:
eol_marker = '\0'
patternc = None
try:
# use python's re.compile as we need to catch re.error in case of bad pattern
lazy_regex.reset_compile()
patternc = re.compile(pattern, re_flags)
except re.error, e:
raise errors.BzrError("Invalid pattern: '%s'" % pattern)
for path in path_list:
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(path)
if osutils.isdir(path):
# setup rpath to open files relative to cwd
rpath = relpath
if relpath:
rpath = os.path.join('..',relpath)
tree.lock_read()
try:
for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
from_dir=relpath, recursive=recursive):
if fc == 'V' and fkind == 'file':
self.file_grep(rpath, fp, patternc, eol_marker)
finally:
tree.unlock()
else:
# if user has explicitly specified a file
# we don't care if its versioned
if not tree.path2id(path):
trace.warning("warning: file '%s' is not versioned." % path)
self.file_grep('.', path, patternc, eol_marker)
def file_grep(self, relpath, path, patternc, eol_marker):
index = 1
path = os.path.normpath(os.path.join(relpath, path))
fmt = path + ":%d:%s" + eol_marker
for line in open(path):
res = patternc.search(line)
if res:
self.outf.write( fmt % (index, line.strip()))
index += 1
register_command(cmd_grep)
|