/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.28 by Parth Malwankar
basic revision spec is used for grepping
33
0.40.2 by Parth Malwankar
initial framework for grep
34
import bzrlib
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
35
from bzrlib.revisionspec import RevisionSpec, RevisionInfo
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
36
from bzrlib.workingtree import WorkingTree
0.40.2 by Parth Malwankar
initial framework for grep
37
from bzrlib import (
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
38
    osutils,
0.40.2 by Parth Malwankar
initial framework for grep
39
    bzrdir,
0.40.7 by Parth Malwankar
issue a warning while searching unversioned file
40
    trace,
0.40.2 by Parth Malwankar
initial framework for grep
41
    )
42
""")
0.40.1 by Parth Malwankar
initial skeleton
43
44
version_info = (0, 1)
45
46
class cmd_grep(Command):
0.40.2 by Parth Malwankar
initial framework for grep
47
    """Print lines matching PATTERN for specified files.
48
    """
49
50
    takes_args = ['pattern', 'path*']
51
    takes_options = [
52
        'verbose',
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
53
        'revision',
0.40.24 by Parth Malwankar
added support for --line-number.
54
        Option('line-number', short_name='n',
55
               help='show 1-based line number.'),
0.40.2 by Parth Malwankar
initial framework for grep
56
        Option('ignore-case', short_name='i',
57
               help='ignore case distinctions while matching.'),
58
        Option('recursive', short_name='R',
59
               help='Recurse into subdirectories.'),
60
        Option('from-root',
0.40.18 by Parth Malwankar
improved help string
61
               help='Search for pattern starting from the root of the branch. '
62
               '(implies --recursive)'),
0.40.19 by Parth Malwankar
null option should be -Z instead of -z
63
        Option('null', short_name='Z',
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
64
               help='Write an ascii NUL (\\0) separator '
65
               'between output lines rather than a newline.'),
0.40.2 by Parth Malwankar
initial framework for grep
66
        ]
67
68
69
    @display_command
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
70
    def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False,
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
71
            null=False, line_number=False, path_list=None, revision=None, pattern=None):
0.40.2 by Parth Malwankar
initial framework for grep
72
        if path_list == None:
73
            path_list = ['.']
74
        else:
75
            if from_root:
76
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
77
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
78
        print_revno = False
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
79
        if revision == None:
80
            revision = [RevisionSpec.from_string("last:1")]
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
81
        else:
82
            print_revno = True # used to print revno in output.
83
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
84
        start_rev = revision[0]
85
        end_rev = None
86
        if len(revision) == 2:
87
            end_rev = revision[1]
88
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
89
        eol_marker = '\n'
90
        if null:
91
            eol_marker = '\0'
92
0.40.3 by Parth Malwankar
basic file grep is working
93
        re_flags = 0
94
        if ignore_case:
95
            re_flags = re.IGNORECASE
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
96
        patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
97
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
98
        wt, relpath = WorkingTree.open_containing('.')
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
99
        id_to_revno = wt.branch.get_revision_id_to_revno_map()
100
101
        rev = start_rev
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
102
103
        wt.lock_read()
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
104
        try:
105
            for path in path_list:
106
                tree = rev.as_tree(wt.branch)
107
                revid = rev.as_revision_id(wt.branch)
108
                try:
109
                    revno = ".".join([str(n) for n in id_to_revno[revid]])
110
                except KeyError, e:
0.40.7 by Parth Malwankar
issue a warning while searching unversioned file
111
                    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
112
                    continue
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
113
114
                if osutils.isdir(path):
115
                    self._grep_dir(tree, relpath, recursive, line_number,
116
                        patternc, from_root, eol_marker, revno, print_revno)
117
                else:
118
                    id = tree.path2id(path)
119
                    if not id:
120
                        trace.warning("warning: file '%s' is not versioned." % path)
121
                        continue
122
                    tree.lock_read()
123
                    try:
124
                        grep.file_grep(tree, id, '.', path, patternc, eol_marker,
125
                            self.outf, line_number, revno, print_revno)
126
                    finally:
127
                        tree.unlock()
128
        finally:
129
            wt.unlock()
0.40.1 by Parth Malwankar
initial skeleton
130
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
131
    def _grep_dir(self, tree, relpath, recursive, line_number, compiled_pattern,
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
132
        from_root, eol_marker, revno, print_revno):
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
133
            # setup relpath to open files relative to cwd
134
            rpath = relpath
135
            if relpath:
136
                rpath = osutils.pathjoin('..',relpath)
137
138
            tree.lock_read()
139
            try:
140
                if from_root:
141
                    # start searching recursively from root
142
                    relpath=None
143
                    recursive=True
144
145
                for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
146
                    from_dir=relpath, recursive=recursive):
147
                    if fc == 'V' and fkind == 'file':
148
                        grep.file_grep(tree, fid, rpath, fp, compiled_pattern,
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
149
                            eol_marker, self.outf, line_number, revno, print_revno)
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
150
            finally:
151
                tree.unlock()
152
153
0.40.1 by Parth Malwankar
initial skeleton
154
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
155
0.40.11 by Parth Malwankar
added basic test
156
def test_suite():
157
    from bzrlib.tests import TestUtil
158
159
    suite = TestUtil.TestSuite()
160
    loader = TestUtil.TestLoader()
161
    testmod_names = [
162
        'test_grep',
163
        ]
164
165
    suite.addTest(loader.loadTestsFromModuleNames(
166
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
167
    return suite
168