/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 12:05:11 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-20100223120511-v7280pk1uotee1f5
initial framework for grep

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
16
"""bzr grep"""
18
17
 
19
18
import os
20
19
 
21
 
from bzrlib.commands import Command, register_command
 
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
""")
22
33
 
23
34
version_info = (0, 1)
24
35
 
25
36
class cmd_grep(Command):
26
 
    """grep command"""
27
 
 
28
 
    takes_args = ['file*']
29
 
 
30
 
    def run(self, file_list):
31
 
        print "Hello World!!!"
32
 
 
 
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 ...'
33
94
 
34
95
register_command(cmd_grep)
 
96