From 33b2f1375d85ad5ee5fc129dc11f5223f09e1482 Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Sun, 7 Jun 2020 16:01:20 +0200 Subject: [PATCH 1/4] bashlib/ocrd_wrap_cli_processor: show help if METS does not exist, fix #438, OCR-D/spec#156 --- ocrd/bashlib/src/parse_argv.bash | 3 ++- ocrd/ocrd/decorators.py | 15 +++++++-------- ocrd/ocrd/lib.bash | 3 ++- tests/test_decorators.py | 20 ++++++++++++++++---- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/ocrd/bashlib/src/parse_argv.bash b/ocrd/bashlib/src/parse_argv.bash index 36cdbeb83..194ca0796 100644 --- a/ocrd/bashlib/src/parse_argv.bash +++ b/ocrd/bashlib/src/parse_argv.bash @@ -36,7 +36,8 @@ ocrd__parse_argv () { done if [[ ! -r "${ocrd__argv[mets_file]:=$PWD/mets.xml}" ]];then - ocrd__raise "METS '${ocrd__argv[mets_file]}' not readable. Use -m/--mets-file to set correctly" + ocrd__usage + exit 1 fi if [[ ! -d "${ocrd__argv[working_dir]:=$(dirname "${ocrd__argv[mets_file]}")}" ]];then diff --git a/ocrd/ocrd/decorators.py b/ocrd/ocrd/decorators.py index 424466bb5..ec09efd86 100644 --- a/ocrd/ocrd/decorators.py +++ b/ocrd/ocrd/decorators.py @@ -1,4 +1,5 @@ from os.path import isfile +import sys import click @@ -31,19 +32,17 @@ def ocrd_cli_wrap_processor(processorClass, ocrd_tool=None, mets=None, working_d LOG = getLogger('ocrd_cli_wrap_processor') if dump_json: processorClass(workspace=None, dump_json=True) + sys.exit() elif help: processorClass(workspace=None, show_help=True) + sys.exit() elif version: processorClass(workspace=None, show_version=True) - elif mets is None: - msg = 'Error: Missing option "-m" / "--mets".' - LOG.error(msg) - raise Exception(msg) + sys.exit() else: - if is_local_filename(mets) and not isfile(get_local_filename(mets)): - msg = "File does not exist: %s" % mets - LOG.error(msg) - raise Exception(msg) + if not mets or (is_local_filename(mets) and not isfile(get_local_filename(mets))): + processorClass(workspace=None, show_help=True) + sys.exit(1) resolver = Resolver() workspace = resolver.workspace_from_url(mets, working_dir) # TODO once we implement 'overwrite' CLI option and mechanism, disable the diff --git a/ocrd/ocrd/lib.bash b/ocrd/ocrd/lib.bash index 0564f7688..9096473f3 100644 --- a/ocrd/ocrd/lib.bash +++ b/ocrd/ocrd/lib.bash @@ -75,7 +75,8 @@ ocrd__parse_argv () { done if [[ ! -r "${ocrd__argv[mets_file]:=$PWD/mets.xml}" ]];then - ocrd__raise "METS '${ocrd__argv[mets_file]}' not readable. Use -m/--mets-file to set correctly" + ocrd__usage + exit 1 fi if [[ ! -d "${ocrd__argv[working_dir]:=$(dirname "${ocrd__argv[mets_file]}")}" ]];then diff --git a/tests/test_decorators.py b/tests/test_decorators.py index cf4386cec..07ecfe7a9 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -2,7 +2,7 @@ import click from click.testing import CliRunner -from tests.base import TestCase, assets, main, copy_of_directory # pylint: disable=import-error, no-name-in-module +from tests.base import CapturingTestCase as TestCase, assets, main, copy_of_directory # pylint: disable=import-error, no-name-in-module from ocrd import Processor from ocrd.decorators import ( @@ -26,8 +26,11 @@ def cli_with_ocrd_loglevel(*args, **kwargs): # pylint: disable=unused-ar DUMMY_TOOL = { 'executable': 'ocrd-test', 'steps': ['recognition/post-correction'], + 'description': 'A dummy processor for testing sigh', 'parameters': { 'foo': { + 'type': 'number', + 'description': 'dummy parameter for a dummy procesor', 'required': True } } @@ -73,6 +76,15 @@ def test_loglevel_override(self): self.assertEqual(logging.getLogger('PIL').getEffectiveLevel(), logging.DEBUG) initLogging() + def test_processor_no_mets(self): + """ + https://github.com/OCR-D/spec/pull/156 + """ + _, out_help, _ = self.invoke_cli(cli_dummy_processor, ['--help']) + exit_code, out_none, _ = self.invoke_cli(cli_dummy_processor, []) + self.assertEqual(exit_code, 1) + self.assertEqual(out_help, out_none) + def test_processor_dump_json(self): result = self.runner.invoke(cli_dummy_processor, ['--dump-json']) self.assertEqual(result.exit_code, 0) @@ -91,9 +103,9 @@ def test_processor_version(self): def test_processor_run(self): with copy_of_directory(assets.path_to('SBB0000F29300010000/data')) as tempdir: with pushd_popd(tempdir): - result = self.runner.invoke(cli_dummy_processor, ['-p', '{"foo": 42}', '--mets', 'mets.xml', '-I', 'OCR-D-IMG']) - self.assertEqual(result.exit_code, 0) + exit_code, out, err = self.invoke_cli(cli_dummy_processor, ['-p', '{"foo": 42}', '--mets', 'mets.xml', '-I', 'OCR-D-IMG']) + self.assertEqual(exit_code, 0) if __name__ == '__main__': - main() + main(__file__) From 2f7caf00e0f2a03b740143c7678483eae67a6c49 Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Tue, 21 Jul 2020 15:39:24 +0200 Subject: [PATCH 2/4] tests: description for dummy ocrd-tool --- tests/base.py | 2 +- tests/data/__init__.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/base.py b/tests/base.py index 4f26d8b93..d4734667e 100644 --- a/tests/base.py +++ b/tests/base.py @@ -48,7 +48,7 @@ def invoke_cli(self, cli, args): cli.main(args=args) except SystemExit as e: out, err = self.capture_out_err() - return e.code, out, err + return e.code or 0, out, err def capture_out_err(self): return self.capfd.readouterr() diff --git a/tests/data/__init__.py b/tests/data/__init__.py index 393e4bed1..506807694 100644 --- a/tests/data/__init__.py +++ b/tests/data/__init__.py @@ -3,11 +3,13 @@ DUMMY_TOOL = { 'executable': 'ocrd-test', + 'description': 'dolor sit', 'steps': ['recognition/post-correction'], 'parameters': { 'baz': { 'type': 'string', - 'default': 'bla' + 'default': 'bla', + 'description': 'lorem ipsum' } } } From b2c6e6e2de2af7b05925bf02f5f21cddd5f7194b Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Tue, 21 Jul 2020 16:30:08 +0200 Subject: [PATCH 3/4] bashlib: fix ocrd__minversion logic --- ocrd/bashlib/src/logging.bash | 33 +++++++++++++++++---------------- ocrd/ocrd/lib.bash | 33 +++++++++++++++++---------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/ocrd/bashlib/src/logging.bash b/ocrd/bashlib/src/logging.bash index f465852c4..12ca81b96 100644 --- a/ocrd/bashlib/src/logging.bash +++ b/ocrd/bashlib/src/logging.bash @@ -25,24 +25,25 @@ ocrd__log () { ocrd__minversion () { local minversion="$1" local version=$(ocrd --version|sed 's/ocrd, version //') - echo "$minversion < $version?" - if [[ $minversion == $version ]];then - return 0 - fi + #echo "$minversion < $version?" local IFS=. version=($version) minversion=($minversion) - # fill empty fields in version with zeros - for ((i=${#version[@]}; i<${#minversion[@]}; i++));do - version[i]=0 - done - for ((i=0; i<${#version[@]}; i++));do - if [[ -z ${minversion[i]} ]];then - # fill empty fields in minversion with zeros - minversion[i]=0 - fi - if ((10#${version[i]} < 10#${minversion[i]}));then - ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core" + # MAJOR > MAJOR + if (( ${version[0]} > ${minversion[0]} ));then + return + # MAJOR == MAJOR + elif (( ${version[0]} == ${minversion[0]} ));then + # MINOR > MINOR + if (( ${version[1]} > ${minversion[1]} ));then + return + # MINOR == MINOR + elif (( ${version[1]} == ${minversion[1]} ));then + # PATCH > PATCH + if (( ${version[2]} >= ${minversion[2]} ));then + return + fi fi - done + fi + ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core" } diff --git a/ocrd/ocrd/lib.bash b/ocrd/ocrd/lib.bash index b16550211..37cfb5f65 100644 --- a/ocrd/ocrd/lib.bash +++ b/ocrd/ocrd/lib.bash @@ -32,26 +32,27 @@ ocrd__log () { ocrd__minversion () { local minversion="$1" local version=$(ocrd --version|sed 's/ocrd, version //') - echo "$minversion < $version?" - if [[ $minversion == $version ]];then - return 0 - fi + #echo "$minversion < $version?" local IFS=. version=($version) minversion=($minversion) - # fill empty fields in version with zeros - for ((i=${#version[@]}; i<${#minversion[@]}; i++));do - version[i]=0 - done - for ((i=0; i<${#version[@]}; i++));do - if [[ -z ${minversion[i]} ]];then - # fill empty fields in minversion with zeros - minversion[i]=0 - fi - if ((10#${version[i]} < 10#${minversion[i]}));then - ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core" + # MAJOR > MAJOR + if (( ${version[0]} > ${minversion[0]} ));then + return + # MAJOR == MAJOR + elif (( ${version[0]} == ${minversion[0]} ));then + # MINOR > MINOR + if (( ${version[1]} > ${minversion[1]} ));then + return + # MINOR == MINOR + elif (( ${version[1]} == ${minversion[1]} ));then + # PATCH > PATCH + if (( ${version[2]} >= ${minversion[2]} ));then + return + fi fi - done + fi + ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core" } # END-INCLUDE From db5ae64756169addd703a1efde0bf0ed5b4f1d05 Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Tue, 21 Jul 2020 16:32:25 +0200 Subject: [PATCH 4/4] :memo: changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09a668fe0..e50c6a808 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,10 +9,12 @@ Changed: * Refactoring of `ocrd_utils.__init__` into thematic submodules, #536 * validation of file groups downgraded to notice, allow PRE fileGrp/USE prefix, #541 + * BaseProcessor: :fire: show help if no METS was specified, OCR-D/spec#156, #438, #503 Fixed: * bashlib: Don't set `-x` in `ocrd__minversion`, #535 + * bashlib: `ocrd__minversion` logic was broken ## [2.11.0] - 2020-07-13