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