/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
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
"""bzr grep"""
17
18
19
from bzrlib.lazy_import import lazy_import
20
lazy_import(globals(), """
21
import os
22
import re
0.40.47 by Parth Malwankar
fixes bug #531336. binary files are now skipped.
23
import cStringIO
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
24
25
from bzrlib import (
26
    errors,
27
    lazy_regex,
0.40.47 by Parth Malwankar
fixes bug #531336. binary files are now skipped.
28
    osutils,
29
    textfile,
30
    trace,
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
31
    )
32
""")
33
34
def compile_pattern(pattern, flags=0):
35
    patternc = None
36
    try:
37
        # use python's re.compile as we need to catch re.error in case of bad pattern
38
        lazy_regex.reset_compile()
39
        patternc = re.compile(pattern, flags)
40
    except re.error, e:
41
        raise errors.BzrError("Invalid pattern: '%s'" % pattern)
42
    return patternc
43
0.40.43 by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep
44
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
45
    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
46
        # setup relpath to open files relative to cwd
47
        rpath = relpath
48
        if relpath:
49
            rpath = osutils.pathjoin('..',relpath)
50
51
        tree.lock_read()
52
        try:
53
            from_dir = osutils.pathjoin(relpath, path)
54
            if from_root:
55
                # start searching recursively from root
56
                from_dir=None
57
                recursive=True
58
59
            for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
60
                from_dir=from_dir, recursive=recursive):
61
                if fc == 'V' and fkind == 'file':
62
                    file_grep(tree, fid, rpath, fp, compiled_pattern,
0.40.44 by Parth Malwankar
improved display of path when dir is given as argument
63
                        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
64
        finally:
65
            tree.unlock()
66
67
68
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
69
        line_number, revno, print_revno, outf, path_prefix = None):
0.40.42 by Parth Malwankar
fix to make grep paths relative to cwd
70
0.40.15 by Parth Malwankar
some fixes and test updates
71
    if relpath:
0.40.52 by Parth Malwankar
code cleanup and documentation
72
        # update path so to display it w.r.t cwd
73
        # handle windows slash separator
0.40.20 by Parth Malwankar
used path functions from bzrlib.osutils
74
        path = osutils.normpath(osutils.pathjoin(relpath, path))
0.40.22 by Parth Malwankar
fixed display path formatting on windows
75
        path = path.replace('\\', '/')
76
        path = path.replace(relpath + '/', '', 1)
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
77
0.40.52 by Parth Malwankar
code cleanup and documentation
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
    revfmt = ''
83
    if print_revno:
84
        revfmt = "~%s"
85
86
    fmt_with_n = path + revfmt + ":%d:%s" + eol_marker
87
    fmt_without_n = path + revfmt + ":%s" + eol_marker
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
88
0.40.52 by Parth Malwankar
code cleanup and documentation
89
    # test and skip binary files
0.40.47 by Parth Malwankar
fixes bug #531336. binary files are now skipped.
90
    str_file = cStringIO.StringIO(tree.get_file_text(id))
91
    try:
92
        iter_file = textfile.text_file(str_file)
93
    except errors.BinaryFile, e:
94
        trace.warning("Binary file '%s' skipped." % path)
95
        return
96
0.40.24 by Parth Malwankar
added support for --line-number.
97
    index = 1
0.40.47 by Parth Malwankar
fixes bug #531336. binary files are now skipped.
98
    for line in iter_file:
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
99
        res = patternc.search(line)
100
        if res:
0.40.24 by Parth Malwankar
added support for --line-number.
101
            if line_number:
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
102
                if print_revno:
103
                    out = (revno, index, line.strip())
104
                else:
105
                    out = (index, line.strip())
106
                outf.write(fmt_with_n % out)
0.40.24 by Parth Malwankar
added support for --line-number.
107
            else:
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
108
                if print_revno:
109
                    out = (revno, line.strip())
110
                else:
111
                    out = (line.strip(),)
112
                outf.write(fmt_without_n % out)
113
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
114
        index += 1
115
116