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