Commit 2bfdbeea authored by Jérémy Bobbio's avatar Jérémy Bobbio

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
parent 3e6b3fd4
......@@ -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
@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
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# debbindiff: highlight differences between two builds of Debian packages
#
# Copyright © 2015 Jérémy Bobbio <lunar@debian.org>
# 2015 Helmut Grohne <helmut@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('│')
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment