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 |
|
|
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.3
by Parth Malwankar
basic file grep is working |
21 |
from bzrlib import errors, lazy_regex |
|
0.40.2
by Parth Malwankar
initial framework for grep |
22 |
from bzrlib.commands import Command, register_command, display_command |
23 |
from bzrlib.option import ( |
|
24 |
Option, |
|
25 |
)
|
|
26 |
||
27 |
from bzrlib.lazy_import import lazy_import |
|
28 |
lazy_import(globals(), """ |
|
|
0.40.3
by Parth Malwankar
basic file grep is working |
29 |
import re
|
30 |
||
|
0.40.2
by Parth Malwankar
initial framework for grep |
31 |
import bzrlib
|
32 |
from bzrlib import (
|
|
33 |
bzrdir,
|
|
34 |
)
|
|
35 |
""") |
|
|
0.40.1
by Parth Malwankar
initial skeleton |
36 |
|
37 |
version_info = (0, 1) |
|
38 |
||
39 |
class cmd_grep(Command): |
|
|
0.40.2
by Parth Malwankar
initial framework for grep |
40 |
"""Print lines matching PATTERN for specified files. |
41 |
"""
|
|
42 |
||
43 |
takes_args = ['pattern', 'path*'] |
|
44 |
takes_options = [ |
|
45 |
'verbose', |
|
46 |
Option('line-number', short_name='n', |
|
47 |
help='prefix each line of output with 1-based line number.'), |
|
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 'line-number:', line_number |
|
70 |
print 'null:', null |
|
71 |
print 'recursive:', recursive |
|
72 |
print 'from-root:', from_root |
|
73 |
print '=' * 20 |
|
74 |
||
75 |
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch('.') |
|
76 |
||
|
0.40.3
by Parth Malwankar
basic file grep is working |
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 |
||
|
0.40.2
by Parth Malwankar
initial framework for grep |
90 |
tree.lock_read() |
91 |
self.add_cleanup(tree.unlock) |
|
92 |
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': |
|
|
0.40.3
by Parth Malwankar
basic file grep is working |
101 |
self.file_grep(fp, patternc) |
|
0.40.2
by Parth Malwankar
initial framework for grep |
102 |
print '~' * 30 |
103 |
||
|
0.40.3
by Parth Malwankar
basic file grep is working |
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 |
|
|
0.40.1
by Parth Malwankar
initial skeleton |
111 |
|
112 |
register_command(cmd_grep) |
|
|
0.40.2
by Parth Malwankar
initial framework for grep |
113 |