/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 17:31:06 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-20100223173106-270r9457tcqz4bx5
intermediate checkin. added support for --null

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
import bzrlib
32
32
from bzrlib import (
 
33
    osutils,
33
34
    bzrdir,
34
35
    )
35
36
""")
43
44
    takes_args = ['pattern', 'path*']
44
45
    takes_options = [
45
46
        'verbose',
46
 
        Option('line-number', short_name='n',
47
 
               help='prefix each line of output with 1-based line number.'),
48
47
        Option('ignore-case', short_name='i',
49
48
               help='ignore case distinctions while matching.'),
50
49
        Option('recursive', short_name='R',
51
50
               help='Recurse into subdirectories.'),
52
51
        Option('from-root',
53
52
               help='Search for pattern starting from the root of the branch.'),
 
53
        Option('null', short_name='z',
 
54
               help='Write an ascii NUL (\\0) separator '
 
55
               'between output lines rather than a newline.'),
54
56
        ]
55
57
 
56
58
 
57
59
    @display_command
58
 
    def run(self, verbose=False, line_number=False, null=False,
59
 
            ignore_case=False, recursive=False, from_root=False,
60
 
            path_list=None, pattern=None):
 
60
    def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False,
 
61
            null=False, path_list=None, pattern=None):
61
62
        if path_list == None:
62
63
            path_list = ['.']
63
64
        else:
64
65
            if from_root:
65
66
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
66
67
 
67
 
        print 'pattern:', pattern
68
 
        print 'path_list:', path_list
69
 
        print 'line-number:', line_number
70
 
        print 'null:', null
71
 
        print 'recursive:', recursive
72
 
        print 'from-root:', from_root
73
 
        print '=' * 20
74
 
 
75
68
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch('.')
76
69
 
77
70
        re_flags = 0
78
71
        if ignore_case:
79
72
            re_flags = re.IGNORECASE
80
73
 
 
74
        eol_marker = '\n'
 
75
        if null:
 
76
            eol_marker = '\0'
 
77
 
81
78
        patternc = None
82
 
 
83
79
        try:
84
80
            # use python's re.compile as we need to catch re.error in case of bad pattern
85
81
            lazy_regex.reset_compile()
90
86
        tree.lock_read()
91
87
        self.add_cleanup(tree.unlock)
92
88
        for path in path_list:
93
 
            for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
94
 
                from_dir=relpath, recursive=recursive):
95
 
                print 'fp:', fp
96
 
                print 'fc:', fc
97
 
                print 'fkind:', fkind
98
 
                print 'fid:', fid
99
 
                print 'entry:', entry
100
 
                if fc == 'V' and fkind == 'file':
101
 
                    self.file_grep(fp, patternc)
102
 
                print '~' * 30
 
89
            if osutils.isdir(path):
 
90
                from_dir = os.path.abspath(os.path.join(relpath, path))
 
91
                for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
 
92
                    from_dir=from_dir, recursive=recursive):
 
93
                    if fc == 'V' and fkind == 'file':
 
94
                        self.file_grep(fp, patternc, eol_marker)
 
95
            else:
 
96
                # if user has explicitly specified a file
 
97
                # we don't care if its versioned
 
98
                self.file_grep(path, patternc, eol_marker)
103
99
 
104
 
    def file_grep(self, path, patternc):
 
100
    def file_grep(self, path, patternc, eol_marker):
105
101
        index = 1
 
102
        fmt = "%s:%d:%s" + eol_marker
106
103
        for line in open(path):
107
104
            res = patternc.search(line)
108
105
            if res:
109
 
                self.outf.write("%s:%d:%s\n" % (path, index, line.strip()))
 
106
                self.outf.write( fmt % (path, index, line.strip()))
110
107
            index += 1
111
108
 
112
109
register_command(cmd_grep)