Commit 23225665 by devttys0

Fixed rehash bug in display of file paths when diffing multiple directories;…

Fixed rehash bug in display of file paths when diffing multiple directories; added --abspath option to rehash.
parent e5c1558a
...@@ -39,6 +39,7 @@ def usage(fd): ...@@ -39,6 +39,7 @@ def usage(fd):
fd.write("\n") fd.write("\n")
fd.write("General Options:\n") fd.write("General Options:\n")
fd.write("\t-f, --file=<file> Log results to file\n") fd.write("\t-f, --file=<file> Log results to file\n")
fd.write("\t-a, --abspath Display the absolute path of each file\n")
fd.write("\t-c, --csv Log results to file in csv format\n") fd.write("\t-c, --csv Log results to file in csv format\n")
fd.write("\t-q, --quiet Suppress output to stdout\n") fd.write("\t-q, --quiet Suppress output to stdout\n")
fd.write("\t-t, --term Format output to fit the terminal window\n") fd.write("\t-t, --term Format output to fit the terminal window\n")
...@@ -65,6 +66,7 @@ def main(): ...@@ -65,6 +66,7 @@ def main():
types = {} types = {}
matches = {} matches = {}
abspath = False
log_file = None log_file = None
log_csv = False log_csv = False
fit_to_width = False fit_to_width = False
...@@ -76,8 +78,9 @@ def main(): ...@@ -76,8 +78,9 @@ def main():
cutoff = None cutoff = None
max_results = None max_results = None
short_options = "cdf:hlm:no:qSstx:X:y:Y:" short_options = "acdf:hlm:no:qSstx:X:y:Y:"
long_options = [ long_options = [
"abspath",
"help", "help",
"cutoff=", "cutoff=",
"strings", "strings",
...@@ -131,6 +134,8 @@ def main(): ...@@ -131,6 +134,8 @@ def main():
include_types.append(arg.lower()) include_types.append(arg.lower())
elif opt in ("-X", "--exclude-types"): elif opt in ("-X", "--exclude-types"):
exclude_types.append(arg.lower()) exclude_types.append(arg.lower())
elif opt in ("-a", "--abspath"):
abspath = True
# Keep track of the options and arguments. # Keep track of the options and arguments.
# This is used later to determine which argv entries are file names. # This is used later to determine which argv entries are file names.
...@@ -166,6 +171,7 @@ def main(): ...@@ -166,6 +171,7 @@ def main():
log=log_file, log=log_file,
csv=log_csv, csv=log_csv,
format_to_screen=fit_to_width, format_to_screen=fit_to_width,
abspath=abspath,
types=types, types=types,
matches=matches) matches=matches)
......
...@@ -28,7 +28,7 @@ class HashMatch(object): ...@@ -28,7 +28,7 @@ class HashMatch(object):
FUZZY_DEFAULT_CUTOFF = 50 FUZZY_DEFAULT_CUTOFF = 50
def __init__(self, cutoff=None, strings=False, same=False, symlinks=False, name=False, max_results=None, display=False, log=None, csv=False, quiet=False, format_to_screen=False, matches={}, types={}): def __init__(self, cutoff=None, strings=False, same=False, symlinks=False, name=False, max_results=None, display=False, log=None, csv=False, quiet=False, format_to_screen=False, abspath=False, matches={}, types={}):
''' '''
Class constructor. Class constructor.
...@@ -39,6 +39,11 @@ class HashMatch(object): ...@@ -39,6 +39,11 @@ class HashMatch(object):
@name - Set to True to only compare files whose base names match. @name - Set to True to only compare files whose base names match.
@max_results - Stop searching after x number of matches. @max_results - Stop searching after x number of matches.
@display - Set to True to display results to stdout. @display - Set to True to display results to stdout.
@log - Specify a log file to log results to.
@csv - Set to True to log data in CSV format.
@quiet - Set to True to suppress output to stdout.
@format_to_screen - Set to True to format the output to the terminal window width.
@abspath - Set to True to display absolute file paths.
@matches - A dictionary of file names to diff. @matches - A dictionary of file names to diff.
@types - A dictionary of file types to diff. @types - A dictionary of file types to diff.
...@@ -51,11 +56,12 @@ class HashMatch(object): ...@@ -51,11 +56,12 @@ class HashMatch(object):
self.matches = matches self.matches = matches
self.name = name self.name = name
self.types = types self.types = types
self.abspath = abspath
self.max_results = max_results self.max_results = max_results
if display: if display:
self.pretty_print = PrettyPrint(log=log, csv=csv, format_to_screen=format_to_screen, quiet=quiet) self.pretty_print = PrettyPrint(log=log, csv=csv, format_to_screen=format_to_screen, quiet=quiet)
self.pretty_print.header(header="PERCENTAGE\tFILE NAME") self.pretty_print.header(header="PERCENTAGE\t\t\tFILE NAME")
else: else:
self.pretty_print = None self.pretty_print = None
...@@ -80,7 +86,9 @@ class HashMatch(object): ...@@ -80,7 +86,9 @@ class HashMatch(object):
def _print(self, match, fname): def _print(self, match, fname):
if self.pretty_print: if self.pretty_print:
self.pretty_print.results(None, [{'description' : '%4d\t\t%s\n' % (match, fname)}], formatted=True) if self.abspath:
fname = os.path.abspath(fname)
self.pretty_print._pprint('%4d\t\t\t\t%s\n' % (match, self.pretty_print._format(fname)))
def _print_footer(self): def _print_footer(self):
if self.pretty_print: if self.pretty_print:
...@@ -106,6 +114,7 @@ class HashMatch(object): ...@@ -106,6 +114,7 @@ class HashMatch(object):
hash1 = ctypes.create_string_buffer(self.FUZZY_MAX_RESULT) hash1 = ctypes.create_string_buffer(self.FUZZY_MAX_RESULT)
hash2 = ctypes.create_string_buffer(self.FUZZY_MAX_RESULT) hash2 = ctypes.create_string_buffer(self.FUZZY_MAX_RESULT)
# Check if the last file1 or file2 matches this file1 or file2; no need to re-hash if they match.
if file1 == self.last_file1.name and self.last_file1.hash: if file1 == self.last_file1.name and self.last_file1.hash:
file1_dup = True file1_dup = True
else: else:
...@@ -310,8 +319,8 @@ class HashMatch(object): ...@@ -310,8 +319,8 @@ class HashMatch(object):
m = self._compare_files(file1, file2) m = self._compare_files(file1, file2)
if m is not None and self.is_match(m): if m is not None and self.is_match(m):
self._print(m, f) self._print(m, file2)
results.append((m, f)) results.append((m, file2))
self.total += 1 self.total += 1
if self.max_results and self.total >= self.max_results: if self.max_results and self.total >= self.max_results:
......
...@@ -286,7 +286,7 @@ class PrettyPrint: ...@@ -286,7 +286,7 @@ class PrettyPrint:
else: else:
self._pprint("%s\t %s\t%s\n" % (' '*10, ' '*8, self._format(info['description']))) self._pprint("%s\t %s\t%s\n" % (' '*10, ' '*8, self._format(info['description'])))
else: else:
self._pprint(info['description']) self._pprint(self._format(info['description']))
def easy_results(self, offset, description): def easy_results(self, offset, description):
''' '''
......
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