/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.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
85
            path = os.path.abspath(path)
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
86
            if osutils.isdir(path):
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
87
                tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(path)
88
                tree.lock_read()
89
                try:
90
                    for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
91
                        from_dir=relpath, recursive=recursive):
92
                        if fc == 'V' and fkind == 'file':
93
                            self.file_grep(relpath, fp, patternc, eol_marker)
94
                finally:
95
                    tree.unlock()
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
96
            else:
97
                # if user has explicitly specified a file
98
                # we don't care if its versioned
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
99
                self.file_grep('.', path, patternc, eol_marker)
0.40.2 by Parth Malwankar
initial framework for grep
100
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
101
    def file_grep(self, relpath, path, patternc, eol_marker):
0.40.3 by Parth Malwankar
basic file grep is working
102
        index = 1
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
103
        path = os.path.normpath(os.path.join('..', relpath, path))
104
        fmt = path + ":%d:%s" + eol_marker
0.40.3 by Parth Malwankar
basic file grep is working
105
        for line in open(path):
106
            res = patternc.search(line)
107
            if res:
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
108
                self.outf.write( fmt % (index, line.strip()))
0.40.3 by Parth Malwankar
basic file grep is working
109
            index += 1
0.40.1 by Parth Malwankar
initial skeleton
110
111
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
112