/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 breezy/plugins/grep/grep.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:59:44 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • Revision ID: breezy.the.bot@gmail.com-20181116185944-biefv1sub37qfybm
Sprinkle some PEP8iness.

Merged from https://code.launchpad.net/~jelmer/brz/more-cleanups/+merge/358611

Show diffs side-by-side

added added

removed removed

Lines of Context:
150
150
                self.get_writer = self._get_writer_fixed_highlighted
151
151
            else:
152
152
                flags = opts.patternc.flags
153
 
                self._sub = re.compile(opts.pattern.join(("((?:", ")+)")), flags).sub
 
153
                self._sub = re.compile(
 
154
                    opts.pattern.join(("((?:", ")+)")), flags).sub
154
155
                self._highlight = color_string("\\1", FG.BOLD_RED)
155
156
                self.get_writer = self._get_writer_regexp_highlighted
156
157
        else:
160
161
        """Get function for writing file headers"""
161
162
        write = self.outf.write
162
163
        eol_marker = self.opts.eol_marker
 
164
 
163
165
        def _line_writer(line):
164
166
            write(line + eol_marker)
 
167
 
165
168
        def _line_writer_color(line):
166
169
            write(FG.BOLD_MAGENTA + line + FG.NONE + eol_marker)
167
170
        if self.opts.show_color:
174
177
        """Get function for writing revno lines"""
175
178
        write = self.outf.write
176
179
        eol_marker = self.opts.eol_marker
 
180
 
177
181
        def _line_writer(line):
178
182
            write(line + eol_marker)
 
183
 
179
184
        def _line_writer_color(line):
180
185
            write(FG.BOLD_BLUE + line + FG.NONE + eol_marker)
181
186
        if self.opts.show_color:
188
193
        """Get function for writing uncoloured output"""
189
194
        write = self.outf.write
190
195
        eol_marker = self.opts.eol_marker
 
196
 
191
197
        def _line_writer(line):
192
198
            write(line + eol_marker)
193
199
        return _line_writer
196
202
        """Get function for writing output with regexp match highlighted"""
197
203
        _line_writer = self._get_writer_plain()
198
204
        sub, highlight = self._sub, self._highlight
 
205
 
199
206
        def _line_writer_regexp_highlighted(line):
200
207
            """Write formatted line with matched pattern highlighted"""
201
208
            return _line_writer(line=sub(highlight, line))
205
212
        """Get function for writing output with search string highlighted"""
206
213
        _line_writer = self._get_writer_plain()
207
214
        old, new = self._old, self._new
 
215
 
208
216
        def _line_writer_fixed_highlighted(line):
209
217
            """Write formatted line with string searched for highlighted"""
210
218
            return _line_writer(line=line.replace(old, new))
220
228
        else:
221
229
            # if no revision is sepcified for diff grep we grep all changesets.
222
230
            opts.revision = [RevisionSpec.from_string('revno:1'),
223
 
                RevisionSpec.from_string('last:1')]
 
231
                             RevisionSpec.from_string('last:1')]
224
232
            start_rev = opts.revision[0]
225
233
        start_revid = start_rev.as_revision_id(branch)
226
234
        if start_revid == b'null:':
233
241
                end_revno, end_revid = branch.last_revision_info()
234
242
            erevno_tuple = branch.revision_id_to_dotted_revno(end_revid)
235
243
 
236
 
            grep_mainline = (_rev_on_mainline(srevno_tuple) and
237
 
                _rev_on_mainline(erevno_tuple))
 
244
            grep_mainline = (_rev_on_mainline(srevno_tuple)
 
245
                             and _rev_on_mainline(erevno_tuple))
238
246
 
239
247
            # ensure that we go in reverse order
240
248
            if srevno_tuple > erevno_tuple:
245
253
            # faster when we don't want to look at merged revs. We try this
246
254
            # with _linear_view_revisions. If all revs are to be grepped we
247
255
            # use the slower _graph_view_revisions
