From 83cc025170fcb6f77753ab560a5849d07696725b Mon Sep 17 00:00:00 2001 From: volpino Date: Sun, 22 Jul 2012 10:26:40 +0200 Subject: [PATCH] euscan: euscan_patch_metadata --diff option Signed-off-by: volpino --- bin/euscan_patch_metadata | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/bin/euscan_patch_metadata b/bin/euscan_patch_metadata index 439105e..b5649ed 100755 --- a/bin/euscan_patch_metadata +++ b/bin/euscan_patch_metadata @@ -1,6 +1,7 @@ #!/usr/bin/env python import os +import sys import re import urllib from tempfile import mkstemp @@ -8,6 +9,7 @@ import tarfile import gzip import logging import shutil +import subprocess from gentoolkit.query import Query from BeautifulSoup import BeautifulSoup, SoupStrainer @@ -130,7 +132,7 @@ def get_deb_url(name): return deb_url, deb_type -def patch_metadata(metadata_path, watch_data): +def patch_metadata(metadata_path, watch_data, diff=False): watch_data = "\n".join([line for line in watch_data.split("\n") if not line.startswith("#")]) # comments watch_data = watch_data.replace("\\\n", "") # remove backslashes @@ -167,10 +169,20 @@ def patch_metadata(metadata_path, watch_data): (rindent, watch_tag, rindent) data = data.replace('', rep, 1) - print data + if not diff: + return data + else: + _, data_path = mkstemp() + with open(data_path, "w") as f: + f.write(data) + sub = subprocess.Popen(["diff", metadata_path, data_path], + stdout=subprocess.PIPE) + content = sub.stdout.read() + os.unlink(data_path) + return content -def process_package(query): +def process_package(query, diff=False): matches = Query(query).smart_find( in_installed=True, in_porttree=True, @@ -194,7 +206,7 @@ def process_package(query): if watch_data is None: logger.error(" No watch file found") else: - patch_metadata(metadata_path, watch_data) + return patch_metadata(metadata_path, watch_data, diff=diff) def main(): @@ -202,13 +214,17 @@ def main(): p = optparse.OptionParser( usage="usage: %prog [ [...]]", ) + p.add_option('-d', '--diff', action="store_true", dest="diff", + default=False, + help="Outputs a diff") opts, packages = p.parse_args() - logging.basicConfig(level=logging.INFO, format='%(message)s') + logging.basicConfig(stream=sys.stderr, level=logging.INFO, + format='%(message)s') for package in packages: logger.info("Processing %s..." % package) - process_package(package) + print process_package(package, opts.diff) if __name__ == "__main__": main()