Adding diff functionnality

master
Lertsenem 2017-01-24 12:38:43 +01:00
parent d54b818a2e
commit 86fad9b83f
1 changed files with 21 additions and 7 deletions

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os.path
import os, os.path
import filecmp
import subprocess
from metafile import Metafile, MalformedMetafileError
@ -23,9 +24,9 @@ def init(parser):
"added files" )
parser.add_argument( "-d", "--diff",
type=str,
action="store_true",
help="Print the diff between the saved and the new " \
"file/directory" )
"files" )
# -----------------------------------------------------------------------------
@ -33,6 +34,11 @@ def run(args, persoconf, logger):
logger.debug("Starting 'check' command")
difftool = None
if args.diff:
difftool = os.environ.get('DIFFTOOL', 'vimdiff')
logger.info("Selecting %s as diff tool" % difftool)
# app == None => check all apps
apps_to_check = {}
if args.app is None:
@ -84,7 +90,8 @@ def run(args, persoconf, logger):
_compare_and_log( original_file_path,
persoconf_backup_path,
logger )
logger,
difftool )
# 2) If not, it must be a real filename
else:
@ -101,7 +108,8 @@ def run(args, persoconf, logger):
_compare_and_log( original_file_path,
persoconf_backup_path,
logger )
logger,
difftool )
# 3) Otherwise, no idea what it is
@ -125,11 +133,15 @@ def run(args, persoconf, logger):
_compare_and_log( original_file_path,
persoconf_backup_path,
logger )
logger,
difftool )
# -----------------------------------------------------------------------------
def _compare_and_log(original_file_path, persoconf_backup_path, logger):
def _compare_and_log( original_file_path,
persoconf_backup_path,
logger,
difftool = None ):
"""Compare a file with its persoconf backup and log the result (at info
level if the files are matching, warning level if they are not)."""
@ -158,6 +170,8 @@ def _compare_and_log(original_file_path, persoconf_backup_path, logger):
else:
logger.warning( "File %s was modified"
% original_file_path )
if difftool:
subprocess.call([ difftool, resolved_pbp, resolved_ofp ])
except FileNotFoundError as err:
if err.filename == resolved_pbp: