/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
68
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch('.')
69
0.40.3 by Parth Malwankar
basic file grep is working
70
        re_flags = 0
71
        if ignore_case:
72
            re_flags = re.IGNORECASE
73
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
74
        eol_marker = '\n'
75
        if null:
76
            eol_marker = '\0'
77
0.40.3 by Parth Malwankar
basic file grep is working
78
        patternc = None
79
        try:
80
            # use python's re.compile as we need to catch re.error in case of bad pattern
81
            lazy_regex.reset_compile()
82
            patternc = re.compile(pattern, re_flags)
83
        except re.error, e:
84
            raise errors.BzrError("Invalid pattern: '%s'" % pattern)
85
0.40.2 by Parth Malwankar
initial framework for grep
86
        tree.lock_read()
87
        self.add_cleanup(tree.unlock)
88
        for path in path_list:
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
89
            if osutils.isdir(path):
90
                from_dir = os.path.abspath(os.path.join(relpath, path))
91
                for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
92
                    from_dir=from_dir, recursive=recursive):
93
                    if fc == 'V' and fkind == 'file':
94
                        self.file_grep(fp, patternc, eol_marker)
95
            else:
96
                # if user has explicitly specified a file
97
                # we don't care if its versioned
98
                self.file_grep(path, patternc, eol_marker)
0.40.2 by Parth Malwankar
initial framework for grep
99
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
100
    def file_grep(self, path, patternc, eol_marker):
0.40.3 by Parth Malwankar
basic file grep is working
101
        index = 1
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
102
        fmt = "%s:%d:%s" + eol_marker
0.40.3 by Parth Malwankar
basic file grep is working
103
        for line in open(path):
104
            res = patternc.search(line)
105
            if res:
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
106
                self.outf.write( fmt % (path, index, line.strip()))
0.40.3 by Parth Malwankar
basic file grep is working
107
            index += 1
0.40.1 by Parth Malwankar
initial skeleton
108
109
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
110