Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
cli.ocrodjvu: ensure that exit code is non-zero if the program recove…
Browse files Browse the repository at this point in the history
…red from an error.
  • Loading branch information
jwilk committed Jun 5, 2015
1 parent acfdb66 commit 6bd704f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
4 changes: 3 additions & 1 deletion doc/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ ocrodjvu (0.8) UNRELEASED; urgency=low
* Add the “tesseract: ” prefix to messages Tesseract prints on stderr.
https://bitbucket.org/jwilk/ocrodjvu/issue/10
Thanks to Janusz S. Bień for the bug report.
* Ensure that exit code is non-zero if the program recovered from an error.
https://bitbucket.org/jwilk/ocrodjvu/issue/6

-- Jakub Wilk <jwilk@jwilk.net> Fri, 05 Jun 2015 22:05:09 +0200
-- Jakub Wilk <jwilk@jwilk.net> Fri, 05 Jun 2015 23:14:11 +0200

ocrodjvu (0.7.19) unstable; urgency=low

Expand Down
21 changes: 21 additions & 0 deletions doc/ocrodjvu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,27 @@
</refsection>
</refsection>

<refsection>
<title>Exit status</title>
<para>
One of the following exit values can be returned by &p;:
<variablelist>
<varlistentry>
<term>0</term>
<listitem><para>The program finished successfully.</para></listitem>
</varlistentry>
<varlistentry>
<term>1</term>
<listitem><para>A fatal error occurred.</para></listitem>
</varlistentry>
<varlistentry>
<term>2</term>
<listitem><para>The program recovered from an error (<option>--on-error=resume</option>).</para></listitem>
</varlistentry>
</variablelist>
</para>
</refsection>

<refsection>
<title>Environment</title>
<para>
Expand Down
20 changes: 15 additions & 5 deletions lib/cli/ocrodjvu.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import print_function

import argparse
import collections
import contextlib
import inspect
import locale
Expand Down Expand Up @@ -252,10 +253,10 @@ def __call__(self, parser, namespace, values, option_string=None):
print(language)
except errors.EngineNotFound as ex:
logger.error(ex)
sys.exit(1)
sys.exit(errors.EXIT_FATAL)
except errors.UnknownLanguageList as ex:
logger.error(ex)
sys.exit(1)
sys.exit(errors.EXIT_FATAL)
else:
sys.exit(0)

Expand Down Expand Up @@ -334,6 +335,12 @@ def parse_args(self, args=None, namespace=None):
options.n_jobs = get_cpu_count()
return options

class Results(dict):
seen_exception = False

def __missing__(self, key):
return

class Context(djvu.decode.Context):

def init(self, options):
Expand Down Expand Up @@ -430,6 +437,7 @@ def page_thread(self, pages, results, condition):
if self._options.resume_on_error and not interrupted_by_user:
# As requested by user, don't abort on error and pretend that nothing happened.
results[n] = False
results.seen_exception = True
continue
else:
# The main thread will take care of aborting the application.
Expand All @@ -452,7 +460,7 @@ def _process(self, path, pages=None):
pages = list(document.pages)
else:
pages = [document.pages[i - 1] for i in pages]
results = dict((page.n, None) for page in pages)
results = Results()
condition = threading.Condition()
threads = [
threading.Thread(target=self.page_thread, args=(pages, results, condition))
Expand Down Expand Up @@ -497,7 +505,7 @@ def stop_threads():
for thread in threads:
thread.join()
self._debug = True
sys.exit(1)
sys.exit(errors.EXIT_FATAL)
if result is False:
# No image suitable for OCR.
pass
Expand All @@ -518,6 +526,8 @@ def stop_threads():
raise
finally:
sed_file.close()
if results.seen_exception:
sys.exit(errors.EXIT_NONFATAL)

def process(self, *args, **kwargs):
try:
Expand All @@ -542,7 +552,7 @@ def main(argv=sys.argv):
context.process(options.path, options.pages)
except KeyboardInterrupt:
logger.info('Interrupted by user.')
sys.exit(1)
sys.exit(errors.FATAL)
finally:
temp_dir = context.close()
if temp_dir is not None:
Expand Down
5 changes: 4 additions & 1 deletion lib/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding=UTF-8

# Copyright © 2009, 2010, 2013 Jakub Wilk <jwilk@jwilk.net>
# Copyright © 2009-2015 Jakub Wilk <jwilk@jwilk.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -41,4 +41,7 @@ class MalformedHocr(MalformedOcrOutput):
def __init__(self, message):
Exception.__init__(self, 'Malformed hOCR document: %s' % message)

EXIT_FATAL = 1
EXIT_NONFATAL = 2

# vim:ts=4 sts=4 sw=4 et

0 comments on commit 6bd704f

Please sign in to comment.