Skip to content

Commit

Permalink
Merge pull request #7 from OCR-D/bashlib-version-yak-shaving
Browse files Browse the repository at this point in the history
Bashlib version yak shaving
  • Loading branch information
bertsky authored Aug 15, 2024
2 parents 93a742e + 70ad191 commit 678158a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ repo/assets repo/spec: always-update

.PHONY: spec
# Copy JSON Schema, OpenAPI from OCR-D/spec
spec: repo/spec
cp repo/spec/ocrd_tool.schema.yml ocrd_validators/ocrd_validators/ocrd_tool.schema.yml
cp repo/spec/bagit-profile.yml ocrd_validators/ocrd_validators/bagit-profile.yml
spec: # repo/spec
cp repo/spec/ocrd_tool.schema.yml src/ocrd_validators/ocrd_tool.schema.yml
cp repo/spec/bagit-profile.yml src/ocrd_validators/bagit-profile.yml

#
# Assets
Expand Down
31 changes: 23 additions & 8 deletions src/ocrd/lib.bash
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ ocrd__log () {
## Ensure minimum version
# ht https://stackoverflow.com/posts/4025065
ocrd__minversion () {
local minversion="$1"
local version=$(ocrd --version|sed 's/ocrd, version //')
#echo "$minversion < $version?"
local IFS=.
version=($version)
minversion=($minversion)
local minversion_raw="$1"
set -e
local version_raw=$(ocrd --version|sed 's/ocrd, version //')
local version_mmp=$(echo "$version_raw" | grep -Eo '([0-9]+\.?){3}')
local version_prerelease_suffix="${version_raw#$version_mmp}"
if [[ -z $version_prerelease_suffix ]];then
version_prerelease_suffix=0
fi
local minversion_mmp=$(echo "$minversion_raw" | grep -Eo '([0-9]+\.?){3}')
local minversion_prerelease_suffix="${minversion_raw#$minversion_mmp}"
if [[ -z $minversion_prerelease_suffix ]];then
minversion_prerelease_suffix=0
fi
local IFS='.'
version=($version_mmp)
minversion=($minversion_mmp)
# MAJOR > MAJOR
if (( ${version[0]} > ${minversion[0]} ));then
return
Expand All @@ -44,12 +54,17 @@ ocrd__minversion () {
# MINOR == MINOR
elif (( ${version[1]} == ${minversion[1]} ));then
# PATCH > PATCH
if (( ${version[2]} >= ${minversion[2]} ));then
if (( ${version[2]} > ${minversion[2]} ));then
return
elif (( ${version[2]} == ${minversion[2]}));then
# Match prerelease suffix like a1, b1 alphabetically
if [[ $version_prerelease_suffix = $minversion_prerelease_suffix -o $version_prerelease_suffix > $minversion_prerelease_suffix ]]; then
return
fi
fi
fi
fi
ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core"
ocrd__raise "ocrd/core is too old ($version_raw < $minversion_raw). Please update OCR-D/core"
}

## ### `ocrd__dumpjson`
Expand Down
28 changes: 23 additions & 5 deletions tests/cli/test_bashlib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from contextlib import contextmanager
import re
from typing import Tuple, Union
from tests.base import CapturingTestCase as TestCase, main, assets, copy_of_directory

import os, sys
Expand All @@ -20,6 +22,13 @@

from ocrd_utils import pushd_popd

def parse_version(v : str) -> Union[Tuple[int, int, int], Tuple[int, int, int, str]]:
tokens = re.split('((?:a|b|rc)[0-9]+)', v, 1)
version_wo_suffix = tokens[0]
prerelease_suffix = tokens[1] if len(tokens) > 1 else ''
(major, minor, patch) = map(int, version_wo_suffix.split('.'))
return (major, minor, patch, prerelease_suffix)

class TestBashlibCli(TestCase):

def invoke_bash(self, script, *args, executable=None):
Expand Down Expand Up @@ -101,13 +110,22 @@ def test_bashlib_defs(self):
assert 'function' in out

def test_bashlib_minversion(self):
exit_code, out, err = self.invoke_bash(
"source $(ocrd bashlib filename) && ocrd__minversion 2.29.0")
exit_code, out, err = self.invoke_bash("source $(ocrd bashlib filename) && ocrd__minversion 2.29.0")
assert exit_code == 0
exit_code, out, err = self.invoke_bash(
"source $(ocrd bashlib filename) && ocrd__minversion " + VERSION)
major, minor, patch, prerelease_suffix = parse_version(VERSION)

# test normal version with impossible minimum minor version
version = "%d.%d.%d" % (major, minor + 1, patch)
exit_code, out, err = self.invoke_bash("source $(ocrd bashlib filename) && ocrd__minversion " + version)
assert exit_code > 0
assert f"ERROR: ocrd/core is too old ({VERSION} < {version})" in err

# test non-matching prerelease (the 99th alpha pre-release here)
version = "%d.%d.%da99" % (major, minor, patch)
assert VERSION != version # assuming we will never have 99 alpha prereleases ^^
exit_code, out, err = self.invoke_bash("source $(ocrd bashlib filename) && ocrd__minversion " + version)
assert exit_code > 0
assert "ERROR: ocrd/core is too old" in err
assert f"ERROR: ocrd/core is too old ({VERSION} < {version})" in err

def test_bashlib_cp_processor(self):
# script = (Path(__file__).parent.parent / 'data/bashlib_cp_processor.sh').read_text()
Expand Down

0 comments on commit 678158a

Please sign in to comment.