248
 
            if opts.levels==1 and grep_mainline:
249
 
                given_revs = _linear_view_revisions(branch, start_revid, end_revid)
 
256
            if opts.levels == 1 and grep_mainline:
 
257
                given_revs = _linear_view_revisions(
 
258
                    branch, start_revid, end_revid)
250
259
            else:
251
 
                given_revs = _graph_view_revisions(branch, start_revid, end_revid)
 
260
                given_revs = _graph_view_revisions(
 
261
                    branch, start_revid, end_revid)
252
262
        else:
253
263
            # We do an optimization below. For grepping a specific revison
254
264
            # We don't need to call _graph_view_revisions which is slow.
258
268
            start_rev_tuple = (start_revid, start_revno, 0)
259
269
            given_revs = [start_rev_tuple]
260
270
        repo = branch.repository
261
 
        diff_pattern = re.compile(b"^[+\\-].*(" + opts.pattern.encode(_user_encoding) + b")")
 
271
        diff_pattern = re.compile(
 
272
            b"^[+\\-].*(" + opts.pattern.encode(_user_encoding) + b")")
262
273
        file_pattern = re.compile(b"=== (modified|added|removed) file '.*'")
263
274
        outputter = _GrepDiffOutputter(opts)
264
275
        writeline = outputter.get_writer()
270
281
                # with level=1 show only top level
271
282
                continue
272
283
 
273
 
            rev_spec = RevisionSpec_revid.from_string("revid:"+revid.decode('utf-8'))
 
284
            rev_spec = RevisionSpec_revid.from_string(
 
285
                "revid:" + revid.decode('utf-8'))
274
286
            new_rev = repo.get_revision(revid)
275
287
            new_tree = rev_spec.as_tree(branch)
276
288
            if len(new_rev.parent_ids) == 0:
280
292
            old_tree = repo.revision_tree(ancestor_id)
281
293
            s = BytesIO()
282
294
            diff.show_diff_trees(old_tree, new_tree, s,
283
 
                old_label='', new_label='')
 
295
                                 old_label='', new_label='')
284
296
            display_revno = True
285
297
            display_file = False
286
298
            file_header = None
294
306
                        writerevno("=== revno:%s ===" % (revno,))
295
307
                        display_revno = False
296
308
                    if display_file:
297
 
                        writefileheader("  %s" % (file_header.decode(file_encoding, 'replace'),))
 
309
                        writefileheader(
 
310
                            "  %s" % (file_header.decode(file_encoding, 'replace'),))
298
311
                        display_file = False
299
312
                    line = line.decode(file_encoding, 'replace')
300
313
                    writeline("    %s" % (line,))
318
331
                end_revno, end_revid = branch.last_revision_info()
319
332
            erevno_tuple = branch.revision_id_to_dotted_revno(end_revid)
320
333
 
321
 
            grep_mainline = (_rev_on_mainline(srevno_tuple) and
322
 
                _rev_on_mainline(erevno_tuple))
 
334
            grep_mainline = (_rev_on_mainline(srevno_tuple)
 
335
                             and _rev_on_mainline(erevno_tuple))
323
336
 
324
337
            # ensure that we go in reverse order
325
338
            if srevno_tuple > erevno_tuple:
331
344
            # with _linear_view_revisions. If all revs are to be grepped we
332
345
            # use the slower _graph_view_revisions
333
346
            if opts.levels == 1 and grep_mainline:
334
 
                given_revs = _linear_view_revisions(branch, start_revid, end_revid)
 
347
                given_revs = _linear_view_revisions(
 
348
                    branch, start_revid, end_revid)
335
349
            else:
336
 
                given_revs = _graph_view_revisions(branch, start_revid, end_revid)
 
350
                given_revs = _graph_view_revisions(
 
351
                    branch, start_revid, end_revid)
337
352
        else:
338
353
            # We do an optimization below. For grepping a specific revison
