[postprocessor:compare] add 'equal' option (#1592)

Move functionality from cdd72e14 to its own option,
where it can be used with any 'action'
This commit is contained in:
Mike Fährmann
2021-10-05 03:03:37 +02:00
parent f8410203ef
commit df8050b81d
4 changed files with 55 additions and 37 deletions

View File

@@ -20,36 +20,32 @@ class ComparePP(PostProcessor):
PostProcessor.__init__(self, job)
if options.get("shallow"):
self._compare = self._compare_size
self._equal_exc = self._equal_cnt = 0
action = options.get("action")
if action == "enumerate":
job.register_hooks({"file": self.enumerate}, options)
else:
job.register_hooks({"file": self.compare}, options)
action, _, smax = action.partition(":")
self._skipmax = text.parse_int(smax)
self._skipexc = self._skipcnt = 0
if action == "abort":
self._skipexc = exception.StopExtraction
elif action == "terminate":
self._skipexc = exception.TerminateExtraction
elif action == "exit":
self._skipexc = sys.exit
equal = options.get("equal")
if equal:
equal, _, emax = equal.partition(":")
self._equal_max = text.parse_int(emax)
if equal == "abort":
self._equal_exc = exception.StopExtraction
elif equal == "terminate":
self._equal_exc = exception.TerminateExtraction
elif equal == "exit":
self._equal_exc = sys.exit
def compare(self, pathfmt):
job.register_hooks({"file": (
self.enumerate
if options.get("action") == "enumerate" else
self.replace
)}, options)
def replace(self, pathfmt):
try:
if self._compare(pathfmt.realpath, pathfmt.temppath):
if self._skipexc:
self._skipcnt += 1
if self._skipcnt >= self._skipmax:
util.remove_file(pathfmt.temppath)
print()
raise self._skipexc()
pathfmt.delete = True
else:
self._skipcnt = 0
return self._equal(pathfmt)
except OSError:
pass
self._equal_cnt = 0
def enumerate(self, pathfmt):
num = 1
@@ -58,9 +54,10 @@ class ComparePP(PostProcessor):
pathfmt.prefix = str(num) + "."
pathfmt.set_extension(pathfmt.extension, False)
num += 1
pathfmt.delete = True
return self._equal(pathfmt)
except OSError:
pass
self._equal_cnt = 0
def _compare(self, f1, f2):
return self._compare_size(f1, f2) and self._compare_content(f1, f2)
@@ -81,5 +78,14 @@ class ComparePP(PostProcessor):
if not buf1:
return True
def _equal(self, pathfmt):
if self._equal_exc:
self._equal_cnt += 1
if self._equal_cnt >= self._equal_max:
util.remove_file(pathfmt.temppath)
print()
raise self._equal_exc()
pathfmt.delete = True
__postprocessor__ = ComparePP