[Reproducible-commits] [debbindiff] 02/02: Add plain text output for differences

Jérémy Bobbio lunar at moszumanska.debian.org
Sat Feb 14 22:22:21 UTC 2015


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository debbindiff.

commit 2bfdbeeaa6bd780f72dc071e72c13f4d9a34fc58
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Sat Feb 14 22:16:42 2015 +0000

    Add plain text output for differences
    
    Adding `--text=FILE` to the command line will now write differences in plain
    text to the given FILE.
    
    FILE can be `-` for standard output. `--html` also gained support for this.
    
    Based on a patch by Helmut Grohne.
    
    Closes: #778423
---
 debbindiff.py                 | 33 ++++++++++++++++++++++++--------
 debbindiff/presenters/text.py | 44 +++++++++++++++++++++++++++++++++++++++++++
 debian/debbindiff.1.rst       |  5 ++++-
 3 files changed, 73 insertions(+), 9 deletions(-)

diff --git a/debbindiff.py b/debbindiff.py
index 15de0ab..ae4097f 100755
--- a/debbindiff.py
+++ b/debbindiff.py
@@ -21,11 +21,13 @@
 from __future__ import print_function
 
 import argparse
+from contextlib import contextmanager
 import logging
 import sys
 from debbindiff import logger, VERSION
 import debbindiff.comparators
 from debbindiff.presenters.html import output_html
+from debbindiff.presenters.text import output_text
 
 
 def create_parser():
@@ -37,7 +39,9 @@ def create_parser():
     parser.add_argument('--debug', dest='debug', action='store_true',
                         default=False, help='display debug messages')
     parser.add_argument('--html', metavar='output', dest='html_output',
-                        help='write HTML report to given file')
+                        help='write HTML report to given file (use - for stdout)')
+    parser.add_argument('--text', metavar='output', dest='text_output',
+                        help='write plain text output to given file (use - for stdout)')
     parser.add_argument('--max-report-size', metavar='BYTES',
                         dest='max_report_size', type=int,
                         help='maximum bytes written in report')
@@ -47,6 +51,19 @@ def create_parser():
     parser.add_argument('file2', help='second file to compare')
     return parser
 
+ at contextmanager
+def make_printer(path):
+    if path == '-':
+        output = sys.stdout
+    else:
+        output = open(parsed_args.html_output, 'w')
+    def print_func(*args, **kwargs):
+        kwargs['file'] = output
+        print(*args, **kwargs)
+    yield print_func
+    if path != '-':
+        output.close()
+
 
 def main():
     parser = create_parser()
@@ -55,14 +72,14 @@ def main():
         logger.setLevel(logging.DEBUG)
     differences = debbindiff.comparators.compare_files(
         parsed_args.file1, parsed_args.file2)
-    if len(differences) > 0 and parsed_args.html_output:
-        output = open(parsed_args.html_output, 'w')
-        def print_func(*args, **kwargs):
-            kwargs['file'] = output
-            print(*args, **kwargs)
-        output_html(differences, css_url=parsed_args.css_url, print_func=print_func,
-                    max_page_size=parsed_args.max_report_size)
     if len(differences) > 0:
+        if parsed_args.html_output:
+            with make_printer(parsed_args.html_output) as print_func:
+                output_html(differences, css_url=parsed_args.css_url, print_func=print_func,
+                            max_page_size=parsed_args.max_report_size)
+        if parsed_args.text_output:
+            with make_printer(parsed_args.text_output) as print_func:
+                output_text(differences, print_func=print_func)
         return 1
     return 0
 
diff --git a/debbindiff/presenters/text.py b/debbindiff/presenters/text.py
new file mode 100644
index 0000000..1eda4bb
--- /dev/null
+++ b/debbindiff/presenters/text.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# debbindiff: highlight differences between two builds of Debian packages
+#
+# Copyright © 2015 Jérémy Bobbio <lunar at debian.org>
+#             2015 Helmut Grohne <helmut at subdivi.de>
+#
+# debbindiff is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# debbindiff is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with debbindiff.  If not, see <http://www.gnu.org/licenses/>.
+import sys
+import difflib
+
+
+def output_text(differences, print_func):
+    for difference in differences:
+        if difference.source1 == difference.source2:
+            print_func("├── %s" % difference.source1)
+        else:
+            print_func("│   --- %s" % (difference.source1))
+            print_func("├── +++ %s" % (difference.source2))
+        if difference.comment:
+            for line in difference.comment.split('\n'):
+                print_func("│┄ %s" % line)
+        if difference.lines1 and difference.lines2:
+            for line in difflib.unified_diff(difference.lines1, difference.lines2):
+                if line.startswith('--- ') or line.startswith('+++ '):
+                    continue
+                print_func("│ %s" % line.encode('utf-8'), end='')
+        if difference.details:
+            def new_print_func(*args, **kwargs):
+                print_func('│  ', *args, **kwargs)
+            output_text(difference.details, new_print_func)
+        print_func('│')
diff --git a/debian/debbindiff.1.rst b/debian/debbindiff.1.rst
index c97fb34..2d30557 100644
--- a/debian/debbindiff.1.rst
+++ b/debian/debbindiff.1.rst
@@ -14,7 +14,7 @@ highlight differences between two builds of Debian packages
 SYNOPSIS
 ========
 
-  debbindiff [-h] [--version] [--debug] [--html output] [--max-report-size bytes] [--css url] file1 file2
+  debbindiff [-h] [--version] [--debug] [--html output] [--text output] [--max-report-size bytes] [--css url] file1 file2
 
 DESCRIPTION
 ===========
@@ -35,6 +35,9 @@ OPTIONS
 --version                show program's version number and exit
 --debug                  display debug messages
 --html output            write HTML report to given file
+                         (use - for standard output)
+--text output            write plain text report to given file
+                         (use - for standard output)
 --max-report-size bytes  maximum bytes written in report
 --css url                link to an extra CSS for the HTML report
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/debbindiff.git



More information about the Reproducible-commits mailing list