339
354
            # We don't need to call _graph_view_revisions which is slow.
351
366
                # with level=1 show only top level
352
367
                continue
353
368
 
354
 
            rev = RevisionSpec_revid.from_string("revid:"+revid.decode('utf-8'))
 
369
            rev = RevisionSpec_revid.from_string(
 
370
                "revid:" + revid.decode('utf-8'))
355
371
            tree = rev.as_tree(branch)
356
372
            for path in opts.path_list:
357
373
                tree_path = osutils.pathjoin(relpath, path)
363
379
                    path_prefix = path
364
380
                    dir_grep(tree, path, relpath, opts, revno, path_prefix)
365
381
                else:
366
 
                    versioned_file_grep(tree, tree_path, '.', path, opts, revno)
 
382
                    versioned_file_grep(
 
383
                        tree, tree_path, '.', path, opts, revno)
367
384
 
368
385
 
369
386
def workingtree_grep(opts):
370
 
    revno = opts.print_revno = None # for working tree set revno to None
 
387
    revno = opts.print_revno = None  # for working tree set revno to None
371
388
 
372
389
    tree, branch, relpath = \
373
390
        controldir.ControlDir.open_containing_tree_or_branch('.')
374
391
    if not tree:
375
392
        msg = ('Cannot search working tree. Working tree not found.\n'
376
 
            'To search for specific revision in history use the -r option.')
 
393
               'To search for specific revision in history use the -r option.')
377
394
        raise errors.BzrCommandError(msg)
378
395
 
379
396
    # GZ 2010-06-02: Shouldn't be smuggling this on opts, but easy for now
416
433
    #                for a good reason, otherwise cache might want purging.
417
434
    outputter = opts.outputter
418
435
    for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
419
 
        from_dir=from_dir, recursive=opts.recursive):
 
436
                                                     from_dir=from_dir, recursive=opts.recursive):
420
437
 
421
438
        if _skip_file(opts.include, opts.exclude, fp):
422
439
            continue
450
467
                    with open(path_for_file, 'rb') as f:
451
468
                        _file_grep(f.read(), fp, opts, revno, path_prefix)
452
469
 
453
 
    if revno is not None: # grep versioned files
 
470
    if revno is not None:  # grep versioned files
454
471
        for (path, tree_path), chunks in tree.iter_files_bytes(to_grep):
455
472
            path = _make_display_path(relpath, path)
456
473
            _file_grep(b''.join(chunks), path, opts, revno, path_prefix,
457
 
                tree.get_file_revision(tree_path))
 
474
                       tree.get_file_revision(tree_path))
458
475
 
459
476
 
460
477
def _make_display_path(relpath, path):
472
489
    return path
473
490
 
474
491
 
475
 
def versioned_file_grep(tree, tree_path, relpath, path, opts, revno, path_prefix = None):
 
492
def versioned_file_grep(tree, tree_path, relpath, path, opts, revno, path_prefix=None):
476
493
    """Create a file object for the specified id and pass it on to _file_grep.
477
494
    """
478
495
 
495
512
            trace.warning("Binary file '%s' skipped.", path)
496
513
        return
497
514
 
498
 
    file.seek(0) # search from beginning
 
515
    file.seek(0)  # search from beginning
499
516
 
500
517
    found = False
501
518
    if opts.fixed_string:
504
521
            if pattern in line:
505
522
                found = True
506
523
                break
507
 
    else: # not fixed_string
 
524
    else:  # not fixed_string
508
525
        for line in file:
509
526
            if opts.patternc.search(line):
510
527
                found = True
511
528
                break
512
529
 
513
530
    if (opts.files_with_matches and found) or \
514
 
        (opts.files_without_match and not found):
 
531
            (opts.files_without_match and not found):
515
532
        if path_prefix and path_prefix != '.':
516
533
            # user has passed a dir arg, show that as result prefix
517
534
            path = osutils.pathjoin(path_prefix, path)
