/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 (
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
42
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
43
def file_grep(tree, id, relpath, path, patternc, eol_marker, outf,
44
        line_number, revno, print_revno):
0.40.15 by Parth Malwankar
some fixes and test updates
45
    if relpath:
0.40.20 by Parth Malwankar
used path functions from bzrlib.osutils
46
        path = osutils.normpath(osutils.pathjoin(relpath, path))
0.40.22 by Parth Malwankar
fixed display path formatting on windows
47
        path = path.replace('\\', '/')
48
        path = path.replace(relpath + '/', '', 1)
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
49
50
    revfmt = ''
51
    if print_revno:
52
        revfmt = "~%s"
53
54
    fmt_with_n = path + revfmt + ":%d:%s" + eol_marker
55
    fmt_without_n = path + revfmt + ":%s" + eol_marker
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
56
0.40.24 by Parth Malwankar
added support for --line-number.
57
    index = 1
0.40.16 by Parth Malwankar
tree.get_file_lines is now used to get lines of text
58
    for line in tree.get_file_lines(id):
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
59
        res = patternc.search(line)
60
        if res:
0.40.24 by Parth Malwankar
added support for --line-number.
61
            if line_number:
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
62
                if print_revno:
63
                    out = (revno, index, line.strip())
64
                else:
65
                    out = (index, line.strip())
66
                outf.write(fmt_with_n % out)
0.40.24 by Parth Malwankar
added support for --line-number.
67
            else:
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
68
                if print_revno:
69
                    out = (revno, line.strip())
70
                else:
71
                    out = (line.strip(),)
72
                outf.write(fmt_without_n % out)
73
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
74
        index += 1
75
76