bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.40.1
by Parth Malwankar
initial skeleton |
1 |
# Copyright (C) 2010 Parth Malwankar <parth.malwankar@gmail.com>
|
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
|
|
16 |
"""bzr grep"""
|
|
17 |
||
18 |
import os |
|
19 |
||
|
0.40.2
by Parth Malwankar
initial framework for grep |
20 |
from bzrlib.commands import Command, register_command, display_command |
21 |
from bzrlib.option import ( |
|
22 |
Option, |
|
23 |
)
|
|
24 |
||
25 |
from bzrlib.lazy_import import lazy_import |
|
26 |
lazy_import(globals(), """ |
|
27 |
import bzrlib
|
|
28 |
from bzrlib import (
|
|
29 |
errors,
|
|
30 |
bzrdir,
|
|
31 |
)
|
|
32 |
""") |
|
|
0.40.1
by Parth Malwankar
initial skeleton |
33 |
|
34 |
version_info = (0, 1) |
|
35 |
||
36 |
class cmd_grep(Command): |
|
|
0.40.2
by Parth Malwankar
initial framework for grep |
37 |
"""Print lines matching PATTERN for specified files. |
38 |
"""
|
|
39 |
||
40 |
takes_args = ['pattern', 'path*'] |
|
41 |
takes_options = [ |
|
42 |
'verbose', |
|
43 |
Option('line-number', short_name='n', |
|
44 |
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 |
Option('ignore-case', short_name='i', |
|
49 |
help='ignore case distinctions while matching.'), |
|
50 |
Option('recursive', short_name='R', |
|
51 |
help='Recurse into subdirectories.'), |
|
52 |
Option('from-root', |
|
53 |
help='Search for pattern starting from the root of the branch.'), |
|
54 |
]
|
|
55 |
||
56 |
||
57 |
@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): |
|
61 |
if path_list == None: |
|
62 |
path_list = ['.'] |
|
63 |
else: |
|
64 |
if from_root: |
|
65 |
raise errors.BzrCommandError('cannot specify both --from-root and PATH.') |
|
66 |
||
67 |
print 'pattern:', pattern |
|
68 |
print 'path_list:', path_list |
|
69 |
print 'ignore-case:', ignore_case |
|
70 |
print 'line-number:', line_number |
|
71 |
print 'null:', null |
|
72 |
print 'recursive:', recursive |
|
73 |
print 'from-root:', from_root |
|
74 |
print '=' * 20 |
|
75 |
||
76 |
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch('.') |
|
77 |
||
78 |
tree.lock_read() |
|
79 |
self.add_cleanup(tree.unlock) |
|
80 |
for path in path_list: |
|
81 |
for fp, fc, fkind, fid, entry in tree.list_files(include_root=False, |
|
82 |
from_dir=relpath, recursive=recursive): |
|
83 |
print 'fp:', fp |
|
84 |
print 'fc:', fc |
|
85 |
print 'fkind:', fkind |
|
86 |
print 'fid:', fid |
|
87 |
print 'entry:', entry |
|
88 |
if fc == 'V' and fkind == 'file': |
|
89 |
self.file_grep(fp) |
|
90 |
print '~' * 30 |
|
91 |
||
92 |
def file_grep(self, path): |
|
93 |
print path + ': grepping ...' |
|
|
0.40.1
by Parth Malwankar
initial skeleton |
94 |
|
95 |
register_command(cmd_grep) |
|
|
0.40.2
by Parth Malwankar
initial framework for grep |
96 |