/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
0.40.47 by Parth Malwankar
fixes bug #531336. binary files are now skipped.
24
import cStringIO
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
25
26
from bzrlib import (
27
    errors,
28
    lazy_regex,
0.40.47 by Parth Malwankar
fixes bug #531336. binary files are now skipped.
29
    osutils,
30
    textfile,
31
    trace,
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
32
    )
33
""")
34
35
def compile_pattern(pattern, flags=0):
36
    patternc = None
37
    try:
38
        # use python's re.compile as we need to catch re.error in case of bad pattern
39
        lazy_regex.reset_compile()
40
        patternc = re.compile(pattern, flags)
41
    except re.error, e:
42
        raise errors.BzrError("Invalid pattern: '%s'" % pattern)
43
    return patternc
44
0.40.43 by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep
45
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
46
    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
47
        # setup relpath to open files relative to cwd
48
        rpath = relpath
49
        if relpath:
50
            rpath = osutils.pathjoin('..',relpath)
51
52
        tree.lock_read()
53
        try:
54
            from_dir = osutils.pathjoin(relpath, path)
55
            if from_root:
56
                # start searching recursively from root
57
                from_dir=None
58
                recursive=True
59
60
            for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
61
                from_dir=from_dir, recursive=recursive):
62
                if fc == 'V' and fkind == 'file':
63
                    file_grep(tree, fid, rpath, fp, compiled_pattern,
0.40.44 by Parth Malwankar
improved display of path when dir is given as argument
64
                        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
65
        finally:
66
            tree.unlock()
67
68
69
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
70
        line_number, revno, print_revno, outf, path_prefix = None):
0.40.42 by Parth Malwankar
fix to make grep paths relative to cwd
71
0.40.15 by Parth Malwankar
some fixes and test updates
72
    if relpath:
0.40.20 by Parth Malwankar
used path functions from bzrlib.osutils
73
        path = osutils.normpath(osutils.pathjoin(relpath, path))
0.40.22 by Parth Malwankar
fixed display path formatting on windows
74
        path = path.replace('\\', '/')
75
        path = path.replace(relpath + '/', '', 1)
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
76
77
    revfmt = ''
78
    if print_revno:
79
        revfmt = "~%s"
80
0.40.44 by Parth Malwankar
improved display of path when dir is given as argument
81
    if path_prefix and path_prefix != '.':
82
        # user has passed a dir arg, show that as result prefix
83
        path = osutils.pathjoin(path_prefix, path)
84
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
85
    fmt_with_n = path + revfmt + ":%d:%s" + eol_marker
86
    fmt_without_n = path + revfmt + ":%s" + eol_marker
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
87
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