/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.36 by Parth Malwankar
improved docs and minor code cleanup
47
    """Print lines matching PATTERN for specified files and revisions.
48
49
    This command searches the specified files and revisions for a given pattern.
50
    The pattern is specified as a Python regular expressions[1].
51
    If the file name is not specified the file revisions in the current directory
52
    are searched. If the revision number is not specified, the latest revision is
53
    searched.
54
55
    Note that this command is different from POSIX grep in that it searches the
56
    revisions of the branch and not the working copy. Unversioned files and
57
    uncommitted changes are not seen.
58
59
    When searching a pattern, the output is shown in the 'filepath:string' format.
60
    If a revision is explicitly searched, the output is shown as 'filepath~N:string',
61
    where N is the revision number.
62
63
    [1] http://docs.python.org/library/re.html#regular-expression-syntax
0.40.2 by Parth Malwankar
initial framework for grep
64
    """
65
66
    takes_args = ['pattern', 'path*']
67
    takes_options = [
68
        'verbose',
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
69
        'revision',
0.40.24 by Parth Malwankar
added support for --line-number.
70
        Option('line-number', short_name='n',
71
               help='show 1-based line number.'),
0.40.2 by Parth Malwankar
initial framework for grep
72
        Option('ignore-case', short_name='i',
73
               help='ignore case distinctions while matching.'),
74
        Option('recursive', short_name='R',
75
               help='Recurse into subdirectories.'),
76
        Option('from-root',
0.40.18 by Parth Malwankar
improved help string
77
               help='Search for pattern starting from the root of the branch. '
78
               '(implies --recursive)'),
0.40.19 by Parth Malwankar
null option should be -Z instead of -z
79
        Option('null', short_name='Z',
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
80
               help='Write an ascii NUL (\\0) separator '
81
               'between output lines rather than a newline.'),
0.40.2 by Parth Malwankar
initial framework for grep
82
        ]
83
84
85
    @display_command
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
86
    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
87
            null=False, line_number=False, path_list=None, revision=None, pattern=None):
0.40.2 by Parth Malwankar
initial framework for grep
88
        if path_list == None:
89
            path_list = ['.']
90
        else:
91
            if from_root:
92
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
93
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
94
        print_revno = False
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
95
        if revision == None:
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
96
            # grep on latest revision by default
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
97
            revision = [RevisionSpec.from_string("last:1")]
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
98
        else:
99
            print_revno = True # used to print revno in output.
100
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
101
        start_rev = revision[0]
102
        end_rev = None
103
        if len(revision) == 2:
104
            end_rev = revision[1]
105
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
106
        eol_marker = '\n'
107
        if null:
108
            eol_marker = '\0'
109
0.40.3 by Parth Malwankar
basic file grep is working
110
        re_flags = 0
111
        if ignore_case:
112
            re_flags = re.IGNORECASE
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
113
        patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
114
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
115
        wt, relpath = WorkingTree.open_containing('.')
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
116
        id_to_revno = wt.branch.get_revision_id_to_revno_map()
117
118
        rev = start_rev
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
119
120
        wt.lock_read()
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
121
        try:
122
            for path in path_list:
123
                tree = rev.as_tree(wt.branch)
124
                revid = rev.as_revision_id(wt.branch)
125
                try:
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
126
                    revno = self._revno_str(id_to_revno, revid)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
127
                except KeyError, e:
0.40.33 by Parth Malwankar
better message while skipping unversioned file
128
                    self._skip_file(path)
0.40.16 by Parth Malwankar
tree.get_file_lines is now used to get lines of text
129
                    continue
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
130
131
                if osutils.isdir(path):
132
                    self._grep_dir(tree, relpath, recursive, line_number,
133
                        patternc, from_root, eol_marker, revno, print_revno)
134
                else:
135
                    id = tree.path2id(path)
136
                    if not id:
0.40.33 by Parth Malwankar
better message while skipping unversioned file
137
                        self._skip_file(path)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
138
                        continue
139
                    tree.lock_read()
140
                    try:
141
                        grep.file_grep(tree, id, '.', path, patternc, eol_marker,
142
                            self.outf, line_number, revno, print_revno)
143
                    finally:
144
                        tree.unlock()
145
        finally:
146
            wt.unlock()
0.40.1 by Parth Malwankar
initial skeleton
147
0.40.33 by Parth Malwankar
better message while skipping unversioned file
148
    def _skip_file(self, path):
149
        trace.warning("warning: skipped unversioned file '%s'." % path)
150
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
151
    def _revno_str(self, id_to_revno_dict, revid):
152
        revno = ".".join([str(n) for n in id_to_revno_dict[revid]])
153
        return revno
154
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
155
    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
156
        from_root, eol_marker, revno, print_revno):
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
157
            # setup relpath to open files relative to cwd
158
            rpath = relpath
159
            if relpath:
160
                rpath = osutils.pathjoin('..',relpath)
161
162
            tree.lock_read()
163
            try:
164
                if from_root:
165
                    # start searching recursively from root
166
                    relpath=None
167
                    recursive=True
168
169
                for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
170
                    from_dir=relpath, recursive=recursive):
171
                    if fc == 'V' and fkind == 'file':
172
                        grep.file_grep(tree, fid, rpath, fp, compiled_pattern,
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
173
                            eol_marker, self.outf, line_number, revno, print_revno)
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
174
            finally:
175
                tree.unlock()
176
177
0.40.1 by Parth Malwankar
initial skeleton
178
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
179
0.40.11 by Parth Malwankar
added basic test
180
def test_suite():
181
    from bzrlib.tests import TestUtil
182
183
    suite = TestUtil.TestSuite()
184
    loader = TestUtil.TestLoader()
185
    testmod_names = [
186
        'test_grep',
187
        ]
188
189
    suite.addTest(loader.loadTestsFromModuleNames(
190
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
191
    return suite
192