/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.40.1 by Parth Malwankar
initial skeleton
1
# Copyright (C) 2010 Parth Malwankar <parth.malwankar@gmail.com>
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
import os
0.40.3 by Parth Malwankar
basic file grep is working
19
import sys
0.40.1 by Parth Malwankar
initial skeleton
20
0.40.3 by Parth Malwankar
basic file grep is working
21
from bzrlib import errors, lazy_regex
0.40.2 by Parth Malwankar
initial framework for grep
22
from bzrlib.commands import Command, register_command, display_command
23
from bzrlib.option import (
24
    Option,
25
    )
26
27
from bzrlib.lazy_import import lazy_import
28
lazy_import(globals(), """
0.40.3 by Parth Malwankar
basic file grep is working
29
import re
30
0.40.2 by Parth Malwankar
initial framework for grep
31
import bzrlib
32
from bzrlib import (
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
33
    osutils,
0.40.2 by Parth Malwankar
initial framework for grep
34
    bzrdir,
35
    )
36
""")
0.40.1 by Parth Malwankar
initial skeleton
37
38
version_info = (0, 1)
39
40
class cmd_grep(Command):
0.40.2 by Parth Malwankar
initial framework for grep
41
    """Print lines matching PATTERN for specified files.
42
    """
43
44
    takes_args = ['pattern', 'path*']
45
    takes_options = [
46
        'verbose',
47
        Option('ignore-case', short_name='i',
48
               help='ignore case distinctions while matching.'),
49
        Option('recursive', short_name='R',
50
               help='Recurse into subdirectories.'),
51
        Option('from-root',
52
               help='Search for pattern starting from the root of the branch.'),
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
53
        Option('null', short_name='z',
54
               help='Write an ascii NUL (\\0) separator '
55
               'between output lines rather than a newline.'),
0.40.2 by Parth Malwankar
initial framework for grep
56
        ]
57
58
59
    @display_command
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
60
    def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False,
61
            null=False, path_list=None, pattern=None):
0.40.2 by Parth Malwankar
initial framework for grep
62
        if path_list == None:
63
            path_list = ['.']
64
        else:
65
            if from_root:
66
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
67
0.40.3 by Parth Malwankar
basic file grep is working
68
        re_flags = 0
69
        if ignore_case:
70
            re_flags = re.IGNORECASE
71
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
72
        eol_marker = '\n'
73
        if null:
74
            eol_marker = '\0'
75
0.40.3 by Parth Malwankar
basic file grep is working
76
        patternc = None
77
        try:
78
            # use python's re.compile as we need to catch re.error in case of bad pattern
79
            lazy_regex.reset_compile()
80
            patternc = re.compile(pattern, re_flags)
81
        except re.error, e:
82
            raise errors.BzrError("Invalid pattern: '%s'" % pattern)
83
0.40.2 by Parth Malwankar
initial framework for grep
84
        for path in path_list:
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
85
            if osutils.isdir(path):
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
86
                tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(path)
0.40.6 by Parth Malwankar
fixed relative path handling
87
88
                # setup rpath to open files relative to cwd
89
                rpath = relpath
90
                if relpath:
91
                    rpath = os.path.join('..',relpath)
92
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
93
                tree.lock_read()
94
                try:
95
                    for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
96
                        from_dir=relpath, recursive=recursive):
97
                        if fc == 'V' and fkind == 'file':
0.40.6 by Parth Malwankar
fixed relative path handling
98
                            self.file_grep(rpath, fp, patternc, eol_marker)
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
99
                finally:
100
                    tree.unlock()
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
101
            else:
102
                # if user has explicitly specified a file
103
                # we don't care if its versioned
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
104
                self.file_grep('.', path, patternc, eol_marker)
0.40.2 by Parth Malwankar
initial framework for grep
105
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
106
    def file_grep(self, relpath, path, patternc, eol_marker):
0.40.3 by Parth Malwankar
basic file grep is working
107
        index = 1
0.40.6 by Parth Malwankar
fixed relative path handling
108
        path = os.path.normpath(os.path.join(relpath, path))
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
109
        fmt = path + ":%d:%s" + eol_marker
0.40.3 by Parth Malwankar
basic file grep is working
110
        for line in open(path):
111
            res = patternc.search(line)
112
            if res:
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
113
                self.outf.write( fmt % (index, line.strip()))
0.40.3 by Parth Malwankar
basic file grep is working
114
            index += 1
0.40.1 by Parth Malwankar
initial skeleton
115
116
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
117