524
541
    The idea here is to do this work only once per run, and finally return a
525
542
    function that will do the minimum amount possible for each match.
526
543
    """
 
544
 
527
545
    def __init__(self, opts, use_cache=False):
528
546
        self.outf = opts.outf
529
547
        if use_cache:
546
564
                self.get_writer = self._get_writer_fixed_highlighted
547
565
            else:
548
566
                flags = opts.patternc.flags
549
 
                self._sub = re.compile(opts.pattern.join(("((?:", ")+)")), flags).sub
 
567
                self._sub = re.compile(
 
568
                    opts.pattern.join(("((?:", ")+)")), flags).sub
550
569
                self._highlight = color_string("\\1", FG.BOLD_RED)
551
570
                self.get_writer = self._get_writer_regexp_highlighted
552
571
            path_start = FG.MAGENTA
577
596
    def _get_writer_plain(self, path, revno, cache_id):
578
597
        """Get function for writing uncoloured output"""
579
598
        per_line = self._format_perline
580
 
        start = self._format_initial % {"path":path, "revno":revno}
 
599
        start = self._format_initial % {"path": path, "revno": revno}
581
600
        write = self.outf.write
582
601
        if self.cache is not None and cache_id is not None:
583
602
            result_list = []
584
603
            self.cache[cache_id] = path, result_list
585
604
            add_to_cache = result_list.append
 
605
 
586
606
            def _line_cache_and_writer(**kwargs):
587
607
                """Write formatted line and cache arguments"""
588
608
                end = per_line % kwargs
589
609
                add_to_cache(end)
590
610
                write(start + end)
591
611
            return _line_cache_and_writer
 
612
 
592
613
        def _line_writer(**kwargs):
593
614
            """Write formatted line from arguments given by underlying opts"""
594
615
            write(start + per_line % kwargs)
597
618
    def write_cached_lines(self, cache_id, revno):
598
619
        """Write cached results out again for new revision"""
599
620
        cached_path, cached_matches = self.cache[cache_id]
600
 
        start = self._format_initial % {"path":cached_path, "revno":revno}
 
621
        start = self._format_initial % {"path": cached_path, "revno": revno}
601
622
        write = self.outf.write
602
623
        for end in cached_matches:
603
624
            write(start + end)
606
627
        """Get function for writing output with regexp match highlighted"""
607
628
        _line_writer = self._get_writer_plain(path, revno, cache_id)
608
629
        sub, highlight = self._sub, self._highlight
 
630
 
609
631
        def _line_writer_regexp_highlighted(line, **kwargs):
610
632
            """Write formatted line with matched pattern highlighted"""
611
633
            return _line_writer(line=sub(highlight, line), **kwargs)
615
637
        """Get function for writing output with search string highlighted"""
616
638
        _line_writer = self._get_writer_plain(path, revno, cache_id)
617
639
        old, new = self._old, self._new
 
640
 
618
641
        def _line_writer_fixed_highlighted(line, **kwargs):
619
642
            """Write formatted line with string searched for highlighted"""
620
643
            return _line_writer(line=line.replace(old, new), **kwargs)
671
694
            for index, line in enumerate(file_text.splitlines()):
672
695
                if pattern in line:
673
696
                    line = line.decode(file_encoding, 'replace')
674
 
                    writeline(lineno=index+start, line=line)
 
697
                    writeline(lineno=index + start, line=line)
675
698
        else:
676
699
            for line in file_text.splitlines():
677
700
                if pattern in line:
699
722
            for index, line in enumerate(file_text.splitlines()):
700
723
                if search(line):
701
724
                    line = line.decode(file_encoding, 'replace')
702
 
                    writeline(lineno=index+start, line=line)
 
725
                    writeline(lineno=index + start, line=line)
703
726
        else:
704
727
            for line in file_text.splitlines():
705
728
                if search(line):
706
729
                    line = line.decode(file_encoding, 'replace')
707
730
                    writeline(line=line)
708