/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.1 by Parth Malwankar
initial skeleton
2
# Copyright (C) 2010 Parth Malwankar <parth.malwankar@gmail.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
"""bzr grep"""
18
19
import os
0.40.3 by Parth Malwankar
basic file grep is working
20
import sys
0.40.1 by Parth Malwankar
initial skeleton
21
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
22
from bzrlib import errors
0.40.2 by Parth Malwankar
initial framework for grep
23
from bzrlib.commands import Command, register_command, display_command
24
from bzrlib.option import (
25
    Option,
26
    )
27
28
from bzrlib.lazy_import import lazy_import
29
lazy_import(globals(), """
0.40.3 by Parth Malwankar
basic file grep is working
30
import re
31
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
32
import grep
0.40.2 by Parth Malwankar
initial framework for grep
33
import bzrlib
34
from bzrlib import (
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
35
    osutils,
0.40.2 by Parth Malwankar
initial framework for grep
36
    bzrdir,
0.40.7 by Parth Malwankar
issue a warning while searching unversioned file
37
    trace,
0.40.2 by Parth Malwankar
initial framework for grep
38
    )
39
""")
0.40.1 by Parth Malwankar
initial skeleton
40
41
version_info = (0, 1)
42
43
class cmd_grep(Command):
0.40.2 by Parth Malwankar
initial framework for grep
44
    """Print lines matching PATTERN for specified files.
45
    """
46
47
    takes_args = ['pattern', 'path*']
48
    takes_options = [
49
        'verbose',
0.40.24 by Parth Malwankar
added support for --line-number.
50
        Option('line-number', short_name='n',
51
               help='show 1-based line number.'),
0.40.2 by Parth Malwankar
initial framework for grep
52
        Option('ignore-case', short_name='i',
53
               help='ignore case distinctions while matching.'),
54
        Option('recursive', short_name='R',
55
               help='Recurse into subdirectories.'),
56
        Option('from-root',
0.40.18 by Parth Malwankar
improved help string
57
               help='Search for pattern starting from the root of the branch. '
58
               '(implies --recursive)'),
0.40.19 by Parth Malwankar
null option should be -Z instead of -z
59
        Option('null', short_name='Z',
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
60
               help='Write an ascii NUL (\\0) separator '
61
               'between output lines rather than a newline.'),
0.40.2 by Parth Malwankar
initial framework for grep
62
        ]
63
64
65
    @display_command
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
66
    def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False,
0.40.24 by Parth Malwankar
added support for --line-number.
67
            null=False, line_number=False, path_list=None, pattern=None):
0.40.2 by Parth Malwankar
initial framework for grep
68
        if path_list == None:
69
            path_list = ['.']
70
        else:
71
            if from_root:
72
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
73
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
74
        eol_marker = '\n'
75
        if null:
76
            eol_marker = '\0'
77
0.40.3 by Parth Malwankar
basic file grep is working
78
        re_flags = 0
79
        if ignore_case:
80
            re_flags = re.IGNORECASE
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
81
        patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
82
0.40.2 by Parth Malwankar
initial framework for grep
83
        for path in path_list:
0.40.7 by Parth Malwankar
issue a warning while searching unversioned file
84
            tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(path)
85
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
86
            if osutils.isdir(path):
0.40.6 by Parth Malwankar
fixed relative path handling
87
                # setup rpath to open files relative to cwd
88
                rpath = relpath
89
                if relpath:
0.40.20 by Parth Malwankar
used path functions from bzrlib.osutils
90
                    rpath = osutils.pathjoin('..',relpath)
0.40.6 by Parth Malwankar
fixed relative path handling
91
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
92
                tree.lock_read()
93
                try:
0.40.17 by Parth Malwankar
from-root is working now.
94
                    if from_root:
95
                        # start searching recursively from root
96
                        relpath=None
97
                        recursive=True
98
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
99
                    for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
100
                        from_dir=relpath, recursive=recursive):
101
                        if fc == 'V' and fkind == 'file':
0.40.24 by Parth Malwankar
added support for --line-number.
102
                            grep.file_grep(tree, fid, rpath, fp, patternc,
103
                                eol_marker, self.outf, line_number)
0.40.5 by Parth Malwankar
grep is working now howerver paths are not printed in the
104
                finally:
105
                    tree.unlock()
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
106
            else:
0.40.16 by Parth Malwankar
tree.get_file_lines is now used to get lines of text
107
                id = tree.path2id(path)
108
                if not id:
0.40.7 by Parth Malwankar
issue a warning while searching unversioned file
109
                    trace.warning("warning: file '%s' is not versioned." % path)
0.40.16 by Parth Malwankar
tree.get_file_lines is now used to get lines of text
110
                    continue
111
                tree.lock_read()
112
                try:
0.40.24 by Parth Malwankar
added support for --line-number.
113
                    grep.file_grep(tree, id, '.', path, patternc, eol_marker,
114
                        self.outf, line_number)
0.40.16 by Parth Malwankar
tree.get_file_lines is now used to get lines of text
115
                finally:
116
                    tree.unlock()
0.40.1 by Parth Malwankar
initial skeleton
117
118
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
119
0.40.11 by Parth Malwankar
added basic test
120
def test_suite():
121
    from bzrlib.tests import TestUtil
122
123
    suite = TestUtil.TestSuite()
124
    loader = TestUtil.TestLoader()
125
    testmod_names = [
126
        'test_grep',
127
        ]
128
129
    suite.addTest(loader.loadTestsFromModuleNames(
130
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
131
    return suite
132