/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.40.9 by Parth Malwankar
factored out grep related code to grep.py
29
import grep
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
30
0.40.2 by Parth Malwankar
initial framework for grep
31
import bzrlib
0.41.11 by Parth Malwankar
moved top level grep code to versioned_grep.
32
from bzrlib.revisionspec import RevisionSpec
0.40.2 by Parth Malwankar
initial framework for grep
33
from bzrlib import (
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
34
    osutils,
0.40.2 by Parth Malwankar
initial framework for grep
35
    bzrdir,
0.40.7 by Parth Malwankar
issue a warning while searching unversioned file
36
    trace,
0.40.2 by Parth Malwankar
initial framework for grep
37
    )
38
""")
0.40.1 by Parth Malwankar
initial skeleton
39
0.40.60 by Parth Malwankar
'binary file skipped' warning is only shown with --verbose flag
40
version_info = (0, 2, 0, 'dev', 0)
0.40.1 by Parth Malwankar
initial skeleton
41
0.40.46 by Parth Malwankar
--levels=0 now implicitly prints revnos. added test for --levels=0.
42
# FIXME: _parse_levels should be shared with bzrlib.builtins. this is a copy
43
# to avoid the error
44
#   "IllegalUseOfScopeReplacer: ScopeReplacer object '_parse_levels' was used
45
#   incorrectly: Object already cleaned up, did you assign it to another
46
#   variable?: _factory
47
# with lazy import
48
def _parse_levels(s):
49
    try:
50
        return int(s)
51
    except ValueError:
52
        msg = "The levels argument must be an integer."
53
        raise errors.BzrCommandError(msg)
54
55
0.40.1 by Parth Malwankar
initial skeleton
56
class cmd_grep(Command):
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
57
    """Print lines matching PATTERN for specified files and revisions.
58
0.41.14 by Parth Malwankar
updated help and added test.
59
    This command searches the specified files and revisions for a given
60
    pattern.  The pattern is specified as a Python regular expressions[1].
61
62
    If the file name is not specified, the revisions starting with the
63
    current directory are searched recursively. If the revision number is
64
    not specified, the working copy is searched. To search the last committed
65
    revision, use the '-r -1' or '-r last:1' option.
66
67
    Unversioned files are not searched unless explicitly specified on the
0.41.15 by Parth Malwankar
bzr-grep now searches working copy by default.
68
    command line. Unversioned directores are not searched.
0.41.14 by Parth Malwankar
updated help and added test.
69
70
    When searching a pattern, the output is shown in the 'filepath:string'
71
    format. If a revision is explicitly searched, the output is shown as
72
    'filepath~N:string', where N is the revision number.
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
73
0.41.19 by Parth Malwankar
updated to accept --include and --exclude. unused at this point.
74
    --include and --exclude options can be used to search only (or exclude
75
    from search) files with base name matches the specified Unix style GLOB
76
    pattern.  The GLOB pattern an use *, ?, and [...] as wildcards, and \\
0.41.22 by Parth Malwankar
added basic --exclude/include tests
77
    to quote wildcard or backslash character literally. Note that the glob
78
    pattern is not a regular expression.
0.41.19 by Parth Malwankar
updated to accept --include and --exclude. unused at this point.
79
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
80
    [1] http://docs.python.org/library/re.html#regular-expression-syntax
