/brz/remove-bazaar

To get this branch, use:
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 (
26
    errors,
27
    lazy_regex,
28
    )
29
""")
30
31
def compile_pattern(pattern, flags=0):
32
    patternc = None
33
    try:
34
        # use python's re.compile as we need to catch re.error in case of bad pattern
35
        lazy_regex.reset_compile()
36
        patternc = re.compile(pattern, flags)
37
    except re.error, e:
38
        raise errors.BzrError("Invalid pattern: '%s'" % pattern)
39
    return patternc
40
41
42
def file_grep(relpath, path, patternc, eol_marker, outf):
43
    index = 1
0.40.13 by Parth Malwankar
added tests for -R and -i
44
    path = os.path.normpath(os.path.join(relpath, '/', path))
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
45
46
    path = path.replace(os.path.dirname(relpath) + '/', '', 1)
47
    fmt = path + ":%d:%s" + eol_marker
48
49
    for line in open(path):
50
        res = patternc.search(line)
51
        if res:
52
            outf.write( fmt % (index, line.strip()))
53
        index += 1
54
55