/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Parth Malwankar
  • Date: 2010-02-23 14:14:02 UTC
  • mto: (0.44.2 grep) (6531.3.1 merge-grep)
  • mto: This revision was merged to the branch mainline in revision 6555.
  • Revision ID: parth.malwankar@gmail.com-20100223141402-1ivnqp5rjn4cq2bt
basic file grep is working

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
"""bzr grep"""
17
17
 
18
18
import os
 
19
import sys
19
20
 
 
21
from bzrlib import errors, lazy_regex
20
22
from bzrlib.commands import Command, register_command, display_command
21
23
from bzrlib.option import (
22
24
    Option,
24
26
 
25
27
from bzrlib.lazy_import import lazy_import
26
28
lazy_import(globals(), """
 
29
import re
 
30
 
27
31
import bzrlib
28
32
from bzrlib import (
29
 
    errors,
30
33
    bzrdir,
31
34
    )
32
35
""")
42
45
        'verbose',
43
46
        Option('line-number', short_name='n',
44
47
               help='prefix each line of output with 1-based line number.'),
45
 
        Option('null', short_name='z',
46
 
               help='Write an ascii NUL (\\0) separator '
47
 
               'between files rather than a newline.'),
48
48
        Option('ignore-case', short_name='i',
49
49
               help='ignore case distinctions while matching.'),
50
50
        Option('recursive', short_name='R',
66
66
 
67
67
        print 'pattern:', pattern
68
68
        print 'path_list:', path_list
69
 
        print 'ignore-case:', ignore_case
70
69
        print 'line-number:', line_number
71
70
        print 'null:', null
72
71
        print 'recursive:', recursive
75
74
 
76
75
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch('.')
77
76
 
 
77
        re_flags = 0
 
78
        if ignore_case:
 
79
            re_flags = re.IGNORECASE
 
80
 
 
81
        patternc = None
 
82
 
 
83
        try:
 
84
            # use python's re.compile as we need to catch re.error in case of bad pattern
 
85
            lazy_regex.reset_compile()
 
86
            patternc = re.compile(pattern, re_flags)
 
87
        except re.error, e:
 
88
            raise errors.BzrError("Invalid pattern: '%s'" % pattern)
 
89
 
78
90
        tree.lock_read()
79
91
        self.add_cleanup(tree.unlock)
80
92
        for path in path_list:
86
98
                print 'fid:', fid
87
99
                print 'entry:', entry
88
100
                if fc == 'V' and fkind == 'file':
89
 
                    self.file_grep(fp)
 
101
                    self.file_grep(fp, patternc)
90
102
                print '~' * 30
91
103
 
92
 
    def file_grep(self, path):
93
 
        print path + ': grepping ...'
 
104
    def file_grep(self, path, patternc):
 
105
        index = 1
 
106
        for line in open(path):
 
107
            res = patternc.search(line)
 
108
            if res:
 
109
                self.outf.write("%s:%d:%s\n" % (path, index, line.strip()))
 
110
            index += 1
94
111
 
95
112
register_command(cmd_grep)
96
113