0.40.2 by Parth Malwankar
initial framework for grep
81
    """
82
0.40.82 by Parth Malwankar
chenged encoding_type to 'replace' to handle unicode better
83
    encoding_type = 'replace'
0.40.2 by Parth Malwankar
initial framework for grep
84
    takes_args = ['pattern', 'path*']
85
    takes_options = [
86
        'verbose',
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
87
        'revision',
0.40.111 by Parth Malwankar
ordered the command line options correctly.
88
        ListOption('exclude', type=str, argname='glob', short_name='X',
89
            help="Skip files whose base name matches GLOB."),
90
        ListOption('include', type=str, argname='glob', short_name='I',
91
            help="Search only files whose base name matches GLOB."),
0.40.112 by Parth Malwankar
support for -l, --files-with-matches. no tests yet.
92
        Option('files-with-matches', short_name='l',
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
93
               help='Print only the name of each input file in '
94
               'which PATTERN is found.'),
95
        Option('files-without-matches', short_name='L',
96
               help='Print only the name of each input file in '
97
               'which PATTERN is not found.'),
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
98
        Option('fixed-string', short_name='F',
99
               help='Interpret PATTERN is a single fixed string (not regex).'),
0.40.111 by Parth Malwankar
ordered the command line options correctly.
100
        Option('from-root',
101
               help='Search for pattern starting from the root of the branch. '
102
               '(implies --recursive)'),
103
        Option('ignore-case', short_name='i',
104
               help='ignore case distinctions while matching.'),
105
        Option('levels',
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
106
           help='Number of levels to display - 0 for all, 1 for collapsed '
107
           '(1 is default).',
0.40.111 by Parth Malwankar
ordered the command line options correctly.
108
           argname='N',
109
           type=_parse_levels),
0.40.24 by Parth Malwankar
added support for --line-number.
110
        Option('line-number', short_name='n',
111
               help='show 1-based line number.'),
0.41.6 by Parth Malwankar
renamed --recurse/--no-recurse => --recursive/--no-recursive
112
        Option('no-recursive',
113
               help="Don't recurse into subdirectories. (default is --recursive)"),
0.40.19 by Parth Malwankar
null option should be -Z instead of -z
114
        Option('null', short_name='Z',
0.41.3 by Parth Malwankar
minor fix in help text: ascii => ASCII
115
               help='Write an ASCII NUL (\\0) separator '
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
116
               'between output lines rather than a newline.'),
0.40.2 by Parth Malwankar
initial framework for grep
117
        ]
118
119
120
    @display_command
0.41.19 by Parth Malwankar
updated to accept --include and --exclude. unused at this point.
121
    def run(self, verbose=False, ignore_case=False, no_recursive=False,
122
            from_root=False, null=False, levels=None, line_number=False,
123
            path_list=None, revision=None, pattern=None, include=None,
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
124
            exclude=None, fixed_string=False, files_with_matches=False,
125
            files_without_matches=False):
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
126
0.41.6 by Parth Malwankar
renamed --recurse/--no-recurse => --recursive/--no-recursive
127
        recursive = not no_recursive
0.41.4 by Parth Malwankar
--recurse is default.
128
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
129
        if levels==None:
130
            levels=1
131
0.40.2 by Parth Malwankar
initial framework for grep
132
        if path_list == None:
133
            path_list = ['.']
134
        else:
135
            if from_root:
136
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
137
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
138
        if files_with_matches and files_without_matches:
139
            raise errors.BzrCommandError('cannot specify both '
140
                '-l/--files-with-matches and -L/--files-without-matches.')
141
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
142
        print_revno = False
0.40.46 by Parth Malwankar
--levels=0 now implicitly prints revnos. added test for --levels=0.
143
        if revision != None or levels == 0:
144
            # print revision numbers as we may be showing multiple revisions
145
            print_revno = True
146
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
147
        eol_marker = '\n'
148
        if null:
149
            eol_marker = '\0'
150
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
151
        # 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.
152
        if grep.is_fixed_string(pattern):
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
153
            fixed_string = True
154
155
        patternc = None
156
        if not fixed_string:
157
            re_flags = 0
158
            if ignore_case:
159
                re_flags = re.IGNORECASE
160
            patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
161
0.41.12 by Parth Malwankar
initial support for working tree grep (no test cases yet!)
162
        if revision == None:
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
163
            grep.workingtree_grep(pattern, patternc, path_list, recursive,
0.41.21 by Parth Malwankar
include/exclude working now. tests not added.
164
                line_number, from_root, eol_marker, include, exclude,
0.40.112 by Parth Malwankar
support for -l, --files-with-matches. no tests yet.
165
                verbose, fixed_string, ignore_case, files_with_matches,
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
166
                files_without_matches, self.outf)
0.41.12 by Parth Malwankar
initial support for working tree grep (no test cases yet!)
167
        else:
0.40.83 by Parth Malwankar
added support for -F/--fixed-string.
168
            grep.versioned_grep(revision, pattern, patternc, path_list,
0.41.12 by Parth Malwankar
initial support for working tree grep (no test cases yet!)
169
                recursive, line_number, from_root, eol_marker,
0.40.60 by Parth Malwankar
'binary file skipped' warning is only shown with --verbose flag
170
                print_revno, levels, include, exclude, verbose,
0.40.112 by Parth Malwankar
support for -l, --files-with-matches. no tests yet.
171
                fixed_string, ignore_case, files_with_matches,
0.40.121 by Parth Malwankar
initial implementation of -L/--files-without-matches. no tests.
172
                files_without_matches, self.outf)
0.40.33 by Parth Malwankar
better message while skipping unversioned file
173
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
174
0.40.1 by Parth Malwankar
initial skeleton
175
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
176
0.40.11 by Parth Malwankar
added basic test
177
def test_suite():
178
    from bzrlib.tests import TestUtil
179
180
    suite = TestUtil.TestSuite()
181
    loader = TestUtil.TestLoader()
182
    testmod_names = [
183
        'test_grep',
184
        ]
185
186
    suite.addTest(loader.loadTestsFromModuleNames(
187
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
188
    return suite
189