/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
#
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
0.41.1 by Parth Malwankar
0.0.1 final
16
"""Print lines matching PATTERN for specified files and revisions."""
0.40.1 by Parth Malwankar
initial skeleton
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.9 by Parth Malwankar
factored out grep related code to grep.py
21
from bzrlib import errors
0.40.2 by Parth Malwankar
initial framework for grep
22
from bzrlib.commands import Command, register_command, display_command
0.41.19 by Parth Malwankar
updated to accept --include and --exclude. unused at this point.
23
from bzrlib.option import Option, ListOption
0.40.2 by Parth Malwankar
initial framework for grep
24
25
from bzrlib.lazy_import import lazy_import
26
lazy_import(globals(), """
0.40.3 by Parth Malwankar
basic file grep is working
27
import re
28
0.43.5 by Parth Malwankar
moved color.py to termcolor.py
29
from termcolor import allow_color
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
30
import grep
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
31
0.40.2 by Parth Malwankar
initial framework for grep
32
import bzrlib
0.41.11 by Parth Malwankar
moved top level grep code to versioned_grep.
33
from bzrlib.revisionspec import RevisionSpec
0.40.2 by Parth Malwankar
initial framework for grep
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.44.5 by Parth Malwankar
version information is now taken from version.py
40
import version
0.40.1 by Parth Malwankar
initial skeleton
41
0.44.5 by Parth Malwankar
version information is now taken from version.py
42
version_info = version.version_info
0.40.1 by Parth Malwankar
initial skeleton
43
0.40.46 by Parth Malwankar
--levels=0 now implicitly prints revnos. added test for --levels=0.
44
# FIXME: _parse_levels should be shared with bzrlib.builtins. this is a copy
45
# to avoid the error
46
#   "IllegalUseOfScopeReplacer: ScopeReplacer object '_parse_levels' was used
47
#   incorrectly: Object already cleaned up, did you assign it to another
48
#   variable?: _factory
49
# with lazy import
50
def _parse_levels(s):
51
    try:
52
        return int(s)
53
    except ValueError:
54
        msg = "The levels argument must be an integer."
55
        raise errors.BzrCommandError(msg)
56
57
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
58
class GrepOptions(object):
59
    """Container to pass around grep options.
60
61
    This class is used as a container to pass around user option and
62
    some other params (like outf) to processing functions. This makes
63
    it easier to add more options as grep evolves.
64
    """
65
    verbose = False
66
    ignore_case = False
67
    no_recursive = False
68
    from_root = False
69
    null = False
70
    levels = None
71
    line_number = False
72
    path_list = None
73
    revision = None
74
    pattern = None
75
    include = None
76
    exclude = None
77
    fixed_string = False
78
    files_with_matches = False
79
    files_without_match = False
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
80
    color = None
0.48.2 by Parth Malwankar
intermediate checkin. we now show diff with -p option.
81
    diff = False
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
82
83
    # derived options
84
    recursive = None
85
    eol_marker = None
86
    patternc = None
0.43.8 by Parth Malwankar
added color for regex pattern.
87
    sub_patternc = None
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
88
    print_revno = None
89
    fixed_string = None
90
    outf = None
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
91
    show_color = False
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
92
93
0.40.1 by Parth Malwankar
initial skeleton
94
class cmd_grep(Command):
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
95
    """Print lines matching PATTERN for specified files and revisions.
96
0.41.14 by Parth Malwankar
updated help and added test.
97
    This command searches the specified files and revisions for a given
98
    pattern.  The pattern is specified as a Python regular expressions[1].
99
100
    If the file name is not specified, the revisions starting with the
101
    current directory are searched recursively. If the revision number is
102
    not specified, the working copy is searched. To search the last committed
103
    revision, use the '-r -1' or '-r last:1' option.
104
105
    Unversioned files are not searched unless explicitly specified on the
0.41.15 by Parth Malwankar
bzr-grep now searches working copy by default.
106
    command line. Unversioned directores are not searched.
0.41.14 by Parth Malwankar
updated help and added test.
107
108
    When searching a pattern, the output is shown in the 'filepath:string'
109
    format. If a revision is explicitly searched, the output is shown as
110
    'filepath~N:string', where N is the revision number.
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
111
0.41.19 by Parth Malwankar
updated to accept --include and --exclude. unused at this point.
112
    --include and --exclude options can be used to search only (or exclude
113
    from search) files with base name matches the specified Unix style GLOB
114
    pattern.  The GLOB pattern an use *, ?, and [...] as wildcards, and \\
0.41.22 by Parth Malwankar
added basic --exclude/include tests
115
    to quote wildcard or backslash character literally. Note that the glob
116
    pattern is not a regular expression.
0.41.19 by Parth Malwankar
updated to accept --include and --exclude. unused at this point.
117
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
118
    [1] http://docs.python.org/library/re.html#regular-expression-syntax
0.40.2 by Parth Malwankar
initial framework for grep
119
    """
120
0.40.82 by Parth Malwankar
chenged encoding_type to 'replace' to handle unicode better
121
    encoding_type = 'replace'
0.40.2 by Parth Malwankar
initial framework for grep
122
    takes_args = ['pattern', 'path*']
123
    takes_options = [
124
        'verbose',
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
125
        'revision',
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
126
        Option('color', type=str, argname='when',
127
               help='Show match in color. WHEN is never, always or auto.'),
0.48.2 by Parth Malwankar
intermediate checkin. we now show diff with -p option.
128
        Option('diff', short_name='p',
129
               help='Grep for pattern in changeset for each revision.'),
0.40.111 by Parth Malwankar
ordered the command line options correctly.
130
        ListOption('exclude', type=str, argname='glob', short_name='X',
131
            help="Skip files whose base name matches GLOB."),
132
        ListOption('include', type=str, argname='glob', short_name='I',
133
            help="Search only files whose base name matches GLOB."),
0.40.112 by Parth Malwankar
support for -l, --files-with-matches. no tests yet.
134
        Option('files-with-matches', short_name='l',
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
135
               help='Print only the name of each input file in '
136
               'which PATTERN is found.'),
0.40.126 by Parth Malwankar
renamed --files-without-matches => --files-without-match
137
        Option('files-without-match', short_name='L',
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
138
               help='Print only the name of each input file in '
139
               'which PATTERN is not found.'),
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
140
        Option('fixed-string', short_name='F',
141
               help='Interpret PATTERN is a single fixed string (not regex).'),
0.40.111 by Parth Malwankar
ordered the command line options correctly.
142
        Option('from-root',
143
               help='Search for pattern starting from the root of the branch. '
144
               '(implies --recursive)'),
145
        Option('ignore-case', short_name='i',
146
               help='ignore case distinctions while matching.'),
147
        Option('levels',
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
148
           help='Number of levels to display - 0 for all, 1 for collapsed '
149
           '(1 is default).',
0.40.111 by Parth Malwankar
ordered the command line options correctly.
150
           argname='N',
151
           type=_parse_levels),
0.40.24 by Parth Malwankar
added support for --line-number.
152
        Option('line-number', short_name='n',
153
               help='show 1-based line number.'),
0.41.6 by Parth Malwankar
renamed --recurse/--no-recurse => --recursive/--no-recursive
154
        Option('no-recursive',
155
               help="Don't recurse into subdirectories. (default is --recursive)"),
0.40.19 by Parth Malwankar
null option should be -Z instead of -z
156
        Option('null', short_name='Z',
0.41.3 by Parth Malwankar
minor fix in help text: ascii => ASCII
157
               help='Write an ASCII NUL (\\0) separator '
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
158
               'between output lines rather than a newline.'),
0.40.2 by Parth Malwankar
initial framework for grep
159
        ]
160
161
162
    @display_command
0.41.19 by Parth Malwankar
updated to accept --include and --exclude. unused at this point.
163
    def run(self, verbose=False, ignore_case=False, no_recursive=False,
164
            from_root=False, null=False, levels=None, line_number=False,
165
            path_list=None, revision=None, pattern=None, include=None,
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
166
            exclude=None, fixed_string=False, files_with_matches=False,
0.48.2 by Parth Malwankar
intermediate checkin. we now show diff with -p option.
167
            files_without_match=False, color='never', diff=False):
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
168
0.40.2 by Parth Malwankar
initial framework for grep
169
        if path_list == None:
170
            path_list = ['.']
171
        else:
172
            if from_root:
173
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
174
0.40.126 by Parth Malwankar
renamed --files-without-matches => --files-without-match
175
        if files_with_matches and files_without_match:
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
176
            raise errors.BzrCommandError('cannot specify both '
177
                '-l/--files-with-matches and -L/--files-without-matches.')
178
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
179
        if color not in ['always', 'never', 'auto']:
180
            raise errors.BzrCommandError('Valid values for --color are '
181
                '"always", "never" or "auto".')
182
183
        if levels==None:
184
            levels=1
185
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
186
        print_revno = False
0.40.46 by Parth Malwankar
--levels=0 now implicitly prints revnos. added test for --levels=0.
187
        if revision != None or levels == 0:
188
            # print revision numbers as we may be showing multiple revisions
189
            print_revno = True
190
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
191
        eol_marker = '\n'
192
        if null:
193
            eol_marker = '\0'
194
0.46.1 by Martin
Make -Fi use regexps for re.IGNORECASE rather than double str.lower
195
        if not ignore_case and grep.is_fixed_string(pattern):
196
            # if the pattern isalnum, implicitly use to -F for faster grep
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
197
            fixed_string = True
0.46.1 by Martin
Make -Fi use regexps for re.IGNORECASE rather than double str.lower
198
        elif ignore_case and fixed_string:
199
            # GZ 2010-06-02: Fall back to regexp rather than lowercasing
200
            #                pattern and text which will cause pain later
201
            fixed_string = False
202
            pattern = re.escape(pattern)
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
203
204
        patternc = None
0.47.1 by Martin
Implement whole text search for fast failure on no match
205
        re_flags = re.MULTILINE
0.43.9 by Parth Malwankar
added color support for ignore_case with fixed_string
206
        if ignore_case:
0.47.1 by Martin
Implement whole text search for fast failure on no match
207
            re_flags |= re.IGNORECASE
0.43.9 by Parth Malwankar
added color support for ignore_case with fixed_string
208
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
209
        if not fixed_string:
210
            patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
211
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
212
        if color == 'always':
213
            show_color = True
214
        elif color == 'never':
215
            show_color = False
216
        elif color == 'auto':
0.43.5 by Parth Malwankar
moved color.py to termcolor.py
217
            show_color = allow_color()
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
218
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
219
        GrepOptions.verbose = verbose
220
        GrepOptions.ignore_case = ignore_case
221
        GrepOptions.no_recursive = no_recursive
222
        GrepOptions.from_root = from_root
223
        GrepOptions.null = null
224
        GrepOptions.levels = levels
225
        GrepOptions.line_number = line_number
226
        GrepOptions.path_list = path_list
227
        GrepOptions.revision = revision
228
        GrepOptions.pattern = pattern
229
        GrepOptions.include = include
230
        GrepOptions.exclude = exclude
231
        GrepOptions.fixed_string = fixed_string
232
        GrepOptions.files_with_matches = files_with_matches
233
        GrepOptions.files_without_match = files_without_match
234
        GrepOptions.color = color
0.48.2 by Parth Malwankar
intermediate checkin. we now show diff with -p option.
235
        GrepOptions.diff = False
0.43.8 by Parth Malwankar
added color for regex pattern.
236
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
237
        GrepOptions.eol_marker = eol_marker
0.43.8 by Parth Malwankar
added color for regex pattern.
238
        GrepOptions.print_revno = print_revno
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
239
        GrepOptions.patternc = patternc
240
        GrepOptions.recursive = not no_recursive
241
        GrepOptions.fixed_string = fixed_string
242
        GrepOptions.outf = self.outf
0.43.4 by Parth Malwankar
initial support for color for fixed string grep.
243
        GrepOptions.show_color = show_color
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
244
0.48.2 by Parth Malwankar
intermediate checkin. we now show diff with -p option.
245
        if diff == True:
0.48.9 by Parth Malwankar
added inital test for 'grep -p'
246
            # options not used:
0.48.7 by Parth Malwankar
initial outputter support for diff_grep
247
            # files_with_matches, files_without_match
248
            # levels(?), line_number, from_root
249
            # include, exclude
0.48.9 by Parth Malwankar
added inital test for 'grep -p'
250
            # These are silently ignored.
0.48.2 by Parth Malwankar
intermediate checkin. we now show diff with -p option.
251
            grep.grep_diff(GrepOptions)
252
        elif revision == None:
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
253
            grep.workingtree_grep(GrepOptions)
0.41.12 by Parth Malwankar
initial support for working tree grep (no test cases yet!)
254
        else:
0.43.1 by Parth Malwankar
added GrepOptions object for easy parameter passing
255
            grep.versioned_grep(GrepOptions)
0.40.33 by Parth Malwankar
better message while skipping unversioned file
256
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
257
0.40.1 by Parth Malwankar
initial skeleton
258
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
259
0.40.11 by Parth Malwankar
added basic test
260
def test_suite():
261
    from bzrlib.tests import TestUtil
262
263
    suite = TestUtil.TestSuite()
264
    loader = TestUtil.TestLoader()
265
    testmod_names = [
266
        'test_grep',
267
        ]
268
269
    suite.addTest(loader.loadTestsFromModuleNames(
270
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
271
    return suite
272